// tword.c // // by John D. de Boer #include "twist.h" #include "ttt.h" typedef struct stringtree { char *word; struct stringtree *a, *b; } stringtree; static stringtree Words,Names,Firsts; // Handling string trees static stringtree *newbranch(const char *S) { stringtree *T; if (!S) return NULL; T=pointer(sizeof(stringtree)); T->word=copyof(S); T->a=NULL; T->b=NULL; return T; } static void addtotree(stringtree *T, const char *S) { int A; if (!T) return; if (!T->word) { T->word=copyof(S); return; } // only first time A=alphabetical(S,T->word); switch (A) { case 0: return; case 1: if (T->a) addtotree(T->a,S); else T->a=newbranch(S); return; case -1: if (T->b) addtotree(T->b,S); else T->b=newbranch(S); return; } } static bool isintree(stringtree *T, const char *S) { int A; if (!T || !T->word) return 0; A=alphabetical(S,T->word); if (A==0) return 1; return isintree(A==1 ? T->a : T->b,S); } static int treequantity(stringtree *T) { if (!T) return 0; return 1 + treequantity(T->a) + treequantity(T->b); } static void writetree(stringtree *T) { if (!T) return; writetree(T->a); writetext(T->word,'\r'); writetree(T->b); } static void firststolist(stringtree *T, stringlist *L) { char *W; if (!T || !L) return; if (T->a) { firststolist(T->a,L); old(T->a); T->a=NULL; } if (isintree(&Names,T->word)) old(T->word); else { W=copyof(T->word); makelowercase(W); if (isintree(&Words,W)) old(T->word); else addstringtolist(L,T->word); old(W); } T->word=NULL; if (T->b) { firststolist(T->b,L); old(T->b); T->b=NULL; } } static void writelist(stringlist *L) { int i; if (!L) return; for (i=0;inum;i++) writetext((*(L->list))[i],'\r'); } // Spell checking bool isinteger(const char *S) { long DIG; if (!S || !*S) return 0; while (*S) { DIG= *(S++) - '0'; if (DIG<0 || DIG>9) return 0; } return 1; } static void CheckWord(const char *S, bool ExpectCap) { char *L; if (!S) return; if (isinteger(S)) return; if (!uppercase(*S)) { addtotree(&Words,S); return; } if (!ExpectCap) { addtotree(&Names,S); return; } if (isintree(&Names,S)) return; L=copyof(S); makelowercase(L); if (isintree(&Words,S)) { old(L); return; } old(L); addtotree(&Firsts,S); } void SpellCheck(const char *S, bool ExpectCap) { char *B,*E,*W; if (!S) return; B=(char *)S; while (1) { while (wordbreak(*B)) B++; if (!*B) return; E=B; while (!wordbreak(*E) && *E) E++; W=leftstr(B,E - B); CheckWord(W,ExpectCap); old(W); B=E; ExpectCap=punctuation(*E); } } void SaveDictionary(void) { int N; bool R; file F; stringlist L; return;// initstringlist(&L); firststolist(&Firsts,&L); N=treequantity(&Words); WarnOld(cat(2,"# of words: ",fig(N))); N=treequantity(&Names); WarnOld(cat(2,"# of names: ",fig(N))); N=L.num; WarnOld(cat(2,"# of firsts: ",fig(N))); R=saveas("Save Word List","dictionary.txt",'TEXT','TEXT',&F); if (R) { setpath(&F); writetree(&Words); writetree(&Names); writelist(&L); closefile(&F); } purgestringlist(&L); } // Initialisation static void initstringtree(stringtree *T) { if (!T) return; T->word=NULL; T->a=NULL; T->b=NULL; } static void purgestringtree(stringtree *T) { if (!T) return; if (T->a) { purgestringtree(T->a); old(T->a); T->a=NULL; } if (T->b) { purgestringtree(T->b); old(T->b); T->b=NULL; } old(T->word); T->word=NULL; } void InitDictionary(void) { initstringtree(&Words); initstringtree(&Names); initstringtree(&Firsts); } void PurgeDictionary(void) { purgestringtree(&Words); purgestringtree(&Names); purgestringtree(&Firsts); }