/
suggestmgr.c++
2159 lines (1978 loc) · 64.2 KB
/
suggestmgr.c++
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
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* Copyright (C) 2002-2017 Németh László
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Hunspell is based on MySpell which is Copyright (C) 2002 Kevin Hendricks.
*
* Contributor(s): David Einstein, Davide Prina, Giuseppe Modugno,
* Gianluca Turconi, Simon Brouwer, Noll János, Bíró Árpád,
* Goldman Eleonóra, Sarlós Tamás, Bencsáth Boldizsár, Halácsy Péter,
* Dvornik László, Gefferth András, Nagy Viktor, Varga Dániel, Chris Halls,
* Rene Engelhard, Bram Moolenaar, Dafydd Jones, Harri Pitkänen
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Copyright 2002 Kevin B. Hendricks, Stratford, Ontario, Canada
* And Contributors. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. All modifications to the source code must be clearly marked as
* such. Binary redistributions based on modified source code
* must be clearly marked as modified versions in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY KEVIN B. HENDRICKS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* KEVIN B. HENDRICKS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "suggestmgr.hxx"
#include "htypes.hxx"
#include "csutil.hxx"
const w_char W_VLINE = {'\0', '|'};
SuggestMgr::SuggestMgr(const char* tryme, unsigned int maxn, AffixMgr* aptr) {
// register affix manager and check in string of chars to
// try when building candidate suggestions
pAMgr = aptr;
csconv = NULL;
ckeyl = 0;
ckey = NULL;
ctryl = 0;
ctry = NULL;
utf8 = 0;
langnum = 0;
complexprefixes = 0;
maxSug = maxn;
nosplitsugs = 0;
maxngramsugs = MAXNGRAMSUGS;
maxcpdsugs = MAXCOMPOUNDSUGS;
if (pAMgr) {
langnum = pAMgr->get_langnum();
ckey = pAMgr->get_key_string();
nosplitsugs = pAMgr->get_nosplitsugs();
if (pAMgr->get_maxngramsugs() >= 0)
maxngramsugs = pAMgr->get_maxngramsugs();
utf8 = pAMgr->get_utf8();
if (pAMgr->get_maxcpdsugs() >= 0)
maxcpdsugs = pAMgr->get_maxcpdsugs();
if (!utf8) {
csconv = get_current_cs(pAMgr->get_encoding());
}
complexprefixes = pAMgr->get_complexprefixes();
}
if (ckey) {
if (utf8) {
ckeyl = u8_u16(ckey_utf, ckey);
} else {
ckeyl = strlen(ckey);
}
}
if (tryme) {
ctry = mystrdup(tryme);
if (ctry)
ctryl = strlen(ctry);
if (ctry && utf8) {
ctryl = u8_u16(ctry_utf, tryme);
}
}
}
SuggestMgr::~SuggestMgr() {
pAMgr = NULL;
if (ckey)
free(ckey);
ckey = NULL;
ckeyl = 0;
if (ctry)
free(ctry);
ctry = NULL;
ctryl = 0;
maxSug = 0;
#ifdef MOZILLA_CLIENT
delete[] csconv;
#endif
}
void SuggestMgr::testsug(std::vector<std::string>& wlst,
const std::string& candidate,
int cpdsuggest,
int* timer,
clock_t* timelimit) {
int cwrd = 1;
if (wlst.size() == maxSug)
return;
for (size_t k = 0; k < wlst.size(); ++k) {
if (wlst[k] == candidate) {
cwrd = 0;
break;
}
}
if ((cwrd) && checkword(candidate, cpdsuggest, timer, timelimit)) {
wlst.push_back(candidate);
}
}
// generate suggestions for a misspelled word
// pass in address of array of char * pointers
// onlycompoundsug: probably bad suggestions (need for ngram sugs, too)
void SuggestMgr::suggest(std::vector<std::string>& slst,
const char* w,
int* onlycompoundsug) {
int nocompoundtwowords = 0;
std::vector<w_char> word_utf;
int wl = 0;
size_t nsugorig = slst.size();
std::string w2;
const char* word = w;
size_t oldSug = 0;
// word reversing wrapper for complex prefixes
if (complexprefixes) {
w2.assign(w);
if (utf8)
reverseword_utf(w2);
else
reverseword(w2);
word = w2.c_str();
}
if (utf8) {
wl = u8_u16(word_utf, word);
if (wl == -1) {
return;
}
}
for (int cpdsuggest = 0; (cpdsuggest < 2) && (nocompoundtwowords == 0);
cpdsuggest++) {
// limit compound suggestion
if (cpdsuggest > 0)
oldSug = slst.size();
// suggestions for an uppercase word (html -> HTML)
if (slst.size() < maxSug) {
if (utf8)
capchars_utf(slst, &word_utf[0], wl, cpdsuggest);
else
capchars(slst, word, cpdsuggest);
}
// perhaps we made a typical fault of spelling
if ((slst.size() < maxSug) && (!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
replchars(slst, word, cpdsuggest);
}
// perhaps we made chose the wrong char from a related set
if ((slst.size() < maxSug) &&
(!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
mapchars(slst, word, cpdsuggest);
}
// only suggest compound words when no other suggestion
if ((cpdsuggest == 0) && (slst.size() > nsugorig))
nocompoundtwowords = 1;
// did we swap the order of chars by mistake
if ((slst.size() < maxSug) && (!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
if (utf8)
swapchar_utf(slst, &word_utf[0], wl, cpdsuggest);
else
swapchar(slst, word, cpdsuggest);
}
// did we swap the order of non adjacent chars by mistake
if ((slst.size() < maxSug) && (!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
if (utf8)
longswapchar_utf(slst, &word_utf[0], wl, cpdsuggest);
else
longswapchar(slst, word, cpdsuggest);
}
// did we just hit the wrong key in place of a good char (case and keyboard)
if ((slst.size() < maxSug) && (!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
if (utf8)
badcharkey_utf(slst, &word_utf[0], wl, cpdsuggest);
else
badcharkey(slst, word, cpdsuggest);
}
// did we add a char that should not be there
if ((slst.size() < maxSug) && (!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
if (utf8)
extrachar_utf(slst, &word_utf[0], wl, cpdsuggest);
else
extrachar(slst, word, cpdsuggest);
}
// did we forgot a char
if ((slst.size() < maxSug) && (!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
if (utf8)
forgotchar_utf(slst, &word_utf[0], wl, cpdsuggest);
else
forgotchar(slst, word, cpdsuggest);
}
// did we move a char
if ((slst.size() < maxSug) && (!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
if (utf8)
movechar_utf(slst, &word_utf[0], wl, cpdsuggest);
else
movechar(slst, word, cpdsuggest);
}
// did we just hit the wrong key in place of a good char
if ((slst.size() < maxSug) && (!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
if (utf8)
badchar_utf(slst, &word_utf[0], wl, cpdsuggest);
else
badchar(slst, word, cpdsuggest);
}
// did we double two characters
if ((slst.size() < maxSug) && (!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
if (utf8)
doubletwochars_utf(slst, &word_utf[0], wl, cpdsuggest);
else
doubletwochars(slst, word, cpdsuggest);
}
// perhaps we forgot to hit space and two words ran together
if (!nosplitsugs && (slst.size() < maxSug) &&
(!cpdsuggest || (slst.size() < oldSug + maxcpdsugs))) {
twowords(slst, word, cpdsuggest);
}
} // repeating ``for'' statement compounding support
if (!nocompoundtwowords && (!slst.empty()) && onlycompoundsug)
*onlycompoundsug = 1;
}
// suggestions for an uppercase word (html -> HTML)
void SuggestMgr::capchars_utf(std::vector<std::string>& wlst,
const w_char* word,
int wl,
int cpdsuggest) {
std::vector<w_char> candidate_utf(word, word + wl);
mkallcap_utf(candidate_utf, langnum);
std::string candidate;
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
// suggestions for an uppercase word (html -> HTML)
void SuggestMgr::capchars(std::vector<std::string>& wlst,
const char* word,
int cpdsuggest) {
std::string candidate(word);
mkallcap(candidate, csconv);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
// suggestions for when chose the wrong char out of a related set
int SuggestMgr::mapchars(std::vector<std::string>& wlst,
const char* word,
int cpdsuggest) {
std::string candidate;
clock_t timelimit;
int timer;
int wl = strlen(word);
if (wl < 2 || !pAMgr)
return wlst.size();
const std::vector<mapentry>& maptable = pAMgr->get_maptable();
if (maptable.empty())
return wlst.size();
timelimit = clock();
timer = MINTIMER;
return map_related(word, candidate, 0, wlst, cpdsuggest,
maptable, &timer, &timelimit);
}
int SuggestMgr::map_related(const char* word,
std::string& candidate,
int wn,
std::vector<std::string>& wlst,
int cpdsuggest,
const std::vector<mapentry>& maptable,
int* timer,
clock_t* timelimit) {
if (*(word + wn) == '\0') {
int cwrd = 1;
for (size_t m = 0; m < wlst.size(); ++m) {
if (wlst[m] == candidate) {
cwrd = 0;
break;
}
}
if ((cwrd) && checkword(candidate, cpdsuggest, timer, timelimit)) {
if (wlst.size() < maxSug) {
wlst.push_back(candidate);
}
}
return wlst.size();
}
int in_map = 0;
for (size_t j = 0; j < maptable.size(); ++j) {
for (size_t k = 0; k < maptable[j].size(); ++k) {
size_t len = maptable[j][k].size();
if (strncmp(maptable[j][k].c_str(), word + wn, len) == 0) {
in_map = 1;
size_t cn = candidate.size();
for (size_t l = 0; l < maptable[j].size(); ++l) {
candidate.resize(cn);
candidate.append(maptable[j][l]);
map_related(word, candidate, wn + len, wlst,
cpdsuggest, maptable, timer, timelimit);
if (!(*timer))
return wlst.size();
}
}
}
}
if (!in_map) {
candidate.push_back(*(word + wn));
map_related(word, candidate, wn + 1, wlst, cpdsuggest,
maptable, timer, timelimit);
}
return wlst.size();
}
// suggestions for a typical fault of spelling, that
// differs with more, than 1 letter from the right form.
int SuggestMgr::replchars(std::vector<std::string>& wlst,
const char* word,
int cpdsuggest) {
std::string candidate;
int wl = strlen(word);
if (wl < 2 || !pAMgr)
return wlst.size();
const std::vector<replentry>& reptable = pAMgr->get_reptable();
for (size_t i = 0; i < reptable.size(); ++i) {
const char* r = word;
// search every occurence of the pattern in the word
while ((r = strstr(r, reptable[i].pattern.c_str())) != NULL) {
int type = (r == word) ? 1 : 0;
if (r - word + reptable[i].pattern.size() == strlen(word))
type += 2;
while (type && reptable[i].outstrings[type].empty())
type = (type == 2 && r != word) ? 0 : type - 1;
const std::string&out = reptable[i].outstrings[type];
if (out.empty()) {
++r;
continue;
}
candidate.assign(word);
candidate.resize(r - word);
candidate.append(reptable[i].outstrings[type]);
candidate.append(r + reptable[i].pattern.size());
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
// check REP suggestions with space
size_t sp = candidate.find(' ');
if (sp != std::string::npos) {
size_t prev = 0;
while (sp != std::string::npos) {
std::string prev_chunk = candidate.substr(prev, sp - prev);
if (checkword(prev_chunk, 0, NULL, NULL)) {
size_t oldns = wlst.size();
std::string post_chunk = candidate.substr(sp + 1);
testsug(wlst, post_chunk, cpdsuggest, NULL, NULL);
if (oldns < wlst.size()) {
wlst[wlst.size() - 1] = candidate;
}
}
prev = sp + 1;
sp = candidate.find(' ', prev);
}
}
r++; // search for the next letter
}
}
return wlst.size();
}
// perhaps we doubled two characters (pattern aba -> ababa, for example vacation
// -> vacacation)
int SuggestMgr::doubletwochars(std::vector<std::string>& wlst,
const char* word,
int cpdsuggest) {
int state = 0;
int wl = strlen(word);
if (wl < 5 || !pAMgr)
return wlst.size();
for (int i = 2; i < wl; i++) {
if (word[i] == word[i - 2]) {
state++;
if (state == 3) {
std::string candidate(word, word + i - 1);
candidate.insert(candidate.end(), word + i + 1, word + wl);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
state = 0;
}
} else {
state = 0;
}
}
return wlst.size();
}
// perhaps we doubled two characters (pattern aba -> ababa, for example vacation
// -> vacacation)
int SuggestMgr::doubletwochars_utf(std::vector<std::string>& wlst,
const w_char* word,
int wl,
int cpdsuggest) {
int state = 0;
if (wl < 5 || !pAMgr)
return wlst.size();
for (int i = 2; i < wl; i++) {
if (word[i] == word[i - 2]) {
state++;
if (state == 3) {
std::vector<w_char> candidate_utf(word, word + i - 1);
candidate_utf.insert(candidate_utf.end(), word + i + 1, word + wl);
std::string candidate;
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
state = 0;
}
} else {
state = 0;
}
}
return wlst.size();
}
// error is wrong char in place of correct one (case and keyboard related
// version)
int SuggestMgr::badcharkey(std::vector<std::string>& wlst,
const char* word,
int cpdsuggest) {
std::string candidate(word);
// swap out each char one by one and try uppercase and neighbor
// keyboard chars in its place to see if that makes a good word
for (size_t i = 0; i < candidate.size(); ++i) {
char tmpc = candidate[i];
// check with uppercase letters
candidate[i] = csconv[((unsigned char)tmpc)].cupper;
if (tmpc != candidate[i]) {
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
candidate[i] = tmpc;
}
// check neighbor characters in keyboard string
if (!ckey)
continue;
char* loc = strchr(ckey, tmpc);
while (loc) {
if ((loc > ckey) && (*(loc - 1) != '|')) {
candidate[i] = *(loc - 1);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
if ((*(loc + 1) != '|') && (*(loc + 1) != '\0')) {
candidate[i] = *(loc + 1);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
loc = strchr(loc + 1, tmpc);
}
candidate[i] = tmpc;
}
return wlst.size();
}
// error is wrong char in place of correct one (case and keyboard related
// version)
int SuggestMgr::badcharkey_utf(std::vector<std::string>& wlst,
const w_char* word,
int wl,
int cpdsuggest) {
std::string candidate;
std::vector<w_char> candidate_utf(word, word + wl);
// swap out each char one by one and try all the tryme
// chars in its place to see if that makes a good word
for (int i = 0; i < wl; i++) {
w_char tmpc = candidate_utf[i];
// check with uppercase letters
candidate_utf[i] = upper_utf(candidate_utf[i], 1);
if (tmpc != candidate_utf[i]) {
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
candidate_utf[i] = tmpc;
}
// check neighbor characters in keyboard string
if (!ckey)
continue;
size_t loc = 0;
while ((loc < ckeyl) && ckey_utf[loc] != tmpc)
++loc;
while (loc < ckeyl) {
if ((loc > 0) && ckey_utf[loc - 1] != W_VLINE) {
candidate_utf[i] = ckey_utf[loc - 1];
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
if (((loc + 1) < ckeyl) && (ckey_utf[loc + 1] != W_VLINE)) {
candidate_utf[i] = ckey_utf[loc + 1];
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
do {
loc++;
} while ((loc < ckeyl) && ckey_utf[loc] != tmpc);
}
candidate_utf[i] = tmpc;
}
return wlst.size();
}
// error is wrong char in place of correct one
int SuggestMgr::badchar(std::vector<std::string>& wlst, const char* word, int cpdsuggest) {
std::string candidate(word);
clock_t timelimit = clock();
int timer = MINTIMER;
// swap out each char one by one and try all the tryme
// chars in its place to see if that makes a good word
for (size_t j = 0; j < ctryl; ++j) {
for (std::string::reverse_iterator aI = candidate.rbegin(), aEnd = candidate.rend(); aI != aEnd; ++aI) {
char tmpc = *aI;
if (ctry[j] == tmpc)
continue;
*aI = ctry[j];
testsug(wlst, candidate, cpdsuggest, &timer, &timelimit);
if (!timer)
return wlst.size();
*aI = tmpc;
}
}
return wlst.size();
}
// error is wrong char in place of correct one
int SuggestMgr::badchar_utf(std::vector<std::string>& wlst,
const w_char* word,
int wl,
int cpdsuggest) {
std::vector<w_char> candidate_utf(word, word + wl);
std::string candidate;
clock_t timelimit = clock();
int timer = MINTIMER;
// swap out each char one by one and try all the tryme
// chars in its place to see if that makes a good word
for (size_t j = 0; j < ctryl; ++j) {
for (int i = wl - 1; i >= 0; i--) {
w_char tmpc = candidate_utf[i];
if (tmpc == ctry_utf[j])
continue;
candidate_utf[i] = ctry_utf[j];
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, &timer, &timelimit);
if (!timer)
return wlst.size();
candidate_utf[i] = tmpc;
}
}
return wlst.size();
}
// error is word has an extra letter it does not need
int SuggestMgr::extrachar_utf(std::vector<std::string>& wlst,
const w_char* word,
int wl,
int cpdsuggest) {
std::vector<w_char> candidate_utf(word, word + wl);
if (candidate_utf.size() < 2)
return wlst.size();
// try omitting one char of word at a time
for (size_t i = 0; i < candidate_utf.size(); ++i) {
size_t index = candidate_utf.size() - 1 - i;
w_char tmpc = candidate_utf[index];
candidate_utf.erase(candidate_utf.begin() + index);
std::string candidate;
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
candidate_utf.insert(candidate_utf.begin() + index, tmpc);
}
return wlst.size();
}
// error is word has an extra letter it does not need
int SuggestMgr::extrachar(std::vector<std::string>& wlst,
const char* word,
int cpdsuggest) {
std::string candidate(word);
if (candidate.size() < 2)
return wlst.size();
// try omitting one char of word at a time
for (size_t i = 0; i < candidate.size(); ++i) {
size_t index = candidate.size() - 1 - i;
char tmpc = candidate[index];
candidate.erase(candidate.begin() + index);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
candidate.insert(candidate.begin() + index, tmpc);
}
return wlst.size();
}
// error is missing a letter it needs
int SuggestMgr::forgotchar(std::vector<std::string>& wlst,
const char* word,
int cpdsuggest) {
std::string candidate(word);
clock_t timelimit = clock();
int timer = MINTIMER;
// try inserting a tryme character before every letter (and the null
// terminator)
for (size_t k = 0; k < ctryl; ++k) {
for (size_t i = 0; i <= candidate.size(); ++i) {
size_t index = candidate.size() - i;
candidate.insert(candidate.begin() + index, ctry[k]);
testsug(wlst, candidate, cpdsuggest, &timer, &timelimit);
if (!timer)
return wlst.size();
candidate.erase(candidate.begin() + index);
}
}
return wlst.size();
}
// error is missing a letter it needs
int SuggestMgr::forgotchar_utf(std::vector<std::string>& wlst,
const w_char* word,
int wl,
int cpdsuggest) {
std::vector<w_char> candidate_utf(word, word + wl);
clock_t timelimit = clock();
int timer = MINTIMER;
// try inserting a tryme character at the end of the word and before every
// letter
for (size_t k = 0; k < ctryl; ++k) {
for (size_t i = 0; i <= candidate_utf.size(); ++i) {
size_t index = candidate_utf.size() - i;
candidate_utf.insert(candidate_utf.begin() + index, ctry_utf[k]);
std::string candidate;
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, &timer, &timelimit);
if (!timer)
return wlst.size();
candidate_utf.erase(candidate_utf.begin() + index);
}
}
return wlst.size();
}
/* error is should have been two words */
int SuggestMgr::twowords(std::vector<std::string>& wlst,
const char* word,
int cpdsuggest) {
int c2;
int forbidden = 0;
int cwrd;
int wl = strlen(word);
if (wl < 3)
return wlst.size();
if (langnum == LANG_hu)
forbidden = check_forbidden(word, wl);
char* candidate = (char*)malloc(wl + 2);
strcpy(candidate + 1, word);
// split the string into two pieces after every char
// if both pieces are good words make them a suggestion
for (char* p = candidate + 1; p[1] != '\0'; p++) {
p[-1] = *p;
// go to end of the UTF-8 character
while (utf8 && ((p[1] & 0xc0) == 0x80)) {
*p = p[1];
p++;
}
if (utf8 && p[1] == '\0')
break; // last UTF-8 character
*p = '\0';
int c1 = checkword(candidate, cpdsuggest, NULL, NULL);
if (c1) {
c2 = checkword((p + 1), cpdsuggest, NULL, NULL);
if (c2) {
*p = ' ';
// spec. Hungarian code (need a better compound word support)
if ((langnum == LANG_hu) && !forbidden &&
// if 3 repeating letter, use - instead of space
(((p[-1] == p[1]) &&
(((p > candidate + 1) && (p[-1] == p[-2])) || (p[-1] == p[2]))) ||
// or multiple compounding, with more, than 6 syllables
((c1 == 3) && (c2 >= 2))))
*p = '-';
cwrd = 1;
for (size_t k = 0; k < wlst.size(); ++k) {
if (wlst[k] == candidate) {
cwrd = 0;
break;
}
}
if (wlst.size() < maxSug) {
if (cwrd) {
wlst.push_back(candidate);
}
} else {
free(candidate);
return wlst.size();
}
// add two word suggestion with dash, if TRY string contains
// "a" or "-"
// NOTE: cwrd doesn't modified for REP twoword sugg.
if (ctry && (strchr(ctry, 'a') || strchr(ctry, '-')) &&
mystrlen(p + 1) > 1 && mystrlen(candidate) - mystrlen(p) > 1) {
*p = '-';
for (size_t k = 0; k < wlst.size(); ++k) {
if (wlst[k] == candidate) {
cwrd = 0;
break;
}
}
if (wlst.size() < maxSug) {
if (cwrd) {
wlst.push_back(candidate);
}
} else {
free(candidate);
return wlst.size();
}
}
}
}
}
free(candidate);
return wlst.size();
}
// error is adjacent letter were swapped
int SuggestMgr::swapchar(std::vector<std::string>& wlst,
const char* word,
int cpdsuggest) {
std::string candidate(word);
if (candidate.size() < 2)
return wlst.size();
// try swapping adjacent chars one by one
for (size_t i = 0; i < candidate.size() - 1; ++i) {
std::swap(candidate[i], candidate[i+1]);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
std::swap(candidate[i], candidate[i+1]);
}
// try double swaps for short words
// ahev -> have, owudl -> would
if (candidate.size() == 4 || candidate.size() == 5) {
candidate[0] = word[1];
candidate[1] = word[0];
candidate[2] = word[2];
candidate[candidate.size() - 2] = word[candidate.size() - 1];
candidate[candidate.size() - 1] = word[candidate.size() - 2];
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
if (candidate.size() == 5) {
candidate[0] = word[0];
candidate[1] = word[2];
candidate[2] = word[1];
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
}
return wlst.size();
}
// error is adjacent letter were swapped
int SuggestMgr::swapchar_utf(std::vector<std::string>& wlst,
const w_char* word,
int wl,
int cpdsuggest) {
std::vector<w_char> candidate_utf(word, word + wl);
if (candidate_utf.size() < 2)
return wlst.size();
std::string candidate;
// try swapping adjacent chars one by one
for (size_t i = 0; i < candidate_utf.size() - 1; ++i) {
std::swap(candidate_utf[i], candidate_utf[i+1]);
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
std::swap(candidate_utf[i], candidate_utf[i+1]);
}
// try double swaps for short words
// ahev -> have, owudl -> would, suodn -> sound
if (candidate_utf.size() == 4 || candidate_utf.size() == 5) {
candidate_utf[0] = word[1];
candidate_utf[1] = word[0];
candidate_utf[2] = word[2];
candidate_utf[candidate_utf.size() - 2] = word[candidate_utf.size() - 1];
candidate_utf[candidate_utf.size() - 1] = word[candidate_utf.size() - 2];
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
if (candidate_utf.size() == 5) {
candidate_utf[0] = word[0];
candidate_utf[1] = word[2];
candidate_utf[2] = word[1];
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
}
return wlst.size();
}
// error is not adjacent letter were swapped
int SuggestMgr::longswapchar(std::vector<std::string>& wlst,
const char* word,
int cpdsuggest) {
std::string candidate(word);
// try swapping not adjacent chars one by one
for (std::string::iterator p = candidate.begin(); p < candidate.end(); ++p) {
for (std::string::iterator q = candidate.begin(); q < candidate.end(); ++q) {
if (std::abs(std::distance(q, p)) > 1) {
std::swap(*p, *q);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
std::swap(*p, *q);
}
}
}
return wlst.size();
}
// error is adjacent letter were swapped
int SuggestMgr::longswapchar_utf(std::vector<std::string>& wlst,
const w_char* word,
int wl,
int cpdsuggest) {
std::vector<w_char> candidate_utf(word, word + wl);
// try swapping not adjacent chars
for (std::vector<w_char>::iterator p = candidate_utf.begin(); p < candidate_utf.end(); ++p) {
for (std::vector<w_char>::iterator q = candidate_utf.begin(); q < candidate_utf.end(); ++q) {
if (std::abs(std::distance(q, p)) > 1) {
std::swap(*p, *q);
std::string candidate;
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
std::swap(*p, *q);
}
}
}
return wlst.size();
}
// error is a letter was moved
int SuggestMgr::movechar(std::vector<std::string>& wlst,
const char* word,
int cpdsuggest) {
std::string candidate(word);
if (candidate.size() < 2)
return wlst.size();
// try moving a char
for (std::string::iterator p = candidate.begin(); p < candidate.end(); ++p) {
for (std::string::iterator q = p + 1; q < candidate.end() && std::distance(p, q) < 10; ++q) {
std::swap(*q, *(q - 1));
if (std::distance(p, q) < 2)
continue; // omit swap char
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
std::copy(word, word + candidate.size(), candidate.begin());
}
for (std::string::reverse_iterator p = candidate.rbegin(), pEnd = candidate.rend() - 1; p != pEnd; ++p) {
for (std::string::reverse_iterator q = p + 1, qEnd = candidate.rend(); q != qEnd && std::distance(p, q) < 10; ++q) {
std::swap(*q, *(q - 1));
if (std::distance(p, q) < 2)
continue; // omit swap char
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
std::copy(word, word + candidate.size(), candidate.begin());
}
return wlst.size();
}
// error is a letter was moved
int SuggestMgr::movechar_utf(std::vector<std::string>& wlst,
const w_char* word,
int wl,
int cpdsuggest) {
std::vector<w_char> candidate_utf(word, word + wl);
if (candidate_utf.size() < 2)
return wlst.size();
// try moving a char
for (std::vector<w_char>::iterator p = candidate_utf.begin(); p < candidate_utf.end(); ++p) {
for (std::vector<w_char>::iterator q = p + 1; q < candidate_utf.end() && std::distance(p, q) < 10; ++q) {
std::swap(*q, *(q - 1));
if (std::distance(p, q) < 2)
continue; // omit swap char
std::string candidate;
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
std::copy(word, word + candidate_utf.size(), candidate_utf.begin());
}
for (std::vector<w_char>::reverse_iterator p = candidate_utf.rbegin(); p < candidate_utf.rend(); ++p) {
for (std::vector<w_char>::reverse_iterator q = p + 1; q < candidate_utf.rend() && std::distance(p, q) < 10; ++q) {
std::swap(*q, *(q - 1));
if (std::distance(p, q) < 2)
continue; // omit swap char
std::string candidate;
u16_u8(candidate, candidate_utf);
testsug(wlst, candidate, cpdsuggest, NULL, NULL);
}
std::copy(word, word + candidate_utf.size(), candidate_utf.begin());
}
return wlst.size();
}
// generate a set of suggestions for very poorly spelled words
void SuggestMgr::ngsuggest(std::vector<std::string>& wlst,
const char* w,
const std::vector<HashMgr*>& rHMgr) {
int lval;
int sc;
int lp, lpphon;