#include #include #define posVerb 1 #define posTransitive 2 #define posIntransitive 4 #define posNoun 8 #define posAdjective 16 #define posAdverb 32 #define posPreposition 64 #define posInterjection 128 #define posConnective 256 #define posQuestion 512 #define posArticle 1024 #define posRelative 2048 #define posPronoun 4096 #define posBad 8192 int nextstart=-1; char line[500]; char *prompt=": "; char *end_of_file="****"; char *its_a_number="#"; int thenumber; typedef struct WE { char *word; int pos; struct WE *next; } wordentry; #define hashtabsize 20000 wordentry *hashtable[hashtabsize]; char *safecopy(char *s) { int i, n=strlen(s)+1; char *r=(char *)malloc(n); for (i=0; iword=safecopy(s); p->pos=pos; p->next=hashtable[h]; hashtable[h]=p; } void scan(void (*f)(wordentry *)) { int i; for (i=0; inext; } } } int findword(char *s) { int i, h; wordentry *p; for (i=0; s[i]!='\0'; i+=1) { char c=s[i]; if (c>='A' && c<='Z') s[i]=c-'A'+'a'; } h=hashval(s); p=hashtable[h]; while (p!=NULL) { if (strcmp(p->word,s)==0) return p->pos; p=p->next; } return 0; } void init_dict(void) { int i; for (i=0; i'9' && c<'A' || c>'Z' && c<'a' || c>'z')) { i+=1; c=line[i]; } if (c=='\0') { nextstart=-1; return NULL; } if (c>='0' && c<='9') { result=its_a_number; thenumber=0; while (c>='0' && c<='9') { thenumber=thenumber*10+c-'0'; i+=1; c=line[i]; } } else { result=&line[i]; while (c>='A' && c<='Z' || c>='a' && c<='z') { if (c<'a') c+='a'-'A'; i+=1; c=line[i]; } } if (c=='\0') nextstart=-1; else { nextstart=i+1; line[i]='\0'; } return result; } int remove_postfix(char *newword, char *postfix, char *origword) { int plen=strlen(postfix); int wlen=strlen(origword); int result=1; while (plen>0) { wlen-=1; if (wlen<=0) { result=0; break; } plen-=1; if (origword[wlen]!=postfix[plen]) { result=0; break; } } strcpy(newword,origword); if (result!=0) newword[wlen]='\0'; return result; } void fixit(wordentry *p) { int pos=p->pos; if ((pos&posVerb)!=0 && (pos&(posTransitive|posIntransitive))==0) p->pos=pos|posTransitive|posIntransitive; } void main(void) { init_dict(); load_dict_file("dict.txt"); load_dict_file("extradict.txt"); scan(fixit); while (1) { char *word=nextword(); if (word==NULL) printf("\n"); else if (word==end_of_file) { printf("\n"); break; } else if (word==its_a_number) printf("\n", thenumber); else { char originalword[100]; int pos=0, failed=0, ok; strncpy(originalword,word,99); pos=findword(word); if (pos==0) failed=1; if (pos==0) { ok=remove_postfix(word,"es",originalword); if (ok) { pos=findword(word); if ((pos & (posVerb|posNoun))==0) /* ...es is only OK for verbs & nouns */ pos=0; } } if (pos==0) { ok=remove_postfix(word,"s",originalword); if (ok) { pos=findword(word); if ((pos & (posVerb|posNoun))==0) /* ...s is only OK for verbs & nouns */ pos=0; } } if (pos==0) { ok=remove_postfix(word,"ed",originalword); if (ok) { pos=findword(word); if ((pos & posVerb)==0) /* ...ed is only OK for verbs */ pos=0; } } if (pos==0) { ok=remove_postfix(word,"ing",originalword); if (ok) { pos=findword(word); if ((pos & posVerb)==0) /* ...ing converts a verb to an adjective */ pos=0; else pos=posAdjective; } } printf("\"%s\"", originalword); if (failed && pos!=0) printf(" -> \"%s\"", word); if (pos==0) printf(": Unknown\n"); else { printf(": "); if (pos & posVerb) { printf("[VERB"); if (pos & posTransitive) printf(",t"); if (pos & posIntransitive) printf(",i"); printf("] "); } if (pos & posNoun) printf("[NOUN] "); if (pos & posArticle) printf("[ARTIC] "); if (pos & posAdjective) printf("[ADJEC] "); if (pos & posAdverb) printf("[ADVB] "); if (pos & posPreposition) printf("[PREP] "); if (pos & posInterjection) printf("[INTERJ] "); if (pos & posConnective) printf("[CONN] "); if (pos & posQuestion) printf("[QUEST] "); if (pos & posRelative) printf("[REL] "); if (pos & posPronoun) printf("[PRON] "); if (pos & posBad) printf("[unk]"); printf("\n"); } } } printf("Done.\n"); }