~michael-sheldon/+junk/stentorian

« back to all changes in this revision

Viewing changes to plugins/ko/src/koreanplugin.cpp

  • Committer: Michael Sheldon
  • Date: 2017-05-03 09:43:18 UTC
  • Revision ID: michael.sheldon@canonical.com-20170503094318-pe41gzsmqwicq0qt
Convert Ubuntu Keyboard into a standalone speech recognition input

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include "koreanplugin.h"
2
 
#include "koreanlanguagefeatures.h"
3
 
#include "spellpredictworker.h"
4
 
 
5
 
#include <QDebug>
6
 
 
7
 
KoreanPlugin::KoreanPlugin(QObject *parent) :
8
 
    AbstractLanguagePlugin(parent)
9
 
  , m_koreanLanguageFeatures(new KoreanLanguageFeatures)
10
 
  , m_spellCheckEnabled(false)
11
 
  , m_processingSpelling(false)
12
 
{
13
 
    m_spellPredictThread = new QThread();
14
 
    m_spellPredictWorker = new SpellPredictWorker();
15
 
    m_spellPredictWorker->moveToThread(m_spellPredictThread);
16
 
 
17
 
    connect(m_spellPredictWorker, SIGNAL(newSpellingSuggestions(QString, QStringList)), this, SLOT(spellCheckFinishedProcessing(QString, QStringList)));
18
 
    connect(m_spellPredictWorker, SIGNAL(newPredictionSuggestions(QString, QStringList)), this, SIGNAL(newPredictionSuggestions(QString, QStringList)));
19
 
    connect(this, SIGNAL(newSpellCheckWord(QString)), m_spellPredictWorker, SLOT(newSpellCheckWord(QString)));
20
 
    connect(this, SIGNAL(setSpellPredictLanguage(QString, QString)), m_spellPredictWorker, SLOT(setLanguage(QString, QString)));
21
 
    connect(this, SIGNAL(setSpellCheckLimit(int)), m_spellPredictWorker, SLOT(setSpellCheckLimit(int)));
22
 
    connect(this, SIGNAL(parsePredictionText(QString, QString)), m_spellPredictWorker, SLOT(parsePredictionText(QString, QString)));
23
 
    connect(this, SIGNAL(addToUserWordList(QString)), m_spellPredictWorker, SLOT(addToUserWordList(QString)));
24
 
    connect(this, SIGNAL(addOverride(QString, QString)), m_spellPredictWorker, SLOT(addOverride(QString, QString)));
25
 
    m_spellPredictThread->start();
26
 
 
27
 
}
28
 
 
29
 
KoreanPlugin::~KoreanPlugin()
30
 
{
31
 
    m_spellPredictWorker->deleteLater();
32
 
    m_spellPredictThread->quit();
33
 
    m_spellPredictThread->wait();
34
 
}
35
 
 
36
 
AbstractLanguageFeatures* KoreanPlugin::languageFeature()
37
 
{
38
 
    return m_koreanLanguageFeatures;
39
 
}
40
 
 
41
 
void KoreanPlugin::predict(const QString& surroundingLeft, const QString& preedit)
42
 
{
43
 
    Q_EMIT parsePredictionText(surroundingLeft, preedit);
44
 
}
45
 
 
46
 
void KoreanPlugin::wordCandidateSelected(QString word)
47
 
{
48
 
    Q_UNUSED(word);
49
 
}
50
 
 
51
 
 
52
 
void KoreanPlugin::spellCheckerSuggest(const QString& word, int limit)
53
 
{
54
 
    m_nextSpellWord = word;
55
 
    // Don't accept new words whilst we're processing, so we only process the
56
 
    // most recent input once the current processing has completed
57
 
    if (!m_processingSpelling) {
58
 
        m_processingSpelling = true;
59
 
        Q_EMIT setSpellCheckLimit(limit);
60
 
        Q_EMIT newSpellCheckWord(word);
61
 
    }
62
 
}
63
 
 
64
 
void KoreanPlugin::addToSpellCheckerUserWordList(const QString& word)
65
 
{
66
 
    Q_EMIT addToUserWordList(word);
67
 
}
68
 
 
69
 
bool KoreanPlugin::setLanguage(const QString& languageId, const QString& pluginPath)
70
 
{
71
 
    Q_EMIT setSpellPredictLanguage(languageId, pluginPath);
72
 
    loadOverrides(pluginPath);
73
 
    return true;
74
 
}
75
 
 
76
 
void KoreanPlugin::addSpellingOverride(const QString& orig, const QString& overriden)
77
 
{
78
 
    Q_EMIT addOverride(orig, overriden);
79
 
}
80
 
 
81
 
void KoreanPlugin::loadOverrides(const QString& pluginPath) {
82
 
    QFile overrideFile(pluginPath + QDir::separator() + "overrides.csv");
83
 
    if (overrideFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
84
 
        QTextStream overrideStream(&overrideFile);
85
 
        while (!overrideStream.atEnd()) {
86
 
            QString line = overrideStream.readLine();
87
 
            QStringList components = line.split(",");
88
 
            if (components.length() == 2) {
89
 
                addSpellingOverride(components.first(), components.last());
90
 
            }
91
 
        }
92
 
    }
93
 
}
94
 
 
95
 
void KoreanPlugin::spellCheckFinishedProcessing(QString word, QStringList suggestions) {
96
 
    Q_EMIT newSpellingSuggestions(word, suggestions);
97
 
    if (word != m_nextSpellWord) {
98
 
        Q_EMIT newSpellCheckWord(m_nextSpellWord);
99
 
    } else {
100
 
        m_processingSpelling = false;
101
 
    }
102
 
}