📄 lwmap.c
字号:
/* CreateListorMap: Create a word list or map structure */static void CreateListorMap(char *fn, WordMap *wm, int freeSlots, Boolean isMap){ Source src; char buf[MAXSTRLEN],buf2[MAXSTRLEN]; /* Initialise configuration values */ strcpy(buf,(isMap)?"WMap:":"WList:"); if (fn != NULL) strcat(buf,NameOf(fn,buf2)); CreateHeap(&(wm->mem),buf,MSTAK,1,0.0,100,10000000); wm->used = wm->seqno = 0; wm->name = wm->lang = wm->source = NULL; wm->htkEsc = TRUE; wm->isMap = wm->hasCnts = FALSE; wm->lastUsed = BASEWORDNDX-1; if (fn != NULL) { /* read header */ if (InitSource(fn,&src,LWMapFilter) == FAIL) { HError(15110, "File is required to continue"); } ReadMapHeader(&src,wm); if (isMap != wm->isMap) HError(15150,"CreateListorMap: File %s is not a map",fn); if (trace&T_WMP) printf("Creating map/list from %s with %d entries\n", fn, wm->used); } else { wm->isMap = isMap; if (trace&T_WMP) printf("Creating empty map/list\n"); } /* Create Data Storage */ wm->size = wm->used + freeSlots; wm->isSorted = FALSE; wm->firstNdx = -1; wm->id = (LabId *)New(&(wm->mem),wm->size*sizeof(LabId)); wm->me = (wm->isMap)?(MapEntry *)New(&(wm->mem),wm->size*sizeof(MapEntry)):NULL; if (fn != NULL) { LoadMapData(&src,wm); if (trace&T_WMP) printf("%d words loaded\n",wm->used); } wm->wlt = wm->clt = NULL; BuildLookupTables(wm);}/* --------------------- Output Routines ---------------------- *//* WriteMapHeader: write HLM header to f */static void WriteMapHeader(FILE *f, WordMap *w){ fprintf(f,"Name = %s\n",w->name); fprintf(f,"SeqNo = %d\n",w->seqno); fprintf(f,"Entries = %d\n",w->used); fprintf(f,"EscMode = %s\n",(w->htkEsc)?"HTK":"RAW"); if (w->isMap) fprintf(f,"Fields = ID%s\n",(w->hasCnts)?",WFC":""); if (w->lang != NULL) fprintf(f,"Language = %s\n",w->lang); if (w->source != NULL) fprintf(f,"Source = %s\n",w->source); fprintf(f,"\\Words\\\n");}/* WriteWordMap: Write given word map to file f */static void WriteWordMap(FILE *f, WordMap *w, Boolean noHeader, Boolean debug){ int i; MapEntry *me; LabId id; if (w == NULL) HError(15190,"WriteWordMap: Word map is NULL"); if (!noHeader) WriteMapHeader(f,w); for (i=0; i<w->used; i++){ id = w->id[i]; if (!w->htkEsc || outWMapRaw) fprintf(f, "%s", id->name); else WriteString(f,id->name,ESCAPE_CHAR); if (w->isMap) { me = &(w->me[i]); if (me == NULL || me != (MapEntry *)id->aux) HError(15190,"WriteWordMap: Map is broken"); fprintf(f,"\t%d", me->ndx); if (w->hasCnts) fprintf(f,"\t%d", me->count); if (debug) fprintf(f,"\t%d", me->sort); } fprintf(f,"\n"); } if (debug) { fprintf(f,"Map is%s sorted\n", w->isSorted?"":" not"); fprintf(f,"Used=%d, Size=%d\n", w->used, w->size); fprintf(f,"FirstNdx=%d, lastUsed=%d\n", w->firstNdx, w->lastUsed); }}/* ------------------- Interface Routines ------------------- *//* EXPORT->CreateWordList: Create a word list */void CreateWordList(char *fn, WordMap *w, int freeSlots){ CreateListorMap(fn,w,freeSlots,FALSE); if (w->used>0) SortWordMap(w);}/* EXPORT->CreateWordMap: Create a word map */void CreateWordMap(char *fn, WordMap *w, int freeSlots){ CreateListorMap(fn,w,freeSlots,TRUE);}/* EXPORT->SaveWordMap: Write given word map to file fn */void SaveWordMap(char *fn, WordMap *w, Boolean noHeader){ FILE *f; Boolean isPipe; f = FOpen(fn,LWMapOFilter,&isPipe); if (f==NULL) HError(15111,"SaveWordMap: Cannot create %s",fn); WriteWordMap(f,w,noHeader,FALSE); FClose(f,isPipe);}/* EXPORT->ShowWordMap: Show given word map */void ShowWordMap(WordMap *w){ WriteWordMap(stdout,w,FALSE,TRUE);}/* EXPORT->AddWordToMap: add word to wm and return its id */void AddWordToMap(WordMap *wm, LabId word){ int i,ndx; MapEntry *me; if (wm==NULL) HError(15190,"AddWordToMap: Word map is NULL"); if (!wm->isMap) HError(15190,"AddWordToMap: Cannot add word to word list"); me = (MapEntry *) word->aux; if (me == NULL) { /* new word */ if (wm->used == wm->size) HError(15190,"AddWordToMap: Word map is full"); i = wm->used++; wm->id[i] = word; word->aux = me = &(wm->me[i]); me->ndx = ndx = ++wm->lastUsed; me->sort = 0; me->count = 1; me->class = NULL; me->aux.auxint = 0; wm->isSorted = FALSE; /* update the lookup table */ wm->wlt->tlb[LTBNDX(wm->wlt,ndx)]=i; } else ++me->count; /* seen this before */}/* EXPORT->MarkWordList: mark words in wl by setting me->aux.auxint to 1 */void MarkWordList(WordMap *wl){ int i; MapEntry *me; for (i=0; i<wl->used; i++) { me = (MapEntry *)wl->id[i]->aux; if (me == NULL) HError(15190,"MarkWordList: Word %s is not in map",wl->id[i]->name); me->aux.auxint = 1; }}/* CmpMapEntry: qsort compare routine for map sorting */static int CmpMapEntry(const void *p1, const void *p2){ int b, *w1, *w2; w1 = (int *) p1; w2 = (int *) p2; b = strcmp(sortTab[*w1]->name, sortTab[*w2]->name); return b;}/* EXPORT->SortWordMap: sort the map */void SortWordMap(WordMap *wm){ int i,*smap; if (wm==NULL) HError(15190,"SortWordMap: Word map is NULL"); if (wm->isSorted || wm->used==0) return; if (trace&T_SRT) printf(" sorting %d entries in map\n", wm->used); if (!wm->isMap) { usort(wm->id,wm->used,sizeof(LabId),CmpListEntry); } else { sortTab = wm->id; smap = (int *) New(&gstack,wm->size*sizeof(int)); for (i=0; i<wm->used; i++) smap[i] = i; usort(smap, wm->used, sizeof(int), CmpMapEntry); wm->firstNdx = smap[0]; for (i=0; i<wm->used; i++) { wm->me[smap[i]].sort = i; if (trace&T_SRT) { printf("%-12s %d\n",wm->id[i]->name,smap[i]); } } Dispose(&gstack,smap); } wm->isSorted = TRUE;}/* ------------------- Access Routines ------------------- *//* EXPORT->WordLMIndex: Returns word map index, -1 if not in map */int WordLMIndex(LabId id){ MapEntry *me; if ((id==NULL) || ((me = (MapEntry *)id->aux)==NULL)) return -1; return me->ndx;}/* EXPORT->WordLMCount: Returns word map count */int WordLMCount(LabId id){ MapEntry *me; if (id == NULL) HError(15190,"WordLMCount: null id"); if ((me = (MapEntry *)id->aux) == NULL) HError(15190,"WordLMCount: Word %s not in map",id->name); return me->count;}/* EXPORT->WordLMName: returns word given index */LabId WordLMName(int ndx, WordMap *wm){ int i; if (wm == NULL) HError(15190,"WordLMName: Word map is NULL"); if ((i=GetMEIndex(wm,ndx)) < 0) return NULL; return wm->id[i];}/* EXPORT->WordLMCmp: true if word with ndx1 is < ndx2 */int WordLMCmp(int ndx1, int ndx2, WordMap *wm){ int i1,i2,s1,s2; if (wm == NULL) HError(15190,"WordLMCmp: Word map is NULL"); if (!wm->isSorted) HError(15190,"WordLMCmp: Map is not sorted"); if ((i1 = GetMEIndex(wm,ndx1)) < 0) HError(15190,"WordLMCmp: Index %d not in map",ndx1); if ((i2 = GetMEIndex(wm,ndx2)) < 0) HError(15190,"WordLMCmp: Index %d not in map",ndx2); s1 = wm->me[i1].sort; s2 = wm->me[i2].sort; if (s1 < s2) return -1; if (s1 > s2) return +1; return 0;}/* -------------------- End of LWMap.c ---------------------- */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -