#include #include #include #include"dictionary.h" DICTIONARY *NewDictionary( const int size ) { DICTIONARY *dict = (DICTIONARY*)calloc(1,sizeof(DICTIONARY)); dict->nmax = size; dict->words = (WORD_ID*)calloc(size, sizeof(WORD_ID)); dict->inverse = (char**)calloc(size, sizeof(char*)); return(dict); } int widcmp( const void *a, const void *b ) { const WORD_ID *A = (const WORD_ID*)a; const WORD_ID *B = (const WORD_ID*)b; return( strcmp( A->word, B->word)); } int LookupDictionary( char *word, DICTIONARY *dict) { WORD_ID *w, key; key.word = word; key.id = 0; if ( dict->nwords > 0 ) { w = (WORD_ID*)bsearch( &key, dict->words, dict->nwords, sizeof(WORD_ID), widcmp ); if ( w != NULL ) return( w->id); } return(-1); } char *InverseLookupDictionary( const int id, DICTIONARY *dict ) { return( dict->inverse[id] ); } int UpdateDictionary( char *word, DICTIONARY *dict) { WORD_ID *w, key; key.word = word; key.id = 0; if ( dict->nwords > 0 ) { w = (WORD_ID*)bsearch( &key, dict->words, dict->nwords, sizeof(WORD_ID), widcmp ); if ( w == NULL ) { if ( dict->nwords >= dict->nmax ) { dict->nmax *= 1.5; dict->words = (WORD_ID*)realloc(dict->words, dict->nmax*sizeof(WORD_ID)); dict->inverse = (char**)realloc(dict->inverse, dict->nmax*sizeof(char*)); } dict->words[dict->nwords].word = strdup(word); dict->words[dict->nwords].id = dict->nwords; dict->inverse[dict->nwords] = dict->words[dict->nwords].word; dict->nwords++; qsort(dict->words, dict->nwords, sizeof(WORD_ID), widcmp ); w = (WORD_ID*)bsearch( &key, dict->words, dict->nwords, sizeof(WORD_ID), widcmp ); } return(w->id); } else { // strcmp( word, dict->words[0].word )) { dict->words[0].word = strdup(word); dict->words[0].id = 0; dict->inverse[0] = dict->words[0].word; dict->nwords=1; return(0); } } int WriteBinaryDictionary( FILE *fp, DICTIONARY *d ) { fprintf(fp, "DICTIONARY %d\n", d->nwords ); int i, len; for(i=0;inwords;i++) { fwrite( &(d->words[i].id), sizeof(int), 1, fp); len = strlen( d->words[i].word)+1; fwrite( &len, sizeof(int), 1, fp); fwrite( d->words[i].word, sizeof(char), len, fp); } return(1); } DICTIONARY *ReadBinaryDictionary( FILE *fp ) { int i, nwords=0, t=0; char *s, buffer[1000]; s = fgets( buffer, 1000, fp); if ( sscanf(buffer, "DICTIONARY %d", &nwords) == 1 ) { DICTIONARY *d = (DICTIONARY*)calloc(1,sizeof(DICTIONARY)); d->nwords = nwords; d->nmax = nwords; d->words = (WORD_ID*)calloc(nwords, sizeof(WORD_ID)); d->inverse = (char**)calloc(nwords, sizeof(char*)); for(i=0;iwords[i].id = id; d->inverse[id] = d->words[i].word = strdup(buffer); } return(d); } return(NULL); } char *suffix( char *s, char sep ) { int k = strlen(s)-1; while( k>0 && s[k] != sep )k--; if ( s[k] == sep ) return &(s[k+1]); else return(s); } char *prefix( char *s, char sep ) { int k = strlen(s); int i=0; while(i