forked from tkuebler/ChatScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspellcheck.cpp
More file actions
1567 lines (1479 loc) · 50.9 KB
/
Copy pathspellcheck.cpp
File metadata and controls
1567 lines (1479 loc) · 50.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
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
#include "common.h"
static MEANING lengthLists[100]; // lists of valid words by length
bool fixedSpell = false;
bool spellTrace = false;
char spellCheckWord[MAX_WORD_SIZE];
typedef struct SUFFIX
{
char* word;
uint64 flags;
} SUFFIX;
static SUFFIX stems[] =
{
{ (char*)"less",NOUN},
{ (char*)"ness",ADJECTIVE|NOUN},
{ (char*)"est",ADJECTIVE},
{ (char*)"er",ADJECTIVE},
{ (char*)"ly",ADJECTIVE},
{0},
};
static SUFFIX stems_french[] =
{
{ (char*)"âtre",ADJECTIVE},
{ (char*)"able",ADJECTIVE},
{ (char*)"ade",NOUN},
{ (char*)"age",NOUN},
{ (char*)"aille",NOUN},
{ (char*)"ain",NOUN|ADJECTIVE},
{ (char*)"ais",NOUN|ADJECTIVE},
{ (char*)"al",ADJECTIVE},
{ (char*)"ance",NOUN},
{ (char*)"ant",ADJECTIVE},
{ (char*)"ard",ADJECTIVE},
{ (char*)"aud",ADJECTIVE},
{ (char*)"ère",NOUN},
{ (char*)"ée",NOUN},
{ (char*)"el",ADJECTIVE},
{ (char*)"et",ADJECTIVE},
{ (char*)"esse",NOUN},
{ (char*)"eur",ADJECTIVE|NOUN},
{ (char*)"euse",NOUN},
{ (char*)"eux",ADJECTIVE},
{ (char*)"ible",ADJECTIVE},
{ (char*)"isme",NOUN},
{ (char*)"iste",NOUN|ADJECTIVE},
{ (char*)"ien",NOUN|ADJECTIVE},
{ (char*)"ier",NOUN},
{ (char*)"ie",NOUN},
{ (char*)"if",ADJECTIVE},
{ (char*)"in",ADJECTIVE},
{ (char*)"ir",VERB},
{ (char*)"asser",VERB},
{ (char*)"ater",VERB},
{ (char*)"ailler",VERB},
{ (char*)"ifier",VERB},
{ (char*)"iner",VERB},
{ (char*)"iser",VERB},
{ (char*)"oter",VERB},
{ (char*)"ot",ADJECTIVE},
{ (char*)"oyer",VERB},
{ (char*)"er",VERB|NOUN},
{ (char*)"ment",ADVERB|NOUN},
{ (char*)"ois",NOUN},
{ (char*)"son",NOUN},
{ (char*)"tion",NOUN},
{ (char*)"ure",NOUN},
{ (char*)"logue",NOUN},
{ (char*)"logie",NOUN},
{ (char*)"gène",NOUN},
{ (char*)"gramme",NOUN},
{ (char*)"manie",NOUN},
{ (char*)"phobe",NOUN},
{ (char*)"phobie",NOUN},
{ (char*)"ose",NOUN},
{0},
};
bool multichoice = false;
void InitSpellCheck()
{
memset(lengthLists,0,sizeof(MEANING) * 100);
WORDP D = dictionaryBase;
while (++D <= dictionaryFree)
{
if (!D->word || !IsAlphaUTF8(*D->word) || D->length >= 100 || strchr(D->word,'~') || strchr(D->word,USERVAR_PREFIX) || strchr(D->word,'^') || strchr(D->word,' ') || strchr(D->word,'_')) continue;
if (D->properties & PART_OF_SPEECH || D->systemFlags & PATTERN_WORD)
{
WORDINFO wordData;
ComputeWordData(D->word, &wordData);
D->spellNode = lengthLists[wordData.charlen];
lengthLists[wordData.charlen] = MakeMeaning(D);
}
}
}
static bool SameUTF(char* word, char* utfstring)
{
size_t len = strlen(utfstring);
return (!strncmp(word, utfstring, len));
}
static int SplitWord(char* word,int i)
{
size_t len1 = strlen(word);
// Do not split acronyms (all uppercase) unless word before or after is lowercase
int j;
for (j = 0; j < len1; ++j)
{
if (!IsUpperCase(word[j])) break;
}
if (j == len1) // looks like acrynum but is any neighbor non caps
{
if (wordStarts[i - 1] && IsUpperCase(wordStarts[i - 1][0])) j = 0;
if (wordStarts[i + 1] && IsUpperCase(wordStarts[i + 1][0])) j = 0;
}
if (j == len1) return 0;
WORDP D2;
bool good;
int breakAt = 0;
if (IsDigit(*word))
{
while (IsDigit(word[++breakAt]) || word[breakAt] == '.' || word[breakAt] == ','){;} // find end of number
if (word[breakAt]) // found end of number
{
D2 = FindWord(word+breakAt,0,PRIMARY_CASE_ALLOWED);
if (D2)
{
good = (D2->properties & (PART_OF_SPEECH|FOREIGN_WORD)) != 0 || (D2->internalBits & HAS_SUBSTITUTE) != 0;
if (good && (D2->systemFlags & AGE_LEARNED))// must be common words we find
{
char number[MAX_WORD_SIZE];
strncpy(number,word,breakAt);
number[breakAt] = 0;
StoreWord(number,ADJECTIVE|NOUN|ADJECTIVE_NUMBER|NOUN_NUMBER);
return breakAt; // split here
}
}
}
}
// dont split acronyms
// try all combinations of breaking the word into two known words
breakAt = 0;
size_t len = strlen(word);
for (unsigned int k = 1; k < len-1; ++k)
{
if (!stricmp(language,"english") && k == 1 && *word != 'a' && *word != 'A' && *word != 'i' && *word != 'I') continue; // only a and i are allowed single-letter words
else if (!stricmp(language,"french") && k == 1 && *word != 'y' && *word != 'a' && *word != 'A' && !SameUTF(word,"à") && !SameUTF(word, "À") && !SameUTF(word, "ô") && !SameUTF(word,"Ô")) continue; // in french only y, a and ô are allowed single-letter words
WORDP D1 = FindWord(word,k,PRIMARY_CASE_ALLOWED);
if (!D1) continue;
good = (D1->properties & (PART_OF_SPEECH|FOREIGN_WORD)) != 0 || (D1->internalBits & HAS_SUBSTITUTE) != 0;
if (!good || !(D1->systemFlags & AGE_LEARNED)) continue; // must be normal common words we find
D2 = FindWord(word+k,len-k,PRIMARY_CASE_ALLOWED);
if (!D2) continue;
good = (D2->properties & (PART_OF_SPEECH|FOREIGN_WORD)) != 0 || (D2->internalBits & HAS_SUBSTITUTE) != 0;
if (!good || !(D2->systemFlags & AGE_LEARNED) ) continue; // must be normal common words we find
if (!breakAt) breakAt = k; // found a split
else // found multiple places to split... dont know what to do
{
breakAt = -1;
break;
}
}
return breakAt;
}
static char* SpellCheck( int i)
{
// on entry we will have passed over words which are KnownWord (including bases) or isInitialWord (all initials)
// wordstarts from 1 ... wordCount is the incoming sentence words (original). We are processing the ith word here.
char* word = wordStarts[i];
if (!*word) return NULL;
if (!stricmp(word,loginID) || !stricmp(word,computerID)) return word; // dont change his/our name ever
size_t len = strlen(word);
if (len > 2 && word[len-2] == '\'') return word; // dont do anything with ' words
// test for run togetherness like "talkabout fingers"
int breakAt = SplitWord(word,i);
if (breakAt > 0)// we found a split, insert 2nd word into word stream
{
char* tokens[3];
WORDP D = FindWord(word,breakAt,PRIMARY_CASE_ALLOWED);
tokens[1] = D->word;
tokens[2] = word+breakAt;
ReplaceWords("Splitword",i,1,2,tokens);
fixedSpell = true;
return NULL;
}
// now imagine partial runtogetherness, like "talkab out fingers"
if (i < wordCount)
{
char tmp[MAX_WORD_SIZE*2];
strcpy(tmp,word);
strcat(tmp,wordStarts[i+1]);
breakAt = SplitWord(tmp,i);
if (breakAt > 0) // replace words with the dual pair
{
char* tokens[3];
WORDP D = FindWord(tmp,breakAt,PRIMARY_CASE_ALLOWED);
tokens[1] = D->word;
tokens[2] = tmp+breakAt;
ReplaceWords("SplitWords",i,2,2,tokens);
fixedSpell = true;
return NULL;
}
}
// remove any nondigit characters repeated more than once. Dont do this earlier, we want substitutions to have a chance at it first. ammmmmmazing
static char word1[MAX_WORD_SIZE];
char* ptr = word-1;
char* ptr1 = word1;
while (*++ptr)
{
*ptr1 = *ptr;
while (ptr[1] == *ptr1 && ptr[2] == *ptr1 && (*ptr1 < '0' || *ptr1 > '9')) ++ptr; // skip double repeats
++ptr1;
}
*ptr1 = 0;
if (FindCanonical(word1,0,true) && !IsUpperCase(*word1)) return word1; // this is a different form of a canonical word so its ok
// now use word spell checker
size_t lenx = strlen(word); // try for reduced form
if (word[lenx - 1] == 's') // noun plural and verb status
{
word[lenx - 1] = 0;
char* d = SpellFix(word, i, VERB|NOUN);
word[lenx - 1] = 's';
if (d)
{
char plural[MAX_WORD_SIZE];
strcpy(plural, d);
strcat(plural, "s");
WORDP X = StoreWord(plural);
return X->word;
}
}
if (!stricmp(&word[lenx - 3],"ing")) // verb participle present
{
word[lenx - 3] = 0;
char* d = SpellFix(word, i, VERB);
word[lenx - 3] = 'i';
if (d)
{
char plural[MAX_WORD_SIZE];
strcpy(plural, d);
strcat(plural, "ing");
WORDP X = StoreWord(plural);
return X->word;
}
}
char* d = SpellFix(word,i,PART_OF_SPEECH);
if (d) return d;
// if is is a misspelled plural?
char plural[MAX_WORD_SIZE];
if (word[len-1] == 's')
{
strcpy(plural,word);
plural[len-1] = 0;
d = SpellFix(plural,i,PART_OF_SPEECH);
if (d) return d; // dont care that it is plural
}
return NULL;
}
char* ProbableKnownWord(char* word)
{
if (strchr(word,' ') || strchr(word,'_')) return word; // not user input, is synthesized
size_t len = strlen(word);
char lower[MAX_WORD_SIZE];
MakeLowerCopy(lower,word);
// do we know the word in lower case?
WORDP D = FindWord(word,0,LOWERCASE_LOOKUP);
if (D) // direct recognition
{
if (D->properties & FOREIGN_WORD || *D->word == '~' || D->systemFlags & PATTERN_WORD) return D->word; // we know this word clearly or its a concept set ref emotion
if (D->properties & PART_OF_SPEECH && !IS_NEW_WORD(D)) return D->word; // old word we know
if (D <= dictionaryPreBuild[LAYER_0]) return D->word; // in dictionary
if (stricmp(language,"English") && !IS_NEW_WORD(D)) return D->word; // foreign word we know
if (IsConceptMember(D)) return D->word;
// are there facts using this word?
// if (GetSubjectNondeadHead(D) || GetObjectNondeadHead(D) || GetVerbNondeadHead(D)) return D->word;
}
// do we know the word in upper case?
char upper[MAX_WORD_SIZE];
MakeLowerCopy(upper,word);
upper[0] = GetUppercaseData(upper[0]);
D = FindWord(upper,0,UPPERCASE_LOOKUP);
if (D) // direct recognition
{
if (D->properties & FOREIGN_WORD || *D->word == '~' || D->systemFlags & PATTERN_WORD) return D->word; // we know this word clearly or its a concept set ref emotion
if (D->properties & PART_OF_SPEECH && !IS_NEW_WORD(D)) return D->word; // old word we know
if (D <= dictionaryPreBuild[LAYER_0]) return D->word; // in dictionary
if (stricmp(language,"English") && !IS_NEW_WORD(D)) return D->word; // foreign word we know
if (IsConceptMember(D)) return D->word;
// are there facts using this word?
// if (GetSubjectNondeadHead(D) || GetObjectNondeadHead(D) || GetVerbNondeadHead(D)) return D->word;
}
// interpolate to lower case words
if (!stricmp(language, "english"))
{
uint64 expectedBase = 0;
if (ProbableAdjective(word, len, expectedBase) && expectedBase) return word;
expectedBase = 0;
if (ProbableAdverb(word, len, expectedBase) && expectedBase) return word;
// is it a verb form
char* verb = GetInfinitive(lower, true); // no new verbs
if (verb) return StoreWord(lower, 0)->word; // verb form recognized
// is it simple plural of a noun?
if (word[len - 1] == 's')
{
WORDP E = FindWord(lower, len - 1, LOWERCASE_LOOKUP);
if (E && E->properties & NOUN)
{
E = StoreWord(word, NOUN | NOUN_PLURAL);
return E->word;
}
E = FindWord(lower, len - 1, UPPERCASE_LOOKUP);
if (E && E->properties & NOUN)
{
*word = toUppercaseData[*word];
E = StoreWord(word, NOUN | NOUN_PROPER_PLURAL);
return E->word;
}
}
}
return NULL;
}
bool SpellCheckSentence()
{
WORDP E;
fixedSpell = false;
int badcount = 0;
int goodcount = 0;
int startWord = FindOOBEnd(1);
for (int i = startWord; i <= wordCount; ++i)
{
char* word = wordStarts[i];
char* tokens[2];
if (spellTrace)
{
strcpy(spellCheckWord, word);
echo = true;
}
// change any \ to /
char newword[MAX_WORD_SIZE];
bool altered = false;
int size = strlen(word);
if (size < MAX_WORD_SIZE)
{
strcpy(newword, word);
char* at = newword;
while ((at = strchr(at,'\\')))
{
*at = '/';
altered = true;
}
if (altered) word = wordStarts[i] = StoreWord(newword, AS_IS)->word;
}
// do we know the word meaningfully as is?
WORDP D = FindWord(word, 0, PRIMARY_CASE_ALLOWED);
if (D && !IS_NEW_WORD(D))
{
bool good = false;
if (D->properties & TAG_TEST|| *D->word == '~' || D->systemFlags & PATTERN_WORD) good = true; // we know this word clearly or its a concept set ref emotion
else if (D <= dictionaryPreBuild[LAYER_0]) good = true; // in dictionary - if a substitute would have happend by now
else if (stricmp(language, "English")) good = true; // foreign word we know
else if (IsConceptMember(D)) good = true;
if (good)
{
++goodcount;
continue;
}
}
if (IsDate(word)) continue; // allow 1970/10/5 or similar
// he's probably messing with us
if (++badcount > 10 && !goodcount) break;
if (badcount > 30 ) break;
// degrees
if (*word == 0xc2 && word[1] == 0xb0 && !word[3]) continue; // leave degreeC,F,K, etc alone
if (*word == '\'' && !word[1] && i != startWord && IsDigit(*wordStarts[i - 1]) && !stricmp(language, "english")) // fails if not digit bug
{
tokens[1] = (char*)"foot";
ReplaceWords("' as feet", i, 1, 1, tokens);
fixedSpell = true;
continue;
}
if (*word == '"' && !word[1] && i != startWord && IsDigit(*wordStarts[i - 1]) && !stricmp(language, "english")) // fails if not digit bug
{
tokens[1] = (char*)"inch";
ReplaceWords("' as feet", i, 1, 1, tokens);
fixedSpell = true;
continue;
}
if (!word || !word[1] || *word == '"' ) continue; // illegal or single char or quoted thingy
size_t len = size;
// dont spell check uppercase not at start or joined word
if (IsUpperCase(word[0]) && (i != startWord || strchr(word,'_')) && tokenControl & NO_PROPER_SPELLCHECK) continue;
// dont spell check email or other things with @ or . in them
if (strchr(word, '@') || strchr(word, '&') || strchr(word, '$')) continue;
// dont spell check names of json objects or arrays
if (!strnicmp(word, "ja-", 3) || !strnicmp(word, "jo-", 3)) continue;
// dont spell check web addresses
if (!strnicmp(word, "http", 4) || !strnicmp(word, "www", 3)) continue;
// dont spell check floats
char* end = word + strlen(word);
if (IsFloat(word, end, numberStyle)) continue;
// dont spell check abbreviations with dots, e.g. p.m.
char* dot = strchr(word, '.');
if (dot && FindWord(word, 0)) continue;
// split conjoined sentetence Missouri.Fix or Missouri..Fix
if (dot && dot != word && dot[1])
{
*dot = 0;
WORDP X = FindWord(word, 0);
char* rest = dot + 1;
while (rest[1] == '.') ++rest; // swallow all excess dots
WORDP Y = FindWord(rest + 1, 0);
if (X && Y) // we recognize the words
{
char* tokens[4];
char oper[10];
tokens[1] = word;
tokens[2] = oper;
*oper = '.';
oper[1] = 0;
tokens[3] = rest + 1;
ReplaceWords("dotsentence", i, 1, 3, tokens);
fixedSpell = true;
continue;
}
else
{
*dot = '.'; // restore the dot
}
}
// dont spell check things with . in them
if (dot) continue;
// nor fractions
if (IsFraction(word)) continue; // fraction?
// joined number words like 100dollars
char* at = word - 1;
while (IsDigit(*++at) || *at == numberPeriod);
if (IsDigit(*word) && strlen(at) > 3 && ProbableKnownWord(at))
{
char first[MAX_WORD_SIZE];
strncpy(first, word, (at - word));
first[at - word] = 0;
char* tokens[3];
tokens[1] = first;
tokens[2] = at;
ReplaceWords("joined number word", i, 1, 2, tokens);
continue;
}
// nor model numbers
if (IsModelNumber(word))
{
WORDP X = FindWord(word, 0, UPPERCASE_LOOKUP);
if (IsConceptMember(X) && !strcmp(word,X->word))
{
char* tokens[2];
tokens[1] = X->word;
ReplaceWords("KnownUpperModelNumber", i, 1, 1, tokens);
fixedSpell = true;
}
continue;
}
char* number;
if (GetCurrency((unsigned char*)word, number)) continue; // currency
if (!stricmp(word, (char*)"am") && i != startWord &&
(IsDigit(*wordStarts[i-1]) || IsNumber(wordStarts[i-1], numberStyle) ==REAL_NUMBER) && !stricmp(language,"english")) // fails if not digit bug
{
char* tokens[2];
tokens[1] = (char*)"a.m.";
ReplaceWords("am as time", i, 1, 1, tokens);
fixedSpell = true;
continue;
}
// words with excess repeated characters >2 => 2
char excess[MAX_WORD_SIZE];
len = strlen(word);
if (len < MAX_WORD_SIZE && !IsDigit(word[1]) && word[1] != '.' && word[1] != ',')
{
strcpy(excess, word);
bool change = false;
// refuse to believe any 3 or more repeats
for (int j = 0; j < len; ++j)
{
if (excess[j] == excess[j + 1] && excess[j + 2] == excess[j])
{
memmove(excess + j + 1, excess + j + 2, strlen(excess + j + 1));
j -= 1;
--len;
change = true;
continue;
}
}
if (change)
{
char* tokens[2];
tokens[1] = excess;
ReplaceWords("multiple repeat letters", i, 1, 1, tokens);
fixedSpell = true;
word = wordStarts[i];
WORDP D = FindWord(excess);
if (D && !IS_NEW_WORD(D)) continue;
}
}
// words with excess repeated characters 2=>1 unless root noun or verb
len = strlen(word);
if (len < MAX_WORD_SIZE && !FindWord(word) && !IsDigit(word[1]) && word[1] != '.' && word[1] != ',')
{
if (!stricmp(language, "english"))
{
char* noun = GetSingularNoun(word, false, true);
if (noun) continue;
char* verb = GetInfinitive(word, true);
if (verb) continue;
}
strcpy(excess, word);
bool change = false;
for (int j = 0; j < len; ++j)
{
if (excess[j] == excess[j + 1])
{
memmove(excess + j + 1, excess + j + 2, strlen(excess + j + 1));
if (FindWord(excess))
{
char* tokens[2];
tokens[1] = excess;
ReplaceWords("2 repeat letters", i, 1, 1, tokens);
fixedSpell = true;
break;
}
else strcpy(excess, word);
}
}
if (change) continue;
}
// split arithmetic 1+2
if (IsDigit(*word) && IsDigit(word[size - 1]))
{
char* at = word;
while (IsDigit(*++at) || *at == '.' || *at == ',') { ; }
char* op = at;
if (*at == '+' || *at == '-' || *at == '*' || *at == '/')
{
while (IsDigit(*++at) || *at == '.' || *at == ',') { ; }
if (!*at && (size != 9 || *op != '-')) // 445+455 but not zip code
{
char* tokens[4];
char oper[10];
tokens[2] = oper;
*oper = *op;
oper[1] = 0;
*op = 0;
tokens[1] = word;
tokens[3] = op + 1;
ReplaceWords("smooshed 1+2", i, 1, 3, tokens);
fixedSpell = true;
continue;
}
}
}
// merge with next token?
if (i != wordCount && *wordStarts[i + 1] != '"')
{
char join[MAX_WORD_SIZE * 3];
// direct merge as a single word
strcpy(join, word);
strcat(join, wordStarts[i + 1]);
WORDP D = FindWord(join, 0, (tokenControl & ONLY_LOWERCASE) ? PRIMARY_CASE_ALLOWED : STANDARD_LOOKUP);
if (D && D->properties & PART_OF_SPEECH && !(D->properties & AUX_VERB)) {} // merge these two, except "going to" or wordnet composites of normal words
else // merge with underscore? shia tsu
{
strcpy(join, word);
strcat(join, "_");
strcat(join, wordStarts[i + 1]);
D = FindWord(join, 0, (tokenControl & ONLY_LOWERCASE) ? PRIMARY_CASE_ALLOWED : STANDARD_LOOKUP);
if (D && D->properties & PART_OF_SPEECH && !(D->properties & AUX_VERB)) // allow these two, except "going to" or wordnet composites of normal words
{
++i;
continue;
}
}
if (D && D->properties & PART_OF_SPEECH && !(D->properties & AUX_VERB)) // merge these two, except "going to" or wordnet composites of normal words
{
WORDP P1 = FindWord(word, 0, LOWERCASE_LOOKUP);
WORDP P2 = FindWord(wordStarts[i + 1], 0, LOWERCASE_LOOKUP);
if (!P1 || !P2 || !(P1->properties & PART_OF_SPEECH) || !(P2->properties & PART_OF_SPEECH))
{
char* tokens[2];
tokens[1] = D->word;
ReplaceWords("merge", i, 2, 1, tokens);
fixedSpell = true;
continue;
}
}
}
// sloppy omitted g in lookin
if (word[len - 1] == 'n' && word[len - 2] == 'i')
{
WORDP D = FindWord(word, len - 2);
if (D && D->properties & BASIC_POS)
{
char test[MAX_WORD_SIZE];
strcpy(test, word);
strcat(test, "g");
char* tokens[2];
tokens[1] = test;
ReplaceWords("omitg", i, 1, 1, tokens);
fixedSpell = true;
continue;
}
}
char* known = ProbableKnownWord(word);
if (known && !strcmp(known,word)) continue; // we know it
if (known && strcmp(known,word))
{
WORDP D = FindWord(known);
char* tokens[2];
if ((!D || !(D->internalBits & UPPERCASE_HASH)) && !IsUpperCase(*known)) // revised the word to lower case (avoid to upper case like "fields" to "Fields"
{
WORDP X = FindWord(known,0,LOWERCASE_LOOKUP);
if (X)
{
tokens[1] = X->word;
ReplaceWords("KnownWord",i,1,1,tokens);
fixedSpell = true;
continue;
}
}
else // is uppercase a concept member? then revise upwards
{
WORDP X = FindWord(known,0,UPPERCASE_LOOKUP);
if (IsConceptMember(X) || stricmp(language,"english")) // all german nouns are uppercase
{
tokens[1] = X->word;
ReplaceWords("KnownUpper",i,1,1,tokens);
fixedSpell = true;
continue;
}
}
}
char* p = word -1;
unsigned char c;
char* hyphen = 0;
while ((c = *++p) != 0)
{
++len;
if (c == '-') hyphen = p; // note is hyphenated - use trailing
}
if (len == 0 || GetTemperatureLetter(word)) continue; // bad ignore utf word or llegal length - also no composite words
if (c && c != '@' && c != '.') // illegal word character
{
if (IsDigit(word[0]) || len == 1){;} // probable numeric?
// accidental junk on end of word we do know immedately?
else if (i > 1 && !IsAlphaUTF8OrDigit(wordStarts[i][len-1]) )
{
WORDP entry,canonical;
char word[MAX_WORD_SIZE];
strcpy(word,wordStarts[i]);
word[len-1] = 0;
uint64 sysflags = 0;
uint64 cansysflags = 0;
WORDP revise;
GetPosData(i,word,revise,entry,canonical,sysflags,cansysflags,true,true); // dont create a non-existent word
if (entry && entry->properties & PART_OF_SPEECH)
{
wordStarts[i] = entry->word;
fixedSpell = true;
continue; // not a legal word character, leave it alone
}
}
}
// see if we know the other case
if (!(tokenControl & (ONLY_LOWERCASE|STRICT_CASING)) || (i == startSentence && !(tokenControl & ONLY_LOWERCASE)))
{
WORDP E = FindWord(word,0,SECONDARY_CASE_ALLOWED);
bool useAlternateCase = false;
if (E && E->systemFlags & PATTERN_WORD) useAlternateCase = true;
if (E && E->properties & (PART_OF_SPEECH|FOREIGN_WORD))
{
// if the word we find is UPPER case, and this might be a lower case noun plural, don't change case.
size_t len = size;
if (word[len-1] == 's' )
{
WORDP F = FindWord(word,len-1);
if (!F || !(F->properties & (PART_OF_SPEECH|FOREIGN_WORD))) useAlternateCase = true;
else continue;
}
else useAlternateCase = true;
}
else if (E) // does it have a member concept fact
{
if (IsConceptMember(E))
{
useAlternateCase = true;
break;
}
}
if (useAlternateCase)
{
char* tokens[2];
tokens[1] = E->word;
ReplaceWords("Alternatecase",i,1,1,tokens);
fixedSpell = true;
continue;
}
}
// break apart slashed pair like eat/feed
char* slash = strchr(word,'/');
if (slash && !slash[1] && len < MAX_WORD_SIZE) // remove trailing slash
{
strcpy(newword, word);
newword[slash - word] = 0;
word = wordStarts[i] = StoreWord(newword, AS_IS)->word;
}
char* slash1 = NULL;
if (slash) slash1 = strchr(slash + 1, '/');
if (slash && slash != word && slash[1] && !slash1) // break apart word/word unless date
{
if ((wordCount + 2 ) >= REAL_SENTENCE_LIMIT) continue; // no room
*slash = 0;
D = StoreWord(word);
*slash = '/';
E = StoreWord(slash+1);
char* tokens[4];
tokens[1] = D->word;
tokens[2] = "/";
tokens[3] = E->word;
ReplaceWords("Split",i,1,3,tokens);
fixedSpell = true;
--i;
continue;
}
// see if hypenated word should be separate or joined (ignore obvious adjective suffix)
if (hyphen && !stricmp(hyphen,(char*)"-like"))
{
StoreWord(word,ADJECTIVE_NORMAL|ADJECTIVE); // accept it as a word
continue;
}
else if (hyphen && (hyphen-word) > 1 && !IsPlaceNumber(word,numberStyle)) // dont break up fifty-second
{
char test[MAX_WORD_SIZE];
char first[MAX_WORD_SIZE];
// test for split
*hyphen = 0;
strcpy(test,hyphen+1);
strcpy(first,word);
*hyphen = '-';
WORDP E = FindWord(test,0,LOWERCASE_LOOKUP);
WORDP D = FindWord(first,0,LOWERCASE_LOOKUP);
if (*first == 0)
{
wordStarts[i] = AllocateHeap(wordStarts[i] + 1); // -pieces want to lose the leading hypen (2-pieces)
fixedSpell = true;
}
else if (D && E) // 1st word gets replaced, we added another word after
{
if ((wordCount + 1 ) >= REAL_SENTENCE_LIMIT) continue; // no room
char* tokens[3];
tokens[1] = D->word;
tokens[2] = E->word;
ReplaceWords("Pair",i,1,2,tokens);
fixedSpell = true;
--i;
}
else if (!stricmp(test,(char*)"old") || !stricmp(test,(char*)"olds")) // break apart 5-year-old
{
if ((wordCount + 1 ) >= REAL_SENTENCE_LIMIT) continue; // no room
D = StoreWord(first);
E = StoreWord(test);
char* tokens[3];
tokens[1] = D->word;
tokens[2] = E->word;
ReplaceWords("Break old",i,1,2,tokens);
fixedSpell = true;
--i;
}
else // remove hyphen entirely?
{
strcpy(test,first);
strcat(test,hyphen+1);
D = FindWord(test,0,(tokenControl & ONLY_LOWERCASE) ? PRIMARY_CASE_ALLOWED : STANDARD_LOOKUP);
if (D)
{
wordStarts[i] = D->word;
fixedSpell = true;
--i;
}
}
continue; // ignore hypenated errors that we couldnt solve, because no one mistypes a hypen
}
// see if number in front of unit split like 10mg
if (IsDigit(*word))
{
char* at = word;
while (*++at && IsDigit(*at)) {;}
WORDP E = FindWord(at);
if (E && strlen(at) > 2 && *at != 'm') // number in front of known word ( but must be longer than 2 char, 5th) but allow mg
{
char token1[MAX_WORD_SIZE];
int len = at - word;
strncpy(token1,word,len);
token1[len] = 0;
D = StoreWord(token1);
char* tokens[4];
tokens[1] = D->word;
tokens[2] = E->word;
ReplaceWords("Split",i,1,2,tokens);
fixedSpell = true;
continue;
}
}
// leave uppercase in first position if not adjusted yet... but check for lower case spell error
if (IsUpperCase(word[0]) && tokenControl & NO_PROPER_SPELLCHECK)
{
char lower[MAX_WORD_SIZE];
MakeLowerCopy(lower,word);
WORDP D = FindWord(lower,0,LOWERCASE_LOOKUP);
if (!D && i == startWord)
{
char* okword = SpellFix(lower,i,PART_OF_SPEECH);
if (okword)
{
char* tokens[2];
WORDP E = StoreWord(okword);
tokens[1] = E->word;
ReplaceWords("Spell",i,1,1,tokens);
fixedSpell = true;
}
}
continue;
}
// see if smooshed word pair
if (*word != '\'' && (!FindCanonical(word, i,true) || IsUpperCase(word[0]))) // dont check quoted or findable words unless they are capitalized
{
word = SpellCheck(i);
// dont spell check proper names to improper, if word before or after is lower case originally
// unless a substitute like g-mail-> Gmail
if (word && i != 1 && originalCapState[i] && !IsUpperCase(*word))
{
WORDP X = FindWord(word);
if (X && X->internalBits & HAS_SUBSTITUTE) {}
else if (!originalCapState[i-1]) continue;
else if (i != wordCount && !originalCapState[i+1]) continue;
}
if (word && !*word) // performed substitution on prior word, restart this one
{
fixedSpell = true;
--i;
continue;
}
if (word)
{
char* tokens[2];
tokens[1] = word;
ReplaceWords("Nearest real word",i,1,1,tokens);
fixedSpell = true;
continue;
}
}
}
return fixedSpell;
}
static char UnaccentedChar(char* str)
{
unsigned char c = (unsigned char)*str;
if (!(c & 0x80)) return c;
unsigned char c1 = (unsigned char)str[1];
// can we change an accented lowercase letter to unaccented?
if (c == 0xc3)
{// b0? be
if ((c1 >= 0xa0 && c1 <= 0xa5)) return 'a';
else if ((c1 == 0xa7)) return 'c';
else if ((c1 >= 0xa8 && c1 <= 0xab)) return 'e';
else if ((c1 >= 0xac && c1 <= 0xaf)) return 'i';
else if ((c1 == 0xb1)) return 'n';
else if ((c1 >= 0xb2 && c1 <= 0xb6)) return 'o';
else if ((c1 == 0xb8 )) return 'o';
else if ((c1 >= 0xb9 && c1 <= 0xbc)) return 'u';
else if ((c1 == 0xbd || (unsigned char)str[2] == 0xbf)) return 'y';
// not doing ae else if (*currentCharReal == 'a' && *nextCharReal == 'e' && *nextCharDict == 0xa6)
// else if ((c1 >= 0xb9 && c1 <= 0xbc)) return 'd';
// else if ((c1 >= 0xb9 && c1 <= 0xbc)) return 'g';
// else if ((c1 >= 0xb9 && c1 <= 0xbc)) return 'r';
// else if ((c1 >= 0xb9 && c1 <= 0xbc)) return 's';
// else if ((c1 >= 0xb9 && c1 <= 0xbc)) return 't';
// else if ((c1 >= 0xb9 && c1 <= 0xbc)) return 'w';
// else if ((c1 >= 0xb9 && c1 <= 0xbc)) return 'z';
}
else if (c == 0xc4)
{
if ((c1 == 0x81 || c1 == 0x83 || c1 == 0x85)) return 'a';
else if ((c1 == 0x87 || c1 == 0x89 || c1 == 0x8b || c1 == 0x8d)) return 'c';
else if ((c1 == 0x8f) || c1 == 0x91) return 'd';
else if ((c1 == 0x93 || c1 == 0x95 || c1 == 0x97 || c1 == 0x99 || c1 == 0x9b)) return 'e';
else if ((c1 == 0x9d || c1 == 0x9f|| c1 == 0xa1 || c1 == 0xa3)) return 'g';
else if ((c1 == 0xa5 || c1 == 0xa7)) return 'h';
else if ((c1 == 0xa9 || c1 == 0xab || c1 == 0xad || c1 == 0xaf || c1 == 0xb1)) return 'i';
else if ((c1 == 0xb5)) return 'j';
else if ((c1 == 0xb7)) return 'k';
else if ((c1 == 0xba || c1 == 0xbc || c1 == 0xbe)) return 'l';
}
else if (c == 0xc5)
{
if ((c1 == 0x80 || c1 == 0x82 || c1 == 0x86 || c1 == 0x88)) return 'l';
else if ((c1 == 0x80 || c1 == 0x82)) return 'n';
else if ((c1 == 0x8d || c1 == 0x8f || c1 == 0x91)) return 'o';
else if ((c1 == 0x97 || c1 == 0x99)) return 'r';
else if ((c1 == 0x9b || c1 == 0x9d || c1 == 0x9f || c1 == 0xa1)) return 's';
else if ((c1 == 0xa3 || c1 == 0xa5 || c1 == 0xa7)) return 't';
else if ((c1 == 0xa9 || c1 == 0xab || c1 == 0xad || c1 == 0xaf || c1 == 0xb1 || c1 == 0xb3)) return 'u';
else if ((c1 == 0xb5 )) return 'w';
else if ((c1 == 0xb7)) return 'y';
else if ((c1 == 0xba || c1 == 0xbc || c1 == 0xbe)) return 'z';
}
else if (c == 0xc7)
{
if ((c1 == 0x8e || c1 == 0xa1)) return 'a';
else if ((c1 == 0x90)) return 'i';
else if ((c1 == 0x92)) return 'o';
else if ((c1 == 0x94 || c1 == 0x96 || c1 == 0x98 || c1 == 0x9a || c1 == 0x9c)) return 'u';
else if ((c1 == 0xa5 || c1 == 0xa7|| c1 == 0x98 )) return 'g';
else if ((c1 == 0xa9)) return 'k';
else if ((c1 == 0xab || c1 == 0xad)) return 'o';
else if ((c1 == 0xb0)) return 'j';
else if ((c1 == 0xb5)) return 'g';
else if ((c1 == 0xb9)) return 'n';
else if ((c1 == 0xbb)) return 'a';
else if ((c1 == 0xbf)) return 'o';
}
return 0;
}
static int EditDistance(WORDINFO& dictWordData, WORDINFO& realWordData,int min)
{// dictword has no underscores, inputSet is already lower case
char dictw[MAX_WORD_SIZE];
MakeLowerCopy(dictw, dictWordData.word);
char* dictinfo = dictw;
char* realinfo = realWordData.word;
char* dictstart = dictinfo;