~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/contrib/SpellChecker/hunspell/src/hunspell/suggestmgr.cxx

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

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