forked from MGuffin/xTranslator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTESVT_SpellCheck.pas
More file actions
499 lines (453 loc) · 13.9 KB
/
TESVT_SpellCheck.pas
File metadata and controls
499 lines (453 loc) · 13.9 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
{
Fallout4translator - TESVTranslator - sseTranslator by McGuffin mguffin[arobase]gmail.com
main distribution here: http://www.nexusmods.com/skyrimspecialedition/mods/134?
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in
compliance with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS"
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
License for the specific language governing rights and limitations
under the License.
Note on extra Components used by this Tool:
-SynEdit: https://github.com/SynEdit/SynEdit
-VirtualStringTree: written by Mike Lischke (www.soft-gems.net).
-Diff: Search Heuristic compare is using the Diff Unit http://www.angusj.com/delphi/textdiff.html credit to Angus Johnson
-HtmlViewer: https://github.com/BerndGabriel/HtmlViewer
-ZLibex: 1.2.3 http://www.dellapasqua.com and xEdit https://github.com/TES5Edit
-LZ4 https://github.com/atelierw/LZ4Delphi and xEdit https://github.com/TES5Edit
-Part of the code for Bsa/ba2/Stream loading from xEdit https://github.com/TES5Edit
-OmniXML: https://github.com/mremec/omnixml
-PCRE Regex library: http://www.regular-expressions.info/delphi.html (unicode version)
-Hunspell Module is distributed under the Mozilla Public License Version 1.1 https://github.com/hunspell/hunspell/releases
-xmw to wav converter: xWMAEncode.exe from directX SDK/microsoft
==============================================================
SpellCheck use:
MSWORD api (oleAutomation) if installed on the system
or
Hunspell library: https://github.com/hunspell/hunspell/releases with OpenOffice/Mozilla dictionaries
}
unit TESVT_SpellCheck;
interface
uses Windows, Messages, SysUtils, Classes, Controls, Forms, Graphics, math, Dialogs, Menus, ComCtrls, ComObj, TESVT_Const, Generics.Collections, LibHunspell;
type
spParseOpt = (spParseIgnoreFirstUpper, spParseIgnoreMultiUpper, spParseIgnoreAlias);
spParseOpts = set of spParseOpt;
TscWord = class
private
fWord: string;
procedure setsWord(s: string);
public
hash: Cardinal;
startPos, endPos: integer;
sSize: tSize;
deleteMe, isFault, sSizeSet: boolean;
opentag: integer;
property sWord: string read fWord write setsWord;
constructor initdata(s: string; sp, tag: integer; sl, b: boolean; spOpts: spParseOpts);
end;
TSpellCheck = class(TObject)
private
MsWordApp, MsSuggestions: OleVariant;
Hunspell: THunspellChecker;
fActive: boolean;
spCache: tlist<Cardinal>;
spCacheFault: tlist<Cardinal>;
spDictList: tstringlist;
spIgnoreList: tstringlist;
function CreateHunspell(dic: string = ''): boolean;
function CreateMSWord: boolean;
procedure saveIgnoreList;
procedure LoadIgnoreList;
procedure AddIgnoreListToCacheList;
procedure AddWordToCacheList(word: string);
public
spWords: tlist;
spellCheckType: integer; // 0: MSWord, 1:Hunspell
spSuggestionList: tstringlist;
parseOptions: spParseOpts;
bFaultRatioLock: boolean;
constructor Create(SPType: integer; b: boolean; iPOpt: byte; dic: string = '');
destructor Destroy; override;
property Active: boolean read fActive;
function CheckSpellingSimple(StrWord: string): boolean;
function CheckSpelling(StrWord: string; Suggestions: TStrings): boolean;
function CheckSpellingMSWord(StrWord: string; Suggestions: TStrings): boolean;
function CheckSpellingSimpleMSWord(StrWord: string): boolean;
function GetDict(Path: String; FDictList: TStrings): boolean;
procedure clearWordsList;
procedure SplitTextIntoWords(const s: string);
function isWordInCache(word: string): boolean;
procedure update(SPType: integer; b: boolean; dic: string = '');
procedure AddWordToIgnoreList(word: string);
function analyzeText(text: string; doIdle: boolean = true): boolean;
function getWordFromCharIndex(charIndex: integer): TscWord;
procedure reset;
end;
var
spCheck: TSpellCheck;
implementation
function TSpellCheck.GetDict(Path: String; FDictList: TStrings): boolean;
var
i: integer;
begin
for i:= 0 to pred(spDictList.count) do
FDictList.add(extractFileName(spDictList[i]));
result:= FDictList.count > 0;
end;
constructor TscWord.initdata(s: string; sp, tag: integer; sl, b: boolean; spOpts: spParseOpts);
var
i, newStartPos, newEndPos: integer;
begin
startPos:= sp - 1;
opentag:= tag;
isFault:= b;
sSizeSet:= false;
// clean Word:
newStartPos:= 1;
newEndPos:= length(s);
// cleanup Cut hyphen at start or end of a word
for i:= 1 to length(s) do
if isChar_WordDelimitersSPAcceptedInside(s[i]) then
inc(newStartPos)
else
break;
for i:= length(s) downTo 1 do
if isChar_WordDelimitersSPAcceptedInside(s[i]) then
dec(newEndPos)
else
break;
// cleanup apostrophe when charPos = 2 only
if (length(s) > (newStartPos + 1)) and isChar_WordDelimitersSPAcceptedInside(s[newStartPos + 1]) then
inc(newStartPos, 2);
// word cleaned
sWord:= copy(s, newStartPos, newEndPos - (newStartPos - 1));
// prepare deletion for special case
deleteMe:= (length(sWord) < 2);
// checkUppercase en debut de ligne (ignore)
deleteMe:= deleteMe or ((spParseIgnoreFirstUpper in spOpts) and (not sl) and (length(sWord) > 1) and (sWord[1] = ansiupperCase(sWord[1])));
// check for more uppercase
for i:= 2 to length(sWord) do
begin
if deleteMe then
break;
deleteMe:= deleteMe or ((spParseIgnoreMultiUpper in spOpts) and (not isChar_WordDelimitersSPAcceptedInside(sWord[i]) and (sWord[i] = ansiupperCase(sWord[i]))));
end;
// checkfor Number
for i:= 1 to length(sWord) do
begin
if deleteMe then
break;
deleteMe:= deleteMe or charInSet(sWord[i], ['0' .. '9']);
end;
// define new pos in string
startPos:= startPos + (newStartPos - 1);
endPos:= startPos + length(sWord);
end;
procedure TscWord.setsWord(s: string);
begin
fWord:= s;
hash:= stringHash(s);
end;
constructor TSpellCheck.Create(SPType: integer; b: boolean; iPOpt: byte; dic: string = '');
begin
parseOptions:= spParseOpts(iPOpt);
spDictList:= tstringlist.Create;
spIgnoreList:= tstringlist.Create;
spWords:= tlist.Create;
spCache:= tlist<Cardinal>.Create;
spCacheFault:= tlist<Cardinal>.Create;
spSuggestionList:= tstringlist.Create;
LoadIgnoreList;
FindDictEx(mainpath + SPELL_CHECK_DICPATH, spDictList);
update(SPType, b, dic);
end;
destructor TSpellCheck.Destroy;
begin
clearWordsList;
spDictList.free;
saveIgnoreList;
spIgnoreList.free;
spCache.free;
spCacheFault.free;
spWords.free;
spSuggestionList.free;
reset;
inherited Destroy;
end;
procedure TSpellCheck.LoadIgnoreList;
begin
if not FileExists(mainpath + SPELL_CHECK_PATH + SPELL_CHECK_IGNORELIST) then
exit;
try
spIgnoreList.LoadFromFile(mainpath + SPELL_CHECK_PATH + SPELL_CHECK_IGNORELIST, TEncoding.UTF8);
except
on E: Exception do
begin
spIgnoreList.clear;
doFeedback(E.Message, false);
end;
end;
end;
procedure TSpellCheck.saveIgnoreList;
begin
if spIgnoreList.count = 0 then
exit;
try
ForceDirectories(mainpath + SPELL_CHECK_PATH);
spIgnoreList.SaveToFile(mainpath + SPELL_CHECK_PATH + SPELL_CHECK_IGNORELIST, TEncoding.UTF8);
except
on E: Exception do
doFeedback(E.Message, false);
end;
end;
procedure TSpellCheck.AddIgnoreListToCacheList;
var
i: integer;
begin
for i:= 0 to pred(spIgnoreList.count) do
AddWordToCacheList(spIgnoreList[i]);
end;
procedure TSpellCheck.AddWordToCacheList(word: string);
var
hash: Cardinal;
index: integer;
begin
hash:= stringHash(word);
if not spCache.BinarySearch(hash, index) then
spCache.insert(index, hash);
end;
procedure TSpellCheck.AddWordToIgnoreList(word: string);
begin
spIgnoreList.add(word);
AddWordToCacheList(word);
end;
procedure TSpellCheck.reset;
begin
if spellCheckType = 1 then
freeandNil(Hunspell)
else
begin
if fActive then
MsWordApp.Quit;
MsWordApp:= VarNull;
end;
fActive:= false;
end;
procedure TSpellCheck.update(SPType: integer; b: boolean; dic: string = '');
begin
spellCheckType:= SPType;
spCache.clear;
spCacheFault.clear;
if b then
if spellCheckType = 1 then
fActive:= CreateHunspell(dic)
else
fActive:= CreateMSWord;
if fActive then
AddIgnoreListToCacheList;
end;
function TSpellCheck.CreateHunspell(dic: string = ''): boolean;
begin
result:= false;
if spDictList.count = 0 then
exit;
try
Hunspell:= THunspellChecker.Create(HUNSPELLDLL);
if dic = '' then
dic:= extractFileName(spDictList[0]);
result:= Hunspell.Initialize(mainpath + SPELL_CHECK_DICPATH + ChangeFileExt(dic, '.aff'), mainpath + SPELL_CHECK_DICPATH + dic);
sUseSpellCheckDic:= dic;
except
on E: Exception do
doFeedback('Cannot Connect to Hunspell', false);
end;
end;
function TSpellCheck.CreateMSWord: boolean;
begin
result:= false;
try
MsWordApp:= CreateOleObject('Word.Application');
MsWordApp.Documents.add;
result:= true;
except
on E: Exception do
begin
doFeedback('Cannot Connect to MSWord', false);
end;
end;
end;
procedure TSpellCheck.clearWordsList;
var
i: integer;
begin
for i:= 0 to spWords.count - 1 do
TscWord(spWords[i]).free;
spWords.clear;
end;
function TSpellCheck.CheckSpelling(StrWord: string; Suggestions: TStrings): boolean;
begin
Suggestions.clear;
if fActive then
begin
if spellCheckType = 1 then
result:= Hunspell.Suggest(StrWord, Suggestions) = 0
else
result:= CheckSpellingMSWord(StrWord, Suggestions);
end
else
result:= true;
end;
function TSpellCheck.CheckSpellingMSWord(StrWord: string; Suggestions: TStrings): boolean;
var
i: integer;
begin
Suggestions.clear;
if fActive then
begin
MsSuggestions:= MsWordApp.GetSpellingSuggestions(StrWord);
for i:= 1 to MsSuggestions.count do
Suggestions.add(MsSuggestions.Item(i));
MsSuggestions:= VarNull;
end;
result:= Suggestions.count = 0;
end;
function TSpellCheck.CheckSpellingSimple(StrWord: string): boolean;
begin
if fActive then
begin
if length(StrWord) = 1 then
exit(true); // skip string of 1 char
if spellCheckType = 1 then
result:= Hunspell.Spell(StrWord)
else
result:= CheckSpellingSimpleMSWord(StrWord);
end
else
result:= true;
end;
function TSpellCheck.CheckSpellingSimpleMSWord(StrWord: string): boolean;
var
Retvar: boolean;
begin
Retvar:= false;
if fActive then
begin
if MsWordApp.CheckSpelling(StrWord) then
Retvar:= true
end;
result:= Retvar;
end;
procedure TSpellCheck.SplitTextIntoWords(const s: string);
var
startPos, endPos, opentag, i: integer;
bOpenTag, bStartLine: boolean;
begin
clearWordsList;
startPos:= 1;
opentag:= 0;
bOpenTag:= false;
bStartLine:= true;
while startPos <= length(s) do
begin
while (startPos <= length(s)) and isChar_WordDelimiter(s[startPos]) do // charInSet(S[startpos], cWordDelimiters+ cWordDelimitersSP) do
begin
// --skip all stuff between < >
if (s[startPos] = '<') and not bOpenTag then
begin
inc(opentag);
bOpenTag:= true;
end;
if (s[startPos] = '>') and bOpenTag then
bOpenTag:= false;
if isChar_WordDelimitersEndLine(s[startPos]) then
bStartLine:= true;
inc(startPos);
end;
if startPos <= length(s) then
begin
endPos:= startPos + 1;
while (endPos <= length(s)) and (not isChar_WordDelimiter(s[endPos])) do
inc(endPos);
spWords.add(TscWord.initdata(copy(s, startPos, endPos - startPos), startPos, ifthen(bOpenTag, opentag, 0), bStartLine, false, parseOptions));
bStartLine:= false;
startPos:= endPos; // + 1;
end;
end;
// cleanUp: if bOpenTag then last tag is only opened
if spParseIgnoreAlias in parseOptions then
begin
if bOpenTag then
dec(opentag);
end
else
opentag:= 0;
// cleanup list for word in opentag (or empty)
for i:= pred(spWords.count) downto 0 do
if (TscWord(spWords[i]).deleteMe) or // delete word with uppercase in middle of a sentence
((TscWord(spWords[i]).opentag > 0) and (TscWord(spWords[i]).opentag <= opentag)) // delete word inside <>
then
begin
TscWord(spWords[i]).free;
spWords.delete(i);
end;
end;
function TSpellCheck.isWordInCache(word: string): boolean;
var
index: integer;
begin
result:= spCache.BinarySearch(stringHash(word), index);
end;
function TSpellCheck.analyzeText(text: string; doIdle: boolean = true): boolean;
var
i, index, indexFault: integer;
scw: TscWord;
cleanWord: boolean;
iFaultRatioBase, iFaultRatio: integer;
begin
// cache correct words in spCache, and faulty words in spCache Fault to avoid redundant spellcheck call
result:= false;
SplitTextIntoWords(text);
iFaultRatioBase:= spWords.count;
for i:= spWords.count - 1 downto 0 do
begin
scw:= spWords[i];
cleanWord:= spCache.BinarySearch(scw.hash, index);
if not cleanWord then
begin
if spCacheFault.BinarySearch(scw.hash, indexFault) then
scw.isFault:= true
else if not CheckSpellingSimple(scw.sWord) then
begin
scw.isFault:= true;
spCacheFault.insert(indexFault, scw.hash);
end;
if scw.isFault then
result:= true
else
begin
spCache.insert(index, scw.hash);
cleanWord:= true;
end
end;
if cleanWord then
begin
scw.free;
spWords.delete(i);
end;
if doIdle and (i mod 50 = 0) then
application.processmessages;
end;
iFaultRatio:= spWords.count;
bFaultRatioLock:= (iFaultRatio > maxSpCheckUnderline) and ((divide(iFaultRatio, iFaultRatioBase) * 100) > maxSpCheckUnderlineRatio);
end;
function TSpellCheck.getWordFromCharIndex(charIndex: integer): TscWord;
var
i: integer;
begin
result:= nil;
for i:= 0 to pred(spWords.count) do
if inrange(charIndex, TscWord(spWords[i]).startPos, TscWord(spWords[i]).endPos) then
exit(spWords[i]);
end;
end.