#include "license.readme" #include #include #include #include #include "suggestmgr.hxx" extern char * mystrdup(const char *); SuggestMgr::SuggestMgr(const char * tryme, int maxn, AffixMgr * aptr) { // register affix manager and check in string of chars to // try when building candidate suggestions pAMgr = aptr; ctry = mystrdup(tryme); ctryl = 0; if (ctry) ctryl = strlen(ctry); maxSug = maxn; } SuggestMgr::~SuggestMgr() { pAMgr = NULL; if (ctry) free(ctry); ctry = NULL; ctryl = 0; maxSug = 0; } // generate suggestions for a mispelled word // pass in address of array of char * pointers int SuggestMgr::suggest(char*** slst, const char * word) { int nsug = 0; char ** wlst = (char **) calloc(maxSug, sizeof(char *)); if (wlst == NULL) return -1; // perhaps we made a typical fault of spelling nsug = replchars(wlst, word, nsug); // did we forget to add a char if ((nsug < maxSug) && (nsug > -1)) nsug = forgotchar(wlst, word, nsug); // did we swap the order of chars by mistake if ((nsug < maxSug) && (nsug > -1)) nsug = swapchar(wlst, word, nsug); // did we add a char that should not be there if ((nsug < maxSug) && (nsug > -1)) nsug = extrachar(wlst, word, nsug); // did we just hit the wrong key in place of a good char if ((nsug < maxSug) && (nsug > -1)) nsug = badchar(wlst, word, nsug); // perhaps we forgot to hit space and two words ran together if ((nsug < maxSug) && (nsug > -1)) nsug = twowords(wlst, word, nsug); if (nsug < 0) { for (int i=0;iget_numrep(); struct replentry* reptable = pAMgr->get_reptable(); if (reptable==NULL) return 0; for (int i=0; i < numrep; i++ ) { r = word; lenr = strlen(reptable[i].replacement); lenp = strlen(reptable[i].pattern); // search every occurence of the pattern in the word while ((r=strstr(r, reptable[i].pattern)) != NULL) { strcpy(candidate, word); if (r-word + lenr + strlen(r+lenp) >= MAXSWL) break; strcpy(candidate+(r-word),reptable[i].replacement); strcpy(candidate+(r-word)+lenr, r+lenp); cwrd = 1; for (int k=0; k < ns; k++) if (strcmp(candidate,wlst[k]) == 0) cwrd = 0; if ((cwrd) && check(candidate,strlen(candidate))) { if (ns < maxSug) { wlst[ns] = mystrdup(candidate); if (wlst[ns] == NULL) { for (int j=0; jlookup(word); if (rv == NULL) rv = pAMgr->affix_check(word,len); } if (rv) return 1; return 0; }