⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hnet.c

📁 Hidden Markov Toolkit (HTK) 3.2.1 HTK is a toolkit for use in research into automatic speech recogn
💻 C
📖 第 1 页 / 共 5 页
字号:
         if (IsWd0Link(&nextNode->links[i]))            return TRUE;      return FALSE;   }   else      return FALSE;}static void AddChain(Network*net, NetNode *hd) {   NetNode *tl;   if (hd == NULL)       return;   tl = hd;   while (tl->chain != NULL)      tl = tl->chain;   tl->chain = net->chain;   net->chain = hd;}/*    Networks are created directly from lattices (which may be   of type shortArc to minimise lattice storage requirements).      Cross word context dependent networks are created in an   automagic manner from the hmmlist and monophone dictionary.   Contexts   -1 == context free - skip this phone when determining context    0 == context independent - matches any context - or undefined    n == unique context identifier   Many labids may map to a single context (generalised contexts)   or there may be single context per phone (triphone contexts)   There is currently no way to determine automatically which   context a model belongs to for generalised contexts.*/typedef struct pronholder{   LNode *ln;       /* Node that created this instance */   Pron pron;       /* Actual pronunciation */   short nphones;   /* Number of phones for this instance */   LabId *phones;   /* Phone sequence for the instance */   LogFloat fct;    /* LM likelihood to be factored into each phone */   int ic;          /* Initial context - cache saves finding for all links */   int fc;          /* Final context - cache saves finding for all links */   Boolean fci;     /* Final phone context independent */   Boolean tee;     /* TRUE if word consists solely of tee models */   int clen;        /* Number of non-cf phones in pronunciation */   NetNode **lc;    /* Left contexts - linked to word initial models */   NetNode **rc;    /* Right contexts - linked to word end nodes */   int nstart;      /* Number of models in starts chain */   int nend;        /* Number of models in ends chain */   NetNode *starts; /* Chain of initial models */   NetNode *ends;   /* Chain of final models */   NetNode *chain;  /* Chain of other nodes in word */   struct pronholder *next;}PronHolder;#define HCI_CXT_BLOCKSIZE 256/* Word end nodes are accessed through a hash table for fast access */#define WNHASHSIZE 5701static NetNode *wnHashTab[WNHASHSIZE];static HMMSetCxtInfo *NewHMMSetCxtInfo(HMMSet *hset, Boolean frcCxtInd){   HMMSetCxtInfo *hci;   hci=(HMMSetCxtInfo *) New(&gcheap,sizeof(HMMSetCxtInfo));   hci->hset=hset;   hci->nc=hci->xc=hci->nci=hci->ncf=0;   hci->sLeft=hci->sRight=FALSE;   if (frcCxtInd) {      hci->cxs=hci->cis=hci->cfs=NULL;   }   else {      hci->cxs=(LabId *) New(&gcheap,sizeof(LabId)*HCI_CXT_BLOCKSIZE);      hci->cis=(LabId *) New(&gcheap,sizeof(LabId)*HCI_CXT_BLOCKSIZE);      hci->cfs=(LabId *) New(&gcheap,sizeof(LabId)*HCI_CXT_BLOCKSIZE);            hci->cxs[0]=hci->cis[0]=hci->cfs[0]=GetLabId("<undef>",TRUE);   }   return(hci);}/* Binary search to find labid in n element array */static int BSearch(LabId labid, int n,LabId *array){   int l,u,c;   l=1;u=n;   while(l<=u) {      c=(l+u)/2;      if (array[c]==labid) return(c);      else if (array[c]<labid) l=c+1;       else u=c-1;   }   return(0);}/* Binary search to find labid in n element array */static int BAddSearch(HMMSetCxtInfo *hci,LabId labid, int *np,LabId **ap){   LabId *array;   int l,u,c;   array=*ap;   l=1;u=*np;   while(l<=u) {      c=(l+u)/2;      if (array[c]==labid) return(c);      else if (array[c]<labid) l=c+1;       else u=c-1;   }   if (((*np+1)%HCI_CXT_BLOCKSIZE)==0) {      LabId *newId;      newId=(LabId *) New(&gcheap,sizeof(LabId)*(*np+1+HCI_CXT_BLOCKSIZE));      for (c=1;c<=*np;c++)         newId[c]=array[c];      Dispose(&gcheap,array);      *ap=array=newId;   }   for (c=1;c<=*np;c++)      if (labid<array[c]) break;   for (u=(*np)++;u>=c;u--)      array[u+1]=array[u];   array[c]=labid;   return(c);}int AddHCIContext(HMMSetCxtInfo *hci,LabId labid){   int c;   c=BAddSearch(hci,labid,&hci->nc,&hci->cxs);   return(c);}/* Return context defined by given labid (after triphone context stripping) */int GetHCIContext(HMMSetCxtInfo *hci,LabId labid){   LabId cxt;   char buf[80];   int c;   if (hci->nc==0) return(0);   strcpy(buf,labid->name);   TriStrip(buf);   cxt=GetLabId(buf,FALSE);      c=BSearch(cxt,hci->nc,hci->cxs);   if (c>0) return(c);   c=BSearch(cxt,hci->ncf,hci->cfs);   if (c>0) {      if (hci->xc>0 || !cfWordBoundary) return(-1);      else return(0); /* Context free are word boundaries */   }   return(0);}/* Check to see if CI model exists for labid */Boolean IsHCIContextInd(HMMSetCxtInfo *hci,LabId labid){   int c;   if (hci->nc==0) return(TRUE);   c=BSearch(labid,hci->nci,hci->cis);   if (c>0) return(TRUE);   else return(FALSE);}/* Search through pron for left context for phone in position pos *//*  If beginning of word is reached return lc */static int FindLContext(HMMSetCxtInfo *hci,PronHolder *p,int pos,int lc){   int i,c;   for (i=pos-1,c=-1;i>=0;i--)      if ((c=GetHCIContext(hci,p->phones[i]))>=0)         break;   if (c<0) c=lc;   return(c);}/* Search through pron for right context for phone in position pos *//*  If end of word is reached return rc */static int FindRContext(HMMSetCxtInfo *hci,PronHolder *p,int pos,int rc){   int i,c;   for (i=pos+1,c=-1;i<p->nphones;i++)      if ((c=GetHCIContext(hci,p->phones[i]))>=0)         break;   if (c<0) c=rc;   return(c);}/* Determine if dictionary contains any models not in list *//* Notionally checks whether dictionary is phone or model based */static Boolean ClosedDict(Vocab *voc,HMMSet *hset){   Word word;   Pron pron;   int i,h;   for (h=0; h<VHASHSIZE; h++)      for (word=voc->wtab[h]; word!=NULL; word=word->next)         for (pron=word->pron; pron!=NULL; pron=pron->next)            for (i=0;i<pron->nphones;i++)               if (FindMacroName(hset,'l',pron->phones[i])==NULL)                  return(FALSE);   return(TRUE);}/* Defines contexts from vocabulary and hmmset */static int DefineContexts(HMMSetCxtInfo *hci){   MLink ml,il;   LabId labid;   char buf[80],*ptr;   int h,c,*temp;   hci->nc=0; hci->sLeft=hci->sRight=FALSE;   /* Scan for all contexts that appear */   for (h=0; h<MACHASHSIZE; h++)      for (ml=hci->hset->mtab[h]; ml!=NULL; ml=ml->next)         if (ml->type=='l') {            strcpy(buf,ml->id->name);            TriStrip(buf);            labid=GetLabId(buf,FALSE);            il=FindMacroName(hci->hset,'l',labid);                        /* Start by adding all models then check later if CI */            BAddSearch(hci,labid,&hci->nci,&hci->cis);                        /* Check for left context */            if (strchr(ml->id->name,'-')!=NULL) {               strcpy(buf,ml->id->name);               strchr(buf,'-')[0]=0;               labid=GetLabId(buf,TRUE);               AddHCIContext(hci,labid);               hci->sLeft=TRUE;            }                        /* Check for right context */            if (strchr(ml->id->name,'+')!=NULL) {               strcpy(buf,ml->id->name);               ptr=strchr(buf,'+');               labid=GetLabId(ptr+1,TRUE);               AddHCIContext(hci,labid);               hci->sRight=TRUE;            }         }   temp=(int *) New(&gstack,sizeof(int)*hci->nci);temp--;   for (c=1;c<=hci->nci;c++) temp[c]=0;   /* Otherwise scan for all contexts that appear */   for (h=0; h<MACHASHSIZE; h++)      for (ml=hci->hset->mtab[h]; ml!=NULL; ml=ml->next)         if (ml->type=='l') {            strcpy(buf,ml->id->name);            TriStrip(buf);            labid=GetLabId(buf,FALSE);            il=FindMacroName(hci->hset,'l',labid);            c=BSearch(labid,hci->nci,hci->cis);            if (c!=0 && il!=ml)               temp[c]++;         }   for (c=1;c<=hci->nci;c++)      if (temp[c]!=0) hci->cis[c]=NULL;   Dispose(&gstack,temp+1);      for (c=1,h=1;c<=hci->nci;c++,h++) {      for (;h<=hci->nci;h++) if (hci->cis[h]!=NULL) break;      if (h>hci->nci) break;      hci->cis[c]=hci->cis[h];   }   hci->nci=c-1;   /* Now define which models represent which contexts */   /*  Do nothing for assumed one-to-one corespondance */   /* And finally set all other to context free */         for (h=0; h<MACHASHSIZE; h++)      for (ml=hci->hset->mtab[h]; ml!=NULL; ml=ml->next)         if (ml->type=='l') {            c=GetHCIContext(hci,ml->id);            if (c==0) {               if (!IsHCIContextInd(hci,ml->id))                   HError(8230,"DefineContexts: Context free models must be context independent (%s)",ml->id->name);               BAddSearch(hci,ml->id,&hci->ncf,&hci->cfs);            }         }   return(hci->nc);}/* Return labid for given context */static LabId ContextName(HMMSetCxtInfo *hci,int c){   if (c<0 || c>hci->nc)      HError(8290,"ContextName: Context %d not defined (-1..%d)",c,hci->nc);   return(hci->cxs[c]);}/* Counting variables:  Number of *//* Word-internal nodes, Word-initial nodes, Word final-nodes, chainNodes *//*  Null nodes, context-free nodes, word end nodes *//* Word-internal links, Cross-word links and null-word links */int nwi=0,nin=0,nfi=0,ncn=0,nll=0,ncf=0,nwe=0,nil=0,nxl=0,nnl=0;/* Use hash table to lookup word end node */static NetNode *FindWordNode(MemHeap *heap,Pron pron,                             PronHolder *pInst,NetNodeType type){   union {      Ptr ptrs[3];      unsigned char chars[12];   }   un;   unsigned int hash,i;   NetNode *node;   hash=0;   un.ptrs[0]=pron;un.ptrs[1]=pInst;un.ptrs[2]=(Ptr)type;   for (i=0;i<12;i++)      hash=((hash<<8)+un.chars[i])%WNHASHSIZE;   for (node=wnHashTab[hash];node!=NULL;node=node->chain)      if (node->info.pron==pron && node->inst==(NetInst*)pInst &&          node->type==type) break;   if (node==NULL) {      nwe++;      if (heap==NULL)         HError(8291,"FindWordNode: Node %s[%d] %d not created",                pron->word->wordName->name,pron->pnum,type);      node=(NetNode *) New(heap,sizeof(NetNode));      node->info.pron=pron;      node->type=type;      node->inst=(NetInst*)pInst;      node->nlinks=0;      node->links=NULL;      node->tag=NULL;      node->aux=0;      node->chain=wnHashTab[hash];      wnHashTab[hash]=node;   }   return(node);}/* Create NetNode (and optionally NetLinks as well) */static NetNode *NewNode(MemHeap *heap,HLink hmm,int nlinks){   NetNode *node;   node=(NetNode *)New(heap,sizeof(NetNode));   node->type=(hmm->transP[1][hmm->numStates]>LSMALL?               (n_hmm|n_tr0) : n_hmm );   node->info.hmm=hmm;   node->inst=NULL;node->chain=NULL;   node->nlinks=nlinks;   node->tag=NULL;   node->aux=0;   if (nlinks==0)      node->links=NULL;   else      node->links=(NetLink*) New(heap,sizeof(NetLink)*node->nlinks);   return(node);}/* Find model matching context */static HLink FindModel(HMMSetCxtInfo *hci,int lc,LabId name,int rc){   LabId labid;   MLink ml;   char buf[80];   /* Word internal hack */   /* Cross word will need proper specification of context */   /* as well as knowledge of which models are cd/ci.      */   /* First try constructing the cd name */   if (!allowCxtExp || (lc<=0 && rc<=0) || IsHCIContextInd(hci,name)) {      strcpy(buf,name->name);      labid=name;   }   else if ((lc==0 || forceRightBiphones || !hci->sLeft) &&             rc>0 && !forceLeftBiphones) {      sprintf(buf,"%s+%s",name->name,ContextName(hci,rc)->name);      labid=GetLabId(buf,FALSE);   }   else if ((rc==0 || forceLeftBiphones || !hci->sRight) &&             lc>0 && !forceRightBiphones) {      sprintf(buf,"%s-%s",ContextName(hci,lc)->name,name->name);      labid=GetLabId(buf,FALSE);   }   else if (!forceLeftBiphones && !forceRightBiphones) {      sprintf(buf,"%s-%s+%s",ContextName(hci,lc)->name,              name->name,ContextName(hci,rc)->name);      labid=GetLabId(buf,FALSE);   }   else{      strcpy(buf, name->name);      labid=name;   }   ml=FindMacroName(hci->hset,'l',labid);      /* Then try the name itself */   if (ml==NULL && (((lc==0 && rc==0) || !forceCxtExp) ||                     (lc==0 || 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -