📄 lwmap.c
字号:
/* ----------------------------------------------------------- *//* *//* ___ *//* |_| | |_/ SPEECH *//* | | | | \ RECOGNITION *//* ========= SOFTWARE */ /* *//* *//* ----------------------------------------------------------- *//* developed at: *//* *//* Speech Vision and Robotics group *//* Cambridge University Engineering Department *//* http://svr-www.eng.cam.ac.uk/ *//* *//* main authors: Valtcho Valtchev, Steve Young, *//* Julian Odell, Gareth Moore *//* ----------------------------------------------------------- *//* Copyright: *//* *//* 1994-2002 Cambridge University *//* Engineering Department *//* *//* Use of this software is governed by a License Agreement *//* ** See the file License for the Conditions of Use ** *//* ** This banner notice must not be removed ** *//* *//* ----------------------------------------------------------- *//* File: LWMap: Word List and Word Map Routines *//* ----------------------------------------------------------- */char *lwmap_version = "!HVER!LWMap: 3.2 [CUED 09/12/02]";char *lwmap_vc_id = "$Id: LWMap.c,v 1.1 2002/12/19 16:35:33 ge204 Exp $";#include "HShell.h"#include "HMem.h"#include "HMath.h"#include "HWave.h"#include "HLabel.h"#include "LUtil.h"#include "LWMap.h"/* ------------------------ Trace Flags --------------------- */static int trace = 0;#define T_TOP 0001 /* top level tracing */#define T_WMP 0002 /* word map loading */#define T_SRT 0004 /* word map sorting *//* --------------------- Global Variables ------------------- */static ConfParam *cParm[MAXGLOBS]; /* config parameters */static int nParm = 0;static Boolean outWMapRaw = FALSE; /* Output file in raw mode */static Boolean inWMapRaw = FALSE; /* Read input files in raw mode */static LabId *sortTab; /* global temp used by qsort *//* --------------------- Initialisation --------------------- *//* EXPORT -> InitWMap: initialise the module */void InitWMap(void){ int i; Boolean b; Register(lwmap_version,lwmap_vc_id); nParm = GetConfig("LWMAP", TRUE, cParm, MAXGLOBS); if (nParm>0){ if (GetConfInt(cParm,nParm,"TRACE",&i)) trace = i; if (GetConfBool(cParm,nParm,"INWMAPRAW",&b)) inWMapRaw = b; if (GetConfBool(cParm,nParm,"OUTWMAPRAW",&b)) outWMapRaw = b; }}/* --------------------- Input Routines ------------------ *//* CmpListEntry: qsort compare routine for word list sorting */static int CmpListEntry(const void *p1, const void *p2){ LabId *w1,*w2; w1 = (LabId *) p1; w2 = (LabId *) p2; return strcmp((*w1)->name,(*w2)->name);}/* EXPORT->GetSrcString: get next string from src in appropriate mode */Boolean GetSrcString(Source *src, char *s, Boolean htkEsc){ if (htkEsc) return ReadString(src,s); else { src->wasQuoted=FALSE; return ReadRawString(src,s); }}/* ReadMapHeader: read header and store info in wm */static void ReadMapHeader(Source *src, WordMap *wm){ LMFileHdr hdr; LMHdrKind kind; int n; char *s; if (trace&T_TOP) printf("Reading header from %s\n",src->name); kind = ReadLMHeader(&(wm->mem),src,LWMapFilter,&hdr,&n); if (n<0 || n > 9999000) HError(15151,"ReadMapHeader: Unlikely num map entries[%d] in %s",n,src->name); wm->used = n; wm->isMap = wm->hasCnts = FALSE; if (kind == NO_HDR) { wm->seqno = 0; wm->htkEsc = !inWMapRaw; wm->lang = NULL; wm->source = NULL; } else { wm->name = GetLMHdrStr("NAME",hdr,FALSE); if (wm->name == NULL) HError(15152,"ReadMapHeader: No NAME header in %s",src->name); if (!GetLMHdrInt("SEQNO",&n,hdr)) HError(15153,"ReadMapHeader: No SEQNO header in %s",src->name); wm->seqno = n; wm->htkEsc = TRUE; if ((s=GetLMHdrStr("ESCMODE",hdr,TRUE)) != NULL) { if (strcmp(s,"HTK")==0) wm->htkEsc = TRUE; else if (strcmp(s,"RAW")==0) wm->htkEsc = FALSE; else HError(15155,"ReadMapHeader: Unknown escmode %s in %s",s,src->name); } wm->lang = GetLMHdrStr("LANGUAGE",hdr,FALSE); wm->source = GetLMHdrStr("SOURCE",hdr,FALSE); if ((s=GetLMHdrStr("FIELDS",hdr,TRUE)) != NULL) { if (strcmp(s,"ID")==0) wm->isMap = TRUE; else if (strcmp(s,"ID,WFC")==0 || strcmp(s,"WFC,ID")==0) wm->isMap = wm->hasCnts = TRUE; else HError(15150,"ReadMapHeader: Unknown fields tag %s in %s",s,src->name); } }}#define LTBNDX(ltb,i) (i - ltb->minId)/* EXPORT->GetMEIndex: obtain index of ME corresponding to ndx */int GetMEIndex(WordMap *wm, int ndx){ LookupTable *ltb; ltb = (ndx < BASEWORDNDX) ? wm->clt : wm->wlt; return (ndx < ltb->minId || ndx > ltb->maxId) ? -1 : ltb->tlb[LTBNDX(ltb,ndx)];}/* EXPORT->BuildLookupTables: construct ndx -> ME lookup tables */void BuildLookupTables(WordMap *wm){ int i,j,freeSlots; MapEntry *me; LookupTable *wlt,*clt; if (!wm->isMap) { wm->wlt = NULL; wm->clt = NULL; return; } if (wm->me==NULL || wm->id==NULL) HError(15190,"BuildLookupTables: Map not initialised"); freeSlots = wm->size - wm->used; if (wm->wlt!=NULL) { Dispose(&wm->mem,wm->wlt); wm->wlt = wm->clt = NULL; } /* build word lookup table first */ wlt = (LookupTable *) New(&wm->mem,sizeof(LookupTable)); wlt->minId = wlt->maxId = BASEWORDNDX; for (me = wm->me,i=0; i<wm->used; i++,me++) { if (me->ndx >= BASEWORDNDX) { wlt->minId = wlt->maxId = me->ndx; break; } } for (me=wm->me,i=0; i<wm->used; i++,me++) { if (me->ndx >= BASEWORDNDX) { if (me->ndx < wlt->minId) wlt->minId = me->ndx; if (me->ndx > wlt->maxId) wlt->maxId = me->ndx; } } wlt->maxId += freeSlots; wlt->size = wlt->maxId - wlt->minId + 1; wlt->tlb = (int *) New(&wm->mem,wlt->size*sizeof(int)); for (i=0; i<wlt->size; i++) wlt->tlb[i]=-1; for (me=wm->me,i=0; i<wm->used; i++,me++) { if (me->ndx >= BASEWORDNDX) { j = LTBNDX(wlt,me->ndx); if (wlt->tlb[j]!=-1) HError(15190,"BuildLookupTables: Duplicate word %s",wm->id[i]->name); wlt->tlb[j] = i; } } wm->wlt = wlt; /* build class lookup table second */ clt = (LookupTable *) New(&wm->mem,sizeof(LookupTable)); clt->minId = clt->maxId = 0; for (me = wm->me,i=0; i<wm->used; i++,me++) { if (me->ndx < BASEWORDNDX) { clt->minId = clt->maxId = me->ndx; break; } } for (me=wm->me,i=0; i<wm->used; i++,me++) { if (me->ndx < BASEWORDNDX) { if (me->ndx < clt->minId) clt->minId = me->ndx; if (me->ndx > clt->maxId) clt->maxId = me->ndx; } } clt->size = clt->maxId - clt->minId + 1; clt->tlb = (int *) New(&wm->mem,clt->size*sizeof(int)); for (i=0; i<clt->size; i++) clt->tlb[i]=-1; for (me=wm->me,i=0; i<wm->used; i++,me++) { if (me->ndx < BASEWORDNDX) { j = LTBNDX(clt,me->ndx); if (clt->tlb[j]!=-1) HError(15190,"BuildLookupTables: Duplicate class %s",wm->id[i]->name); clt->tlb[j] = i; } } wm->clt = clt;}#define MAP_ENTRY(map,i) map.me + ((i<BASEWORDNDX) ? i : i-BASEWORDNDX+map.nClass) /* LoadMapData: load contents of src into wordmap */static void LoadMapData(Source *src, WordMap *wm){ char buf[256],*fn; MapEntry *me; int ibuf[2],i,n,ndx; LabId id; fn = src->name; if (wm->isMap) for (i=0; i<wm->used; i++) wm->id[i] = NULL; for (me=wm->me,i=0; i<wm->used; i++,me++){ if (!GetSrcString(src,buf,wm->htkEsc)) HError(15113,"LoadMapData: Cannot read word %d from %s",i+1,fn); id = GetLabId(buf,TRUE); if (wm->isMap){ if (id->aux != NULL) HError(15155,"LoadMapData: Word %s is duplicated in %s",buf,fn); n = (wm->hasCnts) ? 2 : 1; ibuf[1] = 0; if (!ReadInt(src,ibuf,n,FALSE)) HError(15113,"LoadMapData: Cannot read index/count of %s in %s",buf,fn); if ((ndx = ibuf[0]) > wm->lastUsed) wm->lastUsed = ndx; id->aux = (Ptr) me; wm->id[i] = id; me->ndx = ndx; me->count = ibuf[1]; me->sort = 0; me->class = NULL; me->aux.auxint = 0; } else { wm->id[i] = id; } } CloseSource(src);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -