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

📄 hlabel.c

📁 实现HMM算法
💻 C
📖 第 1 页 / 共 4 页
字号:
/* EXPORT->NumCases: find num cases of primary label in label list */int NumCases(LabList *ll, LabId id){   int n = 0;   LLink p;      for (p=ll->head->succ; p->succ!=NULL; p=p->succ)       if (p->labid == id) ++n;   return n;}/* EXPORT->GetCase: find nth occ of primary label in label list */LLink GetCase(LabList *ll, LabId id, int n){   LLink p;   int k=0;      for (p=ll->head->succ; p->succ!=NULL; p=p->succ) {      if (p->labid == id)          if (++k==n) return p;   }   HError(6571,"GetCase: %d case of %s nonexistent",n,id->name);   return NULL;}/* EXPORT->NumAuxCases: find num cases of aux label i in label list */int NumAuxCases(LabList *ll, LabId id, int i){   int n = 0;   LLink p;      if (ll->maxAuxLab < i)      HError(6570,"NumAuxCases: aux idx %d > max[%d]",i,ll->maxAuxLab);   for (p=ll->head->succ; p->succ!=NULL; p=p->succ)       if ((i==0) ? (p->labid==id) : (p->auxLab[i]==id))  ++n;   return n;}/* EXPORT->GetAuxCase: find nth occ of aux label i in label list */LLink GetAuxCase(LabList *ll, LabId id, int n, int i){   LLink p;   int k=0;      if (ll->maxAuxLab < i)      HError(6570,"GetAuxCase: aux idx %d > max[%d]",i,ll->maxAuxLab);   for (p=ll->head->succ; p->succ!=NULL; p=p->succ) {      if ((i==0) ? (p->labid==id) : (p->auxLab[i]==id))         if (++k==n) return p;   }   HError(6571,"GetAuxCase: %d case of %s nonexistent",n,id->name);   return NULL;}/* EXPORT->GetLabN: return n'th primary label */LLink GetLabN(LabList *ll, int n){   int count=0;   LLink p;   for (p=ll->head->succ; p->succ!= NULL; p=p->succ)       if (++count==n) return p;   HError(6571,"GetLabN: %d'th label nonexistent",n);   return NULL;}/* EXPORT->GetAuxLabN: return n'th aux i label */LLink GetAuxLabN(LabList *ll, int n, int i){   int count=0;   LLink p;   if (ll->maxAuxLab < i)      HError(6570,"GetAuxLabN: aux idx %d > max[%d]",i,ll->maxAuxLab);   for (p=ll->head->succ; p->succ!= NULL; p=p->succ)       if (i==0 || p->auxLab[i]!=NULL)               if (++count==n) return p;   HError(6571,"GetAuxLabN: %d'th aux[%d] label nonexistent",n,i);   return NULL;}/* EXPORT->CountLabs: count num primary labels in lab list */int CountLabs(LabList *ll){   int count = 0;   LLink p;   if (ll!=NULL)      for (p=ll->head->succ; p->succ!= NULL; p=p->succ)          ++count;   return(count);}/* EXPORT->CountAuxLabs: count num aux i labels in lab list */int CountAuxLabs(LabList *ll, int i){   int count = 0;   LLink p;   if (ll!=NULL)      for (p=ll->head->succ; p->succ != NULL; p=p->succ)         if (i==0 || p->auxLab[i]!=NULL)            ++count;   return(count);}/* EXPORT->AuxLabEndTime: return the end time for the i'th aux lab */HTime AuxLabEndTime(LLink p, int i){   LLink q;      q = p->succ;   while (i!=0 && q->succ != NULL && q->auxLab[i]==NULL){      p = q; q = q->succ;   }   return p->end;}/* PrintLabel: print the given label on one line */static void PrintLabel(LLink p, int maxAux){   int n;   LabId id;      printf("%8.0f%8.0f",p->start,p->end);   printf(" %8s %5f",p->labid->name,p->score);   for (n=1; n<=maxAux; n++){      id = p->auxLab[n];      printf(" %8s %5f",(id!=NULL)?id->name:"<null>",p->auxScore[n]);   }   printf("\n");}/* PrintList: print the given label list */static void PrintList(LabList *ll){   int i;   LLink p;      for (p=ll->head->succ,i=1; p->succ!= NULL; p=p->succ,i++) {      printf("%4d. ",i);      PrintLabel(p,ll->maxAuxLab);   }}/* EXPORT->PrintTranscription: for diagnostics */void PrintTranscription(Transcription *t, char *title){   int i;   LabList *ll;      printf("Transcription: %s [%d lists]\n",title,t->numLists);   ll = t->head;   for (i=1; i<=t->numLists; i++) {      printf(" List %d\n",i);      PrintList(ll); ll = ll->next;   }}/* FilterLevel: remove all but given level from transcription */static void FilterLevel(Transcription *t, int lev){   LabList *ll;   LLink p;         for (ll = t->head; ll != NULL; ll = ll->next) {      if (ll->maxAuxLab < lev)         HError(6570,"FilterLevel: level %d > max[%d]",lev,ll->maxAuxLab);      if (lev > 0) {         for (p=ll->head->succ; p->succ!=NULL; p=p->succ) {            if (p->auxLab[lev] != NULL) {               p->labid = p->auxLab[lev];               p->score = p->auxScore[lev];               p->end = AuxLabEndTime(p,lev);            } else               DeleteLabel(p);         }      }      ll->maxAuxLab = 0;   }}/* --------------- Format Specific Label File Input Routines --------------- */#define LEVELSEP "///"#define strlen_LEVELSEP 3#define COMMCHAR  ';'#define LFEED   '\012'#define CRETURN '\015'enum _TrSymbol{TRNULL,TRNUM,TRSTR,TREOL,TRLEV,TRCOMMA,TREOF};typedef enum _TrSymbol TrSymbol;static int curch = ' ';static TrSymbol trSym = TRNULL;static double trNum;static char trStr[256];/* IsNumeric: returns true if given string is a number */Boolean IsNumeric(char *s){   int i,len;   #ifdef WIN32   if(*s < 0)      return FALSE;#endif   len = strlen(s)-1;   if (!(isdigit((int) s[0])||s[0]=='+'||s[0]=='-')) return FALSE;   if (!(isdigit((int) s[len]))) return FALSE;      for (i=1; i<len; i++)      if (!(isdigit((int) s[i]) || s[i]=='.' || s[i]=='-' || s[i]=='+' || s[i]=='e' || s[i]=='E' ))         return FALSE;   return TRUE;}/* InitTrScan: initialise the scanner */static void InitTrScan(void){   curch = ' ';   trSym = TRNULL;}/* GetTrSym: get next symbol from f, remember that f might be an MLF             in which case EOF is a period on its own line   */static void GetTrSym(Source *src, Boolean htk){   int nxtch;   Boolean trSOL;   trNum = 0.0; trStr[0]='\0';    if (trSym==TRNULL) curch = GetCh(src);   if (trSym==TREOL || trSym==TRNULL)      trSOL=TRUE;   else      trSOL=FALSE;   while (curch == ' ' || curch == '\t') {      trSOL=FALSE;      curch = GetCh(src);   }   if (!htk && curch == COMMCHAR)      SkipLine(src);      switch (curch) {   case EOF:      trSym = TREOF;      break;   case LFEED:      curch = GetCh(src);      trSym = TREOL;      break;   case CRETURN:      curch = GetCh(src);      if (curch == LFEED) curch = GetCh(src);      trSym = TREOL;      break;   case ',':      if (!htk) {         curch = GetCh(src); trSym = TRCOMMA;         break;      }   case '.':      if (curch=='.' && trSOL==TRUE && mlfUsed>0 && htk) {         nxtch = GetCh(src);         if (nxtch == LFEED  || nxtch == CRETURN) {            trSym = TREOF;            break;         }         UnGetCh(nxtch,src);  /*  Requires more than one character pushback */      }   default:      if (htk) {/*当前htk为真,执行此分支*/         UnGetCh(curch,src);         if (!ReadString(src,trStr)) {            trSym=TREOF;            break;         }/*读出的标签描述字符串保存在trStr中,如trStr=oo,即音素单元名称*/         curch=GetCh(src);         if (trSOL && strcmp(LEVELSEP,trStr)==0) {            if (curch == LFEED  || curch == CRETURN) {               trSym = TRLEV;               break;            }         }      }      else {/*此分支不执行*/         nxtch=0;         do {            if (nxtch>=255) break;            trStr[nxtch++]=curch; curch=GetCh(src);         }         while (!isspace(curch) && curch != ',' && curch != EOF);         trStr[nxtch]='\0';         src->wasQuoted=FALSE;      }      if (!src->wasQuoted && IsNumeric(trStr)){         sscanf(trStr,"%lf",&trNum);         trSym = TRNUM;         break;      }      if (htk && compatMode &&           (strcmp(LEVELSEP,trStr)==0 || strcmp(".",trStr)==0)) {         src->wasNewline=FALSE;         SkipWhiteSpace(src);         if (src->wasNewline) {            curch = CRETURN;            trSym=(strcmp(LEVELSEP,trStr)==0?TRLEV:TREOF);            break;         }         curch = GetCh(src);      }      if (stripTriPhones) TriStrip(trStr);      trSym = TRSTR;      break;   }/*switch语句到此结束*/}/* -------------------- HTK Label Format ------------------ *//* ExtendAux: extend the aux arrays in given lab list to n elems    New elems are set to NULL/0.0 */static void ExtendAux(MemHeap *x, LabList *ll, int n){   int i,oldn;   LabId *id;   float *s;   LLink p;   if (n>=99)      HError(6570, "ExtendAux: Too many auxiliary fields in label file");      oldn = ll->maxAuxLab; ll->maxAuxLab = n;   for (p=ll->head->succ; p->succ!=NULL; p=p->succ){      id = (LabId *)New(x,sizeof(LabId)*n) - 1;       s = (float *)New(x,sizeof(float)*n) - 1;      for (i=1; i<=oldn; i++){         id[i] = p->auxLab[i];         s[i] = p->auxScore[i];      }      for (i=oldn+1; i<=n; i++){         id[i] = NULL;         s[i] = 0.0;      }      p->auxLab = id; p->auxScore = s;   }   if (trace&T_HTKL)      printf("HLabel:     aux extended from %d to %d\n",oldn,n);}/* LoadHTKList: load a single HTK label list - dont create anything if                 transAlt>0 and alt != transAlt */static LabList * LoadHTKList(MemHeap *x, Source *src, int alt){   LabList  *ll = NULL;   LabId labid, auxLab[100];   LLink p = NULL;   HTime start,end;   float score, auxScore[100];   int n,maxAux = 0;   Boolean ok;      ok = (transAlt==0) || (transAlt == alt);/*当前ok为真*/   if (ok) ll = CreateLabelList(x,maxAux);  /* assume no aux labels */   if (trace&T_HTKL)      printf("HLabel: looking for lab list\n");      while (trSym==TRNUM || trSym==TRSTR){/*当前trSym==TRSTR*/      start = -1; end = -1; score = 0.0;      if (trSym==TRNUM) {         start = trNum; GetTrSym(src,TRUE);         start *= htkLabelTimeScale;         if (trSym==TRNUM) {            end = trNum; GetTrSym(src,TRUE);            end *= htkLabelTimeScale;         }      }      if (trSym != TRSTR)         HError(6550,"LoadHTKList: Label Name Expected");      labid = GetLabId(trStr,TRUE);/*用描述标签字符串生成标签id,labid的name即为该标签字符串,如OO*/      GetTrSym(src,TRUE);      if (trSym==TRNUM){         score = trNum;         GetTrSym(src,TRUE);      }      if (trace&T_HTKL)         printf("HLabel: adding %.0f %.0f %s %f\n",start,end,labid->name,score);      if (ok) p = AddLabel(x,ll,labid,start,end,score);      /* Any aux labels ? */      n = 0;/*当前trSym=TREOL,故不执行下面的循环*/      while (trSym != TREOL && trSym!=TREOF) {

⌨️ 快捷键说明

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