1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
//=============================================================================
//
// File : libkvispellchecker.cpp
// Creation date : Thu Dec 26 2014 15:12:12 GMT by Alexey Sokolov
//
// This file is part of the KVIrc IRC client distribution
// Copyright © 2014 Alexey Sokolov <sokolov@google.com>
//
// This program is FREE software. You can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your opinion) any later version.
//
// This program is distributed in the HOPE that it will be USEFUL,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, write to the Free Software Foundation,
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
//=============================================================================
#include "KviModule.h"
#include "KviOptions.h"
#include <enchant.h>
#include <enchant-provider.h>
static EnchantBroker* g_pEnchantBroker = NULL;
static KviPointerList<EnchantDict>* g_pEnchantDicts = NULL;
/*
@doc: spellchecker.available_dictionaries
@type:
function
@title:
$spellchecker.available_dictionaries
@short:
Return available dictionaries.
@syntax:
<hash> $spellchecker.available_dictionaries
@description:
This function returns a hash. For every supported dictionary, key is the language code,
value is name of provider for that language (e.g. Aspell).
*/
static void spellchecker_enumerate_dicts(
const char* szLang,
const char* /*szName*/,
const char* szDesc,
const char* /*szFile*/,
void* pData
)
{
KviKvsHash* pHash = reinterpret_cast<KviKvsHash*>(pData);
pHash->set(szLang, new KviKvsVariant(szDesc));
}
static bool spellchecker_kvs_available_dictionaries(KviKvsModuleFunctionCall* c)
{
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETERS_END(c)
KviKvsHash* pHash = new KviKvsHash;
enchant_broker_list_dicts(g_pEnchantBroker, spellchecker_enumerate_dicts, pHash);
c->returnValue()->setHash(pHash);
return true;
}
/*
@doc: spellchecker.check
@type:
function
@title:
$spellchecker.check
@short:
Check a single work for spelling mistakes.
@syntax:
<bool> $spellchecker.check(<word:string>)
@description:
This function returns true if the word is spelled correctly.
*/
static bool spellchecker_kvs_check(KviKvsModuleFunctionCall* c)
{
QString szWord;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("word", KVS_PT_STRING, 0, szWord)
KVSM_PARAMETERS_END(c)
QByteArray utf8 = szWord.toUtf8();
bool bResult = g_pEnchantDicts->isEmpty();
KviPointerListIterator<EnchantDict> it(*g_pEnchantDicts);
for (bool b = it.moveFirst(); b; b = it.moveNext())
bResult |= enchant_dict_check(*it, utf8.data(), utf8.size()) == 0;
c->returnValue()->setBoolean(bResult);
return true;
}
/*
@doc: spellchecker.suggestions
@type:
function
@title:
$spellchecker.suggestions
@short:
Get spelling suggestions for a single word
@syntax:
<array> $spellchecker.suggestions(<word:string>)
@description:
This function returns the suggestions for the specified word.
If the word seems to be spelled correctly or there are no dictionaries
selected then the functon returns an empty array.
*/
static bool spellchecker_kvs_suggestions(KviKvsModuleFunctionCall* c)
{
QString szWord;
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETER("word", KVS_PT_STRING, 0, szWord)
KVSM_PARAMETERS_END(c)
QHash<QString,int> hAllSuggestions;
if(!g_pEnchantDicts->isEmpty())
{
QByteArray utf8 = szWord.toUtf8();
KviPointerListIterator<EnchantDict> it(*g_pEnchantDicts);
for(bool b = it.moveFirst(); b; b = it.moveNext())
{
size_t iCount = 0;
char ** suggestions = enchant_dict_suggest(*it,utf8.data(),utf8.size(),&iCount);
if(suggestions)
{
for(int i=0;i<iCount;i++)
hAllSuggestions.insert(QString::fromUtf8(suggestions[i]),1);
enchant_dict_free_string_list(*it,suggestions);
}
}
}
KviKvsArray * pArray = new KviKvsArray();
QList<QString> lSuggestions = hAllSuggestions.keys();
Q_FOREACH(QString szSuggestion,lSuggestions)
pArray->append(new KviKvsVariant(szSuggestion));
c->returnValue()->setArray(pArray);
return true;
}
static void spellchecker_reload_dicts()
{
while (!g_pEnchantDicts->isEmpty())
enchant_broker_free_dict(g_pEnchantBroker, g_pEnchantDicts->takeFirst());
const QStringList& wantedDictionaries = KVI_OPTION_STRINGLIST(KviOption_stringlistSpellCheckerDictionaries);
foreach(QString szLang, wantedDictionaries) {
EnchantDict* pDict = enchant_broker_request_dict(g_pEnchantBroker, szLang.toUtf8().data());
if (pDict) {
g_pEnchantDicts->append(pDict);
} else {
qDebug("Can't load spellchecker dictionary %s: %s", szLang.toUtf8().data(), enchant_broker_get_error(g_pEnchantBroker));
}
}
}
/*
@doc: spellchecker.reloadDictionaries
@type:
command
@title:
spellchecker.reloadDictionaries
@short:
Reload spell checker dictionaries
@syntax:
spellchecker.reloadDictionaries
@description:
Reload spell checker dictionaries
*/
static bool spellchecker_kvs_reload_dictionaries(KviKvsModuleCommandCall * c)
{
KVSM_PARAMETERS_BEGIN(c)
KVSM_PARAMETERS_END(c)
spellchecker_reload_dicts();
return true;
}
static bool spellchecker_module_init(KviModule* m)
{
g_pEnchantBroker = enchant_broker_init();
g_pEnchantDicts = new KviPointerList<EnchantDict>(/* bAutoDelete = */ false);
spellchecker_reload_dicts();
KVSM_REGISTER_SIMPLE_COMMAND(m, "reloadDictionaries", spellchecker_kvs_reload_dictionaries);
KVSM_REGISTER_FUNCTION(m, "availableDictionaries", spellchecker_kvs_available_dictionaries);
KVSM_REGISTER_FUNCTION(m, "check", spellchecker_kvs_check);
KVSM_REGISTER_FUNCTION(m, "suggestions", spellchecker_kvs_suggestions);
return true;
}
static bool spellchecker_module_cleanup(KviModule*)
{
while (!g_pEnchantDicts->isEmpty())
enchant_broker_free_dict(g_pEnchantBroker, g_pEnchantDicts->takeFirst());
delete g_pEnchantDicts;
g_pEnchantDicts = NULL;
enchant_broker_free(g_pEnchantBroker);
g_pEnchantBroker = NULL;
return true;
}
KVIRC_MODULE(
"SpellChecker", // module name
"4.0.0", // module version
"Copyright (C) 2014 Alexey Sokolov (sokolov at google dot com)", // author & (C)
"Spell checker",
spellchecker_module_init,
0,
0,
spellchecker_module_cleanup,
0
)
|