/
hunspell.lcb
322 lines (247 loc) · 10.3 KB
/
hunspell.lcb
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
library community.livecode.trevordevore.hunspell
use com.livecode.engine
use com.livecode.foreign
metadata title is "Hunspell"
metadata author is "Trevor DeVore"
metadata version is "0.2.0"
constant kMCStringEncodingUTF8 is 4
public type Hunspell is Pointer
-- size_t strlen(const char *str)
foreign handler CStrLen(in pStrPtr as Pointer) returns UIntSize binds to "c:strlen"
foreign handler CStrLenWin(in pStrPtr as Pointer) returns UIntSize binds to "msvcrt>strlen"
foreign handler MCStringCreateWithCString(in pString as Pointer, out rString as String) returns CBool binds to "<builtin>"
foreign handler MCStringConvertToBytes(in pString as String, in pEncoding as CUInt, in pIsExternalRep as CBool, out rBytes as Pointer, out rByteCount as LCUInt) \
returns CBool binds to "<builtin>"
foreign handler MCStringCreateWithBytes(in pBytes as Pointer, in pByteCount as LCUInt, in pEncoding as CUInt, in pIsExternalRep as CBool, out rString as String) \
returns CBool binds to "<builtin>"
foreign handler MCDataGetBytePtr(in pData as Data) returns Pointer binds to "<builtin>"
foreign handler MCAggregateTypeInfo(in pBinding as String, out rTypeInfo as Pointer) returns CBool binds to "<builtin>"
foreign handler MCValueRelease(in pValue as Pointer) returns nothing binds to "<builtin>"
foreign handler MCProperListCreateWithForeignValues(in pTypeInfo as Pointer, \
in pCArray as Pointer, in pCount as LCUIndex, out pList as List) returns CBool binds to "<builtin>"
foreign handler hunspell_create (in pAffPath as ZStringUTF8, in pDPath as ZStringUTF8) \
returns Hunspell \
binds to "c:libhunspell>Hunspell_create"
foreign handler hunspell_destroy (in pHunspell as Hunspell) \
returns nothing \
binds to "c:libhunspell>Hunspell_destroy"
foreign handler hunspell_add_dic (in pHunspell as Hunspell, in pDPath as ZStringUTF8) \
returns CInt \
binds to "c:libhunspell>Hunspell_add_dic"
foreign handler hunspell_spell (in pHunspell as Hunspell, in pUTF8String as Pointer) \
returns CInt \
binds to "c:libhunspell>Hunspell_spell"
foreign handler hunspell_get_dic_encoding (in pHunspell as Hunspell) \
returns Pointer \
binds to "c:libhunspell>Hunspell_get_dic_encoding"
foreign handler hunspell_suggest (in pHunspell as Hunspell, inout pSuggestionList as optional Pointer, in pUTF8String as Pointer) \
returns CInt \
binds to "c:libhunspell>Hunspell_suggest"
foreign handler hunspell_add (in pHunspell as Hunspell, in pWord as ZStringUTF8) \
returns CInt \
binds to "c:libhunspell>Hunspell_add"
foreign handler hunspell_add_with_affix (in pHunspell as Hunspell, in pWord as ZStringUTF8, in pExample as ZStringUTF8) \
returns CInt \
binds to "c:libhunspell>Hunspell_add_with_affix"
foreign handler hunspell_remove (in pHunspell as Hunspell, in pWord as ZStringUTF8) \
returns CInt \
binds to "c:libhunspell>Hunspell_remove"
// Use `inout` for pSuggestionList. Otherwise app crashes when called.
foreign handler hunspell_free_list (in pHunspell as Hunspell, inout pSuggestionList as Pointer, in pNumSuggestions as CInt) \
returns nothing \
binds to "c:libhunspell>Hunspell_free_list"
private variable mHunspell as optional Hunspell
/**
Summary: Initialize the internal Hunspell pointer with an affix and dictionary file.
Parameters:
pAffixPath: The path to the affix file. Must use UTF-8 encoding.
pDictPath: The path to the dictionary file. Must use UTF-8 encoding.
Returns: nothing
*/
public handler hunspellInitialize(in pAffixPath as String, in pDictPath as String) returns nothing
unsafe
if mHunspell is not nothing then
hunspell_destroy(mHunspell)
end if
put hunspell_create(pAffixPath, pDictPath) into mHunspell
end unsafe
end handler
/**
Summary: Destroy the internal Hunspell pointer before your app quits.
Returns: nothing
*/
public handler hunspellDestroy() returns nothing
unsafe
if (mHunspell is not nothing) then
hunspell_destroy(mHunspell)
put nothing into mHunspell
end if
end unsafe
end handler
/**
Summary: Load additional dictionary files.
Parameters:
pDictPath: The path to the dictionary file. Must use UTF-8 encoding.
Description:
The dictionary file must have the same number on the first line as the dictionary
that was loaded using `hunspellInitialize()`.
A return value of 0 means there are additional dictionary slots available.
A value of 1 means the slots are now full.
Returns: Integer
*/
public handler hunspellAddDic(in pDictPath as String) returns Integer
unsafe
return hunspell_add_dic(mHunspell, pDictPath)
end unsafe
end handler
/**
Summary: Spellcheck word.
Description:
Returns `true` if the word is spelled correctly, `false` otherwise.
Returns: Boolean
*/
public handler hunspellSpell(in pString as String) returns Boolean
variable tRslt as Integer
variable tUTF8String as Pointer
variable tByteCount as Number
unsafe
/*
0 = bad word
not 0 = good word
TODO: Convert string based on dictionary encoding. Currently assuming it is UTF-8.
*/
if MCStringConvertToBytes(pString, kMCStringEncodingUTF8, false, tUTF8String, tByteCount) then
put hunspell_spell(mHunspell, tUTF8String) into tRslt
return tRslt is not 0
else
log "Error: unable to convert string to UTF-8"
return false
end if
end unsafe
end handler
/**
Summary: Returns the encoding of the currently loaded dictionary.
Returns: String
*/
public handler hunspellGetDicEncoding() returns String
variable tEncPtr as Pointer
variable tEncoding as String
unsafe
put hunspell_get_dic_encoding(mHunspell) into tEncPtr
if not MCStringCreateWithCString(tEncPtr, tEncoding) then
log "Error: failed to convert const char * to String"
end if
end unsafe
return tEncoding
end handler
/**
Summary: Returns spelling suggestions for a string.
Parameters:
pString: The string to make suggestions for.
Returns: CR-delimited list of suggestions.
*/
public handler hunspellSuggest(in pString as String) returns String
variable tUTF8String as Pointer
variable tByteCount as Number
variable tNumSuggestions as Integer
variable tSuggestionsCArray as optional Pointer -- array of strings
variable tSuggestionsList as List
variable tSuggestions as String
variable tStrList as List
variable tStrLen as Integer
variable tSuggestion as String
unsafe
if MCStringConvertToBytes(pString, kMCStringEncodingUTF8, false, tUTF8String, tByteCount) then
// as per Hunspell header, tSuggestionsCArray must not be initialized, hence the `optional`
put hunspell_suggest(mHunspell, tSuggestionsCArray, tUTF8String) into tNumSuggestions
if tNumSuggestions > 0 then
variable tList as List
if CArrayToList("r", tSuggestionsCArray, tNumSuggestions, tList) then
repeat for each element tStrList in tList
// TODO: Convert string based on dictionary encoding. Currently assuming it is UTF-8.
if the operating system is "windows" then
put CStrLenWin(tStrList[1]) into tStrLen
else
put CStrLen(tStrList[1]) into tStrLen
end if
if MCStringCreateWithBytes(tStrList[1], tStrLen , kMCStringEncodingUTF8, false, tSuggestion) then
push tSuggestion onto tSuggestionsList
else
log "Error: failed to convert UTF-8 suggestion to string"
end if
end repeat
else
log "Error: failed to convert CArray to List"
end if
hunspell_free_list(mHunspell, tSuggestionsCArray, tNumSuggestions)
end if
else
log "Error: failed to convert string to UTF-8"
end if
end unsafe
combine tSuggestionsList with newline into tSuggestions
return tSuggestions
end handler
/**
Summary: Add a new word to the run-time dictionary.
Parameters:
pWord: The word to add.
Returns: Boolean
*/
public handler hunspellAdd(in pWord as String) returns Boolean
variable tRslt as Integer
unsafe
put hunspell_add(mHunspell, pWord) into tRslt
return tRslt is 0
end unsafe
end handler
/**
Summary: Add a word to the run-time dictionary with affix flags of the example.
Parameters:
pWord: The word to add.
pExample: The affix example.
Returns: Boolean
*/
public handler hunspellAddWithAffix(in pWord as String, in pExample as String) returns Boolean
variable tRslt as Integer
unsafe
put hunspell_add_with_affix(mHunspell, pWord, pExample) into tRslt
return tRslt is 0
end unsafe
end handler
/**
Summary: Remove a word from the run-time dictionary.
Parameters:
pWord: The word to remove.
Returns: Boolean
*/
public handler hunspellRemove(in pWord as String) returns Boolean
variable tRslt as Integer
unsafe
put hunspell_remove(mHunspell, pWord) into tRslt
return tRslt is 0
end unsafe
end handler
/**
Summary: Converts a C Array into a List. The list returns a list of lists.
Description:
This is used to convert the array of strings that is created by `hunspell_suggest`.
Code provided by Monte Goulding.
Returns: Boolean
*/
unsafe handler CArrayToList(in pAggregateBinding as String, in pCArray as Pointer, in pCount as Integer, out rList as List) returns Boolean
variable tTypeInfo as Pointer
if not MCAggregateTypeInfo(pAggregateBinding, tTypeInfo) then
return false
end if
variable tList as List
variable tSuccess as Boolean
put MCProperListCreateWithForeignValues(tTypeInfo, pCArray, pCount, tList) into tSuccess
MCValueRelease(tTypeInfo)
if not tSuccess then
return false
end if
put tList into rList
return true
end handler
end library