Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /******* BEGIN LICENSE BLOCK ******* |
michael@0 | 2 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
michael@0 | 3 | * |
michael@0 | 4 | * The contents of this file are subject to the Mozilla Public License Version |
michael@0 | 5 | * 1.1 (the "License"); you may not use this file except in compliance with |
michael@0 | 6 | * the License. You may obtain a copy of the License at |
michael@0 | 7 | * http://www.mozilla.org/MPL/ |
michael@0 | 8 | * |
michael@0 | 9 | * Software distributed under the License is distributed on an "AS IS" basis, |
michael@0 | 10 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
michael@0 | 11 | * for the specific language governing rights and limitations under the |
michael@0 | 12 | * License. |
michael@0 | 13 | * |
michael@0 | 14 | * The Initial Developers of the Original Code are Kevin Hendricks (MySpell) |
michael@0 | 15 | * and László Németh (Hunspell). Portions created by the Initial Developers |
michael@0 | 16 | * are Copyright (C) 2002-2005 the Initial Developers. All Rights Reserved. |
michael@0 | 17 | * |
michael@0 | 18 | * Contributor(s): Kevin Hendricks (kevin.hendricks@sympatico.ca) |
michael@0 | 19 | * David Einstein (deinst@world.std.com) |
michael@0 | 20 | * László Németh (nemethl@gyorsposta.hu) |
michael@0 | 21 | * Caolan McNamara (caolanm@redhat.com) |
michael@0 | 22 | * Davide Prina |
michael@0 | 23 | * Giuseppe Modugno |
michael@0 | 24 | * Gianluca Turconi |
michael@0 | 25 | * Simon Brouwer |
michael@0 | 26 | * Noll Janos |
michael@0 | 27 | * Biro Arpad |
michael@0 | 28 | * Goldman Eleonora |
michael@0 | 29 | * Sarlos Tamas |
michael@0 | 30 | * Bencsath Boldizsar |
michael@0 | 31 | * Halacsy Peter |
michael@0 | 32 | * Dvornik Laszlo |
michael@0 | 33 | * Gefferth Andras |
michael@0 | 34 | * Nagy Viktor |
michael@0 | 35 | * Varga Daniel |
michael@0 | 36 | * Chris Halls |
michael@0 | 37 | * Rene Engelhard |
michael@0 | 38 | * Bram Moolenaar |
michael@0 | 39 | * Dafydd Jones |
michael@0 | 40 | * Harri Pitkanen |
michael@0 | 41 | * Andras Timar |
michael@0 | 42 | * Tor Lillqvist |
michael@0 | 43 | * |
michael@0 | 44 | * Alternatively, the contents of this file may be used under the terms of |
michael@0 | 45 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
michael@0 | 46 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
michael@0 | 47 | * in which case the provisions of the GPL or the LGPL are applicable instead |
michael@0 | 48 | * of those above. If you wish to allow use of your version of this file only |
michael@0 | 49 | * under the terms of either the GPL or the LGPL, and not to allow others to |
michael@0 | 50 | * use your version of this file under the terms of the MPL, indicate your |
michael@0 | 51 | * decision by deleting the provisions above and replace them with the notice |
michael@0 | 52 | * and other provisions required by the GPL or the LGPL. If you do not delete |
michael@0 | 53 | * the provisions above, a recipient may use your version of this file under |
michael@0 | 54 | * the terms of any one of the MPL, the GPL or the LGPL. |
michael@0 | 55 | * |
michael@0 | 56 | ******* END LICENSE BLOCK *******/ |
michael@0 | 57 | |
michael@0 | 58 | #include <stdlib.h> |
michael@0 | 59 | #include <string.h> |
michael@0 | 60 | #include <stdio.h> |
michael@0 | 61 | #include <ctype.h> |
michael@0 | 62 | |
michael@0 | 63 | #include "suggestmgr.hxx" |
michael@0 | 64 | #include "htypes.hxx" |
michael@0 | 65 | #include "csutil.hxx" |
michael@0 | 66 | |
michael@0 | 67 | const w_char W_VLINE = { '\0', '|' }; |
michael@0 | 68 | |
michael@0 | 69 | SuggestMgr::SuggestMgr(const char * tryme, int maxn, |
michael@0 | 70 | AffixMgr * aptr) |
michael@0 | 71 | { |
michael@0 | 72 | |
michael@0 | 73 | // register affix manager and check in string of chars to |
michael@0 | 74 | // try when building candidate suggestions |
michael@0 | 75 | pAMgr = aptr; |
michael@0 | 76 | |
michael@0 | 77 | csconv = NULL; |
michael@0 | 78 | |
michael@0 | 79 | ckeyl = 0; |
michael@0 | 80 | ckey = NULL; |
michael@0 | 81 | ckey_utf = NULL; |
michael@0 | 82 | |
michael@0 | 83 | ctryl = 0; |
michael@0 | 84 | ctry = NULL; |
michael@0 | 85 | ctry_utf = NULL; |
michael@0 | 86 | |
michael@0 | 87 | utf8 = 0; |
michael@0 | 88 | langnum = 0; |
michael@0 | 89 | complexprefixes = 0; |
michael@0 | 90 | |
michael@0 | 91 | maxSug = maxn; |
michael@0 | 92 | nosplitsugs = 0; |
michael@0 | 93 | maxngramsugs = MAXNGRAMSUGS; |
michael@0 | 94 | maxcpdsugs = MAXCOMPOUNDSUGS; |
michael@0 | 95 | |
michael@0 | 96 | if (pAMgr) { |
michael@0 | 97 | langnum = pAMgr->get_langnum(); |
michael@0 | 98 | ckey = pAMgr->get_key_string(); |
michael@0 | 99 | nosplitsugs = pAMgr->get_nosplitsugs(); |
michael@0 | 100 | if (pAMgr->get_maxngramsugs() >= 0) |
michael@0 | 101 | maxngramsugs = pAMgr->get_maxngramsugs(); |
michael@0 | 102 | utf8 = pAMgr->get_utf8(); |
michael@0 | 103 | if (pAMgr->get_maxcpdsugs() >= 0) |
michael@0 | 104 | maxcpdsugs = pAMgr->get_maxcpdsugs(); |
michael@0 | 105 | if (!utf8) |
michael@0 | 106 | { |
michael@0 | 107 | char * enc = pAMgr->get_encoding(); |
michael@0 | 108 | csconv = get_current_cs(enc); |
michael@0 | 109 | free(enc); |
michael@0 | 110 | } |
michael@0 | 111 | complexprefixes = pAMgr->get_complexprefixes(); |
michael@0 | 112 | } |
michael@0 | 113 | |
michael@0 | 114 | if (ckey) { |
michael@0 | 115 | if (utf8) { |
michael@0 | 116 | w_char t[MAXSWL]; |
michael@0 | 117 | ckeyl = u8_u16(t, MAXSWL, ckey); |
michael@0 | 118 | ckey_utf = (w_char *) malloc(ckeyl * sizeof(w_char)); |
michael@0 | 119 | if (ckey_utf) memcpy(ckey_utf, t, ckeyl * sizeof(w_char)); |
michael@0 | 120 | else ckeyl = 0; |
michael@0 | 121 | } else { |
michael@0 | 122 | ckeyl = strlen(ckey); |
michael@0 | 123 | } |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | if (tryme) { |
michael@0 | 127 | ctry = mystrdup(tryme); |
michael@0 | 128 | if (ctry) ctryl = strlen(ctry); |
michael@0 | 129 | if (ctry && utf8) { |
michael@0 | 130 | w_char t[MAXSWL]; |
michael@0 | 131 | ctryl = u8_u16(t, MAXSWL, tryme); |
michael@0 | 132 | ctry_utf = (w_char *) malloc(ctryl * sizeof(w_char)); |
michael@0 | 133 | if (ctry_utf) memcpy(ctry_utf, t, ctryl * sizeof(w_char)); |
michael@0 | 134 | else ctryl = 0; |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | } |
michael@0 | 138 | |
michael@0 | 139 | |
michael@0 | 140 | SuggestMgr::~SuggestMgr() |
michael@0 | 141 | { |
michael@0 | 142 | pAMgr = NULL; |
michael@0 | 143 | if (ckey) free(ckey); |
michael@0 | 144 | ckey = NULL; |
michael@0 | 145 | if (ckey_utf) free(ckey_utf); |
michael@0 | 146 | ckey_utf = NULL; |
michael@0 | 147 | ckeyl = 0; |
michael@0 | 148 | if (ctry) free(ctry); |
michael@0 | 149 | ctry = NULL; |
michael@0 | 150 | if (ctry_utf) free(ctry_utf); |
michael@0 | 151 | ctry_utf = NULL; |
michael@0 | 152 | ctryl = 0; |
michael@0 | 153 | maxSug = 0; |
michael@0 | 154 | #ifdef MOZILLA_CLIENT |
michael@0 | 155 | delete [] csconv; |
michael@0 | 156 | #endif |
michael@0 | 157 | } |
michael@0 | 158 | |
michael@0 | 159 | int SuggestMgr::testsug(char** wlst, const char * candidate, int wl, int ns, int cpdsuggest, |
michael@0 | 160 | int * timer, clock_t * timelimit) { |
michael@0 | 161 | int cwrd = 1; |
michael@0 | 162 | if (ns == maxSug) return maxSug; |
michael@0 | 163 | for (int k=0; k < ns; k++) { |
michael@0 | 164 | if (strcmp(candidate,wlst[k]) == 0) cwrd = 0; |
michael@0 | 165 | } |
michael@0 | 166 | if ((cwrd) && checkword(candidate, wl, cpdsuggest, timer, timelimit)) { |
michael@0 | 167 | wlst[ns] = mystrdup(candidate); |
michael@0 | 168 | if (wlst[ns] == NULL) { |
michael@0 | 169 | for (int j=0; j<ns; j++) free(wlst[j]); |
michael@0 | 170 | return -1; |
michael@0 | 171 | } |
michael@0 | 172 | ns++; |
michael@0 | 173 | } |
michael@0 | 174 | return ns; |
michael@0 | 175 | } |
michael@0 | 176 | |
michael@0 | 177 | // generate suggestions for a misspelled word |
michael@0 | 178 | // pass in address of array of char * pointers |
michael@0 | 179 | // onlycompoundsug: probably bad suggestions (need for ngram sugs, too) |
michael@0 | 180 | |
michael@0 | 181 | int SuggestMgr::suggest(char*** slst, const char * w, int nsug, |
michael@0 | 182 | int * onlycompoundsug) |
michael@0 | 183 | { |
michael@0 | 184 | int nocompoundtwowords = 0; |
michael@0 | 185 | char ** wlst; |
michael@0 | 186 | w_char word_utf[MAXSWL]; |
michael@0 | 187 | int wl = 0; |
michael@0 | 188 | int nsugorig = nsug; |
michael@0 | 189 | char w2[MAXWORDUTF8LEN]; |
michael@0 | 190 | const char * word = w; |
michael@0 | 191 | int oldSug = 0; |
michael@0 | 192 | |
michael@0 | 193 | // word reversing wrapper for complex prefixes |
michael@0 | 194 | if (complexprefixes) { |
michael@0 | 195 | strcpy(w2, w); |
michael@0 | 196 | if (utf8) reverseword_utf(w2); else reverseword(w2); |
michael@0 | 197 | word = w2; |
michael@0 | 198 | } |
michael@0 | 199 | |
michael@0 | 200 | if (*slst) { |
michael@0 | 201 | wlst = *slst; |
michael@0 | 202 | } else { |
michael@0 | 203 | wlst = (char **) malloc(maxSug * sizeof(char *)); |
michael@0 | 204 | if (wlst == NULL) return -1; |
michael@0 | 205 | for (int i = 0; i < maxSug; i++) { |
michael@0 | 206 | wlst[i] = NULL; |
michael@0 | 207 | } |
michael@0 | 208 | } |
michael@0 | 209 | |
michael@0 | 210 | if (utf8) { |
michael@0 | 211 | wl = u8_u16(word_utf, MAXSWL, word); |
michael@0 | 212 | if (wl == -1) { |
michael@0 | 213 | *slst = wlst; |
michael@0 | 214 | return nsug; |
michael@0 | 215 | } |
michael@0 | 216 | } |
michael@0 | 217 | |
michael@0 | 218 | for (int cpdsuggest=0; (cpdsuggest<2) && (nocompoundtwowords==0); cpdsuggest++) { |
michael@0 | 219 | |
michael@0 | 220 | // limit compound suggestion |
michael@0 | 221 | if (cpdsuggest > 0) oldSug = nsug; |
michael@0 | 222 | |
michael@0 | 223 | // suggestions for an uppercase word (html -> HTML) |
michael@0 | 224 | if ((nsug < maxSug) && (nsug > -1)) { |
michael@0 | 225 | nsug = (utf8) ? capchars_utf(wlst, word_utf, wl, nsug, cpdsuggest) : |
michael@0 | 226 | capchars(wlst, word, nsug, cpdsuggest); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | // perhaps we made a typical fault of spelling |
michael@0 | 230 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) { |
michael@0 | 231 | nsug = replchars(wlst, word, nsug, cpdsuggest); |
michael@0 | 232 | } |
michael@0 | 233 | |
michael@0 | 234 | // perhaps we made chose the wrong char from a related set |
michael@0 | 235 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) { |
michael@0 | 236 | nsug = mapchars(wlst, word, nsug, cpdsuggest); |
michael@0 | 237 | } |
michael@0 | 238 | |
michael@0 | 239 | // only suggest compound words when no other suggestion |
michael@0 | 240 | if ((cpdsuggest == 0) && (nsug > nsugorig)) nocompoundtwowords=1; |
michael@0 | 241 | |
michael@0 | 242 | // did we swap the order of chars by mistake |
michael@0 | 243 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) { |
michael@0 | 244 | nsug = (utf8) ? swapchar_utf(wlst, word_utf, wl, nsug, cpdsuggest) : |
michael@0 | 245 | swapchar(wlst, word, nsug, cpdsuggest); |
michael@0 | 246 | } |
michael@0 | 247 | |
michael@0 | 248 | // did we swap the order of non adjacent chars by mistake |
michael@0 | 249 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) { |
michael@0 | 250 | nsug = (utf8) ? longswapchar_utf(wlst, word_utf, wl, nsug, cpdsuggest) : |
michael@0 | 251 | longswapchar(wlst, word, nsug, cpdsuggest); |
michael@0 | 252 | } |
michael@0 | 253 | |
michael@0 | 254 | // did we just hit the wrong key in place of a good char (case and keyboard) |
michael@0 | 255 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) { |
michael@0 | 256 | nsug = (utf8) ? badcharkey_utf(wlst, word_utf, wl, nsug, cpdsuggest) : |
michael@0 | 257 | badcharkey(wlst, word, nsug, cpdsuggest); |
michael@0 | 258 | } |
michael@0 | 259 | |
michael@0 | 260 | // did we add a char that should not be there |
michael@0 | 261 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) { |
michael@0 | 262 | nsug = (utf8) ? extrachar_utf(wlst, word_utf, wl, nsug, cpdsuggest) : |
michael@0 | 263 | extrachar(wlst, word, nsug, cpdsuggest); |
michael@0 | 264 | } |
michael@0 | 265 | |
michael@0 | 266 | |
michael@0 | 267 | // did we forgot a char |
michael@0 | 268 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) { |
michael@0 | 269 | nsug = (utf8) ? forgotchar_utf(wlst, word_utf, wl, nsug, cpdsuggest) : |
michael@0 | 270 | forgotchar(wlst, word, nsug, cpdsuggest); |
michael@0 | 271 | } |
michael@0 | 272 | |
michael@0 | 273 | // did we move a char |
michael@0 | 274 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) { |
michael@0 | 275 | nsug = (utf8) ? movechar_utf(wlst, word_utf, wl, nsug, cpdsuggest) : |
michael@0 | 276 | movechar(wlst, word, nsug, cpdsuggest); |
michael@0 | 277 | } |
michael@0 | 278 | |
michael@0 | 279 | // did we just hit the wrong key in place of a good char |
michael@0 | 280 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) { |
michael@0 | 281 | nsug = (utf8) ? badchar_utf(wlst, word_utf, wl, nsug, cpdsuggest) : |
michael@0 | 282 | badchar(wlst, word, nsug, cpdsuggest); |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | // did we double two characters |
michael@0 | 286 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) { |
michael@0 | 287 | nsug = (utf8) ? doubletwochars_utf(wlst, word_utf, wl, nsug, cpdsuggest) : |
michael@0 | 288 | doubletwochars(wlst, word, nsug, cpdsuggest); |
michael@0 | 289 | } |
michael@0 | 290 | |
michael@0 | 291 | // perhaps we forgot to hit space and two words ran together |
michael@0 | 292 | if (!nosplitsugs && (nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) { |
michael@0 | 293 | nsug = twowords(wlst, word, nsug, cpdsuggest); |
michael@0 | 294 | } |
michael@0 | 295 | |
michael@0 | 296 | } // repeating ``for'' statement compounding support |
michael@0 | 297 | |
michael@0 | 298 | if (nsug < 0) { |
michael@0 | 299 | // we ran out of memory - we should free up as much as possible |
michael@0 | 300 | for (int i = 0; i < maxSug; i++) |
michael@0 | 301 | if (wlst[i] != NULL) free(wlst[i]); |
michael@0 | 302 | free(wlst); |
michael@0 | 303 | wlst = NULL; |
michael@0 | 304 | } |
michael@0 | 305 | |
michael@0 | 306 | if (!nocompoundtwowords && (nsug > 0) && onlycompoundsug) *onlycompoundsug = 1; |
michael@0 | 307 | |
michael@0 | 308 | *slst = wlst; |
michael@0 | 309 | return nsug; |
michael@0 | 310 | } |
michael@0 | 311 | |
michael@0 | 312 | // generate suggestions for a word with typical mistake |
michael@0 | 313 | // pass in address of array of char * pointers |
michael@0 | 314 | #ifdef HUNSPELL_EXPERIMENTAL |
michael@0 | 315 | int SuggestMgr::suggest_auto(char*** slst, const char * w, int nsug) |
michael@0 | 316 | { |
michael@0 | 317 | int nocompoundtwowords = 0; |
michael@0 | 318 | char ** wlst; |
michael@0 | 319 | int oldSug; |
michael@0 | 320 | |
michael@0 | 321 | char w2[MAXWORDUTF8LEN]; |
michael@0 | 322 | const char * word = w; |
michael@0 | 323 | |
michael@0 | 324 | // word reversing wrapper for complex prefixes |
michael@0 | 325 | if (complexprefixes) { |
michael@0 | 326 | strcpy(w2, w); |
michael@0 | 327 | if (utf8) reverseword_utf(w2); else reverseword(w2); |
michael@0 | 328 | word = w2; |
michael@0 | 329 | } |
michael@0 | 330 | |
michael@0 | 331 | if (*slst) { |
michael@0 | 332 | wlst = *slst; |
michael@0 | 333 | } else { |
michael@0 | 334 | wlst = (char **) malloc(maxSug * sizeof(char *)); |
michael@0 | 335 | if (wlst == NULL) return -1; |
michael@0 | 336 | } |
michael@0 | 337 | |
michael@0 | 338 | for (int cpdsuggest=0; (cpdsuggest<2) && (nocompoundtwowords==0); cpdsuggest++) { |
michael@0 | 339 | |
michael@0 | 340 | // limit compound suggestion |
michael@0 | 341 | if (cpdsuggest > 0) oldSug = nsug; |
michael@0 | 342 | |
michael@0 | 343 | // perhaps we made a typical fault of spelling |
michael@0 | 344 | if ((nsug < maxSug) && (nsug > -1)) |
michael@0 | 345 | nsug = replchars(wlst, word, nsug, cpdsuggest); |
michael@0 | 346 | |
michael@0 | 347 | // perhaps we made chose the wrong char from a related set |
michael@0 | 348 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs))) |
michael@0 | 349 | nsug = mapchars(wlst, word, nsug, cpdsuggest); |
michael@0 | 350 | |
michael@0 | 351 | if ((cpdsuggest==0) && (nsug>0)) nocompoundtwowords=1; |
michael@0 | 352 | |
michael@0 | 353 | // perhaps we forgot to hit space and two words ran together |
michael@0 | 354 | |
michael@0 | 355 | if ((nsug < maxSug) && (nsug > -1) && (!cpdsuggest || (nsug < oldSug + maxcpdsugs)) && check_forbidden(word, strlen(word))) { |
michael@0 | 356 | nsug = twowords(wlst, word, nsug, cpdsuggest); |
michael@0 | 357 | } |
michael@0 | 358 | |
michael@0 | 359 | } // repeating ``for'' statement compounding support |
michael@0 | 360 | |
michael@0 | 361 | if (nsug < 0) { |
michael@0 | 362 | for (int i=0;i<maxSug; i++) |
michael@0 | 363 | if (wlst[i] != NULL) free(wlst[i]); |
michael@0 | 364 | free(wlst); |
michael@0 | 365 | return -1; |
michael@0 | 366 | } |
michael@0 | 367 | |
michael@0 | 368 | *slst = wlst; |
michael@0 | 369 | return nsug; |
michael@0 | 370 | } |
michael@0 | 371 | #endif // END OF HUNSPELL_EXPERIMENTAL CODE |
michael@0 | 372 | |
michael@0 | 373 | // suggestions for an uppercase word (html -> HTML) |
michael@0 | 374 | int SuggestMgr::capchars_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest) |
michael@0 | 375 | { |
michael@0 | 376 | char candidate[MAXSWUTF8L]; |
michael@0 | 377 | w_char candidate_utf[MAXSWL]; |
michael@0 | 378 | memcpy(candidate_utf, word, wl * sizeof(w_char)); |
michael@0 | 379 | mkallcap_utf(candidate_utf, wl, langnum); |
michael@0 | 380 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl); |
michael@0 | 381 | return testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL); |
michael@0 | 382 | } |
michael@0 | 383 | |
michael@0 | 384 | // suggestions for an uppercase word (html -> HTML) |
michael@0 | 385 | int SuggestMgr::capchars(char** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 386 | { |
michael@0 | 387 | char candidate[MAXSWUTF8L]; |
michael@0 | 388 | strcpy(candidate, word); |
michael@0 | 389 | mkallcap(candidate, csconv); |
michael@0 | 390 | return testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL); |
michael@0 | 391 | } |
michael@0 | 392 | |
michael@0 | 393 | // suggestions for when chose the wrong char out of a related set |
michael@0 | 394 | int SuggestMgr::mapchars(char** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 395 | { |
michael@0 | 396 | char candidate[MAXSWUTF8L]; |
michael@0 | 397 | clock_t timelimit; |
michael@0 | 398 | int timer; |
michael@0 | 399 | candidate[0] = '\0'; |
michael@0 | 400 | |
michael@0 | 401 | int wl = strlen(word); |
michael@0 | 402 | if (wl < 2 || ! pAMgr) return ns; |
michael@0 | 403 | |
michael@0 | 404 | int nummap = pAMgr->get_nummap(); |
michael@0 | 405 | struct mapentry* maptable = pAMgr->get_maptable(); |
michael@0 | 406 | if (maptable==NULL) return ns; |
michael@0 | 407 | |
michael@0 | 408 | timelimit = clock(); |
michael@0 | 409 | timer = MINTIMER; |
michael@0 | 410 | return map_related(word, (char *) &candidate, 0, 0, wlst, cpdsuggest, ns, maptable, nummap, &timer, &timelimit); |
michael@0 | 411 | } |
michael@0 | 412 | |
michael@0 | 413 | int SuggestMgr::map_related(const char * word, char * candidate, int wn, int cn, |
michael@0 | 414 | char** wlst, int cpdsuggest, int ns, |
michael@0 | 415 | const mapentry* maptable, int nummap, int * timer, clock_t * timelimit) |
michael@0 | 416 | { |
michael@0 | 417 | if (*(word + wn) == '\0') { |
michael@0 | 418 | int cwrd = 1; |
michael@0 | 419 | *(candidate + cn) = '\0'; |
michael@0 | 420 | int wl = strlen(candidate); |
michael@0 | 421 | for (int m=0; m < ns; m++) |
michael@0 | 422 | if (strcmp(candidate, wlst[m]) == 0) cwrd = 0; |
michael@0 | 423 | if ((cwrd) && checkword(candidate, wl, cpdsuggest, timer, timelimit)) { |
michael@0 | 424 | if (ns < maxSug) { |
michael@0 | 425 | wlst[ns] = mystrdup(candidate); |
michael@0 | 426 | if (wlst[ns] == NULL) return -1; |
michael@0 | 427 | ns++; |
michael@0 | 428 | } |
michael@0 | 429 | } |
michael@0 | 430 | return ns; |
michael@0 | 431 | } |
michael@0 | 432 | int in_map = 0; |
michael@0 | 433 | for (int j = 0; j < nummap; j++) { |
michael@0 | 434 | for (int k = 0; k < maptable[j].len; k++) { |
michael@0 | 435 | int len = strlen(maptable[j].set[k]); |
michael@0 | 436 | if (strncmp(maptable[j].set[k], word + wn, len) == 0) { |
michael@0 | 437 | in_map = 1; |
michael@0 | 438 | for (int l = 0; l < maptable[j].len; l++) { |
michael@0 | 439 | strcpy(candidate + cn, maptable[j].set[l]); |
michael@0 | 440 | ns = map_related(word, candidate, wn + len, strlen(candidate), wlst, |
michael@0 | 441 | cpdsuggest, ns, maptable, nummap, timer, timelimit); |
michael@0 | 442 | if (!(*timer)) return ns; |
michael@0 | 443 | } |
michael@0 | 444 | } |
michael@0 | 445 | } |
michael@0 | 446 | } |
michael@0 | 447 | if (!in_map) { |
michael@0 | 448 | *(candidate + cn) = *(word + wn); |
michael@0 | 449 | ns = map_related(word, candidate, wn + 1, cn + 1, wlst, cpdsuggest, |
michael@0 | 450 | ns, maptable, nummap, timer, timelimit); |
michael@0 | 451 | } |
michael@0 | 452 | return ns; |
michael@0 | 453 | } |
michael@0 | 454 | |
michael@0 | 455 | // suggestions for a typical fault of spelling, that |
michael@0 | 456 | // differs with more, than 1 letter from the right form. |
michael@0 | 457 | int SuggestMgr::replchars(char** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 458 | { |
michael@0 | 459 | char candidate[MAXSWUTF8L]; |
michael@0 | 460 | const char * r; |
michael@0 | 461 | int lenr, lenp; |
michael@0 | 462 | int wl = strlen(word); |
michael@0 | 463 | if (wl < 2 || ! pAMgr) return ns; |
michael@0 | 464 | int numrep = pAMgr->get_numrep(); |
michael@0 | 465 | struct replentry* reptable = pAMgr->get_reptable(); |
michael@0 | 466 | if (reptable==NULL) return ns; |
michael@0 | 467 | for (int i=0; i < numrep; i++ ) { |
michael@0 | 468 | r = word; |
michael@0 | 469 | lenr = strlen(reptable[i].pattern2); |
michael@0 | 470 | lenp = strlen(reptable[i].pattern); |
michael@0 | 471 | // search every occurence of the pattern in the word |
michael@0 | 472 | while ((r=strstr(r, reptable[i].pattern)) != NULL && (!reptable[i].end || strlen(r) == strlen(reptable[i].pattern)) && |
michael@0 | 473 | (!reptable[i].start || r == word)) { |
michael@0 | 474 | strcpy(candidate, word); |
michael@0 | 475 | if (r-word + lenr + strlen(r+lenp) >= MAXSWUTF8L) break; |
michael@0 | 476 | strcpy(candidate+(r-word),reptable[i].pattern2); |
michael@0 | 477 | strcpy(candidate+(r-word)+lenr, r+lenp); |
michael@0 | 478 | ns = testsug(wlst, candidate, wl-lenp+lenr, ns, cpdsuggest, NULL, NULL); |
michael@0 | 479 | if (ns == -1) return -1; |
michael@0 | 480 | // check REP suggestions with space |
michael@0 | 481 | char * sp = strchr(candidate, ' '); |
michael@0 | 482 | if (sp) { |
michael@0 | 483 | char * prev = candidate; |
michael@0 | 484 | while (sp) { |
michael@0 | 485 | *sp = '\0'; |
michael@0 | 486 | if (checkword(prev, strlen(prev), 0, NULL, NULL)) { |
michael@0 | 487 | int oldns = ns; |
michael@0 | 488 | *sp = ' '; |
michael@0 | 489 | ns = testsug(wlst, sp + 1, strlen(sp + 1), ns, cpdsuggest, NULL, NULL); |
michael@0 | 490 | if (ns == -1) return -1; |
michael@0 | 491 | if (oldns < ns) { |
michael@0 | 492 | free(wlst[ns - 1]); |
michael@0 | 493 | wlst[ns - 1] = mystrdup(candidate); |
michael@0 | 494 | if (!wlst[ns - 1]) return -1; |
michael@0 | 495 | } |
michael@0 | 496 | } |
michael@0 | 497 | *sp = ' '; |
michael@0 | 498 | prev = sp + 1; |
michael@0 | 499 | sp = strchr(prev, ' '); |
michael@0 | 500 | } |
michael@0 | 501 | } |
michael@0 | 502 | r++; // search for the next letter |
michael@0 | 503 | } |
michael@0 | 504 | } |
michael@0 | 505 | return ns; |
michael@0 | 506 | } |
michael@0 | 507 | |
michael@0 | 508 | // perhaps we doubled two characters (pattern aba -> ababa, for example vacation -> vacacation) |
michael@0 | 509 | int SuggestMgr::doubletwochars(char** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 510 | { |
michael@0 | 511 | char candidate[MAXSWUTF8L]; |
michael@0 | 512 | int state=0; |
michael@0 | 513 | int wl = strlen(word); |
michael@0 | 514 | if (wl < 5 || ! pAMgr) return ns; |
michael@0 | 515 | for (int i=2; i < wl; i++ ) { |
michael@0 | 516 | if (word[i]==word[i-2]) { |
michael@0 | 517 | state++; |
michael@0 | 518 | if (state==3) { |
michael@0 | 519 | strcpy(candidate,word); |
michael@0 | 520 | strcpy(candidate+i-1,word+i+1); |
michael@0 | 521 | ns = testsug(wlst, candidate, wl-2, ns, cpdsuggest, NULL, NULL); |
michael@0 | 522 | if (ns == -1) return -1; |
michael@0 | 523 | state=0; |
michael@0 | 524 | } |
michael@0 | 525 | } else { |
michael@0 | 526 | state=0; |
michael@0 | 527 | } |
michael@0 | 528 | } |
michael@0 | 529 | return ns; |
michael@0 | 530 | } |
michael@0 | 531 | |
michael@0 | 532 | // perhaps we doubled two characters (pattern aba -> ababa, for example vacation -> vacacation) |
michael@0 | 533 | int SuggestMgr::doubletwochars_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest) |
michael@0 | 534 | { |
michael@0 | 535 | w_char candidate_utf[MAXSWL]; |
michael@0 | 536 | char candidate[MAXSWUTF8L]; |
michael@0 | 537 | int state=0; |
michael@0 | 538 | if (wl < 5 || ! pAMgr) return ns; |
michael@0 | 539 | for (int i=2; i < wl; i++) { |
michael@0 | 540 | if (w_char_eq(word[i], word[i-2])) { |
michael@0 | 541 | state++; |
michael@0 | 542 | if (state==3) { |
michael@0 | 543 | memcpy(candidate_utf, word, (i - 1) * sizeof(w_char)); |
michael@0 | 544 | memcpy(candidate_utf+i-1, word+i+1, (wl-i-1) * sizeof(w_char)); |
michael@0 | 545 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl-2); |
michael@0 | 546 | ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL); |
michael@0 | 547 | if (ns == -1) return -1; |
michael@0 | 548 | state=0; |
michael@0 | 549 | } |
michael@0 | 550 | } else { |
michael@0 | 551 | state=0; |
michael@0 | 552 | } |
michael@0 | 553 | } |
michael@0 | 554 | return ns; |
michael@0 | 555 | } |
michael@0 | 556 | |
michael@0 | 557 | // error is wrong char in place of correct one (case and keyboard related version) |
michael@0 | 558 | int SuggestMgr::badcharkey(char ** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 559 | { |
michael@0 | 560 | char tmpc; |
michael@0 | 561 | char candidate[MAXSWUTF8L]; |
michael@0 | 562 | int wl = strlen(word); |
michael@0 | 563 | strcpy(candidate, word); |
michael@0 | 564 | // swap out each char one by one and try uppercase and neighbor |
michael@0 | 565 | // keyboard chars in its place to see if that makes a good word |
michael@0 | 566 | |
michael@0 | 567 | for (int i=0; i < wl; i++) { |
michael@0 | 568 | tmpc = candidate[i]; |
michael@0 | 569 | // check with uppercase letters |
michael@0 | 570 | candidate[i] = csconv[((unsigned char)tmpc)].cupper; |
michael@0 | 571 | if (tmpc != candidate[i]) { |
michael@0 | 572 | ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL); |
michael@0 | 573 | if (ns == -1) return -1; |
michael@0 | 574 | candidate[i] = tmpc; |
michael@0 | 575 | } |
michael@0 | 576 | // check neighbor characters in keyboard string |
michael@0 | 577 | if (!ckey) continue; |
michael@0 | 578 | char * loc = strchr(ckey, tmpc); |
michael@0 | 579 | while (loc) { |
michael@0 | 580 | if ((loc > ckey) && (*(loc - 1) != '|')) { |
michael@0 | 581 | candidate[i] = *(loc - 1); |
michael@0 | 582 | ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL); |
michael@0 | 583 | if (ns == -1) return -1; |
michael@0 | 584 | } |
michael@0 | 585 | if ((*(loc + 1) != '|') && (*(loc + 1) != '\0')) { |
michael@0 | 586 | candidate[i] = *(loc + 1); |
michael@0 | 587 | ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL); |
michael@0 | 588 | if (ns == -1) return -1; |
michael@0 | 589 | } |
michael@0 | 590 | loc = strchr(loc + 1, tmpc); |
michael@0 | 591 | } |
michael@0 | 592 | candidate[i] = tmpc; |
michael@0 | 593 | } |
michael@0 | 594 | return ns; |
michael@0 | 595 | } |
michael@0 | 596 | |
michael@0 | 597 | // error is wrong char in place of correct one (case and keyboard related version) |
michael@0 | 598 | int SuggestMgr::badcharkey_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest) |
michael@0 | 599 | { |
michael@0 | 600 | w_char tmpc; |
michael@0 | 601 | w_char candidate_utf[MAXSWL]; |
michael@0 | 602 | char candidate[MAXSWUTF8L]; |
michael@0 | 603 | memcpy(candidate_utf, word, wl * sizeof(w_char)); |
michael@0 | 604 | // swap out each char one by one and try all the tryme |
michael@0 | 605 | // chars in its place to see if that makes a good word |
michael@0 | 606 | for (int i=0; i < wl; i++) { |
michael@0 | 607 | tmpc = candidate_utf[i]; |
michael@0 | 608 | // check with uppercase letters |
michael@0 | 609 | mkallcap_utf(candidate_utf + i, 1, langnum); |
michael@0 | 610 | if (!w_char_eq(tmpc, candidate_utf[i])) { |
michael@0 | 611 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl); |
michael@0 | 612 | ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL); |
michael@0 | 613 | if (ns == -1) return -1; |
michael@0 | 614 | candidate_utf[i] = tmpc; |
michael@0 | 615 | } |
michael@0 | 616 | // check neighbor characters in keyboard string |
michael@0 | 617 | if (!ckey) continue; |
michael@0 | 618 | w_char * loc = ckey_utf; |
michael@0 | 619 | while ((loc < (ckey_utf + ckeyl)) && !w_char_eq(*loc, tmpc)) loc++; |
michael@0 | 620 | while (loc < (ckey_utf + ckeyl)) { |
michael@0 | 621 | if ((loc > ckey_utf) && !w_char_eq(*(loc - 1), W_VLINE)) { |
michael@0 | 622 | candidate_utf[i] = *(loc - 1); |
michael@0 | 623 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl); |
michael@0 | 624 | ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL); |
michael@0 | 625 | if (ns == -1) return -1; |
michael@0 | 626 | } |
michael@0 | 627 | if (((loc + 1) < (ckey_utf + ckeyl)) && !w_char_eq(*(loc + 1), W_VLINE)) { |
michael@0 | 628 | candidate_utf[i] = *(loc + 1); |
michael@0 | 629 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl); |
michael@0 | 630 | ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL); |
michael@0 | 631 | if (ns == -1) return -1; |
michael@0 | 632 | } |
michael@0 | 633 | do { loc++; } while ((loc < (ckey_utf + ckeyl)) && !w_char_eq(*loc, tmpc)); |
michael@0 | 634 | } |
michael@0 | 635 | candidate_utf[i] = tmpc; |
michael@0 | 636 | } |
michael@0 | 637 | return ns; |
michael@0 | 638 | } |
michael@0 | 639 | |
michael@0 | 640 | // error is wrong char in place of correct one |
michael@0 | 641 | int SuggestMgr::badchar(char ** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 642 | { |
michael@0 | 643 | char tmpc; |
michael@0 | 644 | char candidate[MAXSWUTF8L]; |
michael@0 | 645 | clock_t timelimit = clock(); |
michael@0 | 646 | int timer = MINTIMER; |
michael@0 | 647 | int wl = strlen(word); |
michael@0 | 648 | strcpy(candidate, word); |
michael@0 | 649 | // swap out each char one by one and try all the tryme |
michael@0 | 650 | // chars in its place to see if that makes a good word |
michael@0 | 651 | for (int j=0; j < ctryl; j++) { |
michael@0 | 652 | for (int i=wl-1; i >= 0; i--) { |
michael@0 | 653 | tmpc = candidate[i]; |
michael@0 | 654 | if (ctry[j] == tmpc) continue; |
michael@0 | 655 | candidate[i] = ctry[j]; |
michael@0 | 656 | ns = testsug(wlst, candidate, wl, ns, cpdsuggest, &timer, &timelimit); |
michael@0 | 657 | if (ns == -1) return -1; |
michael@0 | 658 | if (!timer) return ns; |
michael@0 | 659 | candidate[i] = tmpc; |
michael@0 | 660 | } |
michael@0 | 661 | } |
michael@0 | 662 | return ns; |
michael@0 | 663 | } |
michael@0 | 664 | |
michael@0 | 665 | // error is wrong char in place of correct one |
michael@0 | 666 | int SuggestMgr::badchar_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest) |
michael@0 | 667 | { |
michael@0 | 668 | w_char tmpc; |
michael@0 | 669 | w_char candidate_utf[MAXSWL]; |
michael@0 | 670 | char candidate[MAXSWUTF8L]; |
michael@0 | 671 | clock_t timelimit = clock(); |
michael@0 | 672 | int timer = MINTIMER; |
michael@0 | 673 | memcpy(candidate_utf, word, wl * sizeof(w_char)); |
michael@0 | 674 | // swap out each char one by one and try all the tryme |
michael@0 | 675 | // chars in its place to see if that makes a good word |
michael@0 | 676 | for (int j=0; j < ctryl; j++) { |
michael@0 | 677 | for (int i=wl-1; i >= 0; i--) { |
michael@0 | 678 | tmpc = candidate_utf[i]; |
michael@0 | 679 | if (w_char_eq(tmpc, ctry_utf[j])) continue; |
michael@0 | 680 | candidate_utf[i] = ctry_utf[j]; |
michael@0 | 681 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl); |
michael@0 | 682 | ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, &timer, &timelimit); |
michael@0 | 683 | if (ns == -1) return -1; |
michael@0 | 684 | if (!timer) return ns; |
michael@0 | 685 | candidate_utf[i] = tmpc; |
michael@0 | 686 | } |
michael@0 | 687 | } |
michael@0 | 688 | return ns; |
michael@0 | 689 | } |
michael@0 | 690 | |
michael@0 | 691 | // error is word has an extra letter it does not need |
michael@0 | 692 | int SuggestMgr::extrachar_utf(char** wlst, const w_char * word, int wl, int ns, int cpdsuggest) |
michael@0 | 693 | { |
michael@0 | 694 | char candidate[MAXSWUTF8L]; |
michael@0 | 695 | w_char candidate_utf[MAXSWL]; |
michael@0 | 696 | w_char * p; |
michael@0 | 697 | w_char tmpc = W_VLINE; // not used value, only for VCC warning message |
michael@0 | 698 | if (wl < 2) return ns; |
michael@0 | 699 | // try omitting one char of word at a time |
michael@0 | 700 | memcpy(candidate_utf, word, wl * sizeof(w_char)); |
michael@0 | 701 | for (p = candidate_utf + wl - 1; p >= candidate_utf; p--) { |
michael@0 | 702 | w_char tmpc2 = *p; |
michael@0 | 703 | if (p < candidate_utf + wl - 1) *p = tmpc; |
michael@0 | 704 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl - 1); |
michael@0 | 705 | ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL); |
michael@0 | 706 | if (ns == -1) return -1; |
michael@0 | 707 | tmpc = tmpc2; |
michael@0 | 708 | } |
michael@0 | 709 | return ns; |
michael@0 | 710 | } |
michael@0 | 711 | |
michael@0 | 712 | // error is word has an extra letter it does not need |
michael@0 | 713 | int SuggestMgr::extrachar(char** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 714 | { |
michael@0 | 715 | char tmpc = '\0'; |
michael@0 | 716 | char candidate[MAXSWUTF8L]; |
michael@0 | 717 | char * p; |
michael@0 | 718 | int wl = strlen(word); |
michael@0 | 719 | if (wl < 2) return ns; |
michael@0 | 720 | // try omitting one char of word at a time |
michael@0 | 721 | strcpy (candidate, word); |
michael@0 | 722 | for (p = candidate + wl - 1; p >=candidate; p--) { |
michael@0 | 723 | char tmpc2 = *p; |
michael@0 | 724 | *p = tmpc; |
michael@0 | 725 | ns = testsug(wlst, candidate, wl-1, ns, cpdsuggest, NULL, NULL); |
michael@0 | 726 | if (ns == -1) return -1; |
michael@0 | 727 | tmpc = tmpc2; |
michael@0 | 728 | } |
michael@0 | 729 | return ns; |
michael@0 | 730 | } |
michael@0 | 731 | |
michael@0 | 732 | // error is missing a letter it needs |
michael@0 | 733 | int SuggestMgr::forgotchar(char ** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 734 | { |
michael@0 | 735 | char candidate[MAXSWUTF8L]; |
michael@0 | 736 | char * p; |
michael@0 | 737 | clock_t timelimit = clock(); |
michael@0 | 738 | int timer = MINTIMER; |
michael@0 | 739 | int wl = strlen(word); |
michael@0 | 740 | // try inserting a tryme character before every letter (and the null terminator) |
michael@0 | 741 | for (int i = 0; i < ctryl; i++) { |
michael@0 | 742 | strcpy(candidate, word); |
michael@0 | 743 | for (p = candidate + wl; p >= candidate; p--) { |
michael@0 | 744 | *(p+1) = *p; |
michael@0 | 745 | *p = ctry[i]; |
michael@0 | 746 | ns = testsug(wlst, candidate, wl+1, ns, cpdsuggest, &timer, &timelimit); |
michael@0 | 747 | if (ns == -1) return -1; |
michael@0 | 748 | if (!timer) return ns; |
michael@0 | 749 | } |
michael@0 | 750 | } |
michael@0 | 751 | return ns; |
michael@0 | 752 | } |
michael@0 | 753 | |
michael@0 | 754 | // error is missing a letter it needs |
michael@0 | 755 | int SuggestMgr::forgotchar_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest) |
michael@0 | 756 | { |
michael@0 | 757 | w_char candidate_utf[MAXSWL]; |
michael@0 | 758 | char candidate[MAXSWUTF8L]; |
michael@0 | 759 | w_char * p; |
michael@0 | 760 | clock_t timelimit = clock(); |
michael@0 | 761 | int timer = MINTIMER; |
michael@0 | 762 | // try inserting a tryme character at the end of the word and before every letter |
michael@0 | 763 | for (int i = 0; i < ctryl; i++) { |
michael@0 | 764 | memcpy (candidate_utf, word, wl * sizeof(w_char)); |
michael@0 | 765 | for (p = candidate_utf + wl; p >= candidate_utf; p--) { |
michael@0 | 766 | *(p + 1) = *p; |
michael@0 | 767 | *p = ctry_utf[i]; |
michael@0 | 768 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl + 1); |
michael@0 | 769 | ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, &timer, &timelimit); |
michael@0 | 770 | if (ns == -1) return -1; |
michael@0 | 771 | if (!timer) return ns; |
michael@0 | 772 | } |
michael@0 | 773 | } |
michael@0 | 774 | return ns; |
michael@0 | 775 | } |
michael@0 | 776 | |
michael@0 | 777 | |
michael@0 | 778 | /* error is should have been two words */ |
michael@0 | 779 | int SuggestMgr::twowords(char ** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 780 | { |
michael@0 | 781 | char candidate[MAXSWUTF8L]; |
michael@0 | 782 | char * p; |
michael@0 | 783 | int c1, c2; |
michael@0 | 784 | int forbidden = 0; |
michael@0 | 785 | int cwrd; |
michael@0 | 786 | |
michael@0 | 787 | int wl=strlen(word); |
michael@0 | 788 | if (wl < 3) return ns; |
michael@0 | 789 | |
michael@0 | 790 | if (langnum == LANG_hu) forbidden = check_forbidden(word, wl); |
michael@0 | 791 | |
michael@0 | 792 | strcpy(candidate + 1, word); |
michael@0 | 793 | // split the string into two pieces after every char |
michael@0 | 794 | // if both pieces are good words make them a suggestion |
michael@0 | 795 | for (p = candidate + 1; p[1] != '\0'; p++) { |
michael@0 | 796 | p[-1] = *p; |
michael@0 | 797 | // go to end of the UTF-8 character |
michael@0 | 798 | while (utf8 && ((p[1] & 0xc0) == 0x80)) { |
michael@0 | 799 | *p = p[1]; |
michael@0 | 800 | p++; |
michael@0 | 801 | } |
michael@0 | 802 | if (utf8 && p[1] == '\0') break; // last UTF-8 character |
michael@0 | 803 | *p = '\0'; |
michael@0 | 804 | c1 = checkword(candidate,strlen(candidate), cpdsuggest, NULL, NULL); |
michael@0 | 805 | if (c1) { |
michael@0 | 806 | c2 = checkword((p+1),strlen(p+1), cpdsuggest, NULL, NULL); |
michael@0 | 807 | if (c2) { |
michael@0 | 808 | *p = ' '; |
michael@0 | 809 | |
michael@0 | 810 | // spec. Hungarian code (need a better compound word support) |
michael@0 | 811 | if ((langnum == LANG_hu) && !forbidden && |
michael@0 | 812 | // if 3 repeating letter, use - instead of space |
michael@0 | 813 | (((p[-1] == p[1]) && (((p>candidate+1) && (p[-1] == p[-2])) || (p[-1] == p[2]))) || |
michael@0 | 814 | // or multiple compounding, with more, than 6 syllables |
michael@0 | 815 | ((c1 == 3) && (c2 >= 2)))) *p = '-'; |
michael@0 | 816 | |
michael@0 | 817 | cwrd = 1; |
michael@0 | 818 | for (int k=0; k < ns; k++) |
michael@0 | 819 | if (strcmp(candidate,wlst[k]) == 0) cwrd = 0; |
michael@0 | 820 | if (ns < maxSug) { |
michael@0 | 821 | if (cwrd) { |
michael@0 | 822 | wlst[ns] = mystrdup(candidate); |
michael@0 | 823 | if (wlst[ns] == NULL) return -1; |
michael@0 | 824 | ns++; |
michael@0 | 825 | } |
michael@0 | 826 | } else return ns; |
michael@0 | 827 | // add two word suggestion with dash, if TRY string contains |
michael@0 | 828 | // "a" or "-" |
michael@0 | 829 | // NOTE: cwrd doesn't modified for REP twoword sugg. |
michael@0 | 830 | if (ctry && (strchr(ctry, 'a') || strchr(ctry, '-')) && |
michael@0 | 831 | mystrlen(p + 1) > 1 && |
michael@0 | 832 | mystrlen(candidate) - mystrlen(p) > 1) { |
michael@0 | 833 | *p = '-'; |
michael@0 | 834 | for (int k=0; k < ns; k++) |
michael@0 | 835 | if (strcmp(candidate,wlst[k]) == 0) cwrd = 0; |
michael@0 | 836 | if (ns < maxSug) { |
michael@0 | 837 | if (cwrd) { |
michael@0 | 838 | wlst[ns] = mystrdup(candidate); |
michael@0 | 839 | if (wlst[ns] == NULL) return -1; |
michael@0 | 840 | ns++; |
michael@0 | 841 | } |
michael@0 | 842 | } else return ns; |
michael@0 | 843 | } |
michael@0 | 844 | } |
michael@0 | 845 | } |
michael@0 | 846 | } |
michael@0 | 847 | return ns; |
michael@0 | 848 | } |
michael@0 | 849 | |
michael@0 | 850 | |
michael@0 | 851 | // error is adjacent letter were swapped |
michael@0 | 852 | int SuggestMgr::swapchar(char ** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 853 | { |
michael@0 | 854 | char candidate[MAXSWUTF8L]; |
michael@0 | 855 | char * p; |
michael@0 | 856 | char tmpc; |
michael@0 | 857 | int wl=strlen(word); |
michael@0 | 858 | // try swapping adjacent chars one by one |
michael@0 | 859 | strcpy(candidate, word); |
michael@0 | 860 | for (p = candidate; p[1] != 0; p++) { |
michael@0 | 861 | tmpc = *p; |
michael@0 | 862 | *p = p[1]; |
michael@0 | 863 | p[1] = tmpc; |
michael@0 | 864 | ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL); |
michael@0 | 865 | if (ns == -1) return -1; |
michael@0 | 866 | p[1] = *p; |
michael@0 | 867 | *p = tmpc; |
michael@0 | 868 | } |
michael@0 | 869 | // try double swaps for short words |
michael@0 | 870 | // ahev -> have, owudl -> would |
michael@0 | 871 | if (wl == 4 || wl == 5) { |
michael@0 | 872 | candidate[0] = word[1]; |
michael@0 | 873 | candidate[1] = word[0]; |
michael@0 | 874 | candidate[2] = word[2]; |
michael@0 | 875 | candidate[wl - 2] = word[wl - 1]; |
michael@0 | 876 | candidate[wl - 1] = word[wl - 2]; |
michael@0 | 877 | ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL); |
michael@0 | 878 | if (ns == -1) return -1; |
michael@0 | 879 | if (wl == 5) { |
michael@0 | 880 | candidate[0] = word[0]; |
michael@0 | 881 | candidate[1] = word[2]; |
michael@0 | 882 | candidate[2] = word[1]; |
michael@0 | 883 | ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL); |
michael@0 | 884 | if (ns == -1) return -1; |
michael@0 | 885 | } |
michael@0 | 886 | } |
michael@0 | 887 | return ns; |
michael@0 | 888 | } |
michael@0 | 889 | |
michael@0 | 890 | // error is adjacent letter were swapped |
michael@0 | 891 | int SuggestMgr::swapchar_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest) |
michael@0 | 892 | { |
michael@0 | 893 | w_char candidate_utf[MAXSWL]; |
michael@0 | 894 | char candidate[MAXSWUTF8L]; |
michael@0 | 895 | w_char * p; |
michael@0 | 896 | w_char tmpc; |
michael@0 | 897 | int len = 0; |
michael@0 | 898 | // try swapping adjacent chars one by one |
michael@0 | 899 | memcpy (candidate_utf, word, wl * sizeof(w_char)); |
michael@0 | 900 | for (p = candidate_utf; p < (candidate_utf + wl - 1); p++) { |
michael@0 | 901 | tmpc = *p; |
michael@0 | 902 | *p = p[1]; |
michael@0 | 903 | p[1] = tmpc; |
michael@0 | 904 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl); |
michael@0 | 905 | if (len == 0) len = strlen(candidate); |
michael@0 | 906 | ns = testsug(wlst, candidate, len, ns, cpdsuggest, NULL, NULL); |
michael@0 | 907 | if (ns == -1) return -1; |
michael@0 | 908 | p[1] = *p; |
michael@0 | 909 | *p = tmpc; |
michael@0 | 910 | } |
michael@0 | 911 | // try double swaps for short words |
michael@0 | 912 | // ahev -> have, owudl -> would, suodn -> sound |
michael@0 | 913 | if (wl == 4 || wl == 5) { |
michael@0 | 914 | candidate_utf[0] = word[1]; |
michael@0 | 915 | candidate_utf[1] = word[0]; |
michael@0 | 916 | candidate_utf[2] = word[2]; |
michael@0 | 917 | candidate_utf[wl - 2] = word[wl - 1]; |
michael@0 | 918 | candidate_utf[wl - 1] = word[wl - 2]; |
michael@0 | 919 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl); |
michael@0 | 920 | ns = testsug(wlst, candidate, len, ns, cpdsuggest, NULL, NULL); |
michael@0 | 921 | if (ns == -1) return -1; |
michael@0 | 922 | if (wl == 5) { |
michael@0 | 923 | candidate_utf[0] = word[0]; |
michael@0 | 924 | candidate_utf[1] = word[2]; |
michael@0 | 925 | candidate_utf[2] = word[1]; |
michael@0 | 926 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl); |
michael@0 | 927 | ns = testsug(wlst, candidate, len, ns, cpdsuggest, NULL, NULL); |
michael@0 | 928 | if (ns == -1) return -1; |
michael@0 | 929 | } |
michael@0 | 930 | } |
michael@0 | 931 | return ns; |
michael@0 | 932 | } |
michael@0 | 933 | |
michael@0 | 934 | // error is not adjacent letter were swapped |
michael@0 | 935 | int SuggestMgr::longswapchar(char ** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 936 | { |
michael@0 | 937 | char candidate[MAXSWUTF8L]; |
michael@0 | 938 | char * p; |
michael@0 | 939 | char * q; |
michael@0 | 940 | char tmpc; |
michael@0 | 941 | int wl=strlen(word); |
michael@0 | 942 | // try swapping not adjacent chars one by one |
michael@0 | 943 | strcpy(candidate, word); |
michael@0 | 944 | for (p = candidate; *p != 0; p++) { |
michael@0 | 945 | for (q = candidate; *q != 0; q++) { |
michael@0 | 946 | if (abs((int)(p-q)) > 1) { |
michael@0 | 947 | tmpc = *p; |
michael@0 | 948 | *p = *q; |
michael@0 | 949 | *q = tmpc; |
michael@0 | 950 | ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL); |
michael@0 | 951 | if (ns == -1) return -1; |
michael@0 | 952 | *q = *p; |
michael@0 | 953 | *p = tmpc; |
michael@0 | 954 | } |
michael@0 | 955 | } |
michael@0 | 956 | } |
michael@0 | 957 | return ns; |
michael@0 | 958 | } |
michael@0 | 959 | |
michael@0 | 960 | |
michael@0 | 961 | // error is adjacent letter were swapped |
michael@0 | 962 | int SuggestMgr::longswapchar_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest) |
michael@0 | 963 | { |
michael@0 | 964 | w_char candidate_utf[MAXSWL]; |
michael@0 | 965 | char candidate[MAXSWUTF8L]; |
michael@0 | 966 | w_char * p; |
michael@0 | 967 | w_char * q; |
michael@0 | 968 | w_char tmpc; |
michael@0 | 969 | // try swapping not adjacent chars |
michael@0 | 970 | memcpy (candidate_utf, word, wl * sizeof(w_char)); |
michael@0 | 971 | for (p = candidate_utf; p < (candidate_utf + wl); p++) { |
michael@0 | 972 | for (q = candidate_utf; q < (candidate_utf + wl); q++) { |
michael@0 | 973 | if (abs((int)(p-q)) > 1) { |
michael@0 | 974 | tmpc = *p; |
michael@0 | 975 | *p = *q; |
michael@0 | 976 | *q = tmpc; |
michael@0 | 977 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl); |
michael@0 | 978 | ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL); |
michael@0 | 979 | if (ns == -1) return -1; |
michael@0 | 980 | *q = *p; |
michael@0 | 981 | *p = tmpc; |
michael@0 | 982 | } |
michael@0 | 983 | } |
michael@0 | 984 | } |
michael@0 | 985 | return ns; |
michael@0 | 986 | } |
michael@0 | 987 | |
michael@0 | 988 | // error is a letter was moved |
michael@0 | 989 | int SuggestMgr::movechar(char ** wlst, const char * word, int ns, int cpdsuggest) |
michael@0 | 990 | { |
michael@0 | 991 | char candidate[MAXSWUTF8L]; |
michael@0 | 992 | char * p; |
michael@0 | 993 | char * q; |
michael@0 | 994 | char tmpc; |
michael@0 | 995 | |
michael@0 | 996 | int wl=strlen(word); |
michael@0 | 997 | // try moving a char |
michael@0 | 998 | strcpy(candidate, word); |
michael@0 | 999 | for (p = candidate; *p != 0; p++) { |
michael@0 | 1000 | for (q = p + 1; (*q != 0) && ((q - p) < 10); q++) { |
michael@0 | 1001 | tmpc = *(q-1); |
michael@0 | 1002 | *(q-1) = *q; |
michael@0 | 1003 | *q = tmpc; |
michael@0 | 1004 | if ((q-p) < 2) continue; // omit swap char |
michael@0 | 1005 | ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL); |
michael@0 | 1006 | if (ns == -1) return -1; |
michael@0 | 1007 | } |
michael@0 | 1008 | strcpy(candidate, word); |
michael@0 | 1009 | } |
michael@0 | 1010 | for (p = candidate + wl - 1; p > candidate; p--) { |
michael@0 | 1011 | for (q = p - 1; (q >= candidate) && ((p - q) < 10); q--) { |
michael@0 | 1012 | tmpc = *(q+1); |
michael@0 | 1013 | *(q+1) = *q; |
michael@0 | 1014 | *q = tmpc; |
michael@0 | 1015 | if ((p-q) < 2) continue; // omit swap char |
michael@0 | 1016 | ns = testsug(wlst, candidate, wl, ns, cpdsuggest, NULL, NULL); |
michael@0 | 1017 | if (ns == -1) return -1; |
michael@0 | 1018 | } |
michael@0 | 1019 | strcpy(candidate, word); |
michael@0 | 1020 | } |
michael@0 | 1021 | return ns; |
michael@0 | 1022 | } |
michael@0 | 1023 | |
michael@0 | 1024 | // error is a letter was moved |
michael@0 | 1025 | int SuggestMgr::movechar_utf(char ** wlst, const w_char * word, int wl, int ns, int cpdsuggest) |
michael@0 | 1026 | { |
michael@0 | 1027 | w_char candidate_utf[MAXSWL]; |
michael@0 | 1028 | char candidate[MAXSWUTF8L]; |
michael@0 | 1029 | w_char * p; |
michael@0 | 1030 | w_char * q; |
michael@0 | 1031 | w_char tmpc; |
michael@0 | 1032 | // try moving a char |
michael@0 | 1033 | memcpy (candidate_utf, word, wl * sizeof(w_char)); |
michael@0 | 1034 | for (p = candidate_utf; p < (candidate_utf + wl); p++) { |
michael@0 | 1035 | for (q = p + 1; (q < (candidate_utf + wl)) && ((q - p) < 10); q++) { |
michael@0 | 1036 | tmpc = *(q-1); |
michael@0 | 1037 | *(q-1) = *q; |
michael@0 | 1038 | *q = tmpc; |
michael@0 | 1039 | if ((q-p) < 2) continue; // omit swap char |
michael@0 | 1040 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl); |
michael@0 | 1041 | ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL); |
michael@0 | 1042 | if (ns == -1) return -1; |
michael@0 | 1043 | } |
michael@0 | 1044 | memcpy (candidate_utf, word, wl * sizeof(w_char)); |
michael@0 | 1045 | } |
michael@0 | 1046 | for (p = candidate_utf + wl - 1; p > candidate_utf; p--) { |
michael@0 | 1047 | for (q = p - 1; (q >= candidate_utf) && ((p - q) < 10); q--) { |
michael@0 | 1048 | tmpc = *(q+1); |
michael@0 | 1049 | *(q+1) = *q; |
michael@0 | 1050 | *q = tmpc; |
michael@0 | 1051 | if ((p-q) < 2) continue; // omit swap char |
michael@0 | 1052 | u16_u8(candidate, MAXSWUTF8L, candidate_utf, wl); |
michael@0 | 1053 | ns = testsug(wlst, candidate, strlen(candidate), ns, cpdsuggest, NULL, NULL); |
michael@0 | 1054 | if (ns == -1) return -1; |
michael@0 | 1055 | } |
michael@0 | 1056 | memcpy (candidate_utf, word, wl * sizeof(w_char)); |
michael@0 | 1057 | } |
michael@0 | 1058 | return ns; |
michael@0 | 1059 | } |
michael@0 | 1060 | |
michael@0 | 1061 | // generate a set of suggestions for very poorly spelled words |
michael@0 | 1062 | int SuggestMgr::ngsuggest(char** wlst, char * w, int ns, HashMgr** pHMgr, int md) |
michael@0 | 1063 | { |
michael@0 | 1064 | |
michael@0 | 1065 | int i, j; |
michael@0 | 1066 | int lval; |
michael@0 | 1067 | int sc, scphon; |
michael@0 | 1068 | int lp, lpphon; |
michael@0 | 1069 | int nonbmp = 0; |
michael@0 | 1070 | |
michael@0 | 1071 | // exhaustively search through all root words |
michael@0 | 1072 | // keeping track of the MAX_ROOTS most similar root words |
michael@0 | 1073 | struct hentry * roots[MAX_ROOTS]; |
michael@0 | 1074 | char * rootsphon[MAX_ROOTS]; |
michael@0 | 1075 | int scores[MAX_ROOTS]; |
michael@0 | 1076 | int scoresphon[MAX_ROOTS]; |
michael@0 | 1077 | for (i = 0; i < MAX_ROOTS; i++) { |
michael@0 | 1078 | roots[i] = NULL; |
michael@0 | 1079 | scores[i] = -100 * i; |
michael@0 | 1080 | rootsphon[i] = NULL; |
michael@0 | 1081 | scoresphon[i] = -100 * i; |
michael@0 | 1082 | } |
michael@0 | 1083 | lp = MAX_ROOTS - 1; |
michael@0 | 1084 | lpphon = MAX_ROOTS - 1; |
michael@0 | 1085 | scphon = -20000; |
michael@0 | 1086 | int low = NGRAM_LOWERING; |
michael@0 | 1087 | |
michael@0 | 1088 | char w2[MAXWORDUTF8LEN]; |
michael@0 | 1089 | char f[MAXSWUTF8L]; |
michael@0 | 1090 | char * word = w; |
michael@0 | 1091 | |
michael@0 | 1092 | // word reversing wrapper for complex prefixes |
michael@0 | 1093 | if (complexprefixes) { |
michael@0 | 1094 | strcpy(w2, w); |
michael@0 | 1095 | if (utf8) reverseword_utf(w2); else reverseword(w2); |
michael@0 | 1096 | word = w2; |
michael@0 | 1097 | } |
michael@0 | 1098 | |
michael@0 | 1099 | char mw[MAXSWUTF8L]; |
michael@0 | 1100 | w_char u8[MAXSWL]; |
michael@0 | 1101 | int nc = strlen(word); |
michael@0 | 1102 | int n = (utf8) ? u8_u16(u8, MAXSWL, word) : nc; |
michael@0 | 1103 | |
michael@0 | 1104 | // set character based ngram suggestion for words with non-BMP Unicode characters |
michael@0 | 1105 | if (n == -1) { |
michael@0 | 1106 | utf8 = 0; // XXX not state-free |
michael@0 | 1107 | n = nc; |
michael@0 | 1108 | nonbmp = 1; |
michael@0 | 1109 | low = 0; |
michael@0 | 1110 | } |
michael@0 | 1111 | |
michael@0 | 1112 | struct hentry* hp = NULL; |
michael@0 | 1113 | int col = -1; |
michael@0 | 1114 | phonetable * ph = (pAMgr) ? pAMgr->get_phonetable() : NULL; |
michael@0 | 1115 | char target[MAXSWUTF8L]; |
michael@0 | 1116 | char candidate[MAXSWUTF8L]; |
michael@0 | 1117 | if (ph) { |
michael@0 | 1118 | if (utf8) { |
michael@0 | 1119 | w_char _w[MAXSWL]; |
michael@0 | 1120 | int _wl = u8_u16(_w, MAXSWL, word); |
michael@0 | 1121 | mkallcap_utf(_w, _wl, langnum); |
michael@0 | 1122 | u16_u8(candidate, MAXSWUTF8L, _w, _wl); |
michael@0 | 1123 | } else { |
michael@0 | 1124 | strcpy(candidate, word); |
michael@0 | 1125 | if (!nonbmp) mkallcap(candidate, csconv); |
michael@0 | 1126 | } |
michael@0 | 1127 | phonet(candidate, target, nc, *ph); // XXX phonet() is 8-bit (nc, not n) |
michael@0 | 1128 | } |
michael@0 | 1129 | |
michael@0 | 1130 | FLAG forbiddenword = pAMgr ? pAMgr->get_forbiddenword() : FLAG_NULL; |
michael@0 | 1131 | FLAG nosuggest = pAMgr ? pAMgr->get_nosuggest() : FLAG_NULL; |
michael@0 | 1132 | FLAG nongramsuggest = pAMgr ? pAMgr->get_nongramsuggest() : FLAG_NULL; |
michael@0 | 1133 | FLAG onlyincompound = pAMgr ? pAMgr->get_onlyincompound() : FLAG_NULL; |
michael@0 | 1134 | |
michael@0 | 1135 | for (i = 0; i < md; i++) { |
michael@0 | 1136 | while (0 != (hp = (pHMgr[i])->walk_hashtable(col, hp))) { |
michael@0 | 1137 | if ((hp->astr) && (pAMgr) && |
michael@0 | 1138 | (TESTAFF(hp->astr, forbiddenword, hp->alen) || |
michael@0 | 1139 | TESTAFF(hp->astr, ONLYUPCASEFLAG, hp->alen) || |
michael@0 | 1140 | TESTAFF(hp->astr, nosuggest, hp->alen) || |
michael@0 | 1141 | TESTAFF(hp->astr, nongramsuggest, hp->alen) || |
michael@0 | 1142 | TESTAFF(hp->astr, onlyincompound, hp->alen))) continue; |
michael@0 | 1143 | |
michael@0 | 1144 | sc = ngram(3, word, HENTRY_WORD(hp), NGRAM_LONGER_WORSE + low) + |
michael@0 | 1145 | leftcommonsubstring(word, HENTRY_WORD(hp)); |
michael@0 | 1146 | |
michael@0 | 1147 | // check special pronounciation |
michael@0 | 1148 | if ((hp->var & H_OPT_PHON) && copy_field(f, HENTRY_DATA(hp), MORPH_PHON)) { |
michael@0 | 1149 | int sc2 = ngram(3, word, f, NGRAM_LONGER_WORSE + low) + |
michael@0 | 1150 | + leftcommonsubstring(word, f); |
michael@0 | 1151 | if (sc2 > sc) sc = sc2; |
michael@0 | 1152 | } |
michael@0 | 1153 | |
michael@0 | 1154 | scphon = -20000; |
michael@0 | 1155 | if (ph && (sc > 2) && (abs(n - (int) hp->clen) <= 3)) { |
michael@0 | 1156 | char target2[MAXSWUTF8L]; |
michael@0 | 1157 | if (utf8) { |
michael@0 | 1158 | w_char _w[MAXSWL]; |
michael@0 | 1159 | int _wl = u8_u16(_w, MAXSWL, HENTRY_WORD(hp)); |
michael@0 | 1160 | mkallcap_utf(_w, _wl, langnum); |
michael@0 | 1161 | u16_u8(candidate, MAXSWUTF8L, _w, _wl); |
michael@0 | 1162 | } else { |
michael@0 | 1163 | strcpy(candidate, HENTRY_WORD(hp)); |
michael@0 | 1164 | mkallcap(candidate, csconv); |
michael@0 | 1165 | } |
michael@0 | 1166 | phonet(candidate, target2, -1, *ph); |
michael@0 | 1167 | scphon = 2 * ngram(3, target, target2, NGRAM_LONGER_WORSE); |
michael@0 | 1168 | } |
michael@0 | 1169 | |
michael@0 | 1170 | if (sc > scores[lp]) { |
michael@0 | 1171 | scores[lp] = sc; |
michael@0 | 1172 | roots[lp] = hp; |
michael@0 | 1173 | lval = sc; |
michael@0 | 1174 | for (j=0; j < MAX_ROOTS; j++) |
michael@0 | 1175 | if (scores[j] < lval) { |
michael@0 | 1176 | lp = j; |
michael@0 | 1177 | lval = scores[j]; |
michael@0 | 1178 | } |
michael@0 | 1179 | } |
michael@0 | 1180 | |
michael@0 | 1181 | |
michael@0 | 1182 | if (scphon > scoresphon[lpphon]) { |
michael@0 | 1183 | scoresphon[lpphon] = scphon; |
michael@0 | 1184 | rootsphon[lpphon] = HENTRY_WORD(hp); |
michael@0 | 1185 | lval = scphon; |
michael@0 | 1186 | for (j=0; j < MAX_ROOTS; j++) |
michael@0 | 1187 | if (scoresphon[j] < lval) { |
michael@0 | 1188 | lpphon = j; |
michael@0 | 1189 | lval = scoresphon[j]; |
michael@0 | 1190 | } |
michael@0 | 1191 | } |
michael@0 | 1192 | }} |
michael@0 | 1193 | |
michael@0 | 1194 | // find minimum threshold for a passable suggestion |
michael@0 | 1195 | // mangle original word three differnt ways |
michael@0 | 1196 | // and score them to generate a minimum acceptable score |
michael@0 | 1197 | int thresh = 0; |
michael@0 | 1198 | for (int sp = 1; sp < 4; sp++) { |
michael@0 | 1199 | if (utf8) { |
michael@0 | 1200 | for (int k=sp; k < n; k+=4) *((unsigned short *) u8 + k) = '*'; |
michael@0 | 1201 | u16_u8(mw, MAXSWUTF8L, u8, n); |
michael@0 | 1202 | thresh = thresh + ngram(n, word, mw, NGRAM_ANY_MISMATCH + low); |
michael@0 | 1203 | } else { |
michael@0 | 1204 | strcpy(mw, word); |
michael@0 | 1205 | for (int k=sp; k < n; k+=4) *(mw + k) = '*'; |
michael@0 | 1206 | thresh = thresh + ngram(n, word, mw, NGRAM_ANY_MISMATCH + low); |
michael@0 | 1207 | } |
michael@0 | 1208 | } |
michael@0 | 1209 | thresh = thresh / 3; |
michael@0 | 1210 | thresh--; |
michael@0 | 1211 | |
michael@0 | 1212 | // now expand affixes on each of these root words and |
michael@0 | 1213 | // and use length adjusted ngram scores to select |
michael@0 | 1214 | // possible suggestions |
michael@0 | 1215 | char * guess[MAX_GUESS]; |
michael@0 | 1216 | char * guessorig[MAX_GUESS]; |
michael@0 | 1217 | int gscore[MAX_GUESS]; |
michael@0 | 1218 | for(i=0;i<MAX_GUESS;i++) { |
michael@0 | 1219 | guess[i] = NULL; |
michael@0 | 1220 | guessorig[i] = NULL; |
michael@0 | 1221 | gscore[i] = -100 * i; |
michael@0 | 1222 | } |
michael@0 | 1223 | |
michael@0 | 1224 | lp = MAX_GUESS - 1; |
michael@0 | 1225 | |
michael@0 | 1226 | struct guessword * glst; |
michael@0 | 1227 | glst = (struct guessword *) calloc(MAX_WORDS,sizeof(struct guessword)); |
michael@0 | 1228 | if (! glst) { |
michael@0 | 1229 | if (nonbmp) utf8 = 1; |
michael@0 | 1230 | return ns; |
michael@0 | 1231 | } |
michael@0 | 1232 | |
michael@0 | 1233 | for (i = 0; i < MAX_ROOTS; i++) { |
michael@0 | 1234 | if (roots[i]) { |
michael@0 | 1235 | struct hentry * rp = roots[i]; |
michael@0 | 1236 | int nw = pAMgr->expand_rootword(glst, MAX_WORDS, HENTRY_WORD(rp), rp->blen, |
michael@0 | 1237 | rp->astr, rp->alen, word, nc, |
michael@0 | 1238 | ((rp->var & H_OPT_PHON) ? copy_field(f, HENTRY_DATA(rp), MORPH_PHON) : NULL)); |
michael@0 | 1239 | |
michael@0 | 1240 | for (int k = 0; k < nw ; k++) { |
michael@0 | 1241 | sc = ngram(n, word, glst[k].word, NGRAM_ANY_MISMATCH + low) + |
michael@0 | 1242 | leftcommonsubstring(word, glst[k].word); |
michael@0 | 1243 | |
michael@0 | 1244 | if (sc > thresh) { |
michael@0 | 1245 | if (sc > gscore[lp]) { |
michael@0 | 1246 | if (guess[lp]) { |
michael@0 | 1247 | free (guess[lp]); |
michael@0 | 1248 | if (guessorig[lp]) { |
michael@0 | 1249 | free(guessorig[lp]); |
michael@0 | 1250 | guessorig[lp] = NULL; |
michael@0 | 1251 | } |
michael@0 | 1252 | } |
michael@0 | 1253 | gscore[lp] = sc; |
michael@0 | 1254 | guess[lp] = glst[k].word; |
michael@0 | 1255 | guessorig[lp] = glst[k].orig; |
michael@0 | 1256 | lval = sc; |
michael@0 | 1257 | for (j=0; j < MAX_GUESS; j++) |
michael@0 | 1258 | if (gscore[j] < lval) { |
michael@0 | 1259 | lp = j; |
michael@0 | 1260 | lval = gscore[j]; |
michael@0 | 1261 | } |
michael@0 | 1262 | } else { |
michael@0 | 1263 | free(glst[k].word); |
michael@0 | 1264 | if (glst[k].orig) free(glst[k].orig); |
michael@0 | 1265 | } |
michael@0 | 1266 | } else { |
michael@0 | 1267 | free(glst[k].word); |
michael@0 | 1268 | if (glst[k].orig) free(glst[k].orig); |
michael@0 | 1269 | } |
michael@0 | 1270 | } |
michael@0 | 1271 | } |
michael@0 | 1272 | } |
michael@0 | 1273 | free(glst); |
michael@0 | 1274 | |
michael@0 | 1275 | // now we are done generating guesses |
michael@0 | 1276 | // sort in order of decreasing score |
michael@0 | 1277 | |
michael@0 | 1278 | |
michael@0 | 1279 | bubblesort(&guess[0], &guessorig[0], &gscore[0], MAX_GUESS); |
michael@0 | 1280 | if (ph) bubblesort(&rootsphon[0], NULL, &scoresphon[0], MAX_ROOTS); |
michael@0 | 1281 | |
michael@0 | 1282 | // weight suggestions with a similarity index, based on |
michael@0 | 1283 | // the longest common subsequent algorithm and resort |
michael@0 | 1284 | |
michael@0 | 1285 | int is_swap = 0; |
michael@0 | 1286 | int re = 0; |
michael@0 | 1287 | double fact = 1.0; |
michael@0 | 1288 | if (pAMgr) { |
michael@0 | 1289 | int maxd = pAMgr->get_maxdiff(); |
michael@0 | 1290 | if (maxd >= 0) fact = (10.0 - maxd)/5.0; |
michael@0 | 1291 | } |
michael@0 | 1292 | |
michael@0 | 1293 | for (i=0; i < MAX_GUESS; i++) { |
michael@0 | 1294 | if (guess[i]) { |
michael@0 | 1295 | // lowering guess[i] |
michael@0 | 1296 | char gl[MAXSWUTF8L]; |
michael@0 | 1297 | int len; |
michael@0 | 1298 | if (utf8) { |
michael@0 | 1299 | w_char _w[MAXSWL]; |
michael@0 | 1300 | len = u8_u16(_w, MAXSWL, guess[i]); |
michael@0 | 1301 | mkallsmall_utf(_w, len, langnum); |
michael@0 | 1302 | u16_u8(gl, MAXSWUTF8L, _w, len); |
michael@0 | 1303 | } else { |
michael@0 | 1304 | strcpy(gl, guess[i]); |
michael@0 | 1305 | if (!nonbmp) mkallsmall(gl, csconv); |
michael@0 | 1306 | len = strlen(guess[i]); |
michael@0 | 1307 | } |
michael@0 | 1308 | |
michael@0 | 1309 | int _lcs = lcslen(word, gl); |
michael@0 | 1310 | |
michael@0 | 1311 | // same characters with different casing |
michael@0 | 1312 | if ((n == len) && (n == _lcs)) { |
michael@0 | 1313 | gscore[i] += 2000; |
michael@0 | 1314 | break; |
michael@0 | 1315 | } |
michael@0 | 1316 | // using 2-gram instead of 3, and other weightening |
michael@0 | 1317 | |
michael@0 | 1318 | re = ngram(2, word, gl, NGRAM_ANY_MISMATCH + low + NGRAM_WEIGHTED) + |
michael@0 | 1319 | ngram(2, gl, word, NGRAM_ANY_MISMATCH + low + NGRAM_WEIGHTED); |
michael@0 | 1320 | |
michael@0 | 1321 | gscore[i] = |
michael@0 | 1322 | // length of longest common subsequent minus length difference |
michael@0 | 1323 | 2 * _lcs - abs((int) (n - len)) + |
michael@0 | 1324 | // weight length of the left common substring |
michael@0 | 1325 | leftcommonsubstring(word, gl) + |
michael@0 | 1326 | // weight equal character positions |
michael@0 | 1327 | (!nonbmp && commoncharacterpositions(word, gl, &is_swap) ? 1: 0) + |
michael@0 | 1328 | // swap character (not neighboring) |
michael@0 | 1329 | ((is_swap) ? 10 : 0) + |
michael@0 | 1330 | // ngram |
michael@0 | 1331 | ngram(4, word, gl, NGRAM_ANY_MISMATCH + low) + |
michael@0 | 1332 | // weighted ngrams |
michael@0 | 1333 | re + |
michael@0 | 1334 | // different limit for dictionaries with PHONE rules |
michael@0 | 1335 | (ph ? (re < len * fact ? -1000 : 0) : (re < (n + len)*fact? -1000 : 0)); |
michael@0 | 1336 | } |
michael@0 | 1337 | } |
michael@0 | 1338 | |
michael@0 | 1339 | bubblesort(&guess[0], &guessorig[0], &gscore[0], MAX_GUESS); |
michael@0 | 1340 | |
michael@0 | 1341 | // phonetic version |
michael@0 | 1342 | if (ph) for (i=0; i < MAX_ROOTS; i++) { |
michael@0 | 1343 | if (rootsphon[i]) { |
michael@0 | 1344 | // lowering rootphon[i] |
michael@0 | 1345 | char gl[MAXSWUTF8L]; |
michael@0 | 1346 | int len; |
michael@0 | 1347 | if (utf8) { |
michael@0 | 1348 | w_char _w[MAXSWL]; |
michael@0 | 1349 | len = u8_u16(_w, MAXSWL, rootsphon[i]); |
michael@0 | 1350 | mkallsmall_utf(_w, len, langnum); |
michael@0 | 1351 | u16_u8(gl, MAXSWUTF8L, _w, len); |
michael@0 | 1352 | } else { |
michael@0 | 1353 | strcpy(gl, rootsphon[i]); |
michael@0 | 1354 | if (!nonbmp) mkallsmall(gl, csconv); |
michael@0 | 1355 | len = strlen(rootsphon[i]); |
michael@0 | 1356 | } |
michael@0 | 1357 | |
michael@0 | 1358 | // heuristic weigthing of ngram scores |
michael@0 | 1359 | scoresphon[i] += 2 * lcslen(word, gl) - abs((int) (n - len)) + |
michael@0 | 1360 | // weight length of the left common substring |
michael@0 | 1361 | leftcommonsubstring(word, gl); |
michael@0 | 1362 | } |
michael@0 | 1363 | } |
michael@0 | 1364 | |
michael@0 | 1365 | if (ph) bubblesort(&rootsphon[0], NULL, &scoresphon[0], MAX_ROOTS); |
michael@0 | 1366 | |
michael@0 | 1367 | // copy over |
michael@0 | 1368 | int oldns = ns; |
michael@0 | 1369 | |
michael@0 | 1370 | int same = 0; |
michael@0 | 1371 | for (i=0; i < MAX_GUESS; i++) { |
michael@0 | 1372 | if (guess[i]) { |
michael@0 | 1373 | if ((ns < oldns + maxngramsugs) && (ns < maxSug) && (!same || (gscore[i] > 1000))) { |
michael@0 | 1374 | int unique = 1; |
michael@0 | 1375 | // leave only excellent suggestions, if exists |
michael@0 | 1376 | if (gscore[i] > 1000) same = 1; else if (gscore[i] < -100) { |
michael@0 | 1377 | same = 1; |
michael@0 | 1378 | // keep the best ngram suggestions, unless in ONLYMAXDIFF mode |
michael@0 | 1379 | if (ns > oldns || (pAMgr && pAMgr->get_onlymaxdiff())) { |
michael@0 | 1380 | free(guess[i]); |
michael@0 | 1381 | if (guessorig[i]) free(guessorig[i]); |
michael@0 | 1382 | continue; |
michael@0 | 1383 | } |
michael@0 | 1384 | } |
michael@0 | 1385 | for (j = 0; j < ns; j++) { |
michael@0 | 1386 | // don't suggest previous suggestions or a previous suggestion with prefixes or affixes |
michael@0 | 1387 | if ((!guessorig[i] && strstr(guess[i], wlst[j])) || |
michael@0 | 1388 | (guessorig[i] && strstr(guessorig[i], wlst[j])) || |
michael@0 | 1389 | // check forbidden words |
michael@0 | 1390 | !checkword(guess[i], strlen(guess[i]), 0, NULL, NULL)) unique = 0; |
michael@0 | 1391 | } |
michael@0 | 1392 | if (unique) { |
michael@0 | 1393 | wlst[ns++] = guess[i]; |
michael@0 | 1394 | if (guessorig[i]) { |
michael@0 | 1395 | free(guess[i]); |
michael@0 | 1396 | wlst[ns-1] = guessorig[i]; |
michael@0 | 1397 | } |
michael@0 | 1398 | } else { |
michael@0 | 1399 | free(guess[i]); |
michael@0 | 1400 | if (guessorig[i]) free(guessorig[i]); |
michael@0 | 1401 | } |
michael@0 | 1402 | } else { |
michael@0 | 1403 | free(guess[i]); |
michael@0 | 1404 | if (guessorig[i]) free(guessorig[i]); |
michael@0 | 1405 | } |
michael@0 | 1406 | } |
michael@0 | 1407 | } |
michael@0 | 1408 | |
michael@0 | 1409 | oldns = ns; |
michael@0 | 1410 | if (ph) for (i=0; i < MAX_ROOTS; i++) { |
michael@0 | 1411 | if (rootsphon[i]) { |
michael@0 | 1412 | if ((ns < oldns + MAXPHONSUGS) && (ns < maxSug)) { |
michael@0 | 1413 | int unique = 1; |
michael@0 | 1414 | for (j = 0; j < ns; j++) { |
michael@0 | 1415 | // don't suggest previous suggestions or a previous suggestion with prefixes or affixes |
michael@0 | 1416 | if (strstr(rootsphon[i], wlst[j]) || |
michael@0 | 1417 | // check forbidden words |
michael@0 | 1418 | !checkword(rootsphon[i], strlen(rootsphon[i]), 0, NULL, NULL)) unique = 0; |
michael@0 | 1419 | } |
michael@0 | 1420 | if (unique) { |
michael@0 | 1421 | wlst[ns++] = mystrdup(rootsphon[i]); |
michael@0 | 1422 | if (!wlst[ns - 1]) return ns - 1; |
michael@0 | 1423 | } |
michael@0 | 1424 | } |
michael@0 | 1425 | } |
michael@0 | 1426 | } |
michael@0 | 1427 | |
michael@0 | 1428 | if (nonbmp) utf8 = 1; |
michael@0 | 1429 | return ns; |
michael@0 | 1430 | } |
michael@0 | 1431 | |
michael@0 | 1432 | |
michael@0 | 1433 | // see if a candidate suggestion is spelled correctly |
michael@0 | 1434 | // needs to check both root words and words with affixes |
michael@0 | 1435 | |
michael@0 | 1436 | // obsolote MySpell-HU modifications: |
michael@0 | 1437 | // return value 2 and 3 marks compounding with hyphen (-) |
michael@0 | 1438 | // `3' marks roots without suffix |
michael@0 | 1439 | int SuggestMgr::checkword(const char * word, int len, int cpdsuggest, int * timer, clock_t * timelimit) |
michael@0 | 1440 | { |
michael@0 | 1441 | struct hentry * rv=NULL; |
michael@0 | 1442 | struct hentry * rv2=NULL; |
michael@0 | 1443 | int nosuffix = 0; |
michael@0 | 1444 | |
michael@0 | 1445 | // check time limit |
michael@0 | 1446 | if (timer) { |
michael@0 | 1447 | (*timer)--; |
michael@0 | 1448 | if (!(*timer) && timelimit) { |
michael@0 | 1449 | if ((clock() - *timelimit) > TIMELIMIT) return 0; |
michael@0 | 1450 | *timer = MAXPLUSTIMER; |
michael@0 | 1451 | } |
michael@0 | 1452 | } |
michael@0 | 1453 | |
michael@0 | 1454 | if (pAMgr) { |
michael@0 | 1455 | if (cpdsuggest==1) { |
michael@0 | 1456 | if (pAMgr->get_compound()) { |
michael@0 | 1457 | rv = pAMgr->compound_check(word, len, 0, 0, 100, 0, NULL, 0, 1, 0); //EXT |
michael@0 | 1458 | if (rv && (!(rv2 = pAMgr->lookup(word)) || !rv2->astr || |
michael@0 | 1459 | !(TESTAFF(rv2->astr,pAMgr->get_forbiddenword(),rv2->alen) || |
michael@0 | 1460 | TESTAFF(rv2->astr,pAMgr->get_nosuggest(),rv2->alen)))) return 3; // XXX obsolote categorisation + only ICONV needs affix flag check? |
michael@0 | 1461 | } |
michael@0 | 1462 | return 0; |
michael@0 | 1463 | } |
michael@0 | 1464 | |
michael@0 | 1465 | rv = pAMgr->lookup(word); |
michael@0 | 1466 | |
michael@0 | 1467 | if (rv) { |
michael@0 | 1468 | if ((rv->astr) && (TESTAFF(rv->astr,pAMgr->get_forbiddenword(),rv->alen) |
michael@0 | 1469 | || TESTAFF(rv->astr,pAMgr->get_nosuggest(),rv->alen))) return 0; |
michael@0 | 1470 | while (rv) { |
michael@0 | 1471 | if (rv->astr && (TESTAFF(rv->astr,pAMgr->get_needaffix(),rv->alen) || |
michael@0 | 1472 | TESTAFF(rv->astr, ONLYUPCASEFLAG, rv->alen) || |
michael@0 | 1473 | TESTAFF(rv->astr,pAMgr->get_onlyincompound(),rv->alen))) { |
michael@0 | 1474 | rv = rv->next_homonym; |
michael@0 | 1475 | } else break; |
michael@0 | 1476 | } |
michael@0 | 1477 | } else rv = pAMgr->prefix_check(word, len, 0); // only prefix, and prefix + suffix XXX |
michael@0 | 1478 | |
michael@0 | 1479 | if (rv) { |
michael@0 | 1480 | nosuffix=1; |
michael@0 | 1481 | } else { |
michael@0 | 1482 | rv = pAMgr->suffix_check(word, len, 0, NULL, NULL, 0, NULL); // only suffix |
michael@0 | 1483 | } |
michael@0 | 1484 | |
michael@0 | 1485 | if (!rv && pAMgr->have_contclass()) { |
michael@0 | 1486 | rv = pAMgr->suffix_check_twosfx(word, len, 0, NULL, FLAG_NULL); |
michael@0 | 1487 | if (!rv) rv = pAMgr->prefix_check_twosfx(word, len, 1, FLAG_NULL); |
michael@0 | 1488 | } |
michael@0 | 1489 | |
michael@0 | 1490 | // check forbidden words |
michael@0 | 1491 | if ((rv) && (rv->astr) && (TESTAFF(rv->astr,pAMgr->get_forbiddenword(),rv->alen) || |
michael@0 | 1492 | TESTAFF(rv->astr, ONLYUPCASEFLAG, rv->alen) || |
michael@0 | 1493 | TESTAFF(rv->astr,pAMgr->get_nosuggest(),rv->alen) || |
michael@0 | 1494 | TESTAFF(rv->astr,pAMgr->get_onlyincompound(),rv->alen))) return 0; |
michael@0 | 1495 | |
michael@0 | 1496 | if (rv) { // XXX obsolote |
michael@0 | 1497 | if ((pAMgr->get_compoundflag()) && |
michael@0 | 1498 | TESTAFF(rv->astr, pAMgr->get_compoundflag(), rv->alen)) return 2 + nosuffix; |
michael@0 | 1499 | return 1; |
michael@0 | 1500 | } |
michael@0 | 1501 | } |
michael@0 | 1502 | return 0; |
michael@0 | 1503 | } |
michael@0 | 1504 | |
michael@0 | 1505 | int SuggestMgr::check_forbidden(const char * word, int len) |
michael@0 | 1506 | { |
michael@0 | 1507 | struct hentry * rv = NULL; |
michael@0 | 1508 | |
michael@0 | 1509 | if (pAMgr) { |
michael@0 | 1510 | rv = pAMgr->lookup(word); |
michael@0 | 1511 | if (rv && rv->astr && (TESTAFF(rv->astr,pAMgr->get_needaffix(),rv->alen) || |
michael@0 | 1512 | TESTAFF(rv->astr,pAMgr->get_onlyincompound(),rv->alen))) rv = NULL; |
michael@0 | 1513 | if (!(pAMgr->prefix_check(word,len,1))) |
michael@0 | 1514 | rv = pAMgr->suffix_check(word,len, 0, NULL, NULL, 0, NULL); // prefix+suffix, suffix |
michael@0 | 1515 | // check forbidden words |
michael@0 | 1516 | if ((rv) && (rv->astr) && TESTAFF(rv->astr,pAMgr->get_forbiddenword(),rv->alen)) return 1; |
michael@0 | 1517 | } |
michael@0 | 1518 | return 0; |
michael@0 | 1519 | } |
michael@0 | 1520 | |
michael@0 | 1521 | #ifdef HUNSPELL_EXPERIMENTAL |
michael@0 | 1522 | // suggest possible stems |
michael@0 | 1523 | int SuggestMgr::suggest_pos_stems(char*** slst, const char * w, int nsug) |
michael@0 | 1524 | { |
michael@0 | 1525 | char ** wlst; |
michael@0 | 1526 | |
michael@0 | 1527 | struct hentry * rv = NULL; |
michael@0 | 1528 | |
michael@0 | 1529 | char w2[MAXSWUTF8L]; |
michael@0 | 1530 | const char * word = w; |
michael@0 | 1531 | |
michael@0 | 1532 | // word reversing wrapper for complex prefixes |
michael@0 | 1533 | if (complexprefixes) { |
michael@0 | 1534 | strcpy(w2, w); |
michael@0 | 1535 | if (utf8) reverseword_utf(w2); else reverseword(w2); |
michael@0 | 1536 | word = w2; |
michael@0 | 1537 | } |
michael@0 | 1538 | |
michael@0 | 1539 | int wl = strlen(word); |
michael@0 | 1540 | |
michael@0 | 1541 | |
michael@0 | 1542 | if (*slst) { |
michael@0 | 1543 | wlst = *slst; |
michael@0 | 1544 | } else { |
michael@0 | 1545 | wlst = (char **) calloc(maxSug, sizeof(char *)); |
michael@0 | 1546 | if (wlst == NULL) return -1; |
michael@0 | 1547 | } |
michael@0 | 1548 | |
michael@0 | 1549 | rv = pAMgr->suffix_check(word, wl, 0, NULL, wlst, maxSug, &nsug); |
michael@0 | 1550 | |
michael@0 | 1551 | // delete dash from end of word |
michael@0 | 1552 | if (nsug > 0) { |
michael@0 | 1553 | for (int j=0; j < nsug; j++) { |
michael@0 | 1554 | if (wlst[j][strlen(wlst[j]) - 1] == '-') wlst[j][strlen(wlst[j]) - 1] = '\0'; |
michael@0 | 1555 | } |
michael@0 | 1556 | } |
michael@0 | 1557 | |
michael@0 | 1558 | *slst = wlst; |
michael@0 | 1559 | return nsug; |
michael@0 | 1560 | } |
michael@0 | 1561 | #endif // END OF HUNSPELL_EXPERIMENTAL CODE |
michael@0 | 1562 | |
michael@0 | 1563 | |
michael@0 | 1564 | char * SuggestMgr::suggest_morph(const char * w) |
michael@0 | 1565 | { |
michael@0 | 1566 | char result[MAXLNLEN]; |
michael@0 | 1567 | char * r = (char *) result; |
michael@0 | 1568 | char * st; |
michael@0 | 1569 | |
michael@0 | 1570 | struct hentry * rv = NULL; |
michael@0 | 1571 | |
michael@0 | 1572 | *result = '\0'; |
michael@0 | 1573 | |
michael@0 | 1574 | if (! pAMgr) return NULL; |
michael@0 | 1575 | |
michael@0 | 1576 | char w2[MAXSWUTF8L]; |
michael@0 | 1577 | const char * word = w; |
michael@0 | 1578 | |
michael@0 | 1579 | // word reversing wrapper for complex prefixes |
michael@0 | 1580 | if (complexprefixes) { |
michael@0 | 1581 | strcpy(w2, w); |
michael@0 | 1582 | if (utf8) reverseword_utf(w2); else reverseword(w2); |
michael@0 | 1583 | word = w2; |
michael@0 | 1584 | } |
michael@0 | 1585 | |
michael@0 | 1586 | rv = pAMgr->lookup(word); |
michael@0 | 1587 | |
michael@0 | 1588 | while (rv) { |
michael@0 | 1589 | if ((!rv->astr) || !(TESTAFF(rv->astr, pAMgr->get_forbiddenword(), rv->alen) || |
michael@0 | 1590 | TESTAFF(rv->astr, pAMgr->get_needaffix(), rv->alen) || |
michael@0 | 1591 | TESTAFF(rv->astr,pAMgr->get_onlyincompound(),rv->alen))) { |
michael@0 | 1592 | if (!HENTRY_FIND(rv, MORPH_STEM)) { |
michael@0 | 1593 | mystrcat(result, " ", MAXLNLEN); |
michael@0 | 1594 | mystrcat(result, MORPH_STEM, MAXLNLEN); |
michael@0 | 1595 | mystrcat(result, word, MAXLNLEN); |
michael@0 | 1596 | } |
michael@0 | 1597 | if (HENTRY_DATA(rv)) { |
michael@0 | 1598 | mystrcat(result, " ", MAXLNLEN); |
michael@0 | 1599 | mystrcat(result, HENTRY_DATA2(rv), MAXLNLEN); |
michael@0 | 1600 | } |
michael@0 | 1601 | mystrcat(result, "\n", MAXLNLEN); |
michael@0 | 1602 | } |
michael@0 | 1603 | rv = rv->next_homonym; |
michael@0 | 1604 | } |
michael@0 | 1605 | |
michael@0 | 1606 | st = pAMgr->affix_check_morph(word,strlen(word)); |
michael@0 | 1607 | if (st) { |
michael@0 | 1608 | mystrcat(result, st, MAXLNLEN); |
michael@0 | 1609 | free(st); |
michael@0 | 1610 | } |
michael@0 | 1611 | |
michael@0 | 1612 | if (pAMgr->get_compound() && (*result == '\0')) |
michael@0 | 1613 | pAMgr->compound_check_morph(word, strlen(word), |
michael@0 | 1614 | 0, 0, 100, 0,NULL, 0, &r, NULL); |
michael@0 | 1615 | |
michael@0 | 1616 | return (*result) ? mystrdup(line_uniq(result, MSEP_REC)) : NULL; |
michael@0 | 1617 | } |
michael@0 | 1618 | |
michael@0 | 1619 | #ifdef HUNSPELL_EXPERIMENTAL |
michael@0 | 1620 | char * SuggestMgr::suggest_morph_for_spelling_error(const char * word) |
michael@0 | 1621 | { |
michael@0 | 1622 | char * p = NULL; |
michael@0 | 1623 | char ** wlst = (char **) calloc(maxSug, sizeof(char *)); |
michael@0 | 1624 | if (!**wlst) return NULL; |
michael@0 | 1625 | // we will use only the first suggestion |
michael@0 | 1626 | for (int i = 0; i < maxSug - 1; i++) wlst[i] = ""; |
michael@0 | 1627 | int ns = suggest(&wlst, word, maxSug - 1, NULL); |
michael@0 | 1628 | if (ns == maxSug) { |
michael@0 | 1629 | p = suggest_morph(wlst[maxSug - 1]); |
michael@0 | 1630 | free(wlst[maxSug - 1]); |
michael@0 | 1631 | } |
michael@0 | 1632 | if (wlst) free(wlst); |
michael@0 | 1633 | return p; |
michael@0 | 1634 | } |
michael@0 | 1635 | #endif // END OF HUNSPELL_EXPERIMENTAL CODE |
michael@0 | 1636 | |
michael@0 | 1637 | /* affixation */ |
michael@0 | 1638 | char * SuggestMgr::suggest_hentry_gen(hentry * rv, char * pattern) |
michael@0 | 1639 | { |
michael@0 | 1640 | char result[MAXLNLEN]; |
michael@0 | 1641 | *result = '\0'; |
michael@0 | 1642 | int sfxcount = get_sfxcount(pattern); |
michael@0 | 1643 | |
michael@0 | 1644 | if (get_sfxcount(HENTRY_DATA(rv)) > sfxcount) return NULL; |
michael@0 | 1645 | |
michael@0 | 1646 | if (HENTRY_DATA(rv)) { |
michael@0 | 1647 | char * aff = pAMgr->morphgen(HENTRY_WORD(rv), rv->blen, rv->astr, rv->alen, |
michael@0 | 1648 | HENTRY_DATA(rv), pattern, 0); |
michael@0 | 1649 | if (aff) { |
michael@0 | 1650 | mystrcat(result, aff, MAXLNLEN); |
michael@0 | 1651 | mystrcat(result, "\n", MAXLNLEN); |
michael@0 | 1652 | free(aff); |
michael@0 | 1653 | } |
michael@0 | 1654 | } |
michael@0 | 1655 | |
michael@0 | 1656 | // check all allomorphs |
michael@0 | 1657 | char allomorph[MAXLNLEN]; |
michael@0 | 1658 | char * p = NULL; |
michael@0 | 1659 | if (HENTRY_DATA(rv)) p = (char *) strstr(HENTRY_DATA2(rv), MORPH_ALLOMORPH); |
michael@0 | 1660 | while (p) { |
michael@0 | 1661 | struct hentry * rv2 = NULL; |
michael@0 | 1662 | p += MORPH_TAG_LEN; |
michael@0 | 1663 | int plen = fieldlen(p); |
michael@0 | 1664 | strncpy(allomorph, p, plen); |
michael@0 | 1665 | allomorph[plen] = '\0'; |
michael@0 | 1666 | rv2 = pAMgr->lookup(allomorph); |
michael@0 | 1667 | while (rv2) { |
michael@0 | 1668 | // if (HENTRY_DATA(rv2) && get_sfxcount(HENTRY_DATA(rv2)) <= sfxcount) { |
michael@0 | 1669 | if (HENTRY_DATA(rv2)) { |
michael@0 | 1670 | char * st = (char *) strstr(HENTRY_DATA2(rv2), MORPH_STEM); |
michael@0 | 1671 | if (st && (strncmp(st + MORPH_TAG_LEN, |
michael@0 | 1672 | HENTRY_WORD(rv), fieldlen(st + MORPH_TAG_LEN)) == 0)) { |
michael@0 | 1673 | char * aff = pAMgr->morphgen(HENTRY_WORD(rv2), rv2->blen, rv2->astr, rv2->alen, |
michael@0 | 1674 | HENTRY_DATA(rv2), pattern, 0); |
michael@0 | 1675 | if (aff) { |
michael@0 | 1676 | mystrcat(result, aff, MAXLNLEN); |
michael@0 | 1677 | mystrcat(result, "\n", MAXLNLEN); |
michael@0 | 1678 | free(aff); |
michael@0 | 1679 | } |
michael@0 | 1680 | } |
michael@0 | 1681 | } |
michael@0 | 1682 | rv2 = rv2->next_homonym; |
michael@0 | 1683 | } |
michael@0 | 1684 | p = strstr(p + plen, MORPH_ALLOMORPH); |
michael@0 | 1685 | } |
michael@0 | 1686 | |
michael@0 | 1687 | return (*result) ? mystrdup(result) : NULL; |
michael@0 | 1688 | } |
michael@0 | 1689 | |
michael@0 | 1690 | char * SuggestMgr::suggest_gen(char ** desc, int n, char * pattern) { |
michael@0 | 1691 | char result[MAXLNLEN]; |
michael@0 | 1692 | char result2[MAXLNLEN]; |
michael@0 | 1693 | char newpattern[MAXLNLEN]; |
michael@0 | 1694 | *newpattern = '\0'; |
michael@0 | 1695 | if (n == 0) return 0; |
michael@0 | 1696 | *result2 = '\0'; |
michael@0 | 1697 | struct hentry * rv = NULL; |
michael@0 | 1698 | if (!pAMgr) return NULL; |
michael@0 | 1699 | |
michael@0 | 1700 | // search affixed forms with and without derivational suffixes |
michael@0 | 1701 | while(1) { |
michael@0 | 1702 | |
michael@0 | 1703 | for (int k = 0; k < n; k++) { |
michael@0 | 1704 | *result = '\0'; |
michael@0 | 1705 | // add compound word parts (except the last one) |
michael@0 | 1706 | char * s = (char *) desc[k]; |
michael@0 | 1707 | char * part = strstr(s, MORPH_PART); |
michael@0 | 1708 | if (part) { |
michael@0 | 1709 | char * nextpart = strstr(part + 1, MORPH_PART); |
michael@0 | 1710 | while (nextpart) { |
michael@0 | 1711 | copy_field(result + strlen(result), part, MORPH_PART); |
michael@0 | 1712 | part = nextpart; |
michael@0 | 1713 | nextpart = strstr(part + 1, MORPH_PART); |
michael@0 | 1714 | } |
michael@0 | 1715 | s = part; |
michael@0 | 1716 | } |
michael@0 | 1717 | |
michael@0 | 1718 | char **pl; |
michael@0 | 1719 | char tok[MAXLNLEN]; |
michael@0 | 1720 | strcpy(tok, s); |
michael@0 | 1721 | char * alt = strstr(tok, " | "); |
michael@0 | 1722 | while (alt) { |
michael@0 | 1723 | alt[1] = MSEP_ALT; |
michael@0 | 1724 | alt = strstr(alt, " | "); |
michael@0 | 1725 | } |
michael@0 | 1726 | int pln = line_tok(tok, &pl, MSEP_ALT); |
michael@0 | 1727 | for (int i = 0; i < pln; i++) { |
michael@0 | 1728 | // remove inflectional and terminal suffixes |
michael@0 | 1729 | char * is = strstr(pl[i], MORPH_INFL_SFX); |
michael@0 | 1730 | if (is) *is = '\0'; |
michael@0 | 1731 | char * ts = strstr(pl[i], MORPH_TERM_SFX); |
michael@0 | 1732 | while (ts) { |
michael@0 | 1733 | *ts = '_'; |
michael@0 | 1734 | ts = strstr(pl[i], MORPH_TERM_SFX); |
michael@0 | 1735 | } |
michael@0 | 1736 | char * st = strstr(s, MORPH_STEM); |
michael@0 | 1737 | if (st) { |
michael@0 | 1738 | copy_field(tok, st, MORPH_STEM); |
michael@0 | 1739 | rv = pAMgr->lookup(tok); |
michael@0 | 1740 | while (rv) { |
michael@0 | 1741 | char newpat[MAXLNLEN]; |
michael@0 | 1742 | strcpy(newpat, pl[i]); |
michael@0 | 1743 | strcat(newpat, pattern); |
michael@0 | 1744 | char * sg = suggest_hentry_gen(rv, newpat); |
michael@0 | 1745 | if (!sg) sg = suggest_hentry_gen(rv, pattern); |
michael@0 | 1746 | if (sg) { |
michael@0 | 1747 | char ** gen; |
michael@0 | 1748 | int genl = line_tok(sg, &gen, MSEP_REC); |
michael@0 | 1749 | free(sg); |
michael@0 | 1750 | sg = NULL; |
michael@0 | 1751 | for (int j = 0; j < genl; j++) { |
michael@0 | 1752 | if (strstr(pl[i], MORPH_SURF_PFX)) { |
michael@0 | 1753 | int r2l = strlen(result2); |
michael@0 | 1754 | result2[r2l] = MSEP_REC; |
michael@0 | 1755 | strcpy(result2 + r2l + 1, result); |
michael@0 | 1756 | copy_field(result2 + strlen(result2), pl[i], MORPH_SURF_PFX); |
michael@0 | 1757 | mystrcat(result2, gen[j], MAXLNLEN); |
michael@0 | 1758 | } else { |
michael@0 | 1759 | sprintf(result2 + strlen(result2), "%c%s%s", |
michael@0 | 1760 | MSEP_REC, result, gen[j]); |
michael@0 | 1761 | } |
michael@0 | 1762 | } |
michael@0 | 1763 | freelist(&gen, genl); |
michael@0 | 1764 | } |
michael@0 | 1765 | rv = rv->next_homonym; |
michael@0 | 1766 | } |
michael@0 | 1767 | } |
michael@0 | 1768 | } |
michael@0 | 1769 | freelist(&pl, pln); |
michael@0 | 1770 | } |
michael@0 | 1771 | |
michael@0 | 1772 | if (*result2 || !strstr(pattern, MORPH_DERI_SFX)) break; |
michael@0 | 1773 | strcpy(newpattern, pattern); |
michael@0 | 1774 | pattern = newpattern; |
michael@0 | 1775 | char * ds = strstr(pattern, MORPH_DERI_SFX); |
michael@0 | 1776 | while (ds) { |
michael@0 | 1777 | strncpy(ds, MORPH_TERM_SFX, MORPH_TAG_LEN); |
michael@0 | 1778 | ds = strstr(pattern, MORPH_DERI_SFX); |
michael@0 | 1779 | } |
michael@0 | 1780 | } |
michael@0 | 1781 | return (*result2 ? mystrdup(result2) : NULL); |
michael@0 | 1782 | } |
michael@0 | 1783 | |
michael@0 | 1784 | |
michael@0 | 1785 | // generate an n-gram score comparing s1 and s2 |
michael@0 | 1786 | int SuggestMgr::ngram(int n, char * s1, const char * s2, int opt) |
michael@0 | 1787 | { |
michael@0 | 1788 | int nscore = 0; |
michael@0 | 1789 | int ns; |
michael@0 | 1790 | int l1; |
michael@0 | 1791 | int l2; |
michael@0 | 1792 | int test = 0; |
michael@0 | 1793 | |
michael@0 | 1794 | if (utf8) { |
michael@0 | 1795 | w_char su1[MAXSWL]; |
michael@0 | 1796 | w_char su2[MAXSWL]; |
michael@0 | 1797 | l1 = u8_u16(su1, MAXSWL, s1); |
michael@0 | 1798 | l2 = u8_u16(su2, MAXSWL, s2); |
michael@0 | 1799 | if ((l2 <= 0) || (l1 == -1)) return 0; |
michael@0 | 1800 | // lowering dictionary word |
michael@0 | 1801 | if (opt & NGRAM_LOWERING) mkallsmall_utf(su2, l2, langnum); |
michael@0 | 1802 | for (int j = 1; j <= n; j++) { |
michael@0 | 1803 | ns = 0; |
michael@0 | 1804 | for (int i = 0; i <= (l1-j); i++) { |
michael@0 | 1805 | int k = 0; |
michael@0 | 1806 | for (int l = 0; l <= (l2-j); l++) { |
michael@0 | 1807 | for (k = 0; k < j; k++) { |
michael@0 | 1808 | w_char * c1 = su1 + i + k; |
michael@0 | 1809 | w_char * c2 = su2 + l + k; |
michael@0 | 1810 | if ((c1->l != c2->l) || (c1->h != c2->h)) break; |
michael@0 | 1811 | } |
michael@0 | 1812 | if (k == j) { |
michael@0 | 1813 | ns++; |
michael@0 | 1814 | break; |
michael@0 | 1815 | } |
michael@0 | 1816 | } |
michael@0 | 1817 | if (k != j && opt & NGRAM_WEIGHTED) { |
michael@0 | 1818 | ns--; |
michael@0 | 1819 | test++; |
michael@0 | 1820 | if (i == 0 || i == l1-j) ns--; // side weight |
michael@0 | 1821 | } |
michael@0 | 1822 | } |
michael@0 | 1823 | nscore = nscore + ns; |
michael@0 | 1824 | if (ns < 2 && !(opt & NGRAM_WEIGHTED)) break; |
michael@0 | 1825 | } |
michael@0 | 1826 | } else { |
michael@0 | 1827 | l2 = strlen(s2); |
michael@0 | 1828 | if (l2 == 0) return 0; |
michael@0 | 1829 | l1 = strlen(s1); |
michael@0 | 1830 | char *t = mystrdup(s2); |
michael@0 | 1831 | if (opt & NGRAM_LOWERING) mkallsmall(t, csconv); |
michael@0 | 1832 | for (int j = 1; j <= n; j++) { |
michael@0 | 1833 | ns = 0; |
michael@0 | 1834 | for (int i = 0; i <= (l1-j); i++) { |
michael@0 | 1835 | char c = *(s1 + i + j); |
michael@0 | 1836 | *(s1 + i + j) = '\0'; |
michael@0 | 1837 | if (strstr(t,(s1+i))) { |
michael@0 | 1838 | ns++; |
michael@0 | 1839 | } else if (opt & NGRAM_WEIGHTED) { |
michael@0 | 1840 | ns--; |
michael@0 | 1841 | test++; |
michael@0 | 1842 | if (i == 0 || i == l1-j) ns--; // side weight |
michael@0 | 1843 | } |
michael@0 | 1844 | *(s1 + i + j ) = c; |
michael@0 | 1845 | } |
michael@0 | 1846 | nscore = nscore + ns; |
michael@0 | 1847 | if (ns < 2 && !(opt & NGRAM_WEIGHTED)) break; |
michael@0 | 1848 | } |
michael@0 | 1849 | free(t); |
michael@0 | 1850 | } |
michael@0 | 1851 | |
michael@0 | 1852 | ns = 0; |
michael@0 | 1853 | if (opt & NGRAM_LONGER_WORSE) ns = (l2-l1)-2; |
michael@0 | 1854 | if (opt & NGRAM_ANY_MISMATCH) ns = abs(l2-l1)-2; |
michael@0 | 1855 | ns = (nscore - ((ns > 0) ? ns : 0)); |
michael@0 | 1856 | return ns; |
michael@0 | 1857 | } |
michael@0 | 1858 | |
michael@0 | 1859 | // length of the left common substring of s1 and (decapitalised) s2 |
michael@0 | 1860 | int SuggestMgr::leftcommonsubstring(char * s1, const char * s2) { |
michael@0 | 1861 | if (utf8) { |
michael@0 | 1862 | w_char su1[MAXSWL]; |
michael@0 | 1863 | w_char su2[MAXSWL]; |
michael@0 | 1864 | su1[0].l = su2[0].l = su1[0].h = su2[0].h = 0; |
michael@0 | 1865 | // decapitalize dictionary word |
michael@0 | 1866 | if (complexprefixes) { |
michael@0 | 1867 | int l1 = u8_u16(su1, MAXSWL, s1); |
michael@0 | 1868 | int l2 = u8_u16(su2, MAXSWL, s2); |
michael@0 | 1869 | if (*((short *)su1+l1-1) == *((short *)su2+l2-1)) return 1; |
michael@0 | 1870 | } else { |
michael@0 | 1871 | int i; |
michael@0 | 1872 | u8_u16(su1, 1, s1); |
michael@0 | 1873 | u8_u16(su2, 1, s2); |
michael@0 | 1874 | unsigned short idx = (su2->h << 8) + su2->l; |
michael@0 | 1875 | unsigned short otheridx = (su1->h << 8) + su1->l; |
michael@0 | 1876 | if (otheridx != idx && |
michael@0 | 1877 | (otheridx != unicodetolower(idx, langnum))) return 0; |
michael@0 | 1878 | int l1 = u8_u16(su1, MAXSWL, s1); |
michael@0 | 1879 | int l2 = u8_u16(su2, MAXSWL, s2); |
michael@0 | 1880 | for(i = 1; (i < l1) && (i < l2) && |
michael@0 | 1881 | (su1[i].l == su2[i].l) && (su1[i].h == su2[i].h); i++); |
michael@0 | 1882 | return i; |
michael@0 | 1883 | } |
michael@0 | 1884 | } else { |
michael@0 | 1885 | if (complexprefixes) { |
michael@0 | 1886 | int l1 = strlen(s1); |
michael@0 | 1887 | int l2 = strlen(s2); |
michael@0 | 1888 | if (*(s2+l1-1) == *(s2+l2-1)) return 1; |
michael@0 | 1889 | } else { |
michael@0 | 1890 | char * olds = s1; |
michael@0 | 1891 | // decapitalise dictionary word |
michael@0 | 1892 | if ((*s1 != *s2) && (*s1 != csconv[((unsigned char)*s2)].clower)) return 0; |
michael@0 | 1893 | do { |
michael@0 | 1894 | s1++; s2++; |
michael@0 | 1895 | } while ((*s1 == *s2) && (*s1 != '\0')); |
michael@0 | 1896 | return (int)(s1 - olds); |
michael@0 | 1897 | } |
michael@0 | 1898 | } |
michael@0 | 1899 | return 0; |
michael@0 | 1900 | } |
michael@0 | 1901 | |
michael@0 | 1902 | int SuggestMgr::commoncharacterpositions(char * s1, const char * s2, int * is_swap) { |
michael@0 | 1903 | int num = 0; |
michael@0 | 1904 | int diff = 0; |
michael@0 | 1905 | int diffpos[2]; |
michael@0 | 1906 | *is_swap = 0; |
michael@0 | 1907 | if (utf8) { |
michael@0 | 1908 | w_char su1[MAXSWL]; |
michael@0 | 1909 | w_char su2[MAXSWL]; |
michael@0 | 1910 | int l1 = u8_u16(su1, MAXSWL, s1); |
michael@0 | 1911 | int l2 = u8_u16(su2, MAXSWL, s2); |
michael@0 | 1912 | // decapitalize dictionary word |
michael@0 | 1913 | if (complexprefixes) { |
michael@0 | 1914 | mkallsmall_utf(su2+l2-1, 1, langnum); |
michael@0 | 1915 | } else { |
michael@0 | 1916 | mkallsmall_utf(su2, 1, langnum); |
michael@0 | 1917 | } |
michael@0 | 1918 | for (int i = 0; (i < l1) && (i < l2); i++) { |
michael@0 | 1919 | if (((short *) su1)[i] == ((short *) su2)[i]) { |
michael@0 | 1920 | num++; |
michael@0 | 1921 | } else { |
michael@0 | 1922 | if (diff < 2) diffpos[diff] = i; |
michael@0 | 1923 | diff++; |
michael@0 | 1924 | } |
michael@0 | 1925 | } |
michael@0 | 1926 | if ((diff == 2) && (l1 == l2) && |
michael@0 | 1927 | (((short *) su1)[diffpos[0]] == ((short *) su2)[diffpos[1]]) && |
michael@0 | 1928 | (((short *) su1)[diffpos[1]] == ((short *) su2)[diffpos[0]])) *is_swap = 1; |
michael@0 | 1929 | } else { |
michael@0 | 1930 | int i; |
michael@0 | 1931 | char t[MAXSWUTF8L]; |
michael@0 | 1932 | strcpy(t, s2); |
michael@0 | 1933 | // decapitalize dictionary word |
michael@0 | 1934 | if (complexprefixes) { |
michael@0 | 1935 | int l2 = strlen(t); |
michael@0 | 1936 | *(t+l2-1) = csconv[((unsigned char)*(t+l2-1))].clower; |
michael@0 | 1937 | } else { |
michael@0 | 1938 | mkallsmall(t, csconv); |
michael@0 | 1939 | } |
michael@0 | 1940 | for (i = 0; (*(s1+i) != 0) && (*(t+i) != 0); i++) { |
michael@0 | 1941 | if (*(s1+i) == *(t+i)) { |
michael@0 | 1942 | num++; |
michael@0 | 1943 | } else { |
michael@0 | 1944 | if (diff < 2) diffpos[diff] = i; |
michael@0 | 1945 | diff++; |
michael@0 | 1946 | } |
michael@0 | 1947 | } |
michael@0 | 1948 | if ((diff == 2) && (*(s1+i) == 0) && (*(t+i) == 0) && |
michael@0 | 1949 | (*(s1+diffpos[0]) == *(t+diffpos[1])) && |
michael@0 | 1950 | (*(s1+diffpos[1]) == *(t+diffpos[0]))) *is_swap = 1; |
michael@0 | 1951 | } |
michael@0 | 1952 | return num; |
michael@0 | 1953 | } |
michael@0 | 1954 | |
michael@0 | 1955 | int SuggestMgr::mystrlen(const char * word) { |
michael@0 | 1956 | if (utf8) { |
michael@0 | 1957 | w_char w[MAXSWL]; |
michael@0 | 1958 | return u8_u16(w, MAXSWL, word); |
michael@0 | 1959 | } else return strlen(word); |
michael@0 | 1960 | } |
michael@0 | 1961 | |
michael@0 | 1962 | // sort in decreasing order of score |
michael@0 | 1963 | void SuggestMgr::bubblesort(char** rword, char** rword2, int* rsc, int n ) |
michael@0 | 1964 | { |
michael@0 | 1965 | int m = 1; |
michael@0 | 1966 | while (m < n) { |
michael@0 | 1967 | int j = m; |
michael@0 | 1968 | while (j > 0) { |
michael@0 | 1969 | if (rsc[j-1] < rsc[j]) { |
michael@0 | 1970 | int sctmp = rsc[j-1]; |
michael@0 | 1971 | char * wdtmp = rword[j-1]; |
michael@0 | 1972 | rsc[j-1] = rsc[j]; |
michael@0 | 1973 | rword[j-1] = rword[j]; |
michael@0 | 1974 | rsc[j] = sctmp; |
michael@0 | 1975 | rword[j] = wdtmp; |
michael@0 | 1976 | if (rword2) { |
michael@0 | 1977 | wdtmp = rword2[j-1]; |
michael@0 | 1978 | rword2[j-1] = rword2[j]; |
michael@0 | 1979 | rword2[j] = wdtmp; |
michael@0 | 1980 | } |
michael@0 | 1981 | j--; |
michael@0 | 1982 | } else break; |
michael@0 | 1983 | } |
michael@0 | 1984 | m++; |
michael@0 | 1985 | } |
michael@0 | 1986 | return; |
michael@0 | 1987 | } |
michael@0 | 1988 | |
michael@0 | 1989 | // longest common subsequence |
michael@0 | 1990 | void SuggestMgr::lcs(const char * s, const char * s2, int * l1, int * l2, char ** result) { |
michael@0 | 1991 | int n, m; |
michael@0 | 1992 | w_char su[MAXSWL]; |
michael@0 | 1993 | w_char su2[MAXSWL]; |
michael@0 | 1994 | char * b; |
michael@0 | 1995 | char * c; |
michael@0 | 1996 | int i; |
michael@0 | 1997 | int j; |
michael@0 | 1998 | if (utf8) { |
michael@0 | 1999 | m = u8_u16(su, MAXSWL, s); |
michael@0 | 2000 | n = u8_u16(su2, MAXSWL, s2); |
michael@0 | 2001 | } else { |
michael@0 | 2002 | m = strlen(s); |
michael@0 | 2003 | n = strlen(s2); |
michael@0 | 2004 | } |
michael@0 | 2005 | c = (char *) malloc((m + 1) * (n + 1)); |
michael@0 | 2006 | b = (char *) malloc((m + 1) * (n + 1)); |
michael@0 | 2007 | if (!c || !b) { |
michael@0 | 2008 | if (c) free(c); |
michael@0 | 2009 | if (b) free(b); |
michael@0 | 2010 | *result = NULL; |
michael@0 | 2011 | return; |
michael@0 | 2012 | } |
michael@0 | 2013 | for (i = 1; i <= m; i++) c[i*(n+1)] = 0; |
michael@0 | 2014 | for (j = 0; j <= n; j++) c[j] = 0; |
michael@0 | 2015 | for (i = 1; i <= m; i++) { |
michael@0 | 2016 | for (j = 1; j <= n; j++) { |
michael@0 | 2017 | if ( ((utf8) && (*((short *) su+i-1) == *((short *)su2+j-1))) |
michael@0 | 2018 | || ((!utf8) && ((*(s+i-1)) == (*(s2+j-1))))) { |
michael@0 | 2019 | c[i*(n+1) + j] = c[(i-1)*(n+1) + j-1]+1; |
michael@0 | 2020 | b[i*(n+1) + j] = LCS_UPLEFT; |
michael@0 | 2021 | } else if (c[(i-1)*(n+1) + j] >= c[i*(n+1) + j-1]) { |
michael@0 | 2022 | c[i*(n+1) + j] = c[(i-1)*(n+1) + j]; |
michael@0 | 2023 | b[i*(n+1) + j] = LCS_UP; |
michael@0 | 2024 | } else { |
michael@0 | 2025 | c[i*(n+1) + j] = c[i*(n+1) + j-1]; |
michael@0 | 2026 | b[i*(n+1) + j] = LCS_LEFT; |
michael@0 | 2027 | } |
michael@0 | 2028 | } |
michael@0 | 2029 | } |
michael@0 | 2030 | *result = b; |
michael@0 | 2031 | free(c); |
michael@0 | 2032 | *l1 = m; |
michael@0 | 2033 | *l2 = n; |
michael@0 | 2034 | } |
michael@0 | 2035 | |
michael@0 | 2036 | int SuggestMgr::lcslen(const char * s, const char* s2) { |
michael@0 | 2037 | int m; |
michael@0 | 2038 | int n; |
michael@0 | 2039 | int i; |
michael@0 | 2040 | int j; |
michael@0 | 2041 | char * result; |
michael@0 | 2042 | int len = 0; |
michael@0 | 2043 | lcs(s, s2, &m, &n, &result); |
michael@0 | 2044 | if (!result) return 0; |
michael@0 | 2045 | i = m; |
michael@0 | 2046 | j = n; |
michael@0 | 2047 | while ((i != 0) && (j != 0)) { |
michael@0 | 2048 | if (result[i*(n+1) + j] == LCS_UPLEFT) { |
michael@0 | 2049 | len++; |
michael@0 | 2050 | i--; |
michael@0 | 2051 | j--; |
michael@0 | 2052 | } else if (result[i*(n+1) + j] == LCS_UP) { |
michael@0 | 2053 | i--; |
michael@0 | 2054 | } else j--; |
michael@0 | 2055 | } |
michael@0 | 2056 | free(result); |
michael@0 | 2057 | return len; |
michael@0 | 2058 | } |