-
Notifications
You must be signed in to change notification settings - Fork 155
Expand file tree
/
Copy pathspellcheck.cpp
More file actions
2059 lines (1927 loc) · 67.9 KB
/
spellcheck.cpp
File metadata and controls
2059 lines (1927 loc) · 67.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"
#pragma warning( disable: 4068)
MEANING lengthLists[100]; // lists of valid words by length
bool fixedSpell = false;
bool spellTrace = false;
char spellCheckWord[MAX_WORD_SIZE];
int badspellcount = 0;
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*)u8"\xC3\xA9""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*)u8"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->properties & PART_OF_SPEECH || D->systemFlags & PATTERN_WORD)
{
if (!IsAlphaUTF8(*D->word) || WORDLENGTH(D) >= 50 || strchr(D->word, ' ') || strchr(D->word, '_')) continue;
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));
}
int SplitWord(char* word,int i)
{
size_t len1 = strlen(word);
// Do not split acronyms (all uppercase) unless word before or after is lowercase
size_t j;
for (j = 0; j < len1; ++j)
{
if (!IsUpperCase(word[j])) break;
}
if (j == len1) // looks like acronym 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->systemFlags & 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
bool isEnglish = (!stricmp(current_language, "english") ? true : false);
bool isFrench = (!stricmp(current_language, "french") ? true : false);
breakAt = 0;
size_t len = strlen(word);
for (unsigned int k = 1; k < len-1; ++k)
{
if (isEnglish && k == 1 && *word != 'a' && *word != 'A' && *word != 'i' && *word != 'I') continue; // only a and i are allowed single-letter words
else if (isFrench && 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->systemFlags & HAS_SUBSTITUTE) != 0;
if (good)
{
if (D1->systemFlags & AGE_LEARNED || GetMeaningCount(D1) > 1);
else good = false;
}
if (!good ) 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->systemFlags & HAS_SUBSTITUTE) != 0;
if (good)
{
if (D2->systemFlags & AGE_LEARNED || GetMeaningCount(D2) > 1);
else good = false;
}
if (!good ) 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(unsigned int i)
{
char* tokens[6];
// 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
int start = derivationIndex[i] >> 8;
int end = derivationIndex[i] & 0x00ff;
if (start != end || stricmp(wordStarts[i],derivationSentence[start])) // changing capitalization doesnt count as a change
return word; // got here by a spellcheck so trust it.
size_t len = strlen(word);
if (len > 2 && word[len-2] == '\'') return word; // dont do anything with ' words
#ifdef PRIVATE_CODE
// Check for private hook function to spell check a word, or to stop a word being "corrected" later on in standard processing
static HOOKPTR fn = FindHookFunction((char*)"SpellCheckWord");
if (fn)
{
char* word1 = ((SpellCheckWordHOOKFN) fn)(word, i);
if (word1)
{
return word1;
}
}
#endif
// 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
{
WORDP D = FindWord(word,breakAt,PRIMARY_CASE_ALLOWED);
tokens[1] = D->word;
tokens[2] = word+breakAt;
fixedSpell = ReplaceWords("Splitword",i,1,2,tokens);
return NULL;
}
// now imagine partial runtogetherness, like "talkab out fingers"
if (i < wordCount)
{
char tmpword[MAX_WORD_SIZE*2];
strcpy(tmpword,word);
strcat(tmpword,wordStarts[i+1]);
breakAt = SplitWord(tmpword,i);
if (breakAt > 0 && stricmp(tmpword+breakAt,wordStarts[i+1])) // replace words with the dual pair
{
WORDP D = FindWord(tmpword,breakAt,PRIMARY_CASE_ALLOWED);
tokens[1] = D->word;
tokens[2] = tmpword +breakAt;
fixedSpell = ReplaceWords("SplitWords",i,2,2,tokens);
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(current_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(current_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(current_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;
}
static bool UsefulKnownWord(WORDP D)
{
if (!D) return false;
if (D->properties & TAG_TEST) return true;
if (D->systemFlags & (PATTERN_WORD || HAS_SUBSTITUTE)) return true;
FACT* F = GetSubjectNondeadHead(D);
// concept members are useful. If from api call, it would be the only fact on a new word
if (F && (F->verb == Mmember || F->verb == Mexclude || F->verb == Mremapfact)) return true;
return false;
}
WORDP GetGermanPrimaryTail(char* word, size_t length = 0);
WORDP GetGermanPrimaryTail(char* word,size_t length)
{
WORDP D = NULL;
int len = strlen(word) - 2; // minimum joined piece
while (--len >= 0) // minimum size leftover 2 (dont do tiny words)
{
D = FindWord(word + len, 0, UPPERCASE_LOOKUP); // 2nd word if noun will be upper
if (D && !(D->properties & (NOUN | VERB | ADJECTIVE | ADVERB))) D = NULL;
if (!D) D = FindWord(word + len, 0, LOWERCASE_LOOKUP); // 2nd word other
if (D && !(D->properties & (NOUN | VERB | ADJECTIVE | ADVERB))) D = NULL;
if (D)
{
while (--len >= 0) // keep going
{
WORDP F = FindWord(word + len, 0, UPPERCASE_LOOKUP); // 2nd word if noun will be upper
if (!F) F = FindWord(word + len, 0, LOWERCASE_LOOKUP); // 2nd word other
if (F && !(F->properties & (NOUN | VERB | ADJECTIVE | ADVERB))) F = NULL;
if (len == 0 && length) break; // dont swallow entire known word
if (F) D = F; // found longer, upgrade to it
}
}
}
return D;
}
static bool ValidComposite(unsigned int start, unsigned int end)
{
if (start < 1 || end > wordCount || start > wordCount) return false; // not legal - negative start will appear big
char word[MAX_WORD_SIZE * 5 + 10]; // composite up to 5 words safely
char* ptr = word;
for (unsigned int i = start; i <= end; ++i)
{
strcpy(ptr, wordStarts[i]);
ptr += strlen(wordStarts[i]);
*ptr++ = '_';
}
*--ptr = 0;
return FindWord(word) ? true : false;
}
static bool ValidSequence(unsigned int posn)
{
// check sequences of up to 5 in each position
for (unsigned int n = 1; n <= 4; ++n) // in 1st position ?xxxx
{
unsigned int i = posn + n;
if (ValidComposite(posn, i)) return true;
}
for (unsigned int n = 2; n <= 4; ++n) // in 2nd position x?xxx
{
unsigned int i = posn + n - 1;
if (ValidComposite(posn-1, i)) return true;
}
// in 3rd position xx?xx
if (ValidComposite(posn - 2, posn) || ValidComposite(posn - 2, posn + 1) || ValidComposite(posn - 2, posn + 2)) return true;
// in 4th position xxx?x
if (ValidComposite(posn - 3, posn) || ValidComposite(posn - 3, posn + 1)) return true;
// in 5th position xxxx?
if (ValidComposite(posn - 4, posn)) return true;
return false;
}
bool SpellCheckSentence()
{
if (!stricmp(current_language, "ideographic") || !stricmp(current_language, "japanese") || !stricmp(current_language, "chinese")) return false; // no spell check on them
char retry[256];
memset(retry, 0, sizeof(retry));
char* tokens[6];
fixedSpell = false;
bool isEnglish = (!stricmp(current_language, "english") ? true : false);
bool isGerman = (!stricmp(current_language, "german") ? true : false);
bool isSpanish = (!stricmp(current_language, "spanish") ? true : false);
bool isFilipino = (!stricmp(current_language, "filipino") ? true : false);
unsigned int startWord = FindOOBEnd(1);
unsigned int i;
int badspelllimit = 0;
int badspellsize = 0;
float badspellratio = 1.0;
char* baddata = GetUserVariable("$cs_badspellLimit", false); // 10-20 (50%)
if (*baddata)
{
badspelllimit = atoi(baddata);
baddata = strchr(baddata, '-');
if (baddata)
{
badspellsize = atoi(baddata + 1);
badspellratio = (float) badspelllimit / badspellsize;
}
}
bool delayedspell = false;
int spellbad = 0;
int safetylimit = 400;
// verifyrun safety
if (!stricmp(wordStarts[1], "VERIFY") && (*wordStarts[2] == '^' || *wordStarts[2] == '$' || *wordStarts[2] == '%')) return false;
for (i = startWord; i <= wordCount; ++i)
{
if (retry[i-1]) // prior was a problem needing full spellcheck
{
if (++spellbad >= badspelllimit && badspelllimit) // crossed threshhold of number of spellchecks
{
float ratio = (float)spellbad / (i + 1 - startWord); // we never hit last word
if (ratio >= badspellratio)
{
if ((trace & TRACE_BITS) == TRACE_SPELLING) Log(USERLOG,"BadSpell abort NL\r\n");
char junk[100];
sprintf(junk, "%d-%d", spellbad, (i + 1 - startWord));
SetUserVariable("$$cs_badspell", junk);
break;
}
}
}
if (--safetylimit <= 0) break; // in case of infinite loop
char* word = wordStarts[i];
int size = strlen(word);
if (size > (MAX_WORD_SIZE - 100)) continue;
if (size == 4 && !stricmp(word,"json") ) continue; // may not be in dictionary yet
if (IsValidJSONName(word)) continue;
if (!stricmp(serverlogauthcode,word)) continue;
if (!stricmp("bwinfo", word)) continue;
// spanish conjugated word?
if (isSpanish)
{
WORDP entry, canonical = NULL;
uint64 sysflags = 0;
uint64 properties = ComputeSpanish(i, word, entry, canonical, sysflags);
if (!properties) properties = KnownSpanishUnaccented(word, entry,sysflags); // see if accenting is wrong
if (properties) // we figured it out as a conjugation of a verb, noun, or adjective
{
if (entry && strcmp(word, entry->word)) // changed case?
{
tokens[1] = entry->word;
fixedSpell = ReplaceWords("spanish word case change", i, 1, 1, tokens);
}
continue;
}
}
// filipino conjugated word?
if (isFilipino)
{
WORDP entry, canonical = NULL;
uint64 sysflags = 0;
uint64 properties = ComputeFilipino(i, word, entry, canonical, sysflags);
if (properties) // we figured it out as a conjugation of a verb, noun, or adjective
{
if (entry && strcmp(word, entry->word)) // changed ?
{
tokens[1] = entry->word;
fixedSpell = ReplaceWords("filipino word change", i, 1, 1, tokens);
}
continue;
}
}
char bigword[3 * MAX_WORD_SIZE]; // allows join of 2 words
if (spellTrace)
{
strcpy(spellCheckWord, word);
echo = true;
}
// change any \ to /
char newword[MAX_WORD_SIZE];
bool altered = false;
strcpy(newword, word);
char* at = newword;
while ((at = strchr(at, '\\')))
{
if (at[1] == 'n') // newline
{
*at = ' ';
at[1] = ' ';
}
else *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,true); // must match case perfectly if can (iPhone vs IPhone)
if (!D) D = FindWord(word, 0, PRIMARY_CASE_ALLOWED); // take any casing
if ( D && (!IS_NEW_WORD(D) || D->systemFlags & PATTERN_WORD))
{
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->internalBits & HAS_SUBSTITUTE) good = true;
else if (D <= dictionaryPreBuild[LAYER_0]) good = true; // in dictionary - if a substitute would have happend by now
else if (!isEnglish) good = true; // foreign word we know
else if (IsConceptMember(D)) good = true;
if (good)
{
if (strcmp(D->word, word) && !(D->properties & NOUN_HUMAN)) // different capitalization and not just a human name
{
tokens[1] = D->word;
fixedSpell = ReplaceWords("' alternate capitalization", i, 1, 1, tokens);
}
continue;
}
}
// handle lower case forms of upper case nouns
if (isGerman && !D)
{
WORDP E = FindWord(word, 0, SECONDARY_CASE_ALLOWED);
if (E && E->internalBits & UPPERCASE_HASH && E->properties & NOUN_SINGULAR) // we had lower case, we find upper case
{
tokens[1] = E->word;
fixedSpell = ReplaceWords("' German lowercased noun", i, 1, 1, tokens);
continue;
}
}
// he gave upper case, try easy lower case
WORDP LD = FindWord(word, 0, LOWERCASE_LOOKUP);
if (LD && !IS_NEW_WORD(LD) && IsUpperCase(*wordStarts[i]))
{
bool good = false;
if (LD->properties & TAG_TEST || *LD->word == '~' || LD->systemFlags & PATTERN_WORD) good = true; // we know this word clearly or its a concept set ref emotion
else if (LD <= dictionaryPreBuild[LAYER_0]) good = true; // in dictionary - if a substitute would have happend by now
else if (!isEnglish) good = true; // foreign word we know
else if (IsConceptMember(LD)) good = true;
if (good)
{
tokens[1] = LD->word;
fixedSpell = ReplaceWords("' lowercase", i, 1, 1, tokens);
continue;
}
}
if (IsDate(word)) continue; // allow 1970/10/5 or similar
if (IsEmojiShortname(word)) continue; // allow emojis
// o for 0 in number
char xtra[MAX_WORD_SIZE];
strcpy(xtra, word);
char* p = xtra;
if (IsSign(*p)) ++p; // skip sign indicator, -$1,234.56
char* prefixEnd = IsSymbolCurrency((char*)p); // is it at start
if (prefixEnd) p = prefixEnd;
int period = 0;
bool notnum = false;
if (IsDigit(*p))
{
while (p && *++p)
{
if (*p == '.')
{
if (++period > 1) p = NULL;
}
else if (*p == 'o' || *p == 'O') *p = '0';
else if (!IsDigit(*p))
{
p = NULL;
notnum = true;
}
}
if (period <= 1 && !notnum && stricmp(xtra, word))
{
WORDP X = StoreWord(xtra);
tokens[1] = X->word;
fixedSpell = ReplaceWords("o for 0 number", i, 1, 1, tokens);
continue;
}
}
// dont spell check numbers
size_t l = strlen(word);
char* end = word + l;
if (IsFloat(word, end, numberStyle)) continue;
if (IsFractionNumber(word)) continue;
if (IsDigitWord(word, numberStyle, false, true)) continue;
if (IsUrl(word, word + l)) continue;
// appended "?
if (*word != '&' && !stricmp(word + l - 5, """))
{
word[l - 5] = 0;
WORDP X = StoreWord(word);
tokens[1] = X->word;
tokens[2] = "\"";
fixedSpell = ReplaceWords("split "", i, 1, 2, tokens);
--i; // retry spellcheck
continue;
}
if (*word == '\'')
{
WORDP X = ApostropheBreak(word);
if (X)
{
tokens[1] = X->word;
fixedSpell = ReplaceWords("' replace", i, 1, 1, tokens);
continue;
}
}
// degrees
if ((unsigned char)*word == 0xc2 && (unsigned char)word[1] == 0xb0 && !word[3]) continue; // leave degreeC,F,K, etc alone
if (isEnglish && *word == '\'' && !word[1] && i != startWord && IsDigit(*wordStarts[i - 1])) // fails if not digit bug
{
tokens[1] = (char*)"foot";
fixedSpell = ReplaceWords("' as feet", i, 1, 1, tokens);
continue;
}
if (isEnglish && *word == '"' && !word[1] && i != startWord && IsDigit(*wordStarts[i - 1])) // fails if not digit bug
{
tokens[1] = (char*)"inch";
fixedSpell = ReplaceWords("' as feet", i, 1, 1, tokens);
continue;
}
if (!word[1] || *word == '"') continue; // single char or quoted thingy
// dont spell check email or other things with @ or . in them
if (strchr(word, '@') || strchr(word, '&') || strchr(word, '$')) continue;
// dont spell check hashtags
if (word[0] == '#' && !IsDigit(word[1])) {
bool validHash = true;
for (int j = 1; j < size; ++j) {
if (!IsAlphaUTF8OrDigit(word[j]) && word[j] != '_') {
validHash = false;
break;
}
}
if (validHash) continue;
}
// dont spell check names of json objects or arrays
if (IsValidJSONName(word)) continue;
// dont spell check web addresses
if (!strnicmp(word, "http", 4) || !strnicmp(word, "www", 3)) continue;
// dont spell check abbreviations with dots, e.g. p.m.
char* dot = strchr(word, '.');
if (dot && FindWord(word, 0)) continue;
// joined number words like 100dollars
at = word - 1;
while (IsDigit(*++at) || *at == '.' || *at == ',');
if (IsDigit(*word) && strlen(at) > 3 && ProbableKnownWord(at))
{
char first[MAX_WORD_SIZE];
strncpy(first, word, (at - word));
first[at - word] = 0;
if (IsDigitWord(first, numberStyle, true))
{
tokens[1] = first;
tokens[2] = at;
fixedSpell = ReplaceWords("joined number word", i, 1, 2, tokens);
continue;
}
}
// embedded double quotes
char* quo = strchr(word, '"');
if (quo)
{
// mistaken " for 1 in don"t?
if (quo[1] == 't' && !quo[2])
{
*quo = '\'';
tokens[1] = word;
fixedSpell = ReplaceWords("q for dq", i, 1, 1, tokens);
continue;
}
// number-inch-object 76"element tv
if (IsAlphaUTF8(quo[1]) && IsDigit(*word))
{
char* at = word;
while (IsDigit(*++at) || *at == '.'); // scan number
if (at == quo)
{
*quo = 0;
tokens[1] = word;
tokens[2] = "inch";
tokens[3] = quo+1;
fixedSpell = ReplaceWords("joined word number", i, 1, 3, tokens);
continue;
}
}
}
// allow data1 and its friends for regression tests
// joined number words like dollars100, but allow single digits at end - assume model numbers are not normal words with number after
at = end;
while (at >= word && (IsDigit(*--at) || *at == '.' || *at == ','));
if (IsDigit(*++at) && at[1] && (at - word) > 3 && IsDigitWord(at, numberStyle, true))
{
char first[MAX_WORD_SIZE];
strncpy(first, word, (at - word));
first[at - word] = 0;
if (ProbableKnownWord(first))
{
tokens[1] = first;
tokens[2] = at;
fixedSpell = ReplaceWords("joined word number", i, 1, 2, tokens);
continue;
}
}
// dont spellcheck model numbers
if (IsModelNumber(word))
{
WORDP X = FindWord(word, 0, UPPERCASE_LOOKUP);
if (IsConceptMember(X) && !strcmp(word, X->word))
{
tokens[1] = X->word;
fixedSpell = ReplaceWords("KnownUpperModelNumber", i, 1, 1, tokens);
}
continue;
}
// don't spellcheck initials
if (IsMadeOfInitials(word, end) == ABBREVIATION) continue;
if (ValidSequence(i)) continue; // leave alone if part of phrase
if (i != wordCount) // merge 2 adj words w hyphen if can, even though one but not both are legal words
{
// for German be cognizant that nouns are uppercase and hence more useful, 3 Tage is preferred to 3-Tage
uint64 primaryCaseX = (isGerman && IsUpperCase(*word)) ? UPPERCASE_LOOKUP : LOWERCASE_LOOKUP;
uint64 secondaryCaseX = (primaryCaseX == LOWERCASE_LOOKUP) ? UPPERCASE_LOOKUP : LOWERCASE_LOOKUP;
uint64 primaryCaseY = (isGerman && IsUpperCase(*wordStarts[i + 1])) ? UPPERCASE_LOOKUP : LOWERCASE_LOOKUP;;
uint64 secondaryCaseY = (primaryCaseY == LOWERCASE_LOOKUP) ? UPPERCASE_LOOKUP : LOWERCASE_LOOKUP;;
size_t len = strlen(word);
WORDP X = FindWord(word,len);
WORDP Y = FindWord(wordStarts[i + 1]);
bool useful1 = UsefulKnownWord(X);
bool useful2 = UsefulKnownWord(Y);
if (!(X && Y && useful1 && useful2)) // has-been is a word we dont want merge,but model numbers we do.
{
// first check merge 2 using _
strcpy(bigword, word);
bigword[len] = '_';
strcpy(bigword + len + 1, wordStarts[i + 1]);
WORDP XX = FindWord(bigword);
if (XX && UsefulKnownWord(XX))
{
++i;
continue; // its fine joined, will detect as sequence later
}
// try merge with -
bigword[len] = '-';
XX = FindWord(bigword);
if (XX && UsefulKnownWord(XX))
{
tokens[1] = XX->word;
fixedSpell = ReplaceWords("merge to hyphenword", i, 2, 1, tokens);
continue;
}
}
}
// split conjoined sentetence Missouri.Fix or Missouri..Fix
// but dont split float values like 0.5%
if (dot && dot != word && dot[1] && !IsDigit(dot[1]))
{
*dot = 0;
// don't spell correct if this looks like a filename
char* rest = dot + 1;
if (!IsFileExtension(rest)) {
WORDP X = FindWord(word, 0);
while (rest[0] == '.') ++rest; // swallow all excess dots
WORDP Y = FindWord(rest, 0);
if (X && Y) // we recognize the words
{
char oper[10];
tokens[1] = word;
tokens[2] = oper;
*oper = '.';
oper[1] = 0;
tokens[3] = rest;
fixedSpell = ReplaceWords("dotsentence", i, 1, 3, tokens);
*dot = '.'; // restore the dot so the original is still in derivationSentence
continue;
}
}
*dot = '.'; // restore the dot
}
// dont spell check things with . in them
if (dot) continue;
char* number;
if (GetCurrency((unsigned char*)word, number)) continue; // currency
if (isEnglish && !stricmp(word, (char*)"am") && i != startWord &&
(IsDigit(*wordStarts[i - 1]) || IsNumber(wordStarts[i - 1], numberStyle) == REAL_NUMBER)) // fails if not digit bug
{
tokens[1] = (char*)"a.m.";
fixedSpell = ReplaceWords("am as time", i, 1, 1, tokens);
continue;
}
// words with excess repeated characters >2 => 2
char excess[MAX_WORD_SIZE];
if (!IsDigit(word[1]) && word[1] != '.' && word[1] != ',')
{
size_t len = size;
strcpy(excess, word);
bool change = false;
for (int j = 0; j < size; ++j)
{
if (excess[j] == excess[j + 1])
{
memmove(excess + j + 1, excess + j + 2, strlen(excess + j + 1));
if (FindWord(excess))
{
tokens[1] = excess;
fixedSpell = ReplaceWords("2 repeat letters", i, 1, 1, tokens);
break;
}
else strcpy(excess, word);
}
}
if (change) continue;
}
// words with excess repeated characters 2=>1 unless root noun or verb
if (IS_NEW_WORD(FindWord(word)) && !IsDigit(word[1]) && word[1] != '.' && word[1] != ',')
{
if (!stricmp(current_language, "english"))
{
char* noun = GetSingularNoun(word, false, true);
if (noun && strcmp(word, noun)) continue;
char* verb = GetInfinitive(word, true);
if (verb && strcmp(word, verb)) continue;
}
strcpy(excess, word);
bool change = false;
for (int j = 0; j < size; ++j)
{
if (excess[j] == excess[j + 1])
{
memmove(excess + j + 1, excess + j + 2, strlen(excess + j + 1));
if (FindWord(excess))
{
tokens[1] = excess;
fixedSpell = ReplaceWords("2 repeat letters", i, 1, 1, tokens);
break;
}
else strcpy(excess, word);
}
}
if (change) continue;
}
// split arithmetic 1+2
if (IsDigit(*word) && IsDigit(word[size - 1]))
{
char first[MAX_WORD_SIZE];
strncpy(first, word, size);
first[size] = 0;
at = first;
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 oper[10];
tokens[2] = oper;
*oper = *op;
oper[1] = 0;
*op = 0;
tokens[1] = first;
tokens[3] = op + 1;
fixedSpell = ReplaceWords("smooshed 1+2", i, 1, 3, tokens);
continue;
}
}
}
// split off trailing -
if (size > 1 && word[size - 1] == '-')
{
WORDP X = FindWord(word, size - 1);
if (X)
{
tokens[1] = X->word;
tokens[2] = "-";
fixedSpell = ReplaceWords("trailing hyphen split", i, 1, 2, tokens);
continue;
}
}
// merge with next token?
if (i != wordCount && *wordStarts[i + 1] != '"')
{
// direct merge as a single word
strcpy(bigword, word);
strcat(bigword, wordStarts[i + 1]);
D = FindWord(bigword, 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(bigword, word);
strcat(bigword, "_");
strcat(bigword, wordStarts[i + 1]);
D = FindWord(bigword, 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
{