-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHunspell.cs
1896 lines (1724 loc) · 56.7 KB
/
Hunspell.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.IO;
using System.Text;
using System.Threading;
namespace HunspellSharp
{
using static Utils;
/// <summary>
/// Implements Hunspell checker
/// </summary>
public partial class Hunspell : IDisposable
{
StringHelper helper = new StringHelper();
const string HZIP_EXTENSION = ".hz";
const string SPELL_XML = "<?xml?>";
const int MAXSHARPS = 5;
const int MAXWORDLEN = 100;
const long TIMELIMIT_GLOBAL = 1000 / 4;
ThreadLocal<Context> context = new ThreadLocal<Context>(() => new Context());
Context Context => context.Value;
/// <summary>
/// Initializes a new instance of the <see cref="Hunspell" /> for the specific streams.
/// </summary>
/// <param name="aff">Affix stream</param>
/// <param name="dic">Dictionary stream</param>
public Hunspell(Stream aff, Stream dic)
{
LoadAff(aff is HzipStream hzAff ? (FileMgr)hzAff.CreateHunzip() : new PlainFile(aff));
LoadDic(dic is HzipStream hzDic ? (FileMgr)hzDic.CreateHunzip() : new PlainFile(dic));
InitializeSuggestMgr();
}
/// <summary>
/// Initializes a new instance of the <see cref="Hunspell" /> for the specified affix and dictionary file paths.
/// </summary>
/// <param name="aff">Affix file path</param>
/// <param name="dic">Dictionary file path</param>
/// <param name="key">Optional hzip key</param>
public Hunspell(string aff, string dic, byte[] key = null)
{
try
{
using (var stream = new FileStream(aff, FileMode.Open, FileAccess.Read, FileShare.Read))
LoadAff(new PlainFile(stream));
}
catch (FileNotFoundException e)
{
try
{
using (var stream = new FileStream(aff + HZIP_EXTENSION, FileMode.Open, FileAccess.Read, FileShare.Read))
LoadAff(new Hunzip(stream, key));
}
catch (FileNotFoundException)
{
throw e;
}
}
try
{
using (var stream = new FileStream(dic, FileMode.Open, FileAccess.Read, FileShare.Read))
LoadDic(new PlainFile(stream));
}
catch (FileNotFoundException e)
{
try
{
using (var stream = new FileStream(dic + HZIP_EXTENSION, FileMode.Open, FileAccess.Read, FileShare.Read))
LoadDic(new Hunzip(stream, key));
}
catch (FileNotFoundException)
{
throw e;
}
}
InitializeSuggestMgr();
}
/// <summary>
/// Loads extra dictionary.
/// </summary>
/// <param name="dic">Dictionary stream</param>
/// <returns><code>true</code> on success, <code>false</code> if there were format errors in non-strict mode
/// In strict mode (<code>StrictFormat == true</code>) it throws an exception.
/// It may also throw standard I/O exceptions.</returns>
public bool AddDic(Stream dic)
{
return load_tables(dic is HzipStream hzDic ? (FileMgr)hzDic.CreateHunzip() : new PlainFile(dic));
}
/// <summary>
/// Loads extra dictionary.
/// </summary>
/// <param name="dic">Dictionary file path</param>
/// <param name="key">Optional hzip key</param>
/// <returns><code>true</code> on success, <code>false</code> if there were format errors in non-strict mode
/// In strict mode (<code>StrictFormat == true</code>) it throws an exception.
/// It may also throw standard I/O exceptions.</returns>
public bool AddDic(string dic, byte[] key = null)
{
try
{
using (var stream = new FileStream(dic, FileMode.Open, FileAccess.Read, FileShare.Read))
return load_tables(new PlainFile(stream));
}
catch (FileNotFoundException e)
{
try
{
using (var stream = new FileStream(dic + HZIP_EXTENSION, FileMode.Open, FileAccess.Read, FileShare.Read))
return load_tables(new Hunzip(stream, key));
}
catch (FileNotFoundException)
{
throw e;
}
}
}
// make a copy of src at destination while removing all leading
// blanks and removing any trailing periods after recording
// their presence with the abbreviation flag
// also since already going through character by character,
// set the capitalization type
// return the length of the "cleaned" (and UTF-8 encoded) word
string cleanword2(Context ctx,
string src,
out CapType pcaptype,
out int pabbrev)
{
// remove IGNORE characters from the string
src = remove_ignored_chars(src, ctx);
int q = 0, nl = src.Length;
// first skip over any leading blanks
while (nl > 0 && src[q] == ' ')
{
++q;
nl--;
}
// now strip off any trailing periods (recording their presence)
pabbrev = 0;
while ((nl > 0) && (src[q + nl - 1] == '.'))
{
nl--;
pabbrev++;
}
// if no characters are left it can't be capitalized
if (nl <= 0)
{
pcaptype = CapType.NOCAP;
return string.Empty;
}
if (nl < src.Length) src = src.Substring(q, nl);
pcaptype = get_captype(src, textinfo);
return src;
}
string cleanword(string src,
out CapType pcaptype,
out int pabbrev)
{
int q = 0, nl = src.Length;
// first skip over any leading blanks
while (nl > 0 && src[q] == ' ')
{
++q;
nl--;
}
// now strip off any trailing periods (recording their presence)
pabbrev = 0;
while ((nl > 0) && (src[q + nl - 1] == '.'))
{
nl--;
pabbrev++;
}
// if no characters are left it can't be capitalized
if (nl <= 0)
{
pcaptype = CapType.NOCAP;
return string.Empty;
}
// now determine the capitalization type of the first nl letters
int ncap = 0;
int nneutral = 0;
for (int i = q; i < nl; ++i)
{
if (char.IsUpper(src, i))
ncap++;
if (textinfo.ToUpper(src[i]) == textinfo.ToLower(src[i]))
nneutral++;
}
// remember to terminate the destination string
bool firstcap = char.IsUpper(src, q);
// now finally set the captype
if (ncap == 0)
{
pcaptype = CapType.NOCAP;
}
else if ((ncap == 1) && firstcap)
{
pcaptype = CapType.INITCAP;
}
else if ((ncap == nl) || ((ncap + nneutral) == nl))
{
pcaptype = CapType.ALLCAP;
}
else if ((ncap > 1) && firstcap)
{
pcaptype = CapType.HUHINITCAP;
}
else
{
pcaptype = CapType.HUHCAP;
}
return nl < src.Length ? src.Substring(q, nl) : src;
}
// recursive search for right ss - sharp s permutations
hentry spellsharps(string word,
int n_pos,
int n,
int repnum,
ref SPELL? info,
ref string root)
{
int pos = word.IndexOf("ss", n_pos);
if (pos >= 0 && (n < MAXSHARPS))
{
hentry h = spellsharps(word.Substring(0, pos) + "\x00DF" + word.Substring(pos + 2), pos + 1, n + 1, repnum + 1, ref info, ref root);
if (h != null)
return h;
return spellsharps(word, pos + 2, n + 1, repnum, ref info, ref root);
}
else if (repnum > 0)
{
return checkword(word, ref info, ref root);
}
return null;
}
bool is_keepcase(hentry rv)
{
return rv.astr != null && keepcase != 0 &&
TESTAFF(rv.astr, keepcase);
}
/* insert a word to the beginning of the suggestion array */
void insert_sug(List<string> slst, string word)
{
slst.Insert(0, word);
}
bool spell(string word, List<string> candidate_stack)
{
SPELL? info = null;
string root = null;
return spell(word, candidate_stack, ref info, ref root);
}
bool spell(string word, List<string> candidate_stack, ref SPELL? info)
{
string root = null;
return spell(word, candidate_stack, ref info, ref root);
}
bool spell(string word, List<string> candidate_stack, ref SPELL? info, ref string root)
{
// something very broken if spell ends up calling itself with the same word
if (candidate_stack.IndexOf(word) >= 0)
return false;
candidate_stack.Add(word);
bool r = spell_internal(word, candidate_stack, ref info, ref root);
candidate_stack.RemoveAt(candidate_stack.Count - 1);
if (r && root != null)
{
// output conversion
var rl = get_oconvtable();
if (rl != null) root = rl.conv(root);
}
return r;
}
const int NBEGIN = 0, NNUM = 1, NSEP = 2;
bool spell_internal(string word, List<string> candidate_stack, ref SPELL? info_, ref string root)
{
hentry rv = null;
SPELL? info2 = 0;
ref SPELL? info = ref info2;
if (info_ != null)
{
info_ = 0;
info = ref info_;
}
// Hunspell supports XML input of the simplified API (see manual)
if (word == SPELL_XML)
return true;
if (word.Length >= MAXWORDLEN)
return false;
var ctx = Context;
// input conversion
var rl = get_iconvtable();
if (rl != null) word = rl.conv(word);
word = cleanword2(ctx, word, out var captype, out var abbv);
var wl = word.Length;
#if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
if (wl > 32768)
return false;
#endif
if (wl == 0)
return true;
if (root != null)
root = string.Empty;
// allow numbers with dots, dashes and commas (but forbid double separators:
// "..", "--" etc.)
int nstate = NBEGIN;
int i;
for (i = 0; (i < wl); i++) {
if ((word[i] <= '9') && (word[i] >= '0')) {
nstate = NNUM;
} else if ((word[i] == ',') || (word[i] == '.') || (word[i] == '-')) {
if ((nstate == NSEP) || (i == 0))
break;
nstate = NSEP;
} else
break;
}
if ((i == wl) && (nstate == NNUM))
return true;
switch (captype)
{
case CapType.HUHCAP:
/* FALLTHROUGH */
case CapType.HUHINITCAP:
info |= SPELL.ORIGCAP;
goto case CapType.NOCAP;
/* FALLTHROUGH */
case CapType.NOCAP:
rv = checkword(word, ref info, ref root);
if (abbv != 0 && rv == null)
{
rv = checkword(word + '.', ref info, ref root);
}
break;
case CapType.ALLCAP:
{
info |= SPELL.ORIGCAP;
rv = checkword(word, ref info, ref root);
if (rv != null)
break;
if (abbv != 0)
{
rv = checkword(word + '.', ref info, ref root);
if (rv != null)
break;
}
// Spec. prefix handling for Catalan, French, Italian:
// prefixes separated by apostrophe (SANT'ELIA -> Sant'+Elia).
int apos = word.IndexOf('\'');
if (apos >= 0)
{
word = textinfo.ToLower(word);
//conversion may result in string with different len to pre-mkallsmall2
//so re-scan
if (apos >= 0 && apos < word.Length - 1)
{
string part1 = word.Substring(0, apos + 1), part2 = word.Substring(apos + 1);
part2 = ctx.mkinitcap(part2, textinfo);
word = part1 + part2;
rv = checkword(word, ref info, ref root);
if (rv != null)
break;
word = ctx.mkinitcap(word, textinfo);
rv = checkword(word, ref info, ref root);
if (rv != null)
break;
}
}
if (checksharps && word.IndexOf("SS") >= 0)
{
word = textinfo.ToLower(word);
rv = spellsharps(word, 0, 0, 0, ref info, ref root);
var u8buffer = word;
if (rv == null)
{
word = ctx.mkinitcap(word, textinfo);
rv = spellsharps(word, 0, 0, 0, ref info, ref root);
}
if (abbv != 0 && rv == null)
{
rv = spellsharps(u8buffer + '.', 0, 0, 0, ref info, ref root);
if (rv == null)
{
rv = spellsharps(word + '.', 0, 0, 0, ref info, ref root);
}
}
if (rv != null)
break;
}
}
goto case CapType.INITCAP;
/* FALLTHROUGH */
case CapType.INITCAP:
{
// handle special capitalization of dotted I
info |= SPELL.ORIGCAP;
if (captype == CapType.ALLCAP)
{
word = ctx.mkinitcap(textinfo.ToLower(word), textinfo);
}
if (captype == CapType.INITCAP)
info |= SPELL.INITCAP;
rv = checkword(word, ref info, ref root);
if (captype == CapType.INITCAP)
info &= ~SPELL.INITCAP;
// forbid bad capitalization
// (for example, ijs -> Ijs instead of IJs in Dutch)
// use explicit forms in dic: Ijs/F (F = FORBIDDENWORD flag)
if ((info & SPELL.FORBIDDEN) != 0)
{
rv = null;
break;
}
if (rv != null && is_keepcase(rv) && (captype == CapType.ALLCAP))
rv = null;
if (rv != null)
break;
word = textinfo.ToLower(word);
var u8buffer = word;
word = ctx.mkinitcap(word, textinfo);
rv = checkword(u8buffer, ref info, ref root);
if (abbv != 0 && rv == null)
{
u8buffer += '.';
rv = checkword(u8buffer, ref info, ref root);
if (rv == null)
{
u8buffer = word + '.';
if (captype == CapType.INITCAP)
info |= SPELL.INITCAP;
rv = checkword(u8buffer, ref info, ref root);
if (captype == CapType.INITCAP)
info &= ~SPELL.INITCAP;
if (rv != null && is_keepcase(rv) && (captype == CapType.ALLCAP))
rv = null;
break;
}
}
if (rv != null && is_keepcase(rv) &&
((captype == CapType.ALLCAP) ||
// if CHECKSHARPS: KEEPCASE words with \xDF are allowed
// in INITCAP form, too.
!(checksharps &&
u8buffer.IndexOf('\x00DF') >= 0)))
rv = null;
break;
}
}
if (rv != null)
{
if (warn != 0 && rv.astr != null &&
TESTAFF(rv.astr, warn))
{
info |= SPELL.WARN;
if (forbidwarn)
return false;
return true;
}
return true;
}
// recursive breaking at break points
if (breaktable.Count > 0 && (info & SPELL.FORBIDDEN) == 0)
{
int nbr = 0;
wl = word.Length;
// calculate break points for recursion limit
foreach (var j in breaktable)
{
int pos = 0;
while ((pos = word.IndexOf(j, pos)) >= 0)
{
++nbr;
pos += j.Length;
}
}
if (nbr >= 10)
return false;
// check boundary patterns (^begin and end$)
foreach (var j in breaktable)
{
int plen = j.Length;
if (plen == 1 || plen > wl)
continue;
if (j[0] == '^' &&
string.CompareOrdinal(word, 0, j, 1, plen - 1) == 0 && spell(word.Substring(plen - 1), candidate_stack))
{
info |= SPELL.COMPOUND;
return true;
}
if (j[plen - 1] == '$' &&
string.CompareOrdinal(word, wl - plen + 1, j, 0, plen - 1) == 0 &&
spell(word.Remove(wl - plen + 1), candidate_stack))
{
info |= SPELL.COMPOUND;
return true;
}
}
// other patterns
foreach (var j in breaktable)
{
int plen = j.Length;
int found = word.IndexOf(j);
if ((found > 0) && (found < wl - plen))
{
int found2 = word.IndexOf(j, found + 1);
// try to break at the second occurance
// to recognize dictionary words with breaktable
if (found2 > 0 && (found2 < wl - plen))
found = found2;
if (!spell(word.Substring(found + plen), candidate_stack))
continue;
// examine 2 sides of the break point
if (spell(word.Remove(found), candidate_stack))
{
info |= SPELL.COMPOUND;
return true;
}
// LANG.hu: spec. dash rule
if (langnum == LANG.hu && j == "-" &&
spell(word.Remove(found + 1), candidate_stack))
{
info |= SPELL.COMPOUND;
return true; // check the first part with dash
}
// end of LANG specific region
}
}
// other patterns (break at first break point)
foreach (var j in breaktable)
{
int plen = j.Length, found = word.IndexOf(j);
if ((found > 0) && (found < wl - plen))
{
if (!spell(word.Substring(found + plen), candidate_stack))
continue;
// examine 2 sides of the break point
if (spell(word.Remove(found), candidate_stack))
{
info |= SPELL.COMPOUND;
return true;
}
// LANG.hu: spec. dash rule
if (langnum == LANG.hu && j == "-" &&
spell(word.Remove(found + 1), candidate_stack))
{
info |= SPELL.COMPOUND;
return true; // check the first part with dash
}
// end of LANG specific region
}
}
}
return false;
}
hentry checkword(string word, ref SPELL? info, ref string root)
{
var ctx = Context;
// remove IGNORE characters from the string
word = remove_ignored_chars(word, ctx);
if (word.Length == 0)
return null;
// word reversing wrapper for complex prefixes
if (complexprefixes)
{
word = ctx.reverseword(word);
}
int len = word.Length;
// look word in hash table
hentry he = lookup(word);
// check forbidden and onlyincompound words
if (he != null && he.astr != null &&
TESTAFF(he.astr, forbiddenword))
{
if (info != null) info |= SPELL.FORBIDDEN;
// LANG_hu section: set dash information for suggestions
if (langnum == LANG.hu)
{
if (compoundflag != 0 &&
TESTAFF(he.astr, compoundflag))
{
if (info != null) info |= SPELL.COMPOUND;
}
}
return null;
}
// he = next not needaffix, onlyincompound homonym or onlyupcase word
while (he != null && he.astr != null &&
((needaffix != 0 &&
TESTAFF(he.astr, needaffix)) ||
(onlyincompound != 0 &&
TESTAFF(he.astr, onlyincompound)) ||
(info != null && (info & SPELL.INITCAP) != 0 &&
TESTAFF(he.astr, ONLYUPCASEFLAG))))
he = he.next_homonym;
// check with affixes
if (he == null)
{
// try stripping off affixes
he = affix_check(word, 0, len, 0);
// check compound restriction and onlyupcase
if (he != null && he.astr != null &&
((onlyincompound != 0 &&
TESTAFF(he.astr, onlyincompound)) ||
(info != null && (info & SPELL.INITCAP) != 0 &&
TESTAFF(he.astr, ONLYUPCASEFLAG))))
{
he = null;
}
if (he != null)
{
if (he.astr != null &&
TESTAFF(he.astr, forbiddenword))
{
if (info != null) info |= SPELL.FORBIDDEN;
return null;
}
if (root != null)
{
root = he.word;
if (complexprefixes)
{
root = ctx.reverseword(root);
}
}
// try check compound word
}
else if (get_compound())
{
// first allow only 2 words in the compound
SPELL? setinfo = SPELL.COMPOUND_2;
if (info != null)
setinfo |= info;
he = compound_check(word, 0, 0, 0, null, false, false, setinfo);
if (info != null)
info = setinfo & ~SPELL.COMPOUND_2;
// if not 2-word compoud word, try with 3 or more words
// (only if original info didn't forbid it)
if (he == null && info != null && (info & SPELL.COMPOUND_2) == 0)
{
info &= ~SPELL.COMPOUND_2;
he = compound_check(word, 0, 0, 0, null, false, false, info);
// accept the compound with 3 or more words only if it is
// - not a dictionary word with a typo and
// - not two words written separately,
// - or if it's an arbitrary number accepted by compound rules (e.g. 999%)
if (he != null && !char.IsDigit(word[0]))
{
bool onlycompoundsug = false;
if (suggest(ctx, new List<string>(), word, ref onlycompoundsug, /*test_simplesug=*/true))
he = null;
}
}
// LANG_hu section: `moving rule' with last dash
if (he == null && (langnum == LANG.hu) && (word[len - 1] == '-'))
{
he = compound_check(word.Substring(0, len - 1), -5, 0, 0, null, true, false, info);
}
// end of LANG specific region
if (he != null)
{
if (root != null)
{
root = he.word;
if (complexprefixes)
{
root = ctx.reverseword(root);
}
}
if (info != null)
info |= SPELL.COMPOUND;
}
}
}
return he;
}
#if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
const int MAX_CANDIDATE_STACK_DEPTH = 512;
#else
const int MAX_CANDIDATE_STACK_DEPTH = 2048;
#endif
List<string> suggest(string word, List<string> suggest_candidate_stack)
{
if (suggest_candidate_stack.Count > MAX_CANDIDATE_STACK_DEPTH || // apply a fairly arbitrary depth limit
// something very broken if suggest ends up calling itself with the same word
suggest_candidate_stack.IndexOf(word) >= 0)
{
return new List<string>();
}
var ctx = Context;
bool capwords = false;
var spell_candidate_stack = new List<string>();
suggest_candidate_stack.Add(word);
var slst = suggest_internal(ctx, word, spell_candidate_stack, suggest_candidate_stack,
ref capwords, out var abbv, out var captype);
suggest_candidate_stack.RemoveAt(suggest_candidate_stack.Count - 1);
// word reversing wrapper for complex prefixes
if (complexprefixes)
for (int j = slst.Count - 1; j >=0; --j)
slst[j] = ctx.reverseword(slst[j]);
// capitalize
if (capwords)
for (int j = 0; j < slst.Count; ++j)
slst[j] = ctx.mkinitcap(slst[j], textinfo);
// expand suggestions with dot(s)
if (abbv != 0 && sugswithdots && word.Length >= abbv)
{
for (int j = 0; j < slst.Count; ++j)
{
slst[j] = slst[j] + word.Substring(word.Length - abbv);
}
}
// remove bad capitalized and forbidden forms
if (keepcase != 0 || forbiddenword != 0)
{
switch (captype)
{
case CapType.INITCAP:
case CapType.ALLCAP:
{
int l = 0;
for (int j = 0; j < slst.Count; ++j)
{
if (slst[j].IndexOf(' ') < 0 && !spell(slst[j], spell_candidate_stack))
{
var s = textinfo.ToLower(slst[j]);
if (spell(s, spell_candidate_stack))
{
slst[l] = s;
++l;
}
else
{
s = ctx.mkinitcap(s, textinfo);
if (spell(s, spell_candidate_stack))
{
slst[l] = s;
++l;
}
}
}
else
{
slst[l] = slst[j];
++l;
}
}
slst.RemoveRange(l, slst.Count - l);
}
break;
}
}
// remove duplications
{
int l = 0;
for (int j = 0; j < slst.Count; ++j)
{
slst[l] = slst[j];
for (int k = 0; k < l; ++k)
{
if (slst[k] == slst[j])
{
--l;
break;
}
}
++l;
}
slst.RemoveRange(l, slst.Count - l);
}
// output conversion
var rl = get_oconvtable();
if (rl != null)
{
for (int i = 0; i < slst.Count; ++i)
{
slst[i] = rl.conv(slst[i]);
}
}
return slst;
}
/// <summary>
/// Searchs for suggestions.
/// </summary>
/// <param name="word">The [bad] word</param>
/// <returns>The list of suggestions</returns>
public List<string> Suggest(string word)
{
return suggest(word, new List<string>());
}
List<string> suggest_internal(Context ctx,
string word,
List<string> spell_candidate_stack,
List<string> suggest_candidate_stack,
ref bool capwords, out int abbv, out CapType captype)
{
captype = CapType.NOCAP;
abbv = 0;
capwords = false;
var slst = new List<string>();
bool onlycmpdsug = false;
// process XML input of the simplified API (see manual)
if (string.CompareOrdinal(word, 0, SPELL_XML, 0, SPELL_XML.Length - 2) == 0)
{
return spellml(word);
}
if (word.Length >= MAXWORDLEN)
return slst;
// input conversion
var rl = get_iconvtable();
if (rl != null) word = rl.conv(word);
var scw = cleanword2(ctx, word, out captype, out abbv);
var wl = scw.Length;
if (wl == 0)
return slst;
#if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
if (wl > 32768)
return slst;
#endif
bool good = false;
// initialize in every suggestion call
var timer = Stopwatch.StartNew();
// check capitalized form for FORCEUCASE
if (captype == CapType.NOCAP && forceucase != 0)
{
SPELL? info = SPELL.ORIGCAP;
string root = null;
if (checkword(scw, ref info, ref root) != null)
{
slst.Add(ctx.mkinitcap(scw, textinfo));
return slst;
}
}
switch (captype)
{
case CapType.NOCAP:
{
good |= suggest(ctx, slst, scw, ref onlycmpdsug);
if (timer.ElapsedMilliseconds > TIMELIMIT_GLOBAL)
return slst;
if (abbv != 0)
{
good |= suggest(ctx, slst, scw + ".", ref onlycmpdsug);
if (timer.ElapsedMilliseconds > TIMELIMIT_GLOBAL)
return slst;
}
break;
}
case CapType.INITCAP:
{
capwords = true;
good |= suggest(ctx, slst, scw, ref onlycmpdsug);
if (timer.ElapsedMilliseconds > TIMELIMIT_GLOBAL)
return slst;
good |= suggest(ctx, slst, textinfo.ToLower(scw), ref onlycmpdsug);
if (timer.ElapsedMilliseconds > TIMELIMIT_GLOBAL)
return slst;
break;
}
case CapType.HUHINITCAP:
capwords = true;
goto case CapType.HUHCAP;
/* FALLTHROUGH */
case CapType.HUHCAP:
{
good |= suggest(ctx, slst, scw, ref onlycmpdsug);
if (timer.ElapsedMilliseconds > TIMELIMIT_GLOBAL)
return slst;
// something.The -> something. The
int dot_pos = scw.IndexOf('.');
if (dot_pos >= 0)
{
string postdot = scw.Substring(dot_pos + 1);
if (get_captype(postdot, textinfo) == CapType.INITCAP)
{
insert_sug(slst, scw.Insert(dot_pos + 1, " "));
}
}
string wspace;
if (captype == CapType.HUHINITCAP)
{
// TheOpenOffice.org -> The OpenOffice.org
good |= suggest(ctx, slst, ctx.mkinitsmall(scw, textinfo), ref onlycmpdsug);
if (timer.ElapsedMilliseconds > TIMELIMIT_GLOBAL)
return slst;
}
wspace = textinfo.ToLower(scw);
if (spell(wspace, spell_candidate_stack))
insert_sug(slst, wspace);
int prevns = slst.Count;
good |= suggest(ctx, slst, wspace, ref onlycmpdsug);
if (timer.ElapsedMilliseconds > TIMELIMIT_GLOBAL)
return slst;
if (captype == CapType.HUHINITCAP)
{
wspace = ctx.mkinitcap(wspace, textinfo);
if (spell(wspace, spell_candidate_stack))
insert_sug(slst, wspace);