-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAffixMgr.cs
4962 lines (4424 loc) · 166 KB
/
AffixMgr.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.Globalization;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace HunspellSharp
{
using static Utils;
partial class Hunspell
{
const int SETSIZE = 1 << 8;
const int CONTSIZE = 1 << 16;
const int MINCPDLEN = 3;
const long TIMELIMIT = 1000 / 20;
const byte dupSFX = (1 << 0);
const byte dupPFX = (1 << 1);
PfxEntry pStart0;
SfxEntry sStart0;
Dictionary<char, PfxEntry> pStart;
Dictionary<char, SfxEntry> sStart;
PfxEntry[] pFlag;
SfxEntry[] sFlag;
string keystring;
string trystring;
Encoding encoding;
bool complexprefixes;
ushort compoundflag; // permits word in compound forms
ushort compoundbegin; // may be first word in compound forms
ushort compoundmiddle; // may be middle word in compound forms
ushort compoundend; // may be last word in compound forms
ushort compoundroot; // compound word signing flag
ushort compoundforbidflag; // compound fordidden flag for suffixed word
ushort compoundpermitflag; // compound permitting flag for suffixed word
bool compoundmoresuffixes; // allow more suffixes within compound words
bool checkcompounddup; // forbid double words in compounds
bool checkcompoundrep; // forbid bad compounds (may be non-compound word with a REP substitution)
bool checkcompoundcase; // forbid upper and lowercase combinations at word bounds
bool checkcompoundtriple; // forbid compounds with triple letters
bool simplifiedtriple; // allow simplified triple letters in compounds (Schiff+fahrt -> Schiffahrt)
ushort forbiddenword; // forbidden word signing flag
ushort nosuggest; // don't suggest words signed with NOSUGGEST flag
ushort nongramsuggest;
ushort needaffix; // forbidden root, allowed only with suffixes
int cpdmin;
RepList iconvtable;
RepList oconvtable;
List<mapentry> maptable;
bool parsedbreaktable;
List<string> breaktable;
bool parsedcheckcpd;
List<patentry> checkcpdtable;
bool simplifiedcpd; // allow simplified compound forms (see 3rd field of CHECKCOMPOUNDPATTERN)
bool parseddefcpd;
List<ushort[]> defcpdtable;
phonetable phone;
int maxngramsugs;
int maxcpdsugs;
int maxdiff;
bool onlymaxdiff;
bool nosplitsugs;
bool sugswithdots;
int cpdwordmax;
int cpdmaxsyllable; // default 0: unlimited syllablecount in compound words
char[] cpdvowels; // vowels (for calculating of Hungarian compounding limit,
string cpdsyllablenum; // syllable count incrementing flag
// bool checknum; // checking numbers, and word with numbers
char[] wordchars; // letters + spec. word characters
char[] ignorechars; // letters + spec. word characters
string version; // affix and dictionary file version string
LANG langnum;
TextInfo textinfo;
// LEMMA_PRESENT: not put root into the morphological output. Lemma presents
// in morhological description in dictionary file. It's often combined with
// PSEUDOROOT.
ushort lemma_present;
ushort circumfix;
ushort onlyincompound;
ushort keepcase;
ushort forceucase;
ushort warn;
bool forbidwarn;
ushort substandard;
bool checksharps;
bool fullstrip;
bool havecontclass; // flags of possible continuing classes (double affix)
bool[] contclasses; // flags of possible continuing classes (twofold affix)
enum FlagMode { CHAR, LONG, NUM, UNI };
FlagMode flag_mode;
List<ushort[]> aliasf; // flag vector `compression' with aliases
List<string> aliasm; // morphological desciption `compression' with aliases
// reptable created from REP table of aff file and from "ph:" fields
// of the dic file. It contains phonetic and other common misspellings
// (letters, letter groups and words) for better suggestions
List<replentry> reptable;
void LoadAff(FileMgr afflst)
{
pStart = new Dictionary<char, PfxEntry>();
sStart = new Dictionary<char, SfxEntry>();
pFlag = new PfxEntry[SETSIZE];
sFlag = new SfxEntry[SETSIZE];
breaktable = new List<string>();
checkcpdtable = new List<patentry>();
defcpdtable = new List<ushort[]>();
contclasses = new bool[CONTSIZE];
encoding = Encoding.GetEncoding(28591);
langnum = LANG.xx;
textinfo = CultureInfo.InvariantCulture.TextInfo;
forbiddenword = FORBIDDENWORD;
cpdwordmax = -1; // default: unlimited wordcount in compound words
cpdmin = -1; // undefined
maxngramsugs = -1; // undefined
maxdiff = -1; // undefined
maxcpdsugs = -1; // undefined
aliasf = new List<ushort[]>();
aliasm = new List<string>();
reptable = new List<replentry>();
// Load affix data from aff file
parse_file(afflst);
// convert affix trees to sorted list
process_pfx_tree_to_list();
process_sfx_tree_to_list();
// affix trees are sorted now
// now we can speed up performance greatly taking advantage of the
// relationship between the affixes and the idea of "subsets".
// View each prefix as a potential leading subset of another and view
// each suffix (reversed) as a potential trailing subset of another.
// To illustrate this relationship if we know the prefix "ab" is found in the
// word to examine, only prefixes that "ab" is a leading subset of need be
// examined.
// Furthermore is "ab" is not present then none of the prefixes that "ab" is
// is a subset need be examined.
// The same argument goes for suffix string that are reversed.
// Then to top this off why not examine the first char of the word to quickly
// limit the set of prefixes to examine (i.e. the prefixes to examine must
// be leading supersets of the first character of the word (if they exist)
// To take advantage of this "subset" relationship, we need to add two links
// from entry. One to take next if the current prefix is found (call it
// nexteq)
// and one to take next if the current prefix is not found (call it nextne).
// Since we have built ordered lists, all that remains is to properly
// initialize
// the nextne and nexteq pointers that relate them
process_pfx_order();
process_sfx_order();
// default BREAK definition
if (!parsedbreaktable) {
breaktable.Add("-");
breaktable.Add("^-");
breaktable.Add("-$");
parsedbreaktable = true;
}
#if FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
// not entirely sure this is invalid, so only for fuzzing for now
if (iconvtable && !iconvtable.check_against_breaktable(breaktable)) {
delete iconvtable;
iconvtable = nullptr;
}
#endif
if (cpdmin == -1)
cpdmin = MINCPDLEN;
}
// read in aff file and build up prefix and suffix entry objects
bool parse_file(FileMgr afflst)
{
// checking flag duplication
byte[] dupflags = null;
string enc = null, lang = null;
// step one is to parse the affix file building up the internal
// affix data structures
// read in each line ignoring any that do not
// start with a known line type indicator
while (afflst.getline(out var line))
using (var parts = line.Split())
if (parts.MoveNext())
{
var keyword = parts.Current;
/* parse in the keyboard string */
if (keyword.Equals("KEY"))
{
if (!parse_string(parts, ref keystring, afflst)) return false;
}
/* parse in the try string */
else if (keyword.Equals("TRY"))
parse_string(parts, ref trystring, afflst);
/* parse in the name of the character set used by the .dic and .aff */
else if (keyword.Equals("SET"))
{
if (!parse_string(parts, ref enc, afflst)) return false;
SetEncoding(enc, afflst);
}
/* parse COMPLEXPREFIXES for agglutinative languages with right-to-left
* writing system */
else if (keyword.Equals("COMPLEXPREFIXES"))
complexprefixes = true;
/* parse in the flag used by the controlled compound words */
else if (keyword.Equals("COMPOUNDFLAG"))
{
if (!parse_flag(parts, ref compoundflag, afflst)) return false;
}
/* parse in the flag used by compound words */
else if (keyword.Equals("COMPOUNDBEGIN"))
{
if (complexprefixes)
{
if (!parse_flag(parts, ref compoundend, afflst)) return false;
}
else
{
if (!parse_flag(parts, ref compoundbegin, afflst)) return false;
}
}
/* parse in the flag used by compound words */
else if (keyword.Equals("COMPOUNDMIDDLE"))
{
if (!parse_flag(parts, ref compoundmiddle, afflst)) return false;
}
/* parse in the flag used by compound words */
else if (keyword.Equals("COMPOUNDEND"))
{
if (complexprefixes)
{
if (!parse_flag(parts, ref compoundbegin, afflst)) return false;
}
else
{
if (!parse_flag(parts, ref compoundend, afflst)) return false;
}
}
/* parse in the data used by compound_check() method */
else if (keyword.Equals("COMPOUNDWORDMAX"))
{
if (!parse_num(parts, ref cpdwordmax, afflst)) return false;
}
/* parse in the flag sign compounds in dictionary */
else if (keyword.Equals("COMPOUNDROOT"))
{
if (!parse_flag(parts, ref compoundroot, afflst)) return false;
}
/* parse in the flag used by compound_check() method */
else if (keyword.Equals("COMPOUNDPERMITFLAG"))
{
if (!parse_flag(parts, ref compoundpermitflag, afflst)) return false;
}
/* parse in the flag used by compound_check() method */
else if (keyword.Equals("COMPOUNDFORBIDFLAG"))
{
if (!parse_flag(parts, ref compoundforbidflag, afflst)) return false;
}
else if (keyword.Equals("COMPOUNDMORESUFFIXES"))
compoundmoresuffixes = true;
else if (keyword.Equals("CHECKCOMPOUNDDUP"))
checkcompounddup = true;
else if (keyword.Equals("CHECKCOMPOUNDREP"))
checkcompoundrep = true;
else if (keyword.Equals("CHECKCOMPOUNDTRIPLE"))
checkcompoundtriple = true;
else if (keyword.Equals("SIMPLIFIEDTRIPLE"))
simplifiedtriple = true;
else if (keyword.Equals("CHECKCOMPOUNDCASE"))
checkcompoundcase = true;
else if (keyword.Equals("NOSUGGEST"))
{
if (!parse_flag(parts, ref nosuggest, afflst)) return false;
}
else if (keyword.Equals("NONGRAMSUGGEST"))
{
if (!parse_flag(parts, ref nongramsuggest, afflst)) return false;
}
else if (keyword.Equals("FLAG"))
{
if (flag_mode != FlagMode.CHAR) HUNSPELL_WARNING(true, Properties.Resources.MultipleDefinitions, "FLAG", afflst);
if (parts.MoveNext())
{
keyword = parts.Current;
if (keyword.Contains("long"))
flag_mode = FlagMode.LONG;
if (keyword.Contains("num"))
flag_mode = FlagMode.NUM;
if (keyword.Contains("UTF-8"))
flag_mode = FlagMode.UNI;
}
if (flag_mode == FlagMode.CHAR) HUNSPELL_WARNING(true, Properties.Resources.InvalidFlagMode, afflst);
}
/* parse in the flag used by forbidden words */
else if (keyword.Equals("FORBIDDENWORD"))
{
if (!parse_flag(parts, ref forbiddenword, afflst)) return false;
}
/* parse in the flag used by forbidden words (is deprecated) */
else if (keyword.Equals("LEMMA_PRESENT"))
{
if (!parse_flag(parts, ref lemma_present, afflst)) return false;
}
/* parse in the flag used by circumfixes */
else if (keyword.Equals("CIRCUMFIX"))
{
if (!parse_flag(parts, ref circumfix, afflst)) return false;
}
/* parse in the flag used by fogemorphemes */
else if (keyword.Equals("ONLYINCOMPOUND"))
{
if (!parse_flag(parts, ref onlyincompound, afflst)) return false;
}
/* parse in the flag used by `needaffixs' (is deprecated) */
else if (keyword.Equals("PSEUDOROOT"))
{
if (!parse_flag(parts, ref needaffix, afflst)) return false;
}
/* parse in the flag used by `needaffixs' */
else if (keyword.Equals("NEEDAFFIX"))
{
if (!parse_flag(parts, ref needaffix, afflst)) return false;
}
/* parse in the minimal length for words in compounds */
else if (keyword.Equals("COMPOUNDMIN"))
{
if (!parse_num(parts, ref cpdmin, afflst)) return false;
if (cpdmin < 1) cpdmin = 1;
}
/* parse in the max. words and syllables in compounds */
else if (keyword.Equals("COMPOUNDSYLLABLE"))
{
if (!parse_cpdsyllable(parts, afflst)) return false;
}
/* parse in the flag used by compound_check() method */
else if (keyword.Equals("SYLLABLENUM"))
{
if (!parse_string(parts, ref cpdsyllablenum, afflst)) return false;
}
/* parse in the flag used by the controlled compound words */
else if (keyword.Equals("CHECKNUM"))
{
// checknum = true;
}
/* parse in the extra word characters */
else if (keyword.Equals("WORDCHARS"))
{
if (!parse_array(parts, ref wordchars, afflst)) return false;
}
/* parse in the ignored characters (for example, Arabic optional diacretics
* charachters */
else if (keyword.Equals("IGNORE"))
{
if (!parse_array(parts, ref ignorechars, afflst)) return false;
}
/* parse in the input conversion table */
else if (keyword.Equals("ICONV"))
{
if (!parse_convtable(parts, ref iconvtable, "ICONV", afflst)) return false;
}
/* parse in the output conversion table */
else if (keyword.Equals("OCONV"))
{
if (!parse_convtable(parts, ref oconvtable, "OCONV", afflst)) return false;
}
/* parse in the phonetic translation table */
else if (keyword.Equals("PHONE"))
{
if (!parse_phonetable(parts, afflst)) return false;
}
/* parse in the checkcompoundpattern table */
else if (keyword.Equals("CHECKCOMPOUNDPATTERN"))
{
if (!parse_checkcpdtable(parts, afflst)) return false;
}
/* parse in the defcompound table */
else if (keyword.Equals("COMPOUNDRULE"))
{
if (!parse_defcpdtable(parts, afflst)) return false;
}
/* parse in the related character map table */
else if (keyword.Equals("MAP"))
{
if (!parse_maptable(parts, afflst)) return false;
}
/* parse in the word breakpoints table */
else if (keyword.Equals("BREAK"))
{
if (!parse_breaktable(parts, afflst)) return false;
}
/* parse in the language for language specific codes */
else if (keyword.Equals("LANG"))
{
if (!parse_string(parts, ref lang, afflst)) return false;
GetLanguageAndTextInfo(lang, out langnum, ref textinfo);
}
else if (keyword.Equals("VERSION"))
{
if (parts.MoveNext())
version = parts.Current.ExpandToEndOf(line).String(encoding);
}
else if (keyword.Equals("MAXNGRAMSUGS"))
{
if (!parse_num(parts, ref maxngramsugs, afflst)) return false;
}
else if (keyword.Equals("ONLYMAXDIFF"))
onlymaxdiff = true;
else if (keyword.Equals("MAXDIFF"))
{
if (!parse_num(parts, ref maxdiff, afflst)) return false;
}
else if (keyword.Equals("MAXCPDSUGS"))
{
if (!parse_num(parts, ref maxcpdsugs, afflst)) return false;
}
else if (keyword.Equals("NOSPLITSUGS"))
nosplitsugs = true;
else if (keyword.Equals("FULLSTRIP"))
fullstrip = true;
else if (keyword.Equals("SUGSWITHDOTS"))
sugswithdots = true;
/* parse in the flag used by forbidden words */
else if (keyword.Equals("KEEPCASE"))
{
if (!parse_flag(parts, ref keepcase, afflst)) return false;
}
/* parse in the flag used by `forceucase' */
else if (keyword.Equals("FORCEUCASE"))
{
if (!parse_flag(parts, ref forceucase, afflst)) return false;
}
/* parse in the flag used by `warn' */
else if (keyword.Equals("WARN"))
{
if (!parse_flag(parts, ref warn, afflst)) return false;
}
else if (keyword.Equals("FORBIDWARN"))
forbidwarn = true;
/* parse in the flag used by the affix generator */
else if (keyword.Equals("SUBSTANDARD"))
{
if (!parse_flag(parts, ref substandard, afflst)) return false;
}
else if (keyword.Equals("CHECKSHARPS"))
checksharps = true;
/* parse in the typical fault correcting table */
else if (keyword.Equals("REP"))
{
if (!parse_reptable(parts, afflst)) return false;
}
else if (keyword.Equals("AF"))
{
if (!parse_aliasf(parts, afflst)) return false;
}
else if (keyword.Equals("AM"))
{
if (!parse_aliasm(parts, afflst)) return false;
}
/* parse this affix: P - prefix, S - suffix */
else if (keyword.Equals("PFX") || keyword.Equals("SFX"))
{
if (dupflags == null) dupflags = new byte[CONTSIZE];
if (!parse_affix(parts, (keyword[0] == (byte)'P') != complexprefixes ? 'P' : 'S', afflst, dupflags))
return false;
}
}
return true;
}
// we want to be able to quickly access prefix information
// both by prefix flag, and sorted by prefix string itself
// so we need to set up two indexes
void build_pfxtree(PfxEntry pfxptr)
{
PfxEntry ptr;
PfxEntry pptr;
PfxEntry ep = pfxptr;
// get the right starting points
string key = ep.getKey();
var flg = (byte)(ep.getFlag() & 0x00FF);
// first index by flag which must exist
ptr = pFlag[flg];
ep.setFlgNxt(ptr);
pFlag[flg] = ep;
// handle the special case of null affix string
if (key.Length == 0) {
// always inset them at head of list at element 0
ptr = pStart0;
ep.setNext(ptr);
pStart0 = ep;
return;
}
// now handle the normal case
ep.setNextEQ(null);
ep.setNextNE(null);
char sp = key[0];
// handle the first insert
if (!pStart.TryGetValue(sp, out ptr))
{
pStart.Add(sp, ep);
return;
}
// otherwise use binary tree insertion so that a sorted
// list can easily be generated later
pptr = null;
for (;;) {
pptr = ptr;
if (string.CompareOrdinal(ep.getKey(), ptr.getKey()) <= 0) {
ptr = ptr.getNextEQ();
if (ptr == null) {
pptr.setNextEQ(ep);
break;
}
} else {
ptr = ptr.getNextNE();
if (ptr == null) {
pptr.setNextNE(ep);
break;
}
}
}
}
// we want to be able to quickly access suffix information
// both by suffix flag, and sorted by the reverse of the
// suffix string itself; so we need to set up two indexes
void build_sfxtree(SfxEntry sfxptr)
{
sfxptr.initReverseWord(helper);
SfxEntry ptr;
SfxEntry pptr;
SfxEntry ep = sfxptr;
/* get the right starting point */
string key = ep.getKey();
var flg = (byte)(ep.getFlag() & 0x00FF);
// first index by flag which must exist
ptr = sFlag[flg];
ep.setFlgNxt(ptr);
sFlag[flg] = ep;
// next index by affix string
// handle the special case of null affix string
if (key.Length == 0) {
// always inset them at head of list at element 0
ptr = sStart0;
ep.setNext(ptr);
sStart0 = ep;
return;
}
// now handle the normal case
ep.setNextEQ(null);
ep.setNextNE(null);
char sp = key[0];
// handle the first insert
if (!sStart.TryGetValue(sp, out ptr))
{
sStart.Add(sp, ep);
return;
}
// otherwise use binary tree insertion so that a sorted
// list can easily be generated later
pptr = null;
for (;;) {
pptr = ptr;
if (string.CompareOrdinal(ep.getKey(), ptr.getKey()) <= 0) {
ptr = ptr.getNextEQ();
if (ptr == null) {
pptr.setNextEQ(ep);
break;
}
} else {
ptr = ptr.getNextNE();
if (ptr == null) {
pptr.setNextNE(ep);
break;
}
}
}
}
// convert from binary tree to sorted list
void process_pfx_tree_to_list()
{
foreach (var i in pStart.Keys.ToArray())
pStart[i] = process_pfx_in_order(pStart[i], null);
}
PfxEntry process_pfx_in_order(PfxEntry ptr, PfxEntry nptr)
{
if (ptr != null)
{
nptr = process_pfx_in_order(ptr.getNextNE(), nptr);
ptr.setNext(nptr);
nptr = process_pfx_in_order(ptr.getNextEQ(), ptr);
}
return nptr;
}
// convert from binary tree to sorted list
void process_sfx_tree_to_list()
{
foreach (var i in sStart.Keys.ToArray())
sStart[i] = process_sfx_in_order(sStart[i], null);
}
SfxEntry process_sfx_in_order(SfxEntry ptr, SfxEntry nptr)
{
if (ptr != null)
{
nptr = process_sfx_in_order(ptr.getNextNE(), nptr);
ptr.setNext(nptr);
nptr = process_sfx_in_order(ptr.getNextEQ(), ptr);
}
return nptr;
}
// reinitialize the PfxEntry links NextEQ and NextNE to speed searching
// using the idea of leading subsets this time
int process_pfx_order()
{
// loop through each prefix list starting point
foreach (var kv in pStart)
{
var ptr = kv.Value;
// look through the remainder of the list
// and find next entry with affix that
// the current one is not a subset of
// mark that as destination for NextNE
// use next in list that you are a subset
// of as NextEQ
for (; ptr != null; ptr = ptr.getNext())
{
PfxEntry nptr = ptr.getNext();
for (; nptr != null; nptr = nptr.getNext())
{
if (!isSubset(ptr.getKey(), nptr.getKey()))
break;
}
ptr.setNextNE(nptr);
ptr.setNextEQ(null);
if (ptr.getNext() != null &&
isSubset(ptr.getKey(), ptr.getNext().getKey()))
ptr.setNextEQ(ptr.getNext());
}
// now clean up by adding smart search termination strings:
// if you are already a superset of the previous prefix
// but not a subset of the next, search can end here
// so set NextNE properly
ptr = kv.Value;
for (; ptr != null; ptr = ptr.getNext())
{
PfxEntry nptr = ptr.getNext();
PfxEntry mptr = null;
for (; nptr != null; nptr = nptr.getNext())
{
if (!isSubset(ptr.getKey(), nptr.getKey()))
break;
mptr = nptr;
}
if (mptr != null)
mptr.setNextNE(null);
}
}
return 0;
}
// initialize the SfxEntry links NextEQ and NextNE to speed searching
// using the idea of leading subsets this time
int process_sfx_order()
{
// loop through each prefix list starting point
foreach (var kv in sStart)
{
var ptr = kv.Value;
// look through the remainder of the list
// and find next entry with affix that
// the current one is not a subset of
// mark that as destination for NextNE
// use next in list that you are a subset
// of as NextEQ
for (; ptr != null; ptr = ptr.getNext())
{
SfxEntry nptr = ptr.getNext();
for (; nptr != null; nptr = nptr.getNext())
{
if (!isSubset(ptr.getKey(), nptr.getKey()))
break;
}
ptr.setNextNE(nptr);
ptr.setNextEQ(null);
if (ptr.getNext() != null &&
isSubset(ptr.getKey(), ptr.getNext().getKey()))
ptr.setNextEQ(ptr.getNext());
}
// now clean up by adding smart search termination strings:
// if you are already a superset of the previous suffix
// but not a subset of the next, search can end here
// so set NextNE properly
ptr = kv.Value;
for (; ptr != null; ptr = ptr.getNext())
{
SfxEntry nptr = ptr.getNext();
SfxEntry mptr = null;
for (; nptr != null; nptr = nptr.getNext())
{
if (!isSubset(ptr.getKey(), nptr.getKey()))
break;
mptr = nptr;
}
if (mptr != null)
mptr.setNextNE(null);
}
}
return 0;
}
// add flags to the result for dictionary debugging
void debugflag(StringBuilder result, ushort flag) {
result.Append(MSEP.FLD);
result.Append(MORPH.FLAG);
encode_flag(flag, result);
}
// calculate the character length of the condition
int condlen(char[] s) {
int l = 0;
bool group = false;
int st = 0, end = s.Length;
while (st != end) {
if (s[st] == '[') {
group = true;
l++;
} else if (s[st] == ']')
group = false;
else if (!group)
l++;
++st;
}
return l;
}
void encodeit(AffEntry entry, char[] cs) {
if (cs.Length != 1 || cs[0] != '.') {
entry.numconds = (byte)condlen(cs);
entry.conds = cs;
} else {
entry.numconds = 0;
entry.conds = EmptyCharArray;
}
}
// return 1 if s1 is a leading subset of s2 (dots are for infixes)
bool isSubset(string s1, string s2, int start = 0) {
int i1, i2;
for (i1 = 0, i2 = start; i1 < s1.Length && i2 < s2.Length; ++i1, ++i2)
if (s1[i1] != s2[i2] && s1[i1] != '.') break;
return i1 == s1.Length;
}
bool isSubset(string s1, IEnumerable<char> s2_, int start = 0)
{
switch (s2_)
{
case string s: return isSubset(s1, s, start);
case char[] s2:
int i1, i2;
for (i1 = 0, i2 = start; i1 < s1.Length && i2 < s2.Length; ++i1, ++i2)
if (s1[i1] != s2[i2] && s1[i1] != '.') break;
return i1 == s1.Length;
default: throw new NotSupportedException();
}
}
// check word for prefixes
hentry prefix_check(IEnumerable<char> word,
int start,
int len,
IN_CPD in_compound,
ushort needflag = 0)
{
hentry rv = null;
var ctx = Context;
ctx.pfx = null;
ctx.sfxappnd = null;
ctx.sfxextra = 0;
// first handle the special case of 0 length prefixes
for (PfxEntry pe = pStart0; pe != null; pe = pe.getNext())
{
if (
// fogemorpheme
((in_compound != IN_CPD.NOT) ||
!(pe.getCont() != null &&
onlyincompound != 0 && TESTAFF(pe.getCont(), onlyincompound))) &&
// permit prefixes in compounds
((in_compound != IN_CPD.END) ||
(pe.getCont() != null &&
compoundpermitflag != 0 && TESTAFF(pe.getCont(), compoundpermitflag))))
{
// check prefix
rv = pe.checkword(this, word, start, len, in_compound, needflag);
if (rv != null)
{
ctx.pfx = pe; // BUG: pfx not stateless
return rv;
}
}
}
// now handle the general case
char sp = word.At(start);
if (pStart.TryGetValue(sp, out var pptr))
do
{
if (isSubset(pptr.getKey(), word, start))
{
if (
// fogemorpheme
((in_compound != IN_CPD.NOT) ||
!(pptr.getCont() != null &&
onlyincompound != 0 && TESTAFF(pptr.getCont(), onlyincompound))) &&
// permit prefixes in compounds
((in_compound != IN_CPD.END) ||
(pptr.getCont() != null && compoundpermitflag != 0 && TESTAFF(pptr.getCont(), compoundpermitflag))))
{
// check prefix
rv = pptr.checkword(this, word, start, len, in_compound, needflag);
if (rv != null)
{
ctx.pfx = pptr; // BUG: pfx not stateless
return rv;
}
}
pptr = pptr.getNextEQ();
}
else
{
pptr = pptr.getNextNE();
}
} while (pptr != null);
return null;
}
// check word for prefixes and two-level suffixes
hentry prefix_check_twosfx(IEnumerable<char> word,
int start,
int len,
IN_CPD in_compound,
ushort needflag = 0)
{
hentry rv = null;
var ctx = Context;
ctx.pfx = null;
ctx.sfxappnd = null;
ctx.sfxextra = 0;
// first handle the special case of 0 length prefixes
for (var pe = pStart0; pe != null; pe = pe.getNext())
{
rv = pe.check_twosfx(this, word, start, len, in_compound, needflag);
if (rv != null)
return rv;
}
// now handle the general case
char sp = word.At(start);
if (pStart.TryGetValue(sp, out var pptr))
do
{
if (isSubset(pptr.getKey(), word, start))
{
rv = pptr.check_twosfx(this, word, start, len, in_compound, needflag);
if (rv != null)
{
ctx.pfx = pptr;
return rv;
}
pptr = pptr.getNextEQ();
}
else
{
pptr = pptr.getNextNE();
}
} while (pptr != null);
return null;
}
// check word for prefixes and morph
void prefix_check_morph(StringBuilder result,
string word,
int start,
int len,
IN_CPD in_compound,
ushort needflag = 0)