-
Notifications
You must be signed in to change notification settings - Fork 1
/
SuggestMgr.cs
1616 lines (1441 loc) · 49.4 KB
/
SuggestMgr.cs
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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
namespace HunspellSharp
{
using static Utils;
partial class Hunspell
{
string ckey;
int ckeyl;
string ctry;
int ctryl;
bool lang_with_dash_usage;
const int maxSug = 15;
internal const int MAX_ROOTS = 100;
internal const int MAX_WORDS = 100;
internal const int MAX_GUESS = 200;
const int MAXNGRAMSUGS = 4;
const int MAXPHONSUGS = 2;
const int MAXCOMPOUNDSUGS = 3;
const int MAX_CHAR_DISTANCE = 4;
const int NGRAM_LONGER_WORSE = (1 << 0);
const int NGRAM_ANY_MISMATCH = (1 << 1);
const int NGRAM_LOWERING = (1 << 2);
const int NGRAM_WEIGHTED = (1 << 3);
const long TIMELIMIT_SUGGESTION = 1000 / 10;
void InitializeSuggestMgr()
{
// register affix manager and check in string of chars to
// try when building candidate suggestions
if (maxngramsugs < 0) maxngramsugs = MAXNGRAMSUGS;
if (maxcpdsugs < 0) maxcpdsugs = MAXCOMPOUNDSUGS;
ckey = get_key_string();
if (!string.IsNullOrEmpty(ckey))
{
ckeyl = ckey.Length;
}
ctry = trystring;
if (!string.IsNullOrEmpty(ctry))
{
ctryl = ctry.Length;
// language with possible dash usage
// (latin letters or dash in TRY characters)
lang_with_dash_usage = ctry.IndexOf('-') >= 0 || ctry.IndexOf('a') >= 0;
}
}
void testsug(List<string> wlst,
string candidate,
int cpdsuggest,
CountdownTimer timer,
ref SPELL info)
{
if (wlst.Count == maxSug)
return;
if (wlst.IndexOf(candidate) < 0)
{
int result = checkword(candidate, cpdsuggest, timer);
if (result != 0) {
// compound word in the dictionary
if (cpdsuggest == 0 && result >= 2)
info |= SPELL.COMPOUND;
wlst.Add(candidate);
}
}
}
/* generate suggestions for a misspelled word
* pass in address of array of char * pointers
* onlycompoundsug: probably bad suggestions (need for ngram sugs, too)
* return value: true, if there is a good suggestion
* (REP, ph: or a dictionary word pair)
*/
bool suggest(Context ctx, List<string> slst, string word, ref bool onlycompoundsug,
// for testing compound words formed from 3 or more words:
// if test_simplesug == true, suggest() doesn't suggest compound words,
// and it returns with true at the first suggestion found
bool test_simplesug = false)
{
bool nocompoundtwowords = false; // no second or third loops, see below
int nsugorig = slst.Count, oldSug = 0;
bool good_suggestion = false;
// word reversing wrapper for complex prefixes
if (complexprefixes)
{
word = ctx.reverseword(word);
}
var timer = new Stopwatch();
// three loops:
// - the first without compounding,
// - the second one with 2-word compounding,
// - the third one with 3-or-more-word compounding
// Run second and third loops only if:
// - no ~good suggestion in the first loop
// - not for testing compound words with 3 or more words (test_simplesug == false)
SPELL info = 0;
for (int cpdsuggest = 0; cpdsuggest < 3 && !nocompoundtwowords; cpdsuggest++)
{
// initialize both in non-compound and compound cycles
timer.Restart();
// limit compound suggestion
if (cpdsuggest > 0)
oldSug = slst.Count;
// suggestions for an uppercase word (html -> HTML)
if (slst.Count < maxSug)
{
int i = slst.Count;
capchars(slst, word, cpdsuggest, ref info);
if (slst.Count > i)
good_suggestion = true;
}
// perhaps we made a typical fault of spelling
if ((slst.Count < maxSug) && (cpdsuggest == 0 || (slst.Count < oldSug + maxcpdsugs)))
{
int i = slst.Count;
replchars(ctx, slst, word, cpdsuggest, ref info);
if (slst.Count > i)
{
good_suggestion = true;
if ((info & SPELL.BEST_SUG) != 0)
return true;
}
}
if (timer.ElapsedMilliseconds > TIMELIMIT_SUGGESTION)
return good_suggestion;
if (test_simplesug && slst.Count > 0)
return true;
// perhaps we made chose the wrong char from a related set
if ((slst.Count < maxSug) &&
(cpdsuggest == 0 || (slst.Count < oldSug + maxcpdsugs)))
{
mapchars(ctx, slst, word, cpdsuggest, ref info);
}
if (timer.ElapsedMilliseconds > TIMELIMIT_SUGGESTION)
return good_suggestion;
if (test_simplesug && slst.Count > 0)
return true;
// only suggest compound words when no other ~good suggestion
if ((cpdsuggest == 0) && (slst.Count > nsugorig))
nocompoundtwowords = true;
// did we swap the order of chars by mistake
if ((slst.Count < maxSug) && (cpdsuggest == 0 || (slst.Count < oldSug + maxcpdsugs)))
{
swapchar(ctx, slst, word, cpdsuggest, ref info);
}
if (timer.ElapsedMilliseconds > TIMELIMIT_SUGGESTION)
return good_suggestion;
if (test_simplesug && slst.Count > 0)
return true;
// did we swap the order of non adjacent chars by mistake
if ((slst.Count < maxSug) && (cpdsuggest == 0 || (slst.Count < oldSug + maxcpdsugs)))
{
longswapchar(ctx, slst, word, cpdsuggest, ref info);
}
if (timer.ElapsedMilliseconds > TIMELIMIT_SUGGESTION)
return good_suggestion;
if (test_simplesug && slst.Count > 0)
return true;
// did we just hit the wrong key in place of a good char (case and keyboard)
if ((slst.Count < maxSug) && (cpdsuggest == 0 || (slst.Count < oldSug + maxcpdsugs)))
{
badcharkey(ctx, slst, word, cpdsuggest, ref info);
}
if (timer.ElapsedMilliseconds > TIMELIMIT_SUGGESTION)
return good_suggestion;
if (test_simplesug && slst.Count > 0)
return true;
// did we add a char that should not be there
if ((slst.Count < maxSug) && (cpdsuggest == 0 || (slst.Count < oldSug + maxcpdsugs)))
{
extrachar(slst, word, cpdsuggest, ref info);
}
if (timer.ElapsedMilliseconds > TIMELIMIT_SUGGESTION)
return good_suggestion;
if (test_simplesug && slst.Count > 0)
return true;
// did we forgot a char
if ((slst.Count < maxSug) && (cpdsuggest == 0 || (slst.Count < oldSug + maxcpdsugs)))
{
forgotchar(ctx, slst, word, cpdsuggest, ref info);
}
if (timer.ElapsedMilliseconds > TIMELIMIT_SUGGESTION)
return good_suggestion;
if (test_simplesug && slst.Count > 0)
return true;
// did we move a char
if ((slst.Count < maxSug) && (cpdsuggest == 0 || (slst.Count < oldSug + maxcpdsugs)))
{
movechar(ctx, slst, word, cpdsuggest, ref info);
}
if (timer.ElapsedMilliseconds > TIMELIMIT_SUGGESTION)
return good_suggestion;
if (test_simplesug && slst.Count > 0)
return true;
// did we just hit the wrong key in place of a good char
if ((slst.Count < maxSug) && (cpdsuggest == 0 || (slst.Count < oldSug + maxcpdsugs)))
{
badchar(ctx, slst, word, cpdsuggest, ref info);
}
if (timer.ElapsedMilliseconds > TIMELIMIT_SUGGESTION)
return good_suggestion;
if (test_simplesug && slst.Count > 0)
return true;
// did we double two characters
if ((slst.Count < maxSug) && (cpdsuggest == 0 || (slst.Count < oldSug + maxcpdsugs)))
{
doubletwochars(ctx, slst, word, cpdsuggest, ref info);
}
if (timer.ElapsedMilliseconds > TIMELIMIT_SUGGESTION)
return good_suggestion;
if (test_simplesug && slst.Count > 0)
return true;
// perhaps we forgot to hit space and two words ran together
// (dictionary word pairs have top priority here, so
// we always suggest them, in despite of nosplitsugs, and
// drop compound word and other suggestions)
if (cpdsuggest == 0 || (!nosplitsugs && slst.Count < oldSug + maxcpdsugs))
{
good_suggestion = twowords(ctx, slst, word, cpdsuggest, good_suggestion, ref info);
if ((info & SPELL.BEST_SUG) != 0)
return true;
}
if (timer.ElapsedMilliseconds > TIMELIMIT_SUGGESTION)
return good_suggestion;
// testing returns after the first loop
if (test_simplesug)
return slst.Count > 0;
// don't need third loop, if the second loop was successful or
// the first loop found a dictionary-based compound word
// (we don't need more, likely worse and false 3-or-more-word compound words)
if (cpdsuggest == 1 && (slst.Count > oldSug || (info & SPELL.COMPOUND) != 0))
nocompoundtwowords = true;
} // repeating ``for'' statement compounding support
if (!nocompoundtwowords && slst.Count > 0)
onlycompoundsug = true;
return good_suggestion;
}
// suggestions for an uppercase word (html -> HTML)
void capchars(List<string> wlst,
string word,
int cpdsuggest, ref SPELL info)
{
testsug(wlst, textinfo.ToUpper(word), cpdsuggest, null, ref info);
}
// suggestions for when chose the wrong char out of a related set
int mapchars(Context ctx,
List<string> wlst,
string word,
int cpdsuggest, ref SPELL info)
{
if (word.Length < 2)
return wlst.Count;
if (maptable == null)
return wlst.Count;
var timer = ctx.suggestInnerTimer;
timer.Restart();
var candidate = string.Empty;
return map_related(word, ref candidate, 0, wlst, cpdsuggest,
maptable, timer, 0, ref info);
}
int map_related(string word,
ref string candidate,
int wn,
List<string> wlst,
int cpdsuggest,
List<mapentry> maptable,
CountdownTimer timer,
int depth, ref SPELL info)
{
if (word.Length == wn)
{
if (!wlst.Contains(candidate) && checkword(candidate, cpdsuggest, timer) != 0)
{
if (wlst.Count < maxSug)
{
wlst.Add(candidate);
}
}
return wlst.Count;
}
if (depth > 0x3F00)
{
if (timer != null) timer.IsExpired = true;
return wlst.Count;
}
bool in_map = false;
for (int j = 0; j < maptable.Count; ++j)
{
for (int k = 0; k < maptable[j].Count; ++k)
{
int len = maptable[j][k].Length;
if (len > 0 && string.CompareOrdinal(word, wn, maptable[j][k], 0, len) == 0)
{
in_map = true;
var candidate_ = candidate;
for (int l = 0; l < maptable[j].Count; ++l)
{
candidate = candidate_ + maptable[j][l];
map_related(word, ref candidate, wn + len, wlst,
cpdsuggest, maptable, timer, depth + 1, ref info);
if (timer != null && timer.IsExpired)
return wlst.Count;
}
}
}
}
if (!in_map)
{
candidate += word[wn];
map_related(word, ref candidate, wn + 1, wlst, cpdsuggest,
maptable, timer, depth + 1, ref info);
}
return wlst.Count;
}
// suggestions for a typical fault of spelling, that
// differs with more, than 1 letter from the right form.
int replchars(Context ctx,
List<string> wlst,
string word,
int cpdsuggest, ref SPELL info)
{
int wl = word.Length;
if (wl < 2)
return wlst.Count;
var sb = ctx.PopStringBuilder();
for (int i = 0; i < reptable.Count; ++i)
{
var entry = reptable[i];
int r = 0;
// search every occurence of the pattern in the word
while ((r = word.IndexOf(entry.pattern, r)) >= 0) {
int type = (r == 0) ? 1 : 0;
if (r + entry.pattern.Length == word.Length)
type += 2;
while (type != 0 && string.IsNullOrEmpty(entry.outstrings[type]))
type = (type == 2 && r != 0) ? 0 : type - 1;
if (string.IsNullOrEmpty(entry.outstrings[type])) {
++r;
continue;
}
var candidate = sb.Clear().Append(word, 0, r).Append(entry.outstrings[type]).Append(word, r + entry.pattern.Length, wl - (r + entry.pattern.Length)).ToString();
int sp = candidate.IndexOf(' ');
int oldns = wlst.Count;
testsug(wlst, candidate, cpdsuggest, null, ref info);
if (oldns < wlst.Count)
{
// REP suggestions are the best, don't search other type of suggestions
info |= SPELL.BEST_SUG;
}
// check REP suggestions with space
if (sp >= 0) {
int prev = 0;
do
{
string prev_chunk = candidate.Substring(prev, sp - prev);
if (checkword(prev_chunk, 0, null) != 0)
{
oldns = wlst.Count;
string post_chunk = candidate.Substring(sp + 1);
testsug(wlst, post_chunk, cpdsuggest, null, ref info);
if (oldns < wlst.Count)
{
wlst[wlst.Count - 1] = candidate;
}
}
prev = sp + 1;
sp = candidate.IndexOf(' ', prev);
} while (sp >= 0);
}
r++; // search for the next letter
}
}
ctx.PushStringBuilder(sb);
return wlst.Count;
}
// perhaps we doubled two characters
// (for example vacation -> vacacation)
// The recognized pattern with regex back-references:
// "(.)(.)\1\2\1" or "..(.)(.)\1\2"
void doubletwochars(Context ctx,
List<string> wlst,
string word,
int cpdsuggest, ref SPELL info)
{
int wl = word.Length;
if (wl < 5) return;
var buf = ctx.PopBuffer(wl - 2);
int state = 0;
for (int i = 2; i < wl; ++i)
{
if (word[i] == word[i - 2])
{
state++;
if (state == 3 || (state == 2 && i >= 4))
{
word.CopyTo(0, buf, 0, i - 1);
word.CopyTo(i + 1, buf, i - 1, wl - (i + 1));
testsug(wlst, new string(buf, 0, wl - 2), cpdsuggest, null, ref info);
state = 0;
}
} else
{
state = 0;
}
}
ctx.PushBuffer(buf);
}
// error is wrong char in place of correct one (case and keyboard related
// version)
void badcharkey(Context ctx,
List<string> wlst,
string word,
int cpdsuggest, ref SPELL info)
{
int wl = word.Length;
var candidate = ctx.PopBuffer(wl);
word.CopyTo(0, candidate, 0, wl);
// swap out each char one by one and try uppercase and neighbor
// keyboard chars in its place to see if that makes a good word
for (int i = 0; i < wl; ++i)
{
char tmpc = candidate[i];
// check with uppercase letters
candidate[i] = char.ToUpper(tmpc);
if (tmpc != candidate[i])
{
testsug(wlst, new string(candidate, 0, wl), cpdsuggest, null, ref info);
candidate[i] = tmpc;
}
// check neighbor characters in keyboard string
if (ckeyl == 0)
continue;
int loc = 0;
while ((loc < ckeyl) && ckey[loc] != tmpc)
++loc;
while (loc < ckeyl)
{
if ((loc > 0) && ckey[loc - 1] != '|')
{
candidate[i] = ckey[loc - 1];
testsug(wlst, new string(candidate, 0, wl), cpdsuggest, null, ref info);
}
if (((loc + 1) < ckeyl) && (ckey[loc + 1] != '|'))
{
candidate[i] = ckey[loc + 1];
testsug(wlst, new string(candidate, 0, wl), cpdsuggest, null, ref info);
}
do
{
loc++;
} while ((loc < ckeyl) && ckey[loc] != tmpc);
}
candidate[i] = tmpc;
}
ctx.PushBuffer(candidate);
}
// error is wrong char in place of correct one
void badchar(Context ctx,
List<string> wlst,
string word,
int cpdsuggest, ref SPELL info)
{
int wl = word.Length;
var candidate = ctx.PopBuffer(wl);
word.CopyTo(0, candidate, 0, wl);
try
{
var timer = ctx.suggestInnerTimer;
timer.Restart();
// swap out each char one by one and try all the tryme
// chars in its place to see if that makes a good word
for (int j = 0; j < ctryl; ++j)
{
for (int aI = wl - 1; aI >= 0; --aI)
{
char tmpc = candidate[aI];
if (ctry[j] == tmpc)
continue;
candidate[aI] = ctry[j];
testsug(wlst, new string(candidate, 0, wl), cpdsuggest, timer, ref info);
if (timer.IsExpired) return;
candidate[aI] = tmpc;
}
}
}
finally
{
ctx.PushBuffer(candidate);
}
}
// error is word has an extra letter it does not need
void extrachar(List<string> wlst,
string word,
int cpdsuggest, ref SPELL info)
{
if (word.Length < 2) return;
// try omitting one char of word at a time
for (int i = word.Length - 1; i >= 0; --i)
{
testsug(wlst, word.Remove(i, 1), cpdsuggest, null, ref info);
}
}
// error is missing a letter it needs
void forgotchar(Context ctx,
List<string> wlst,
string word,
int cpdsuggest, ref SPELL info)
{
var timer = ctx.suggestInnerTimer;
timer.Restart();
int n = word.Length;
var candidate = ctx.PopBuffer(n + 1);
try
{
word.CopyTo(0, candidate, 0, n);
// try inserting a tryme character before every letter (and the null
// terminator)
for (int k = 0; k < ctryl; ++k)
{
for (int i = n; ; --i)
{
candidate[i] = ctry[k];
testsug(wlst, new string(candidate, 0, n + 1), cpdsuggest, timer, ref info);
if (timer.IsExpired) return;
if (i == 0) break;
candidate[i] = candidate[i - 1];
}
if (++k >= ctryl) break;
for (int i = 0; ; ++i)
{
candidate[i] = ctry[k];
testsug(wlst, new string(candidate, 0, n + 1), cpdsuggest, timer, ref info);
if (timer.IsExpired) return;
if (i == n) break;
candidate[i] = candidate[i + 1];
}
}
}
finally
{
ctx.PushBuffer(candidate);
}
}
/* error is should have been two words
* return value is true, if there is a dictionary word pair,
* or there was already a good suggestion before calling
* this function.
*/
bool twowords(Context ctx,
List<string> wlst,
string word,
int cpdsuggest,
bool good, ref SPELL info)
{
int c2, wl = word.Length;
if (wl < 3)
return false;
bool cwrd, forbidden = langnum == LANG.hu && check_forbidden(word);
var candidate = ctx.PopBuffer(wl + 1);
word.CopyTo(0, candidate, 1, wl);
// split the string into two pieces after every char
// if both pieces are good words make them a suggestion
for (int p = 1; p < wl; p++)
{
candidate[p - 1] = candidate[p];
// Suggest only word pairs, if they are listed in the dictionary.
// For example, adding "a lot" to the English dic file will
// result only "alot" -> "a lot" suggestion instead of
// "alto, slot, alt, lot, allot, aloft, aloe, clot, plot, blot, a lot".
// Note: using "ph:alot" keeps the other suggestions:
// a lot ph:alot
// alot -> a lot, alto, slot...
if (cpdsuggest == 0)
{
candidate[p] = ' ';
var candidate_ = new string(candidate, 0, wl + 1);
if (checkword(candidate_, cpdsuggest, null) != 0)
{
// best solution
info |= SPELL.BEST_SUG;
// remove not word pair suggestions
if (!good)
{
good = true;
wlst.Clear();
}
wlst.Insert(0, candidate_);
}
}
// word pairs with dash?
if (lang_with_dash_usage && cpdsuggest == 0)
{
candidate[p] = '-';
var candidate_ = new string(candidate, 0, wl + 1);
if (checkword(candidate_, cpdsuggest, null) != 0)
{
// best solution
info |= SPELL.BEST_SUG;
// remove not word pair suggestions
if (!good)
{
good = true;
wlst.Clear();
}
wlst.Insert(0, candidate_);
}
}
if (wlst.Count < maxSug && !nosplitsugs && !good)
{
int c1 = checkword(new string(candidate, 0, p), cpdsuggest, null);
if (c1 != 0)
{
c2 = checkword(new string(candidate, p + 1, wl - p), cpdsuggest, null);
if (c2 != 0)
{
// spec. Hungarian code (TODO need a better compound word support)
if ((langnum == LANG.hu) && !forbidden &&
// if 3 repeating letter, use - instead of space
(((candidate[p - 1] == candidate[p + 1]) &&
((p > 1 && (candidate[p - 1] == candidate[p - 2])) || (candidate[p - 1] == candidate[p + 2]))) ||
// or multiple compounding, with more, than 6 syllables
((c1 == 3) && (c2 >= 2))))
candidate[p] = '-';
else
candidate[p] = ' ';
var candidate_ = new string(candidate, 0, wl + 1);
cwrd = wlst.IndexOf(candidate_) < 0;
if (cwrd && (wlst.Count < maxSug))
wlst.Add(candidate_);
// add two word suggestion with dash, depending on the language
// Note that cwrd doesn't modified for REP twoword sugg.
if (!nosplitsugs && lang_with_dash_usage &&
wl - p > 1 && p > 1)
{
candidate[p] = '-';
candidate_ = new string(candidate, 0, wl + 1);
if (wlst.Contains(candidate_)) cwrd = false;
if ((wlst.Count < maxSug) && cwrd)
wlst.Add(candidate_);
}
}
}
}
}
ctx.PushBuffer(candidate);
return good;
}
// error is adjacent letter were swapped
void swapchar(Context ctx,
List<string> wlst,
string word,
int cpdsuggest, ref SPELL info)
{
int wl = word.Length;
if (wl < 2) return;
var candidate = ctx.PopBuffer(wl);
word.CopyTo(0, candidate, 0, wl);
// try swapping adjacent chars one by one
for (int i = 0; i < wl - 1; ++i)
{
char t = candidate[i]; candidate[i] = candidate[i + 1]; candidate[i + 1] = t;
testsug(wlst, new string(candidate, 0, wl), cpdsuggest, null, ref info);
candidate[i + 1] = candidate[i]; candidate[i] = t;
}
// try double swaps for short words
// ahev -> have, owudl -> would
if (wl == 4 || wl == 5)
{
candidate[0] = word[1];
candidate[1] = word[0];
candidate[2] = word[2];
candidate[wl - 2] = word[wl - 1];
candidate[wl - 1] = word[wl - 2];
testsug(wlst, new string(candidate, 0, wl), cpdsuggest, null, ref info);
if (wl == 5)
{
candidate[0] = word[0];
candidate[1] = word[2];
candidate[2] = word[1];
testsug(wlst, new string(candidate, 0, wl), cpdsuggest, null, ref info);
}
}
ctx.PushBuffer(candidate);
}
// error is not adjacent letter were swapped
void longswapchar(Context ctx,
List<string> wlst,
string word,
int cpdsuggest, ref SPELL info)
{
int n = word.Length;
var candidate = ctx.PopBuffer(n);
word.CopyTo(0, candidate, 0, n);
// try swapping not adjacent chars one by one
for (var p = 0; p < n; ++p)
{
for (var q = 0; q < n; ++q)
{
int distance = Math.Abs(q - p);
if (distance > 1 && distance <= MAX_CHAR_DISTANCE)
{
var t = candidate[p]; candidate[p] = candidate[q]; candidate[q] = t;
testsug(wlst, new string(candidate, 0, n), cpdsuggest, null, ref info);
candidate[q] = candidate[p]; candidate[p] = t;
}
}
}
ctx.PushBuffer(candidate);
}
// error is a letter was moved
void movechar(Context ctx,
List<string> wlst,
string word,
int cpdsuggest, ref SPELL info)
{
int n = word.Length;
if (n < 2) return;
var candidate = ctx.PopBuffer(n);
// try moving a char
for (var p = 0; p < n - 1; ++p)
{
word.CopyTo(0, candidate, 0, n);
for (var q = p + 1; q < n && q - p <= MAX_CHAR_DISTANCE; ++q)
{
var t = candidate[q]; candidate[q] = candidate[q - 1]; candidate[q - 1] = t;
if (q - p < 2)
continue; // omit swap char
testsug(wlst, new string(candidate, 0, n), cpdsuggest, null, ref info);
}
}
for (var p = n - 1; p >= 1; --p)
{
word.CopyTo(0, candidate, 0, n);
for (var q = p - 1; q >= 0 && p - q <= MAX_CHAR_DISTANCE; --q)
{
var t = candidate[q]; candidate[q] = candidate[q + 1]; candidate[q + 1] = t;
if (p - q < 2)
continue; // omit swap char
testsug(wlst, new string(candidate, 0, n), cpdsuggest, null, ref info);
}
}
ctx.PushBuffer(candidate);
}
// generate a set of suggestions for very poorly spelled words
void ngsuggest(Context ctx,
List<string> wlst,
string word,
CapType captype)
{
int n = word.Length;
// ofz#59067 a replist entry can generate a very long word, abandon
// ngram if that odd-edge case arises
if (n > Hunspell.MAXWORDLEN * 4)
return;
// word reversing wrapper for complex prefixes
if (complexprefixes)
{
word = ctx.reverseword(word);
}
// exhaustively search through all root words
// keeping track of the MAX_ROOTS most similar root words
ctx.GetRootsBuffers(out var roots, out var rootsphon, out var scores, out var scoresphon);
Array.Clear(roots, 0, MAX_ROOTS);
Array.Clear(rootsphon, 0, MAX_ROOTS);
for (int i = 0; i < MAX_ROOTS; ++i)
scores[i] = scoresphon[i] = -100 * (MAX_ROOTS - 1 - i);
var target = phone == null ? null : phone.phonet(ctx, textinfo.ToUpper(word)); // XXX phonet() is 8-bit (nc, not n)
foreach (var hp in walk_hashtable())
{
// skip exceptions
if (
// skip it, if the word length different by 5 or
// more characters (to avoid strange suggestions)
// (except Unicode characters over BMP)
(Math.Abs(n - hp.word.Length) > 4) ||
// don't suggest capitalized dictionary words for
// lower case misspellings in ngram suggestions, except
// - PHONE usage, or
// - in the case of German, where not only proper
// nouns are capitalized, or
// - the capitalized word has special pronunciation
((captype == CapType.NOCAP) && (hp.var & H_OPT.INITCAP) != 0 &&
phone == null && (langnum != LANG.de) && (hp.var & H_OPT.PHON) == 0) ||
// or it has one of the following special flags
(hp.astr != null &&
(TESTAFF(hp.astr, forbiddenword) ||
TESTAFF(hp.astr, ONLYUPCASEFLAG) ||
TESTAFF(hp.astr, nosuggest) ||
TESTAFF(hp.astr, nongramsuggest) ||
TESTAFF(hp.astr, onlyincompound)))
)
continue;
int leftcommon = leftcommonsubstring(word, hp.word);
int sc = ngram(3, word, hp.word, NGRAM_LONGER_WORSE | NGRAM_LOWERING) + leftcommon;
// check special pronunciation
string f;
if ((hp.var & H_OPT.PHON) != 0 &&
(f = copy_field(hp.data, 0, MORPH.PHON)) != null)
{
leftcommon = leftcommonsubstring(word, f);
int sc2 = ngram(3, word, f, NGRAM_LONGER_WORSE | NGRAM_LOWERING) + leftcommon;
if (sc2 > sc)
sc = sc2;
}
if (sc > scores[0])
{
scores[0] = sc;
roots[0] = hp;
Heapify(scores, roots);
}
if (phone != null && (sc > 2) && (Math.Abs(n - hp.word.Length) <= 3))
{
f = phone.phonet(ctx, textinfo.ToUpper(hp.word));
sc = 2 * ngram(3, target, f, NGRAM_LONGER_WORSE);
if (sc > scoresphon[0])
{
scoresphon[0] = sc;
rootsphon[0] = hp.word;
Heapify(scoresphon, rootsphon);
}
}
}
if (scores[0] == -100 * (MAX_ROOTS - 1) && scoresphon[0] == -100 * (MAX_ROOTS - 1))
{
// with no roots there will be no guesses and no point running
// ngram
return;
}
// find minimum threshold for a passable suggestion
// mangle original word three differnt ways
// and score them to generate a minimum acceptable score
int thresh = 0;
var lword = textinfo.ToLower(word);
var mw = ctx.PeekBuffer(n);
for (int sp = 1; sp < 4; sp++)
{
lword.CopyTo(0, mw, 0, n);
for (int k = sp; k < n; k += 4)
mw[k] = '*';
thresh += ngram(n, word, new string(mw, 0, n), NGRAM_ANY_MISMATCH);
}
thresh = (thresh / 3) - 1;
// now expand affixes on each of these root words and
// and use length adjusted ngram scores to select
// possible suggestions
ctx.GetGuessBuffers(out var guess, out var gscore, out var glst);
Array.Clear(guess, 0, MAX_GUESS);
for (int i = 0; i < MAX_GUESS; ++i)
gscore[i] = Math.Max(thresh, -100 * (MAX_GUESS - 1 - i));
for (int i = 0; i < MAX_ROOTS; ++i)
{
var rp = roots[i];
if (rp == null) continue;
int nw = expand_rootword(ctx, glst, rp.word, rp.astr, word, (rp.var & H_OPT.PHON) != 0 ? copy_field(rp.data, 0, MORPH.PHON) : null);
for (int k = 0; k < nw; k++)
{
var f = glst[k].word;
int leftcommon = leftcommonsubstring(word, f);
int sc = ngram(n, word, f, NGRAM_ANY_MISMATCH | NGRAM_LOWERING) + leftcommon;
if (sc > gscore[0])
{
gscore[0] = sc;
guess[0] = new Guess(glst[k]);
Heapify(gscore, guess);
}
}
}
// now we are done generating guesses
// weight suggestions with a similarity index, based on
// the longest common subsequent algorithm and resort
double fact = maxdiff < 0 ? 1.0 : ((10.0 - maxdiff) / 5.0);
for (int i = 0; i < MAX_GUESS; i++)
{
var gl = guess[i].word;
if (gl == null)
{
gscore[i] = int.MinValue;
continue;
}
// lowering guess[i]
gl = textinfo.ToLower(guess[i].word);
var len = gl.Length;
int _lcs = lcslen(ctx, word, gl);