1 #include "license.hunspell"
2 #include "license.myspell"
9 #include "suggestmgr.hxx"
13 const w_char W_VLINE = { '\0', '|' };
15 SuggestMgr::SuggestMgr(const char * tryme, int maxn,
19 // register affix manager and check in string of chars to
20 // try when building candidate suggestions
39 maxngramsugs = MAXNGRAMSUGS;
40 maxcpdsugs = MAXCOMPOUNDSUGS;
43 langnum = pAMgr->get_langnum();
44 ckey = pAMgr->get_key_string();
45 nosplitsugs = pAMgr->get_nosplitsugs();
46 if (pAMgr->get_maxngramsugs() >= 0)
47 maxngramsugs = pAMgr->get_maxngramsugs();
48 utf8 = pAMgr->get_utf8();
49 if (pAMgr->get_maxcpdsugs() >= 0)
50 maxcpdsugs = pAMgr->get_maxcpdsugs();
53 char * enc = pAMgr->get_encoding();
54 csconv = get_current_cs(enc);
57 complexprefixes = pAMgr->get_complexprefixes();
63 ckeyl = u8_u16(t, MAXSWL, ckey);
64 ckey_utf = (w_char *) malloc(ckeyl * sizeof(w_char));
65 if (ckey_utf) memcpy(ckey_utf, t, ckeyl * sizeof(w_char));
73 ctry = mystrdup(tryme);
74 if (ctry) ctryl = strlen(ctry);
77 ctryl = u8_u16(t, MAXSWL, tryme);
78 ctry_utf = (w_char *) malloc(ctryl * sizeof(w_char));
79 if (ctry_utf) memcpy(ctry_utf, t, ctryl * sizeof(w_char));
86 SuggestMgr::~SuggestMgr()
91 if (ckey_utf) free(ckey_utf);
96 if (ctry_utf) free(ctry_utf);
100 #ifdef MOZILLA_CLIENT
105 int SuggestMgr::testsug(char** wlst, const char * candidate, int wl, int ns, int cpdsuggest,
106 int * timer, clock_t * timelimit) {
108 if (ns == maxSug) return maxSug;
109 for (int k=0; k < ns; k++) {
110 if (strcmp(candidate,wlst[k]) == 0) cwrd = 0;
112 if ((cwrd) && checkword(candidate, wl, cpdsuggest, timer, timelimit)) {
113 wlst[ns] = mystrdup(candidate);
114 if (wlst[ns] == NULL) {
115 for (int j=0; j<ns; j++) free(wlst[j]);
123 // generate suggestions for a misspelled word
124 // pass in address of array of char * pointers
125 // onlycompoundsug: probably bad suggestions (need for ngram sugs, too)
127 int SuggestMgr::suggest(char*** slst, const char * w, int nsug,
128 int * onlycompoundsug)
130 int nocompoundtwowords = 0;
132 w_char word_utf[MAXSWL];
135 char w2[MAXWORDUTF8LEN];
136 const char * word = w;
139 // word reversing wrapper for complex prefixes
140 if (complexprefixes) {
142 if (utf8) reverseword_utf(w2); else reverseword(w2);
149 wlst = (char **) malloc(maxSug * sizeof(char *));
150 if (wlst == NULL) return -1;
151 for (int i = 0; i < maxSug; i++) {
157 wl = u8_u16(word_utf, MAXSWL, word);
160 for (int cpdsuggest=0; (cpdsuggest<2) && (nocompoundtwowords==0); cpdsuggest++) {
162 // limit compound suggestion
163 if (cpdsuggest > 0) oldSug = nsug;
165 // suggestions for an uppercase word (html -> HTML)
166 if ((nsug < maxSug) && (nsug > -1)) {
167 nsug = (utf8) ? capchars_utf(wlst, word_utf, wl, nsug, cpdsuggest) :
168 capchars(wlst, word, nsug, cpdsuggest);
171 // perhaps we made a typical fault of spelling
172 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) {
173 nsug = replchars(wlst, word, nsug, cpdsuggest);
176 // perhaps we made chose the wrong char from a related set
177 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) {
178 nsug = mapchars(wlst, word, nsug, cpdsuggest);
181 // did we swap the order of chars by mistake
182 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) {
183 nsug = (utf8) ? swapchar_utf(wlst, word_utf, wl, nsug, cpdsuggest) :
184 swapchar(wlst, word, nsug, cpdsuggest);
187 // did we swap the order of non adjacent chars by mistake
188 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) {
189 nsug = (utf8) ? longswapchar_utf(wlst, word_utf, wl, nsug, cpdsuggest) :
190 longswapchar(wlst, word, nsug, cpdsuggest);
193 // did we just hit the wrong key in place of a good char (case and keyboard)
194 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) {
195 nsug = (utf8) ? badcharkey_utf(wlst, word_utf, wl, nsug, cpdsuggest) :
196 badcharkey(wlst, word, nsug, cpdsuggest);
199 // only suggest compound words when no other suggestion
200 if ((cpdsuggest == 0) && (nsug > nsugorig)) nocompoundtwowords=1;
202 // did we add a char that should not be there
203 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) {
204 nsug = (utf8) ? extrachar_utf(wlst, word_utf, wl, nsug, cpdsuggest) :
205 extrachar(wlst, word, nsug, cpdsuggest);
209 // did we forgot a char
210 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) {
211 nsug = (utf8) ? forgotchar_utf(wlst, word_utf, wl, nsug, cpdsuggest) :
212 forgotchar(wlst, word, nsug, cpdsuggest);
215 // did we move a char
216 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) {
217 nsug = (utf8) ? movechar_utf(wlst, word_utf, wl, nsug, cpdsuggest) :
218 movechar(wlst, word, nsug, cpdsuggest);
221 // did we just hit the wrong key in place of a good char
222 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) {
223 nsug = (utf8) ? badchar_utf(wlst, word_utf, wl, nsug, cpdsuggest) :
224 badchar(wlst, word, nsug, cpdsuggest);
227 // did we double two characters
228 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) {
229 nsug = (utf8) ? doubletwochars_utf(wlst, word_utf, wl, nsug, cpdsuggest) :
230 doubletwochars(wlst, word, nsug, cpdsuggest);
233 // perhaps we forgot to hit space and two words ran together
234 if (!nosplitsugs && (nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) {
235 nsug = twowords(wlst, word, nsug, cpdsuggest);
238 } // repeating ``for'' statement compounding support
241 // we ran out of memory - we should free up as much as possible
242 for (int i = 0; i < maxSug; i++)
243 if (wlst[i] != NULL) free(wlst[i]);
248 if (!nocompoundtwowords && (nsug > 0) && onlycompoundsug) *onlycompoundsug = 1;
254 // generate suggestions for a word with typical mistake
255 // pass in address of array of char * pointers
256 #ifdef HUNSPELL_EXPERIMENTAL
257 int SuggestMgr::suggest_auto(char*** slst, const char * w, int nsug)
259 int nocompoundtwowords = 0;
263 char w2[MAXWORDUTF8LEN];
264 const char * word = w;
266 // word reversing wrapper for complex prefixes
267 if (complexprefixes) {
269 if (utf8) reverseword_utf(w2); else reverseword(w2);
276 wlst = (char **) malloc(maxSug * sizeof(char *));
277 if (wlst == NULL) return -1;
280 for (int cpdsuggest=0; (cpdsuggest<2) && (nocompoundtwowords==0); cpdsuggest++) {
282 // limit compound suggestion
283 if (cpdsuggest > 0) oldSug = nsug;
285 // perhaps we made a typical fault of spelling
286 if ((nsug < maxSug) && (nsug > -1))
287 nsug = replchars(wlst, word, nsug, cpdsuggest);
289 // perhaps we made chose the wrong char from a related set
290 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs)))
291 nsug = mapchars(wlst, word, nsug, cpdsuggest);
293 if ((cpdsuggest==0) && (nsug>0)) nocompoundtwowords=1;
295 // perhaps we forgot to hit space and two words ran together
297 if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs)) && check_forbidden(word, strlen(word))) {
298 nsug = twowords(wlst, word, nsug, cpdsuggest);
301 } // repeating ``for'' statement compounding support
304 for (int i=0;i<maxSug; i++)
305 if (wlst[i] != NULL) free(wlst[i]);
313 #endif // END OF HUNSPELL_EXPERIMENTAL CODE
315 // suggestions for an uppercase word (html -> HTML)
316 int SuggestMgr::capchars_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest)
318 char candidate[MAXSWUTF8L];
319 w_char candidate_utf[MAXSWL];
320 memcpy(candidate_utf, word, wl * sizeof(w_char));
321 mkallcap_utf(candidate_utf, wl, langnum);
322 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl);
323 return testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL);
326 // suggestions for an uppercase word (html -> HTML)
327 int SuggestMgr::capchars(char** wlst, const char * word, int ns, int cpdsuggest)
329 char candidate[MAXSWUTF8L];
330 strcpy(candidate, word);
331 mkallcap(candidate, csconv);
332 return testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL);
335 // suggestions for when chose the wrong char out of a related set
336 int SuggestMgr::mapchars(char** wlst, const char * word, int ns, int cpdsuggest)
338 char candidate[MAXSWUTF8L];
343 int wl = strlen(word);
344 if (wl < 2 || ! pAMgr) return ns;
346 int nummap = pAMgr->get_nummap();
347 struct mapentry* maptable = pAMgr->get_maptable();
348 if (maptable==NULL) return ns;
352 return map_related(word, (char *) &candidate, 0, 0, wlst, cpdsuggest, ns, maptable, nummap, &timer, &timelimit);
355 int SuggestMgr::map_related(const char * word, char * candidate, int wn, int cn,
356 char** wlst, int cpdsuggest, int ns,
357 const mapentry* maptable, int nummap, int * timer, clock_t * timelimit)
359 if (*(word + wn) == '\0') {
361 *(candidate + cn) = '\0';
362 int wl = strlen(candidate);
363 for (int m=0; m < ns; m++)
364 if (strcmp(candidate, wlst[m]) == 0) cwrd = 0;
365 if ((cwrd) && checkword(candidate, wl, cpdsuggest, timer, timelimit)) {
367 wlst[ns] = mystrdup(candidate);
368 if (wlst[ns] == NULL) return -1;
375 for (int j = 0; j < nummap; j++) {
376 for (int k = 0; k < maptable[j].len; k++) {
377 int len = strlen(maptable[j].set[k]);
378 if (strncmp(maptable[j].set[k], word + wn, len) == 0) {
380 for (int l = 0; l < maptable[j].len; l++) {
381 strcpy(candidate + cn, maptable[j].set[l]);
382 ns = map_related(word, candidate, wn + len, strlen(candidate), wlst,
383 cpdsuggest, ns, maptable, nummap, timer, timelimit);
384 if (!(*timer)) return ns;
390 *(candidate + cn) = *(word + wn);
391 ns = map_related(word, candidate, wn + 1, cn + 1, wlst, cpdsuggest,
392 ns, maptable, nummap, timer, timelimit);
397 // suggestions for a typical fault of spelling, that
398 // differs with more, than 1 letter from the right form.
399 int SuggestMgr::replchars(char** wlst, const char * word, int ns, int cpdsuggest)
401 char candidate[MAXSWUTF8L];
404 int wl = strlen(word);
405 if (wl < 2 || ! pAMgr) return ns;
406 int numrep = pAMgr->get_numrep();
407 struct replentry* reptable = pAMgr->get_reptable();
408 if (reptable==NULL) return ns;
409 for (int i=0; i < numrep; i++ ) {
411 lenr = strlen(reptable[i].pattern2);
412 lenp = strlen(reptable[i].pattern);
413 // search every occurence of the pattern in the word
414 while ((r=strstr(r, reptable[i].pattern)) != NULL && (!reptable[i].end || strlen(r) == strlen(reptable[i].pattern)) &&
415 (!reptable[i].start || r == word)) {
416 strcpy(candidate, word);
417 if (r-word + lenr + strlen(r+lenp) >= MAXSWUTF8L) break;
418 strcpy(candidate+(r-word),reptable[i].pattern2);
419 strcpy(candidate+(r-word)+lenr, r+lenp);
420 ns = testsug(wlst, candidate, wl-lenp+lenr, ns, cpdsuggest, NULL, NULL);
421 if (ns == -1) return -1;
422 // check REP suggestions with space
423 char * sp = strchr(candidate, ' ');
425 char * prev = candidate;
428 if (checkword(prev, strlen(prev), 0, NULL, NULL)) {
431 ns = testsug(wlst, sp + 1, strlen(sp + 1), ns, cpdsuggest, NULL, NULL);
432 if (ns == -1) return -1;
435 wlst[ns - 1] = mystrdup(candidate);
436 if (!wlst[ns - 1]) return -1;
441 sp = strchr(prev, ' ');
444 r++; // search for the next letter
450 // perhaps we doubled two characters (pattern aba -> ababa, for example vacation -> vacacation)
451 int SuggestMgr::doubletwochars(char** wlst, const char * word, int ns, int cpdsuggest)
453 char candidate[MAXSWUTF8L];
455 int wl = strlen(word);
456 if (wl < 5 || ! pAMgr) return ns;
457 for (int i=2; i < wl; i++ ) {
458 if (word[i]==word[i-2]) {
461 strcpy(candidate,word);
462 strcpy(candidate+i-1,word+i+1);
463 ns = testsug(wlst, candidate, wl-2, ns, cpdsuggest, NULL, NULL);
464 if (ns == -1) return -1;
474 // perhaps we doubled two characters (pattern aba -> ababa, for example vacation -> vacacation)
475 int SuggestMgr::doubletwochars_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest)
477 w_char candidate_utf[MAXSWL];
478 char candidate[MAXSWUTF8L];
480 if (wl < 5 || ! pAMgr) return ns;
481 for (int i=2; i < wl; i++) {
482 if (w_char_eq(word[i], word[i-2])) {
485 memcpy(candidate_utf, word, (i - 1) * sizeof(w_char));
486 memcpy(candidate_utf+i-1, word+i+1, (wl-i-1) * sizeof(w_char));
487 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl-2);
488 ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL);
489 if (ns == -1) return -1;
499 // error is wrong char in place of correct one (case and keyboard related version)
500 int SuggestMgr::badcharkey(char ** wlst, const char * word, int ns, int cpdsuggest)
503 char candidate[MAXSWUTF8L];
504 int wl = strlen(word);
505 strcpy(candidate, word);
506 // swap out each char one by one and try uppercase and neighbor
507 // keyboard chars in its place to see if that makes a good word
509 for (int i=0; i < wl; i++) {
511 // check with uppercase letters
512 candidate[i] = csconv[((unsigned char)tmpc)].cupper;
513 if (tmpc != candidate[i]) {
514 ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL);
515 if (ns == -1) return -1;
518 // check neighbor characters in keyboard string
520 char * loc = strchr(ckey, tmpc);
522 if ((loc > ckey) && (*(loc - 1) != '|')) {
523 candidate[i] = *(loc - 1);
524 ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL);
525 if (ns == -1) return -1;
527 if ((*(loc + 1) != '|') && (*(loc + 1) != '\0')) {
528 candidate[i] = *(loc + 1);
529 ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL);
530 if (ns == -1) return -1;
532 loc = strchr(loc + 1, tmpc);
539 // error is wrong char in place of correct one (case and keyboard related version)
540 int SuggestMgr::badcharkey_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest)
543 w_char candidate_utf[MAXSWL];
544 char candidate[MAXSWUTF8L];
545 memcpy(candidate_utf, word, wl * sizeof(w_char));
546 // swap out each char one by one and try all the tryme
547 // chars in its place to see if that makes a good word
548 for (int i=0; i < wl; i++) {
549 tmpc = candidate_utf[i];
550 // check with uppercase letters
551 mkallcap_utf(candidate_utf + i, 1, langnum);
552 if (!w_char_eq(tmpc, candidate_utf[i])) {
553 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl);
554 ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL);
555 if (ns == -1) return -1;
556 candidate_utf[i] = tmpc;
558 // check neighbor characters in keyboard string
560 w_char * loc = ckey_utf;
561 while ((loc < (ckey_utf + ckeyl)) && !w_char_eq(*loc, tmpc)) loc++;
562 while (loc < (ckey_utf + ckeyl)) {
563 if ((loc > ckey_utf) && !w_char_eq(*(loc - 1), W_VLINE)) {
564 candidate_utf[i] = *(loc - 1);
565 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl);
566 ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL);
567 if (ns == -1) return -1;
569 if (((loc + 1) < (ckey_utf + ckeyl)) && !w_char_eq(*(loc + 1), W_VLINE)) {
570 candidate_utf[i] = *(loc + 1);
571 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl);
572 ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL);
573 if (ns == -1) return -1;
575 do { loc++; } while ((loc < (ckey_utf + ckeyl)) && !w_char_eq(*loc, tmpc));
577 candidate_utf[i] = tmpc;
582 // error is wrong char in place of correct one
583 int SuggestMgr::badchar(char ** wlst, const char * word, int ns, int cpdsuggest)
586 char candidate[MAXSWUTF8L];
587 clock_t timelimit = clock();
588 int timer = MINTIMER;
589 int wl = strlen(word);
590 strcpy(candidate, word);
591 // swap out each char one by one and try all the tryme
592 // chars in its place to see if that makes a good word
593 for (int j=0; j < ctryl; j++) {
594 for (int i=wl-1; i >= 0; i--) {
596 if (ctry[j] == tmpc) continue;
597 candidate[i] = ctry[j];
598 ns = testsug(wlst, candidate, wl, ns, cpdsuggest, &timer, &timelimit);
599 if (ns == -1) return -1;
600 if (!timer) return ns;
607 // error is wrong char in place of correct one
608 int SuggestMgr::badchar_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest)
611 w_char candidate_utf[MAXSWL];
612 char candidate[MAXSWUTF8L];
613 clock_t timelimit = clock();
614 int timer = MINTIMER;
615 memcpy(candidate_utf, word, wl * sizeof(w_char));
616 // swap out each char one by one and try all the tryme
617 // chars in its place to see if that makes a good word
618 for (int j=0; j < ctryl; j++) {
619 for (int i=wl-1; i >= 0; i--) {
620 tmpc = candidate_utf[i];
621 if (w_char_eq(tmpc, ctry_utf[j])) continue;
622 candidate_utf[i] = ctry_utf[j];
623 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl);
624 ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, &timer, &timelimit);
625 if (ns == -1) return -1;
626 if (!timer) return ns;
627 candidate_utf[i] = tmpc;
633 // error is word has an extra letter it does not need
634 int SuggestMgr::extrachar_utf(char** wlst, const w_char * word, int wl, int ns, int cpdsuggest)
636 char candidate[MAXSWUTF8L];
637 w_char candidate_utf[MAXSWL];
639 w_char tmpc = W_VLINE; // not used value, only for VCC warning message
640 if (wl < 2) return ns;
641 // try omitting one char of word at a time
642 memcpy(candidate_utf, word, wl * sizeof(w_char));
643 for (p = candidate_utf + wl - 1; p >= candidate_utf; p--) {
645 if (p < candidate_utf + wl - 1) *p = tmpc;
646 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl - 1);
647 ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL);
648 if (ns == -1) return -1;
654 // error is word has an extra letter it does not need
655 int SuggestMgr::extrachar(char** wlst, const char * word, int ns, int cpdsuggest)
658 char candidate[MAXSWUTF8L];
660 int wl = strlen(word);
661 if (wl < 2) return ns;
662 // try omitting one char of word at a time
663 strcpy (candidate, word);
664 for (p = candidate + wl - 1; p >=candidate; p--) {
667 ns = testsug(wlst, candidate, wl-1, ns, cpdsuggest, NULL, NULL);
668 if (ns == -1) return -1;
674 // error is missing a letter it needs
675 int SuggestMgr::forgotchar(char ** wlst, const char * word, int ns, int cpdsuggest)
677 char candidate[MAXSWUTF8L];
679 clock_t timelimit = clock();
680 int timer = MINTIMER;
681 int wl = strlen(word);
682 // try inserting a tryme character before every letter (and the null terminator)
683 for (int i = 0; i < ctryl; i++) {
684 strcpy(candidate, word);
685 for (p = candidate + wl; p >= candidate; p--) {
688 ns = testsug(wlst, candidate, wl+1, ns, cpdsuggest, &timer, &timelimit);
689 if (ns == -1) return -1;
690 if (!timer) return ns;
696 // error is missing a letter it needs
697 int SuggestMgr::forgotchar_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest)
699 w_char candidate_utf[MAXSWL];
700 char candidate[MAXSWUTF8L];
702 clock_t timelimit = clock();
703 int timer = MINTIMER;
704 // try inserting a tryme character at the end of the word and before every letter
705 for (int i = 0; i < ctryl; i++) {
706 memcpy (candidate_utf, word, wl * sizeof(w_char));
707 for (p = candidate_utf + wl; p >= candidate_utf; p--) {
710 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl + 1);
711 ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, &timer, &timelimit);
712 if (ns == -1) return -1;
713 if (!timer) return ns;
720 /* error is should have been two words */
721 int SuggestMgr::twowords(char ** wlst, const char * word, int ns, int cpdsuggest)
723 char candidate[MAXSWUTF8L];
730 if (wl < 3) return ns;
732 if (langnum == LANG_hu) forbidden = check_forbidden(word, wl);
734 strcpy(candidate + 1, word);
735 // split the string into two pieces after every char
736 // if both pieces are good words make them a suggestion
737 for (p = candidate + 1; p[1] != '\0'; p++) {
739 // go to end of the UTF-8 character
740 while (utf8 && ((p[1] & 0xc0) == 0x80)) {
744 if (utf8 && p[1] == '\0') break; // last UTF-8 character
746 c1 = checkword(candidate,strlen(candidate), cpdsuggest, NULL, NULL);
748 c2 = checkword((p+1),strlen(p+1), cpdsuggest, NULL, NULL);
752 // spec. Hungarian code (need a better compound word support)
753 if ((langnum == LANG_hu) && !forbidden &&
754 // if 3 repeating letter, use - instead of space
755 (((p[-1] == p[1]) && (((p>candidate+1) && (p[-1] == p[-2])) || (p[-1] == p[2]))) ||
756 // or multiple compounding, with more, than 6 syllables
757 ((c1 == 3) && (c2 >= 2)))) *p = '-';
760 for (int k=0; k < ns; k++)
761 if (strcmp(candidate,wlst[k]) == 0) cwrd = 0;
764 wlst[ns] = mystrdup(candidate);
765 if (wlst[ns] == NULL) return -1;
769 // add two word suggestion with dash, if TRY string contains
771 // NOTE: cwrd doesn't modified for REP twoword sugg.
772 if (ctry && (strchr(ctry, 'a') || strchr(ctry, '-')) &&
773 mystrlen(p + 1) > 1 &&
774 mystrlen(candidate) - mystrlen(p) > 1) {
776 for (int k=0; k < ns; k++)
777 if (strcmp(candidate,wlst[k]) == 0) cwrd = 0;
780 wlst[ns] = mystrdup(candidate);
781 if (wlst[ns] == NULL) return -1;
793 // error is adjacent letter were swapped
794 int SuggestMgr::swapchar(char ** wlst, const char * word, int ns, int cpdsuggest)
796 char candidate[MAXSWUTF8L];
800 // try swapping adjacent chars one by one
801 strcpy(candidate, word);
802 for (p = candidate; p[1] != 0; p++) {
806 ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL);
807 if (ns == -1) return -1;
811 // try double swaps for short words
812 // ahev -> have, owudl -> would
813 if (wl == 4 || wl == 5) {
814 candidate[0] = word[1];
815 candidate[1] = word[0];
816 candidate[2] = word[2];
817 candidate[wl - 2] = word[wl - 1];
818 candidate[wl - 1] = word[wl - 2];
819 ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL);
820 if (ns == -1) return -1;
822 candidate[0] = word[0];
823 candidate[1] = word[2];
824 candidate[2] = word[1];
825 ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL);
826 if (ns == -1) return -1;
832 // error is adjacent letter were swapped
833 int SuggestMgr::swapchar_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest)
835 w_char candidate_utf[MAXSWL];
836 char candidate[MAXSWUTF8L];
840 // try swapping adjacent chars one by one
841 memcpy (candidate_utf, word, wl * sizeof(w_char));
842 for (p = candidate_utf; p < (candidate_utf + wl - 1); p++) {
846 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl);
847 if (len == 0) len = strlen(candidate);
848 ns = testsug(wlst, candidate, len, ns, cpdsuggest, NULL, NULL);
849 if (ns == -1) return -1;
853 // try double swaps for short words
854 // ahev -> have, owudl -> would, suodn -> sound
855 if (wl == 4 || wl == 5) {
856 candidate_utf[0] = word[1];
857 candidate_utf[1] = word[0];
858 candidate_utf[2] = word[2];
859 candidate_utf[wl - 2] = word[wl - 1];
860 candidate_utf[wl - 1] = word[wl - 2];
861 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl);
862 ns = testsug(wlst, candidate, len, ns, cpdsuggest, NULL, NULL);
863 if (ns == -1) return -1;
865 candidate_utf[0] = word[0];
866 candidate_utf[1] = word[2];
867 candidate_utf[2] = word[1];
868 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl);
869 ns = testsug(wlst, candidate, len, ns, cpdsuggest, NULL, NULL);
870 if (ns == -1) return -1;
876 // error is not adjacent letter were swapped
877 int SuggestMgr::longswapchar(char ** wlst, const char * word, int ns, int cpdsuggest)
879 char candidate[MAXSWUTF8L];
884 // try swapping not adjacent chars one by one
885 strcpy(candidate, word);
886 for (p = candidate; *p != 0; p++) {
887 for (q = candidate; *q != 0; q++) {
888 if (abs((int)(p-q)) > 1) {
892 ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL);
893 if (ns == -1) return -1;
903 // error is adjacent letter were swapped
904 int SuggestMgr::longswapchar_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest)
906 w_char candidate_utf[MAXSWL];
907 char candidate[MAXSWUTF8L];
911 // try swapping not adjacent chars
912 memcpy (candidate_utf, word, wl * sizeof(w_char));
913 for (p = candidate_utf; p < (candidate_utf + wl); p++) {
914 for (q = candidate_utf; q < (candidate_utf + wl); q++) {
915 if (abs((int)(p-q)) > 1) {
919 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl);
920 ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL);
921 if (ns == -1) return -1;
930 // error is a letter was moved
931 int SuggestMgr::movechar(char ** wlst, const char * word, int ns, int cpdsuggest)
933 char candidate[MAXSWUTF8L];
940 strcpy(candidate, word);
941 for (p = candidate; *p != 0; p++) {
942 for (q = p + 1; (*q != 0) && ((q - p) < 10); q++) {
946 if ((q-p) < 2) continue; // omit swap char
947 ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL);
948 if (ns == -1) return -1;
950 strcpy(candidate, word);
952 for (p = candidate + wl - 1; p > candidate; p--) {
953 for (q = p - 1; (q >= candidate) && ((p - q) < 10); q--) {
957 if ((p-q) < 2) continue; // omit swap char
958 ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL);
959 if (ns == -1) return -1;
961 strcpy(candidate, word);
966 // error is a letter was moved
967 int SuggestMgr::movechar_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest)
969 w_char candidate_utf[MAXSWL];
970 char candidate[MAXSWUTF8L];
975 memcpy (candidate_utf, word, wl * sizeof(w_char));
976 for (p = candidate_utf; p < (candidate_utf + wl); p++) {
977 for (q = p + 1; (q < (candidate_utf + wl)) && ((q - p) < 10); q++) {
981 if ((q-p) < 2) continue; // omit swap char
982 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl);
983 ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL);
984 if (ns == -1) return -1;
986 memcpy (candidate_utf, word, wl * sizeof(w_char));
988 for (p = candidate_utf + wl - 1; p > candidate_utf; p--) {
989 for (q = p - 1; (q >= candidate_utf) && ((p - q) < 10); q--) {
993 if ((p-q) < 2) continue; // omit swap char
994 u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl);
995 ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL);
996 if (ns == -1) return -1;
998 memcpy (candidate_utf, word, wl * sizeof(w_char));
1003 // generate a set of suggestions for very poorly spelled words
1004 int SuggestMgr::ngsuggest(char** wlst, char * w, int ns, HashMgr** pHMgr, int md)
1013 // exhaustively search through all root words
1014 // keeping track of the MAX_ROOTS most similar root words
1015 struct hentry * roots[MAX_ROOTS];
1016 char * rootsphon[MAX_ROOTS];
1017 int scores[MAX_ROOTS];
1018 int scoresphon[MAX_ROOTS];
1019 for (i = 0; i < MAX_ROOTS; i++) {
1021 scores[i] = -100 * i;
1022 rootsphon[i] = NULL;
1023 scoresphon[i] = -100 * i;
1026 lpphon = MAX_ROOTS - 1;
1029 char w2[MAXWORDUTF8LEN];
1033 // word reversing wrapper for complex prefixes
1034 if (complexprefixes) {
1036 if (utf8) reverseword_utf(w2); else reverseword(w2);
1040 char mw[MAXSWUTF8L];
1042 int nc = strlen(word);
1043 int n = (utf8) ? u8_u16(u8, MAXSWL, word) : nc;
1045 // set character based ngram suggestion for words with non-BMP Unicode characters
1052 struct hentry* hp = NULL;
1054 phonetable * ph = (pAMgr) ? pAMgr->get_phonetable() : NULL;
1055 char target[MAXSWUTF8L];
1056 char candidate[MAXSWUTF8L];
1060 int _wl = u8_u16(_w, MAXSWL, word);
1061 mkallcap_utf(_w, _wl, langnum);
1062 u16_u8(candidate, MAXSWUTF8L, _w, _wl);
1064 strcpy(candidate, word);
1065 mkallcap(candidate, csconv);
1067 phonet(candidate, target, n, *ph);
1070 FLAG forbiddenword = pAMgr ? pAMgr->get_forbiddenword() : FLAG_NULL;
1071 FLAG nosuggest = pAMgr ? pAMgr->get_nosuggest() : FLAG_NULL;
1072 FLAG nongramsuggest = pAMgr ? pAMgr->get_nongramsuggest() : FLAG_NULL;
1073 FLAG onlyincompound = pAMgr ? pAMgr->get_onlyincompound() : FLAG_NULL;
1075 for (i = 0; i < md; i++) {
1076 while (0 != (hp = (pHMgr[i])->walk_hashtable(col, hp))) {
1077 if ((hp->astr) && (pAMgr) &&
1078 (TESTAFF(hp->astr, forbiddenword, hp->alen) ||
1079 TESTAFF(hp->astr, ONLYUPCASEFLAG, hp->alen) ||
1080 TESTAFF(hp->astr, nosuggest, hp->alen) ||
1081 TESTAFF(hp->astr, nongramsuggest, hp->alen) ||
1082 TESTAFF(hp->astr, onlyincompound, hp->alen))) continue;
1084 sc = ngram(3, word, HENTRY_WORD(hp), NGRAM_LONGER_WORSE + NGRAM_LOWERING) +
1085 leftcommonsubstring(word, HENTRY_WORD(hp));
1087 // check special pronounciation
1088 if ((hp->var & H_OPT_PHON) && copy_field(f, HENTRY_DATA(hp), MORPH_PHON)) {
1089 int sc2 = ngram(3, word, f, NGRAM_LONGER_WORSE + NGRAM_LOWERING) +
1090 + leftcommonsubstring(word, f);
1091 if (sc2 > sc) sc = sc2;
1095 if (ph && (sc > 2) && (abs(n - (int) hp->clen) <= 3)) {
1096 char target2[MAXSWUTF8L];
1099 int _wl = u8_u16(_w, MAXSWL, HENTRY_WORD(hp));
1100 mkallcap_utf(_w, _wl, langnum);
1101 u16_u8(candidate, MAXSWUTF8L, _w, _wl);
1103 strcpy(candidate, HENTRY_WORD(hp));
1104 mkallcap(candidate, csconv);
1106 phonet(candidate, target2, -1, *ph);
1107 scphon = 2 * ngram(3, target, target2, NGRAM_LONGER_WORSE);
1110 if (sc > scores[lp]) {
1114 for (j=0; j < MAX_ROOTS; j++)
1115 if (scores[j] < lval) {
1122 if (scphon > scoresphon[lpphon]) {
1123 scoresphon[lpphon] = scphon;
1124 rootsphon[lpphon] = HENTRY_WORD(hp);
1126 for (j=0; j < MAX_ROOTS; j++)
1127 if (scoresphon[j] < lval) {
1129 lval = scoresphon[j];
1134 // find minimum threshold for a passable suggestion
1135 // mangle original word three differnt ways
1136 // and score them to generate a minimum acceptable score
1138 for (int sp = 1; sp < 4; sp++) {
1140 for (int k=sp; k < n; k+=4) *((unsigned short *) u8 + k) = '*';
1141 u16_u8(mw, MAXSWUTF8L, u8, n);
1142 thresh = thresh + ngram(n, word, mw, NGRAM_ANY_MISMATCH + NGRAM_LOWERING);
1145 for (int k=sp; k < n; k+=4) *(mw + k) = '*';
1146 thresh = thresh + ngram(n, word, mw, NGRAM_ANY_MISMATCH + NGRAM_LOWERING);
1149 thresh = thresh / 3;
1152 // now expand affixes on each of these root words and
1153 // and use length adjusted ngram scores to select
1154 // possible suggestions
1155 char * guess[MAX_GUESS];
1156 char * guessorig[MAX_GUESS];
1157 int gscore[MAX_GUESS];
1158 for(i=0;i<MAX_GUESS;i++) {
1160 guessorig[i] = NULL;
1161 gscore[i] = -100 * i;
1166 struct guessword * glst;
1167 glst = (struct guessword *) calloc(MAX_WORDS,sizeof(struct guessword));
1169 if (nonbmp) utf8 = 1;
1173 for (i = 0; i < MAX_ROOTS; i++) {
1175 struct hentry * rp = roots[i];
1176 int nw = pAMgr->expand_rootword(glst, MAX_WORDS, HENTRY_WORD(rp), rp->blen,
1177 rp->astr, rp->alen, word, nc,
1178 ((rp->var & H_OPT_PHON) ? copy_field(f, HENTRY_DATA(rp), MORPH_PHON) : NULL));
1180 for (int k = 0; k < nw ; k++) {
1181 sc = ngram(n, word, glst[k].word, NGRAM_ANY_MISMATCH + NGRAM_LOWERING) +
1182 leftcommonsubstring(word, glst[k].word);
1185 if (sc > gscore[lp]) {
1188 if (guessorig[lp]) {
1189 free(guessorig[lp]);
1190 guessorig[lp] = NULL;
1194 guess[lp] = glst[k].word;
1195 guessorig[lp] = glst[k].orig;
1197 for (j=0; j < MAX_GUESS; j++)
1198 if (gscore[j] < lval) {
1204 if (glst[k].orig) free(glst[k].orig);
1208 if (glst[k].orig) free(glst[k].orig);
1215 // now we are done generating guesses
1216 // sort in order of decreasing score
1219 bubblesort(&guess[0], &guessorig[0], &gscore[0], MAX_GUESS);
1220 if (ph) bubblesort(&rootsphon[0], NULL, &scoresphon[0], MAX_ROOTS);
1222 // weight suggestions with a similarity index, based on
1223 // the longest common subsequent algorithm and resort
1229 int maxd = pAMgr->get_maxdiff();
1230 if (maxd >= 0) fact = (10.0 - maxd)/5.0;
1233 for (i=0; i < MAX_GUESS; i++) {
1235 // lowering guess[i]
1236 char gl[MAXSWUTF8L];
1240 len = u8_u16(_w, MAXSWL, guess[i]);
1241 mkallsmall_utf(_w, len, langnum);
1242 u16_u8(gl, MAXSWUTF8L, _w, len);
1244 strcpy(gl, guess[i]);
1245 mkallsmall(gl, csconv);
1246 len = strlen(guess[i]);
1249 int _lcs = lcslen(word, gl);
1251 // same characters with different casing
1252 if ((n == len) && (n == _lcs)) {
1256 // using 2-gram instead of 3, and other weightening
1258 // length of longest common subsequent minus length difference
1259 2 * _lcs - abs((int) (n - len)) +
1260 // weight length of the left common substring
1261 leftcommonsubstring(word, gl) +
1262 // weight equal character positions
1263 (commoncharacterpositions(word, gl, &is_swap) ? 1: 0) +
1264 // swap character (not neighboring)
1265 ((is_swap) ? 10 : 0) +
1267 ngram(4, word, gl, NGRAM_ANY_MISMATCH + NGRAM_LOWERING) +
1269 (re = ngram(2, word, gl, NGRAM_ANY_MISMATCH + NGRAM_LOWERING + NGRAM_WEIGHTED)) +
1270 (re += ngram(2, gl, word, NGRAM_ANY_MISMATCH + NGRAM_LOWERING + NGRAM_WEIGHTED)) +
1271 // different limit for dictionaries with PHONE rules
1272 (ph ? (re < len * fact ? -1000 : 0) : (re < (n + len)*fact? -1000 : 0));
1276 bubblesort(&guess[0], &guessorig[0], &gscore[0], MAX_GUESS);
1279 if (ph) for (i=0; i < MAX_ROOTS; i++) {
1281 // lowering rootphon[i]
1282 char gl[MAXSWUTF8L];
1286 len = u8_u16(_w, MAXSWL, rootsphon[i]);
1287 mkallsmall_utf(_w, len, langnum);
1288 u16_u8(gl, MAXSWUTF8L, _w, len);
1290 strcpy(gl, rootsphon[i]);
1291 mkallsmall(gl, csconv);
1292 len = strlen(rootsphon[i]);
1295 // heuristic weigthing of ngram scores
1296 scoresphon[i] += 2 * lcslen(word, gl) - abs((int) (n - len)) +
1297 // weight length of the left common substring
1298 leftcommonsubstring(word, gl);
1302 if (ph) bubblesort(&rootsphon[0], NULL, &scoresphon[0], MAX_ROOTS);
1308 for (i=0; i < MAX_GUESS; i++) {
1310 if ((ns < oldns + maxngramsugs) && (ns < maxSug) && (!same || (gscore[i] > 1000))) {
1312 // leave only excellent suggestions, if exists
1313 if (gscore[i] > 1000) same = 1; else if (gscore[i] < -100) {
1315 // keep the best ngram suggestions, unless in ONLYMAXDIFF mode
1316 if (ns > oldns || (pAMgr && pAMgr->get_onlymaxdiff())) {
1318 if (guessorig[i]) free(guessorig[i]);
1322 for (j = 0; j < ns; j++) {
1323 // don't suggest previous suggestions or a previous suggestion with prefixes or affixes
1324 if ((!guessorig[i] && strstr(guess[i], wlst[j])) ||
1325 (guessorig[i] && strstr(guessorig[i], wlst[j])) ||
1326 // check forbidden words
1327 !checkword(guess[i], strlen(guess[i]), 0, NULL, NULL)) unique = 0;
1330 wlst[ns++] = guess[i];
1333 wlst[ns-1] = guessorig[i];
1337 if (guessorig[i]) free(guessorig[i]);
1341 if (guessorig[i]) free(guessorig[i]);
1347 if (ph) for (i=0; i < MAX_ROOTS; i++) {
1349 if ((ns < oldns + MAXPHONSUGS) && (ns < maxSug)) {
1351 for (j = 0; j < ns; j++) {
1352 // don't suggest previous suggestions or a previous suggestion with prefixes or affixes
1353 if (strstr(rootsphon[i], wlst[j]) ||
1354 // check forbidden words
1355 !checkword(rootsphon[i], strlen(rootsphon[i]), 0, NULL, NULL)) unique = 0;
1358 wlst[ns++] = mystrdup(rootsphon[i]);
1359 if (!wlst[ns - 1]) return ns - 1;
1365 if (nonbmp) utf8 = 1;
1366 // if (specpron) free(specpron);
1371 // see if a candidate suggestion is spelled correctly
1372 // needs to check both root words and words with affixes
1374 // obsolote MySpell-HU modifications:
1375 // return value 2 and 3 marks compounding with hyphen (-)
1376 // `3' marks roots without suffix
1377 int SuggestMgr::checkword(const char * word, int len, int cpdsuggest, int * timer, clock_t * timelimit)
1379 struct hentry * rv=NULL;
1380 struct hentry * rv2=NULL;
1386 if (!(*timer) && timelimit) {
1387 if ((clock() - *timelimit) > TIMELIMIT) return 0;
1388 *timer = MAXPLUSTIMER;
1393 if (cpdsuggest==1) {
1394 if (pAMgr->get_compound()) {
1395 rv = pAMgr->compound_check(word, len, 0, 0, 100, 0, NULL, 0, 1, 0); //EXT
1396 if (rv && (!(rv2 = pAMgr->lookup(word)) || !rv2->astr ||
1397 !(TESTAFF(rv2->astr,pAMgr->get_forbiddenword(),rv2->alen) ||
1398 TESTAFF(rv2->astr,pAMgr->get_nosuggest(),rv2->alen)))) return 3; // XXX obsolote categorisation + only ICONV needs affix flag check?
1403 rv = pAMgr->lookup(word);
1406 if ((rv->astr) && (TESTAFF(rv->astr,pAMgr->get_forbiddenword(),rv->alen)
1407 || TESTAFF(rv->astr,pAMgr->get_nosuggest(),rv->alen))) return 0;
1409 if (rv->astr && (TESTAFF(rv->astr,pAMgr->get_needaffix(),rv->alen) ||
1410 TESTAFF(rv->astr, ONLYUPCASEFLAG, rv->alen) ||
1411 TESTAFF(rv->astr,pAMgr->get_onlyincompound(),rv->alen))) {
1412 rv = rv->next_homonym;
1415 } else rv = pAMgr->prefix_check(word, len, 0); // only prefix, and prefix + suffix XXX
1420 rv = pAMgr->suffix_check(word, len, 0, NULL, NULL, 0, NULL); // only suffix
1423 if (!rv && pAMgr->have_contclass()) {
1424 rv = pAMgr->suffix_check_twosfx(word, len, 0, NULL, FLAG_NULL);
1425 if (!rv) rv = pAMgr->prefix_check_twosfx(word, len, 1, FLAG_NULL);
1428 // check forbidden words
1429 if ((rv) && (rv->astr) && (TESTAFF(rv->astr,pAMgr->get_forbiddenword(),rv->alen) ||
1430 TESTAFF(rv->astr, ONLYUPCASEFLAG, rv->alen) ||
1431 TESTAFF(rv->astr,pAMgr->get_nosuggest(),rv->alen) ||
1432 TESTAFF(rv->astr,pAMgr->get_onlyincompound(),rv->alen))) return 0;
1434 if (rv) { // XXX obsolote
1435 if ((pAMgr->get_compoundflag()) &&
1436 TESTAFF(rv->astr, pAMgr->get_compoundflag(), rv->alen)) return 2 + nosuffix;
1443 int SuggestMgr::check_forbidden(const char * word, int len)
1445 struct hentry * rv = NULL;
1448 rv = pAMgr->lookup(word);
1449 if (rv && rv->astr && (TESTAFF(rv->astr,pAMgr->get_needaffix(),rv->alen) ||
1450 TESTAFF(rv->astr,pAMgr->get_onlyincompound(),rv->alen))) rv = NULL;
1451 if (!(pAMgr->prefix_check(word,len,1)))
1452 rv = pAMgr->suffix_check(word,len, 0, NULL, NULL, 0, NULL); // prefix+suffix, suffix
1453 // check forbidden words
1454 if ((rv) && (rv->astr) && TESTAFF(rv->astr,pAMgr->get_forbiddenword(),rv->alen)) return 1;
1459 #ifdef HUNSPELL_EXPERIMENTAL
1460 // suggest possible stems
1461 int SuggestMgr::suggest_pos_stems(char*** slst, const char * w, int nsug)
1465 struct hentry * rv = NULL;
1467 char w2[MAXSWUTF8L];
1468 const char * word = w;
1470 // word reversing wrapper for complex prefixes
1471 if (complexprefixes) {
1473 if (utf8) reverseword_utf(w2); else reverseword(w2);
1477 int wl = strlen(word);
1483 wlst = (char **) calloc(maxSug, sizeof(char *));
1484 if (wlst == NULL) return -1;
1487 rv = pAMgr->suffix_check(word, wl, 0, NULL, wlst, maxSug, &nsug);
1489 // delete dash from end of word
1491 for (int j=0; j < nsug; j++) {
1492 if (wlst[j][strlen(wlst[j]) - 1] == '-') wlst[j][strlen(wlst[j]) - 1] = '\0';
1499 #endif // END OF HUNSPELL_EXPERIMENTAL CODE
1502 char * SuggestMgr::suggest_morph(const char * w)
1504 char result[MAXLNLEN];
1505 char * r = (char *) result;
1508 struct hentry * rv = NULL;
1512 if (! pAMgr) return NULL;
1514 char w2[MAXSWUTF8L];
1515 const char * word = w;
1517 // word reversing wrapper for complex prefixes
1518 if (complexprefixes) {
1520 if (utf8) reverseword_utf(w2); else reverseword(w2);
1524 rv = pAMgr->lookup(word);
1527 if ((!rv->astr) || !(TESTAFF(rv->astr, pAMgr->get_forbiddenword(), rv->alen) ||
1528 TESTAFF(rv->astr, pAMgr->get_needaffix(), rv->alen) ||
1529 TESTAFF(rv->astr,pAMgr->get_onlyincompound(),rv->alen))) {
1530 if (!HENTRY_FIND(rv, MORPH_STEM)) {
1531 mystrcat(result, " ", MAXLNLEN);
1532 mystrcat(result, MORPH_STEM, MAXLNLEN);
1533 mystrcat(result, word, MAXLNLEN);
1535 if (HENTRY_DATA(rv)) {
1536 mystrcat(result, " ", MAXLNLEN);
1537 mystrcat(result, HENTRY_DATA2(rv), MAXLNLEN);
1539 mystrcat(result, "\n", MAXLNLEN);
1541 rv = rv->next_homonym;
1544 st = pAMgr->affix_check_morph(word,strlen(word));
1546 mystrcat(result, st, MAXLNLEN);
1550 if (pAMgr->get_compound() && (*result == '\0'))
1551 pAMgr->compound_check_morph(word, strlen(word),
1552 0, 0, 100, 0,NULL, 0, &r, NULL);
1554 return (*result) ? mystrdup(line_uniq(result, MSEP_REC)) : NULL;
1557 #ifdef HUNSPELL_EXPERIMENTAL
1558 char * SuggestMgr::suggest_morph_for_spelling_error(const char * word)
1561 char ** wlst = (char **) calloc(maxSug, sizeof(char *));
1562 if (!**wlst) return NULL;
1563 // we will use only the first suggestion
1564 for (int i = 0; i < maxSug - 1; i++) wlst[i] = "";
1565 int ns = suggest(&wlst, word, maxSug - 1, NULL);
1567 p = suggest_morph(wlst[maxSug - 1]);
1568 free(wlst[maxSug - 1]);
1570 if (wlst) free(wlst);
1573 #endif // END OF HUNSPELL_EXPERIMENTAL CODE
1576 char * SuggestMgr::suggest_hentry_gen(hentry * rv, char * pattern)
1578 char result[MAXLNLEN];
1580 int sfxcount = get_sfxcount(pattern);
1582 if (get_sfxcount(HENTRY_DATA(rv)) > sfxcount) return NULL;
1584 if (HENTRY_DATA(rv)) {
1585 char * aff = pAMgr->morphgen(HENTRY_WORD(rv), rv->blen, rv->astr, rv->alen,
1586 HENTRY_DATA(rv), pattern, 0);
1588 mystrcat(result, aff, MAXLNLEN);
1589 mystrcat(result, "\n", MAXLNLEN);
1594 // check all allomorphs
1595 char allomorph[MAXLNLEN];
1597 if (HENTRY_DATA(rv)) p = (char *) strstr(HENTRY_DATA2(rv), MORPH_ALLOMORPH);
1599 struct hentry * rv2 = NULL;
1601 int plen = fieldlen(p);
1602 strncpy(allomorph, p, plen);
1603 allomorph[plen] = '\0';
1604 rv2 = pAMgr->lookup(allomorph);
1606 // if (HENTRY_DATA(rv2) && get_sfxcount(HENTRY_DATA(rv2)) <= sfxcount) {
1607 if (HENTRY_DATA(rv2)) {
1608 char * st = (char *) strstr(HENTRY_DATA2(rv2), MORPH_STEM);
1609 if (st && (strncmp(st + MORPH_TAG_LEN,
1610 HENTRY_WORD(rv), fieldlen(st + MORPH_TAG_LEN)) == 0)) {
1611 char * aff = pAMgr->morphgen(HENTRY_WORD(rv2), rv2->blen, rv2->astr, rv2->alen,
1612 HENTRY_DATA(rv2), pattern, 0);
1614 mystrcat(result, aff, MAXLNLEN);
1615 mystrcat(result, "\n", MAXLNLEN);
1620 rv2 = rv2->next_homonym;
1622 p = strstr(p + plen, MORPH_ALLOMORPH);
1625 return (*result) ? mystrdup(result) : NULL;
1628 char * SuggestMgr::suggest_gen(char ** desc, int n, char * pattern) {
1629 char result[MAXLNLEN];
1630 char result2[MAXLNLEN];
1631 char newpattern[MAXLNLEN];
1633 if (n == 0) return 0;
1635 struct hentry * rv = NULL;
1636 if (!pAMgr) return NULL;
1638 // search affixed forms with and without derivational suffixes
1641 for (int k = 0; k < n; k++) {
1643 // add compound word parts (except the last one)
1644 char * s = (char *) desc[k];
1645 char * part = strstr(s, MORPH_PART);
1647 char * nextpart = strstr(part + 1, MORPH_PART);
1649 copy_field(result + strlen(result), part, MORPH_PART);
1651 nextpart = strstr(part + 1, MORPH_PART);
1659 char * alt = strstr(tok, " | ");
1662 alt = strstr(alt, " | ");
1664 int pln = line_tok(tok, &pl, MSEP_ALT);
1665 for (int i = 0; i < pln; i++) {
1666 // remove inflectional and terminal suffixes
1667 char * is = strstr(pl[i], MORPH_INFL_SFX);
1669 char * ts = strstr(pl[i], MORPH_TERM_SFX);
1672 ts = strstr(pl[i], MORPH_TERM_SFX);
1674 char * st = strstr(s, MORPH_STEM);
1676 copy_field(tok, st, MORPH_STEM);
1677 rv = pAMgr->lookup(tok);
1679 char newpat[MAXLNLEN];
1680 strcpy(newpat, pl[i]);
1681 strcat(newpat, pattern);
1682 char * sg = suggest_hentry_gen(rv, newpat);
1683 if (!sg) sg = suggest_hentry_gen(rv, pattern);
1686 int genl = line_tok(sg, &gen, MSEP_REC);
1689 for (int j = 0; j < genl; j++) {
1690 if (strstr(pl[i], MORPH_SURF_PFX)) {
1691 int r2l = strlen(result2);
1692 result2[r2l] = MSEP_REC;
1693 strcpy(result2 + r2l + 1, result);
1694 copy_field(result2 + strlen(result2), pl[i], MORPH_SURF_PFX);
1695 mystrcat(result2, gen[j], MAXLNLEN);
1697 sprintf(result2 + strlen(result2), "%c%s%s",
1698 MSEP_REC, result, gen[j]);
1701 freelist(&gen, genl);
1703 rv = rv->next_homonym;
1710 if (*result2 || !strstr(pattern, MORPH_DERI_SFX)) break;
1711 strcpy(newpattern, pattern);
1712 pattern = newpattern;
1713 char * ds = strstr(pattern, MORPH_DERI_SFX);
1715 strncpy(ds, MORPH_TERM_SFX, MORPH_TAG_LEN);
1716 ds = strstr(pattern, MORPH_DERI_SFX);
1719 return (*result2 ? mystrdup(result2) : NULL);
1723 // generate an n-gram score comparing s1 and s2
1724 int SuggestMgr::ngram(int n, char * s1, const char * s2, int opt)
1735 l1 = u8_u16(su1, MAXSWL, s1);
1736 l2 = u8_u16(su2, MAXSWL, s2);
1737 if ((l2 <= 0) || (l1 == -1)) return 0;
1738 // lowering dictionary word
1739 if (opt & NGRAM_LOWERING) mkallsmall_utf(su2, l2, langnum);
1740 for (int j = 1; j <= n; j++) {
1742 for (int i = 0; i <= (l1-j); i++) {
1744 for (int l = 0; l <= (l2-j); l++) {
1745 for (k = 0; k < j; k++) {
1746 w_char * c1 = su1 + i + k;
1747 w_char * c2 = su2 + l + k;
1748 if ((c1->l != c2->l) || (c1->h != c2->h)) break;
1755 if (k != j && opt & NGRAM_WEIGHTED) {
1758 if (i == 0 || i == l1-j) ns--; // side weight
1761 nscore = nscore + ns;
1762 if (ns < 2 && !(opt & NGRAM_WEIGHTED)) break;
1766 if (l2 == 0) return 0;
1768 char *t = mystrdup(s2);
1769 if (opt & NGRAM_LOWERING) mkallsmall(t, csconv);
1770 for (int j = 1; j <= n; j++) {
1772 for (int i = 0; i <= (l1-j); i++) {
1773 char c = *(s1 + i + j);
1774 *(s1 + i + j) = '\0';
1775 if (strstr(t,(s1+i))) {
1777 } else if (opt & NGRAM_WEIGHTED) {
1780 if (i == 0 || i == l1-j) ns--; // side weight
1784 nscore = nscore + ns;
1785 if (ns < 2 && !(opt & NGRAM_WEIGHTED)) break;
1791 if (opt & NGRAM_LONGER_WORSE) ns = (l2-l1)-2;
1792 if (opt & NGRAM_ANY_MISMATCH) ns = abs(l2-l1)-2;
1793 ns = (nscore - ((ns > 0) ? ns : 0));
1797 // length of the left common substring of s1 and (decapitalised) s2
1798 int SuggestMgr::leftcommonsubstring(char * s1, const char * s2) {
1802 su1[0].l = su2[0].l = su1[0].h = su2[0].h = 0;
1803 // decapitalize dictionary word
1804 if (complexprefixes) {
1805 int l1 = u8_u16(su1, MAXSWL, s1);
1806 int l2 = u8_u16(su2, MAXSWL, s2);
1807 if (*((short *)su1+l1-1) == *((short *)su2+l2-1)) return 1;
1812 unsigned short idx = (su2->h << 8) + su2->l;
1813 unsigned short otheridx = (su1->h << 8) + su1->l;
1814 if (otheridx != idx &&
1815 (otheridx != unicodetolower(idx, langnum))) return 0;
1816 int l1 = u8_u16(su1, MAXSWL, s1);
1817 int l2 = u8_u16(su2, MAXSWL, s2);
1818 for(i = 1; (i < l1) && (i < l2) &&
1819 (su1[i].l == su2[i].l) && (su1[i].h == su2[i].h); i++);
1823 if (complexprefixes) {
1824 int l1 = strlen(s1);
1825 int l2 = strlen(s2);
1826 if (*(s2+l1-1) == *(s2+l2-1)) return 1;
1829 // decapitalise dictionary word
1830 if ((*s1 != *s2) && (*s1 != csconv[((unsigned char)*s2)].clower)) return 0;
1833 } while ((*s1 == *s2) && (*s1 != '\0'));
1834 return (int)(s1 - olds);
1840 int SuggestMgr::commoncharacterpositions(char * s1, const char * s2, int * is_swap) {
1848 int l1 = u8_u16(su1, MAXSWL, s1);
1849 int l2 = u8_u16(su2, MAXSWL, s2);
1850 // decapitalize dictionary word
1851 if (complexprefixes) {
1852 mkallsmall_utf(su2+l2-1, 1, langnum);
1854 mkallsmall_utf(su2, 1, langnum);
1856 for (int i = 0; (i < l1) && (i < l2); i++) {
1857 if (((short *) su1)[i] == ((short *) su2)[i]) {
1860 if (diff < 2) diffpos[diff] = i;
1864 if ((diff == 2) && (l1 == l2) &&
1865 (((short *) su1)[diffpos[0]] == ((short *) su2)[diffpos[1]]) &&
1866 (((short *) su1)[diffpos[1]] == ((short *) su2)[diffpos[0]])) *is_swap = 1;
1871 // decapitalize dictionary word
1872 if (complexprefixes) {
1874 *(t+l2-1) = csconv[((unsigned char)*(t+l2-1))].clower;
1876 mkallsmall(t, csconv);
1878 for (i = 0; (*(s1+i) != 0) && (*(t+i) != 0); i++) {
1879 if (*(s1+i) == *(t+i)) {
1882 if (diff < 2) diffpos[diff] = i;
1886 if ((diff == 2) && (*(s1+i) == 0) && (*(t+i) == 0) &&
1887 (*(s1+diffpos[0]) == *(t+diffpos[1])) &&
1888 (*(s1+diffpos[1]) == *(t+diffpos[0]))) *is_swap = 1;
1893 int SuggestMgr::mystrlen(const char * word) {
1896 return u8_u16(w, MAXSWL, word);
1897 } else return strlen(word);
1900 // sort in decreasing order of score
1901 void SuggestMgr::bubblesort(char** rword, char** rword2, int* rsc, int n )
1907 if (rsc[j-1] < rsc[j]) {
1908 int sctmp = rsc[j-1];
1909 char * wdtmp = rword[j-1];
1911 rword[j-1] = rword[j];
1915 wdtmp = rword2[j-1];
1916 rword2[j-1] = rword2[j];
1927 // longest common subsequence
1928 void SuggestMgr::lcs(const char * s, const char * s2, int * l1, int * l2, char ** result) {
1937 m = u8_u16(su, MAXSWL, s);
1938 n = u8_u16(su2, MAXSWL, s2);
1943 c = (char *) malloc((m + 1) * (n + 1));
1944 b = (char *) malloc((m + 1) * (n + 1));
1951 for (i = 1; i <= m; i++) c[i*(n+1)] = 0;
1952 for (j = 0; j <= n; j++) c[j] = 0;
1953 for (i = 1; i <= m; i++) {
1954 for (j = 1; j <= n; j++) {
1955 if ( ((utf8) && (*((short *) su+i-1) == *((short *)su2+j-1)))
1956 || ((!utf8) && ((*(s+i-1)) == (*(s2+j-1))))) {
1957 c[i*(n+1) + j] = c[(i-1)*(n+1) + j-1]+1;
1958 b[i*(n+1) + j] = LCS_UPLEFT;
1959 } else if (c[(i-1)*(n+1) + j] >= c[i*(n+1) + j-1]) {
1960 c[i*(n+1) + j] = c[(i-1)*(n+1) + j];
1961 b[i*(n+1) + j] = LCS_UP;
1963 c[i*(n+1) + j] = c[i*(n+1) + j-1];
1964 b[i*(n+1) + j] = LCS_LEFT;
1974 int SuggestMgr::lcslen(const char * s, const char* s2) {
1981 lcs(s, s2, &m, &n, &result);
1982 if (!result) return 0;
1985 while ((i != 0) && (j != 0)) {
1986 if (result[i*(n+1) + j] == LCS_UPLEFT) {
1990 } else if (result[i*(n+1) + j] == LCS_UP) {