00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 #include <stdlib.h>
00224 #include <string.h>
00225 #include <ctype.h>
00226 #include "ispell_checker.h"
00227 #include "msgs.h"
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244 int
00245 ISpellChecker::casecmp (char *a, char *b, int canonical)
00246 {
00247 register ichar_t * ap;
00248 register ichar_t * bp;
00249 ichar_t inta[INPUTWORDLEN + 4 * MAXAFFIXLEN + 4];
00250 ichar_t intb[INPUTWORDLEN + 4 * MAXAFFIXLEN + 4];
00251
00252 strtoichar (inta, a, sizeof inta, canonical);
00253 strtoichar (intb, b, sizeof intb, canonical);
00254 for (ap = inta, bp = intb; *ap != 0; ap++, bp++)
00255 {
00256 if (*ap != *bp)
00257 {
00258 if (*bp == '\0')
00259 return m_hashheader.sortorder[*ap];
00260 else if (mylower (*ap))
00261 {
00262 if (mylower (*bp) || mytoupper (*ap) != *bp)
00263 return static_cast<int>(m_hashheader.sortorder[*ap])
00264 - static_cast<int>(m_hashheader.sortorder[*bp]);
00265 }
00266 else
00267 {
00268 if (myupper (*bp) || mytolower (*ap) != *bp)
00269 return static_cast<int>(m_hashheader.sortorder[*ap])
00270 - static_cast<int>(m_hashheader.sortorder[*bp]);
00271 }
00272 }
00273 }
00274 if (*bp != '\0')
00275 return -static_cast<int>(m_hashheader.sortorder[*bp]);
00276 for (ap = inta, bp = intb; *ap; ap++, bp++)
00277 {
00278 if (*ap != *bp)
00279 {
00280 return static_cast<int>(m_hashheader.sortorder[*ap])
00281 - static_cast<int>(m_hashheader.sortorder[*bp]);
00282 }
00283 }
00284 return 0;
00285 }
00286
00287
00288
00289
00290 void
00291 ISpellChecker::makepossibilities (ichar_t *word)
00292 {
00293 register int i;
00294
00295 for (i = 0; i < MAXPOSSIBLE; i++)
00296 m_possibilities[i][0] = 0;
00297 m_pcount = 0;
00298 m_maxposslen = 0;
00299 m_easypossibilities = 0;
00300
00301 #ifndef NO_CAPITALIZATION_SUPPORT
00302 wrongcapital (word);
00303 #endif
00304
00305
00306
00307
00308
00309
00310
00311
00312 if (m_pcount < MAXPOSSIBLE)
00313 missingletter (word);
00314 if (m_pcount < MAXPOSSIBLE)
00315 transposedletter (word);
00316 if (m_pcount < MAXPOSSIBLE)
00317 extraletter (word);
00318 if (m_pcount < MAXPOSSIBLE)
00319 wrongletter (word);
00320
00321 if ((m_hashheader.compoundflag != COMPOUND_ANYTIME) &&
00322 m_pcount < MAXPOSSIBLE)
00323 missingspace (word);
00324
00325 }
00326
00327
00328
00329
00330
00331
00332 int
00333 ISpellChecker::insert (ichar_t *word)
00334 {
00335 register int i;
00336 register char * realword;
00337
00338 realword = ichartosstr (word, 0);
00339 for (i = 0; i < m_pcount; i++)
00340 {
00341 if (strcmp (m_possibilities[i], realword) == 0)
00342 return (0);
00343 }
00344
00345 strcpy (m_possibilities[m_pcount++], realword);
00346 i = strlen (realword);
00347 if (i > m_maxposslen)
00348 m_maxposslen = i;
00349 if (m_pcount >= MAXPOSSIBLE)
00350 return (-1);
00351 else
00352 return (0);
00353 }
00354
00355 #ifndef NO_CAPITALIZATION_SUPPORT
00356
00357
00358
00359 void
00360 ISpellChecker::wrongcapital (ichar_t *word)
00361 {
00362 ichar_t newword[INPUTWORDLEN + MAXAFFIXLEN];
00363
00364
00365
00366
00367
00368
00369 if (good (word, 0, 1, 0, 0))
00370 {
00371 icharcpy (newword, word);
00372 upcase (newword);
00373 ins_cap (newword, word);
00374 }
00375 }
00376 #endif
00377
00378
00379
00380
00381 void
00382 ISpellChecker::wrongletter (ichar_t *word)
00383 {
00384 register int i;
00385 register int j;
00386 register int n;
00387 ichar_t savechar;
00388 ichar_t newword[INPUTWORDLEN + MAXAFFIXLEN];
00389
00390 n = icharlen (word);
00391 icharcpy (newword, word);
00392 #ifndef NO_CAPITALIZATION_SUPPORT
00393 upcase (newword);
00394 #endif
00395
00396 for (i = 0; i < n; i++)
00397 {
00398 savechar = newword[i];
00399 for (j=0; j < m_Trynum; ++j)
00400 {
00401 if (m_Try[j] == savechar)
00402 continue;
00403 else if (isboundarych (m_Try[j]) && (i == 0 || i == n - 1))
00404 continue;
00405 newword[i] = m_Try[j];
00406 if (good (newword, 0, 1, 0, 0))
00407 {
00408 if (ins_cap (newword, word) < 0)
00409 return;
00410 }
00411 }
00412 newword[i] = savechar;
00413 }
00414 }
00415
00416
00417
00418
00419 void
00420 ISpellChecker::extraletter (ichar_t *word)
00421 {
00422 ichar_t newword[INPUTWORDLEN + MAXAFFIXLEN];
00423 register ichar_t * p;
00424 register ichar_t * r;
00425
00426 if (icharlen (word) < 2)
00427 return;
00428
00429 icharcpy (newword, word + 1);
00430 for (p = word, r = newword; *p != 0; )
00431 {
00432 if (good (newword, 0, 1, 0, 0))
00433 {
00434 if (ins_cap (newword, word) < 0)
00435 return;
00436 }
00437 *r++ = *p++;
00438 }
00439 }
00440
00441
00442
00443
00444 void
00445 ISpellChecker::missingletter (ichar_t *word)
00446 {
00447 ichar_t newword[INPUTWORDLEN + MAXAFFIXLEN + 1];
00448 register ichar_t * p;
00449 register ichar_t * r;
00450 register int i;
00451
00452 icharcpy (newword + 1, word);
00453 for (p = word, r = newword; *p != 0; )
00454 {
00455 for (i = 0; i < m_Trynum; i++)
00456 {
00457 if (isboundarych (m_Try[i]) && r == newword)
00458 continue;
00459 *r = m_Try[i];
00460 if (good (newword, 0, 1, 0, 0))
00461 {
00462 if (ins_cap (newword, word) < 0)
00463 return;
00464 }
00465 }
00466 *r++ = *p++;
00467 }
00468 for (i = 0; i < m_Trynum; i++)
00469 {
00470 if (isboundarych (m_Try[i]))
00471 continue;
00472 *r = m_Try[i];
00473 if (good (newword, 0, 1, 0, 0))
00474 {
00475 if (ins_cap (newword, word) < 0)
00476 return;
00477 }
00478 }
00479 }
00480
00481
00482
00483
00484 void ISpellChecker::missingspace (ichar_t *word)
00485 {
00486 ichar_t firsthalf[MAX_CAPS][INPUTWORDLEN + MAXAFFIXLEN];
00487 int firstno;
00488 ichar_t * firstp;
00489 ichar_t newword[INPUTWORDLEN + MAXAFFIXLEN + 1];
00490 int nfirsthalf;
00491 int nsecondhalf;
00492 register ichar_t * p;
00493 ichar_t secondhalf[MAX_CAPS][INPUTWORDLEN + MAXAFFIXLEN];
00494 int secondno;
00495
00496
00497
00498
00499
00500
00501
00502 nfirsthalf = icharlen (word);
00503 if (nfirsthalf < 3 || nfirsthalf >= INPUTWORDLEN + MAXAFFIXLEN - 1)
00504 return;
00505 icharcpy (newword + 1, word);
00506 for (p = newword + 1; p[1] != '\0'; p++)
00507 {
00508 p[-1] = *p;
00509 *p = '\0';
00510 if (good (newword, 0, 1, 0, 0))
00511 {
00512
00513
00514
00515
00516
00517
00518
00519 nfirsthalf = save_cap (newword, word, firsthalf);
00520 if (good (p + 1, 0, 1, 0, 0))
00521 {
00522 nsecondhalf = save_cap (p + 1, p + 1, secondhalf);
00523 for (firstno = 0; firstno < nfirsthalf; firstno++)
00524 {
00525 firstp = &firsthalf[firstno][p - newword];
00526 for (secondno = 0; secondno < nsecondhalf; secondno++)
00527 {
00528 *firstp = ' ';
00529 icharcpy (firstp + 1, secondhalf[secondno]);
00530 if (insert (firsthalf[firstno]) < 0)
00531 return;
00532 *firstp = '-';
00533 if (insert (firsthalf[firstno]) < 0)
00534 return;
00535 }
00536 }
00537 }
00538 }
00539 }
00540 }
00541
00542
00543
00544
00545
00546 int
00547 ISpellChecker::compoundgood (ichar_t *word, int pfxopts)
00548 {
00549 ichar_t newword[INPUTWORDLEN + MAXAFFIXLEN];
00550 register ichar_t * p;
00551 register ichar_t savech;
00552 long secondcap;
00553
00554
00555
00556
00557 if (m_hashheader.compoundflag == COMPOUND_NEVER)
00558 return 0;
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570 if (icharlen (word) < 2 * m_hashheader.compoundmin)
00571 return 0;
00572 icharcpy (newword, word);
00573 p = newword + m_hashheader.compoundmin;
00574 for ( ; p[m_hashheader.compoundmin - 1] != 0; p++)
00575 {
00576 savech = *p;
00577 *p = 0;
00578 if (good (newword, 0, 0, pfxopts, FF_COMPOUNDONLY))
00579 {
00580 *p = savech;
00581 if (good (p, 0, 1, FF_COMPOUNDONLY, 0)
00582 || compoundgood (p, FF_COMPOUNDONLY))
00583 {
00584 secondcap = whatcap (p);
00585 switch (whatcap (newword))
00586 {
00587 case ANYCASE:
00588 case CAPITALIZED:
00589 case FOLLOWCASE:
00590 return secondcap == ANYCASE;
00591 case ALLCAPS:
00592 return secondcap == ALLCAPS;
00593 }
00594 }
00595 }
00596 else
00597 *p = savech;
00598 }
00599 return 0;
00600 }
00601
00602
00603
00604
00605 void
00606 ISpellChecker::transposedletter (ichar_t *word)
00607 {
00608 ichar_t newword[INPUTWORDLEN + MAXAFFIXLEN];
00609 register ichar_t * p;
00610 register ichar_t temp;
00611
00612 icharcpy (newword, word);
00613 for (p = newword; p[1] != 0; p++)
00614 {
00615 temp = *p;
00616 *p = p[1];
00617 p[1] = temp;
00618 if (good (newword, 0, 1, 0, 0))
00619 {
00620 if (ins_cap (newword, word) < 0)
00621 return;
00622 }
00623 temp = *p;
00624 *p = p[1];
00625 p[1] = temp;
00626 }
00627 }
00628
00637 int
00638 ISpellChecker::ins_cap (ichar_t *word, ichar_t *pattern)
00639 {
00640 int i;
00641 int nsaved;
00642 ichar_t savearea[MAX_CAPS][INPUTWORDLEN + MAXAFFIXLEN];
00643
00644 nsaved = save_cap (word, pattern, savearea);
00645 for (i = 0; i < nsaved; i++)
00646 {
00647 if (insert (savearea[i]) < 0)
00648 return -1;
00649 }
00650 return 0;
00651 }
00652
00662 int
00663 ISpellChecker::save_cap (ichar_t *word, ichar_t *pattern,
00664 ichar_t savearea[MAX_CAPS][INPUTWORDLEN + MAXAFFIXLEN])
00665 {
00666 int hitno;
00667 int nsaved;
00668 int preadd;
00669 int prestrip;
00670 int sufadd;
00671 int sufstrip;
00672
00673 if (*word == 0)
00674 return 0;
00675
00676 for (hitno = m_numhits, nsaved = 0; --hitno >= 0 && nsaved < MAX_CAPS; )
00677 {
00678 if (m_hits[hitno].prefix)
00679 {
00680 prestrip = m_hits[hitno].prefix->stripl;
00681 preadd = m_hits[hitno].prefix->affl;
00682 }
00683 else
00684 prestrip = preadd = 0;
00685 if (m_hits[hitno].suffix)
00686 {
00687 sufstrip = m_hits[hitno].suffix->stripl;
00688 sufadd = m_hits[hitno].suffix->affl;
00689 }
00690 else
00691 sufadd = sufstrip = 0;
00692 save_root_cap (word, pattern, prestrip, preadd,
00693 sufstrip, sufadd,
00694 m_hits[hitno].dictent, m_hits[hitno].prefix, m_hits[hitno].suffix,
00695 savearea, &nsaved);
00696 }
00697 return nsaved;
00698 }
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713 int
00714 ISpellChecker::ins_root_cap (ichar_t *word, ichar_t *pattern,
00715 int prestrip, int preadd, int sufstrip, int sufadd,
00716 struct dent *firstdent, struct flagent *pfxent, struct flagent *sufent)
00717 {
00718 int i;
00719 ichar_t savearea[MAX_CAPS][INPUTWORDLEN + MAXAFFIXLEN];
00720 int nsaved;
00721
00722 nsaved = 0;
00723 save_root_cap (word, pattern, prestrip, preadd, sufstrip, sufadd,
00724 firstdent, pfxent, sufent, savearea, &nsaved);
00725 for (i = 0; i < nsaved; i++)
00726 {
00727 if (insert (savearea[i]) < 0)
00728 return -1;
00729 }
00730 return 0;
00731 }
00732
00733
00747 void
00748 ISpellChecker::save_root_cap (ichar_t *word, ichar_t *pattern,
00749 int prestrip, int preadd, int sufstrip, int sufadd,
00750 struct dent *firstdent, struct flagent *pfxent, struct flagent *sufent,
00751 ichar_t savearea[MAX_CAPS][INPUTWORDLEN + MAXAFFIXLEN],
00752 int * nsaved)
00753 {
00754 #ifndef NO_CAPITALIZATION_SUPPORT
00755 register struct dent * dent;
00756 #endif
00757 int firstisupper;
00758 ichar_t newword[INPUTWORDLEN + 4 * MAXAFFIXLEN + 4];
00759 #ifndef NO_CAPITALIZATION_SUPPORT
00760 register ichar_t * p;
00761 int len;
00762 int i;
00763 int limit;
00764 #endif
00765
00766 if (*nsaved >= MAX_CAPS)
00767 return;
00768 icharcpy (newword, word);
00769 firstisupper = myupper (pattern[0]);
00770 #ifdef NO_CAPITALIZATION_SUPPORT
00771
00772
00773
00774 if (firstisupper)
00775 {
00776 if (myupper (pattern[1]))
00777 upcase (newword);
00778 else
00779 {
00780 lowcase (newword);
00781 newword[0] = mytoupper (newword[0]);
00782 }
00783 }
00784 else
00785 lowcase (newword);
00786 icharcpy (savearea[*nsaved], newword);
00787 (*nsaved)++;
00788 return;
00789 #else
00790 #define flagsareok(dent) \
00791 ((pfxent == NULL \
00792 || TSTMASKBIT (dent->mask, pfxent->flagbit)) \
00793 && (sufent == NULL \
00794 || TSTMASKBIT (dent->mask, sufent->flagbit)))
00795
00796 dent = firstdent;
00797 if ((dent->flagfield & (CAPTYPEMASK | MOREVARIANTS)) == ALLCAPS)
00798 {
00799 upcase (newword);
00800 icharcpy (savearea[*nsaved], newword);
00801 (*nsaved)++;
00802 return;
00803 }
00804 for (p = pattern; *p; p++)
00805 {
00806 if (mylower (*p))
00807 break;
00808 }
00809 if (*p == 0)
00810 {
00811 upcase (newword);
00812 icharcpy (savearea[*nsaved], newword);
00813 (*nsaved)++;
00814 return;
00815 }
00816 for (p = pattern + 1; *p; p++)
00817 {
00818 if (myupper (*p))
00819 break;
00820 }
00821 if (*p == 0)
00822 {
00823
00824
00825
00826
00827 if (firstisupper)
00828 {
00829 if (captype (dent->flagfield) == CAPITALIZED
00830 || captype (dent->flagfield) == ANYCASE)
00831 {
00832 lowcase (newword);
00833 newword[0] = mytoupper (newword[0]);
00834 icharcpy (savearea[*nsaved], newword);
00835 (*nsaved)++;
00836 return;
00837 }
00838 }
00839 else
00840 {
00841 if (captype (dent->flagfield) == ANYCASE)
00842 {
00843 lowcase (newword);
00844 icharcpy (savearea[*nsaved], newword);
00845 (*nsaved)++;
00846 return;
00847 }
00848 }
00849 while (dent->flagfield & MOREVARIANTS)
00850 {
00851 dent = dent->next;
00852 if (captype (dent->flagfield) == FOLLOWCASE
00853 || !flagsareok (dent))
00854 continue;
00855 if (firstisupper)
00856 {
00857 if (captype (dent->flagfield) == CAPITALIZED)
00858 {
00859 lowcase (newword);
00860 newword[0] = mytoupper (newword[0]);
00861 icharcpy (savearea[*nsaved], newword);
00862 (*nsaved)++;
00863 return;
00864 }
00865 }
00866 else
00867 {
00868 if (captype (dent->flagfield) == ANYCASE)
00869 {
00870 lowcase (newword);
00871 icharcpy (savearea[*nsaved], newword);
00872 (*nsaved)++;
00873 return;
00874 }
00875 }
00876 }
00877 }
00878
00879
00880
00881
00882
00883
00884
00885 dent = firstdent;
00886 p = strtosichar (dent->word, 1);
00887 len = icharlen (p);
00888 if (dent->flagfield & MOREVARIANTS)
00889 dent = dent->next;
00890 for ( ; ; )
00891 {
00892 if (flagsareok (dent))
00893 {
00894 if (captype (dent->flagfield) != FOLLOWCASE)
00895 {
00896 lowcase (newword);
00897 if (firstisupper || captype (dent->flagfield) == CAPITALIZED)
00898 newword[0] = mytoupper (newword[0]);
00899 icharcpy (savearea[*nsaved], newword);
00900 (*nsaved)++;
00901 if (*nsaved >= MAX_CAPS)
00902 return;
00903 }
00904 else
00905 {
00906
00907 p = strtosichar (dent->word, 1);
00908 memmove (
00909 reinterpret_cast<char *>(newword + preadd),
00910 reinterpret_cast<char *>(p + prestrip),
00911 (len - prestrip - sufstrip) * sizeof (ichar_t));
00912 if (myupper (p[prestrip]))
00913 {
00914 for (i = 0; i < preadd; i++)
00915 newword[i] = mytoupper (newword[i]);
00916 }
00917 else
00918 {
00919 for (i = 0; i < preadd; i++)
00920 newword[i] = mytolower (newword[i]);
00921 }
00922 limit = len + preadd + sufadd - prestrip - sufstrip;
00923 i = len + preadd - prestrip - sufstrip;
00924 p += len - sufstrip - 1;
00925 if (myupper (*p))
00926 {
00927 for (p = newword + i; i < limit; i++, p++)
00928 *p = mytoupper (*p);
00929 }
00930 else
00931 {
00932 for (p = newword + i; i < limit; i++, p++)
00933 *p = mytolower (*p);
00934 }
00935 icharcpy (savearea[*nsaved], newword);
00936 (*nsaved)++;
00937 if (*nsaved >= MAX_CAPS)
00938 return;
00939 }
00940 }
00941 if ((dent->flagfield & MOREVARIANTS) == 0)
00942 break;
00943 dent = dent->next;
00944 }
00945 return;
00946 #endif
00947 }
00948
00949