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

📄 hdman.c

📁 Hidden Markov Toolkit (HTK) 3.2.1 HTK is a toolkit for use in research into automatic speech recogn
💻 C
📖 第 1 页 / 共 4 页
字号:
   LabId item;      if (l!=NULL && l!=wdBnd && c!=wdBnd){      strcpy(buf,l->name); strcat(buf,"-");       strcat(buf,c->name);   } else       strcpy(buf,c->name);   if (r!=NULL && r!=wdBnd && c!=wdBnd){      strcat(buf,"+"); strcat(buf,r->name);   }   item = GetLabId(buf,TRUE);   return item;}/* TriPhon: convert phone labels in pronunciation to either      left, right or triphone (ie both) contexts */void TriPhon(Pronunciation *p, Boolean left, Boolean right,              LabId stId, LabId enId){   int i;   LabId leftId, centreId, rightId;      if (p->nPhone <= 2)      HError(1430,"TriPhon: cant add context to null phone");   leftId =left?stId:NULL;    /* First Phone */   centreId = p->phone[1];   rightId = right?enId:NULL;   if (p->nPhone > 3 && right)      rightId = p->phone[2];   p->phone[1] = MakeTriId(leftId,centreId,rightId);   for (i=2;i<p->nPhone-2;i++) {         /* Middle Phones */      leftId =left?centreId:NULL;      centreId = p->phone[i];      rightId = right?p->phone[i+1]:NULL;      p->phone[i] = MakeTriId(leftId,centreId,rightId);   }   if (p->nPhone > 3) {        leftId = left?centreId:NULL;     /* Last Phone */      centreId = p->phone[p->nPhone-2];      rightId = right?enId:NULL;      p->phone[p->nPhone-2] = MakeTriId(leftId,centreId,rightId);   }}/* Triphonise: given word buffer */void Triphonise(WordBuf *wb,  Boolean left, Boolean right,                 LabId stId, LabId enId){   int i;   Pronunciation *p;      for (i=0; i<wb->nPron; i++){      p = wb->pron+i;      TriPhon(p,left,right,stId,enId);   }}        /* LCtxtOp: Left context command */void LCtxtOp(WordBuf *wb, int nArgs,LabId *args){   LabId x;      x = nArgs==1?*args:NULL;   Triphonise(wb,TRUE,FALSE,x,x);}/* RCtxtOp: Right context command */void RCtxtOp(WordBuf *wb, int nArgs,LabId *args){   LabId x;      x = nArgs==1?*args:NULL;   Triphonise(wb,FALSE,TRUE,x,x);}/* TCtxtOp: Left and Right context command */void TCtxtOp(WordBuf *wb, int nArgs,LabId *args){   LabId l,r;      if (nArgs == 1)      l = r = *args;   else if (nArgs == 2){      l = *args; r = *(args+1);   } else      l = r = NULL;   Triphonise(wb,TRUE,TRUE,l,r);}/* RemStress: remove stress marks from all phones */void RemStress(WordBuf *wb, LabId *args){   LabId mode = *args;   int i,j,k;   Pronunciation *p;   char buf[50];      if (mode != cmuId)      HError(1450,"RemStress: Unknown stress marking %s",mode->name);   for (k=0; k<wb->nPron; k++){      p = wb->pron+k;      for (i=0; i<p->nPhone; i++)         if (mode == cmuId) {            strcpy(buf,p->phone[i]->name);            j = strlen(buf) - 1;            if (isdigit((int) buf[j])){               buf[j] = '\0';               p->phone[i] = GetLabId(buf,TRUE);            }         }   }}/* EditWordBuf: apply each script command to given DBuffer */ void EditWordBuf(DBuffer *db){   ScriptItem *i;   WordBuf *wb;   if (db->script == NULL) return;   wb = &(db->wbuf);   if (trace&T_EDW0) {      printf(" editing word %s in dict %s\n",(wb->word)->name,db->name);   }   if (trace&T_EDW1) {      printf("   before:\n");       if (db->wbuf.nPron>0 )          WriteDictWord(db,stdout,4, FALSE);      else         printf("    ** deleted **\n");   }   i = db->script;   while (i != NULL && wb->nPron > 0) {      switch (i->cmd.op) {      case UCWORD:  /* done on input */         if (db->wop == NOCMD) /* must be output edit */            wb->word = UCase(wb->word);         break;      case LCWORD:         if (db->wop == NOCMD) /* must be output edit */            wb->word = LCase(wb->word);         break;      case UCPHONE:         UCPhoneOp(wb); break;      case LCPHONE:         LCPhoneOp(wb); break;      case DELETEW:         DeleteWordOp(wb,i->cmd.args); break;      case DELSOURCE:         db->pronsUsed -= DeleteSourceOp(wb,i->cmd.args);          break;      case DELDEF:         DelDefOp(wb,i->cmd.args); break;      case FUNCW:         FunctionWordOp(wb,i->cmd.args); break;      case REPLACEP:         ReplacePhoneOp(wb,i->cmd.args); break;      case REPLACEW:         ReplaceWordOp(wb,i->cmd.args); break;      case CONREPLACE:         ContextReplaceOp(wb,i->cmd.args,db); break;      case MERGEP:         MergePhoneOp(wb,i->cmd.nArgs,i->cmd.args); break;      case SPLITP:         SplitPhoneOp(wb,i->cmd.nArgs,i->cmd.args); break;      case DELETEP:         DeletePhoneOp(wb,i->cmd.args); break;      case APPSIL:         AppendSilenceOp(wb,i->cmd.args); break;      case LCTXT:         LCtxtOp(wb,i->cmd.nArgs,i->cmd.args); break;      case RCTXT:         RCtxtOp(wb,i->cmd.nArgs,i->cmd.args); break;      case TCTXT:         TCtxtOp(wb,i->cmd.nArgs,i->cmd.args); break;      case REMSTRESS:         RemStress(wb,i->cmd.args); break;      default: break;      }      if (trace&T_EDW1) {         printf("      after %s: ",cmdMap[i->cmd.op]);         WriteDictWord(db,stdout,6, FALSE);      }      i = i->next;   }   if (trace&T_EDW0) {      printf("   after:\n");       if (db->wbuf.nPron>0 )          WriteDictWord(db,stdout,4, FALSE);      else         printf("    ** deleted **\n");   }}/* ----------------------- Log Information  --------------------------- */void PrintUsage(DBuffer *db){   char buf[50];   NameOf(db->name,buf); buf[12] = '\0';   fprintf(logF,"%12s %9d %10d %10d %10d\n", buf, db->totalWords, db->wordsUsed,           db->totalProns, db->pronsUsed);}/* PrintLog: output log information to log file */void PrintLog(void){   int i;   fprintf(logF,"\nDictionary Usage Statistics\n");   fprintf(logF,"---------------------------\n");   fprintf(logF,"  Dictionary    TotalWords WordsUsed  TotalProns PronsUsed\n");   for (i=0; i<nInputs; i++)      PrintUsage(inbuf+i);   PrintUsage(&outbuf);   fprintf(logF,"\n");   fprintf(logF,"%d words required, %d missing\n\n", numOut,numMissing);   ListNewPhones();   fprintf(logF,"\n");   if (!nullOutput)      fprintf(logF,"Dictionary %s created\n",outbuf.name);}/* ----------------------- Edit and Merge  --------------------------- *//* SetActiveCount: scan inputs and count num still active */void SetActiveCount(void){   int i,sum;   for (i=0,sum=0; i<nInputs; i++)      if (inbuf[i].nextWord != NULL) ++sum;   numActive = sum;}/* HighestInput: return alphabetically highest next word */LabId HighestInput(void){   int i, hi;   LabId best,next;   i = 0;    while ((next=inbuf[i].nextWord) == NULL) i++;   hi = i++; best = next;   while (i<nInputs){      if ((next=inbuf[i].nextWord) != NULL)         if (strcmp(next->name,best->name) < 0){            best = next; hi = i;         }      i++;   }   return best;}/* ScanDict: scan thru given dictionary until required word is found or   dictionary is exhausted.  Dicts are sorted so stop as soon as next   word is alphabetically after required word. If found then return true   and load required word in wordbuf. */Boolean ScanDict(DBuffer *db, LabId reqd){   int scmp;   if (db->nextWord == NULL) return FALSE;   scmp = strcmp(db->nextWord->name,reqd->name);   while (scmp<0){      ReadDictProns(db);      if (db->nextWord == NULL) return FALSE;      scmp = strcmp(db->nextWord->name,reqd->name);   }   if (scmp==0)      ReadDictProns(db);   return scmp==0;}/* FillInputs: scan inputs until current word in each is >= required word.   If no wordlist is given then the required word is the alphabetically   highest ranked word across all the inputs.  If a wordlist is given   then it is just the next word in the list. Returns false when all   inputs or wordlist is exhausted */Boolean FillInputs(Boolean *valid){   int i;   if (wList != NULL){      if (widx == nWords) return FALSE;      required = wList[widx++];   } else       required = HighestInput();   for (i=0; i<nInputs; i++)      valid[i] = ScanDict(inbuf+i,required);   SetActiveCount();   return TRUE;}/* CopyWordBuf: copy word buf in s to target t */void CopyWordBuf(DBuffer *s, DBuffer *t){   WordBuf *ws,*wt;   Pronunciation *ps,*pt;   int i,j;         ++t->totalWords;   if (s->wbuf.nPron == 0) return;   ++t->wordsUsed;   ++s->wordsUsed;   s->pronsUsed += s->wbuf.nPron;   t->totalProns  += s->wbuf.nPron;   t->pronsUsed   += s->wbuf.nPron;   ws = &(s->wbuf); wt = &(t->wbuf);   if (trace&T_WBUF){      printf(" copy word %-15s in %s to %s\n",             ws->word->name,s->name,t->name); fflush(stdout);   }   wt->nPron = ws->nPron;   wt->word = ws->word;   wt->outsym = ws->outsym;   for (i=0; i<ws->nPron; i++){      ps = ws->pron+i; pt = wt->pron+i;      pt->nPhone = ps->nPhone;      pt->source = ps->source;      pt->prob = ps->prob;      for (j=0; j<ps->nPhone; j++)         pt->phone[j] = ps->phone[j];   }}/* AppendWordBuf: append word buf in s to target t */void AppendWordBuf(DBuffer *s, DBuffer *t){   WordBuf *ws,*wt;   Pronunciation *ps,*pt;   int i,j,k;   if (s->wbuf.nPron == 0) return;   ++s->wordsUsed; ++t->totalWords;   s->pronsUsed += s->wbuf.nPron;   t->totalProns   += s->wbuf.nPron;   ws = &(s->wbuf); wt = &(t->wbuf);   if (trace&T_WBUF)      printf(" appd word %-15s in %s to %s\n",             ws->word->name,s->name,t->name);   j = wt->nPron;   wt->nPron += ws->nPron;   if (wt->nPron > MAXPRONS)      HError(1430,"AppendWordBuf: MAXPRONS exceeded");   if (wt->word != ws->word)      HError(1490,"AppendWordBuf: words differ %s vs %s",             ws->word->name,wt->word->name);   for (i=0; i<ws->nPron; i++,j++){      ps = ws->pron+i; pt = wt->pron+j;      pt->nPhone = ps->nPhone;      pt->source = ps->source;      pt->prob = ps->prob;      for (k=0; k<ps->nPhone; k++)         pt->phone[k] = ps->phone[k];   }}/* HasDuplicate: return true if given pron has preceding duplicate */Boolean HasDuplicate(DBuffer *db, int pronNum){   Pronunciation *s, *t;   int i,k;   Boolean found = FALSE;   s = db->wbuf.pron+pronNum;   for (i=0; !found && i<pronNum; i++) {      t = db->wbuf.pron+i;      if (t->nPhone != s->nPhone) continue;      found = TRUE;      for (k=1; found && k<t->nPhone-1; k++)         if (t->phone[k] != s->phone[k]) found = FALSE;   }   return found;}/* RemDuplicates: remove duplicate pronunciations from buffer */void RemDuplicates(DBuffer *db){   int i,j,k;   Pronunciation *s, *t;   for (i=db->wbuf.nPron-1; i>0; i--) {      if (HasDuplicate(db,i)){         if (trace&T_WBUF)            printf(" removing dup %d from word %s in %s\n",i,                   db->wbuf.word->name,db->name);         --db->wbuf.nPron;         --db->pronsUsed;         for (j=i; j<db->wbuf.nPron; j++){            t = db->wbuf.pron+j; s = t+1;            t->nPhone = s->nPhone;            t->source = s->source;            t->prob = s->prob;            for (k=0; k<t->nPhone; k++)               t->phone[k] = s->phone[k];         }      }   }}/* EditAndMerge: main processing loop */void EditAndMerge(void){   Boolean valid[MAXDICTS];   int i;   SetActiveCount();   while(numActive > 0  && FillInputs(valid)){      ++numOut;      if (trace&T_WBUF) printf("\n%d:\n",numOut);      if (trace&T_VALI){         printf(" valid inputs [");         for (i=0; i<nInputs; i++)            if (valid[i]) printf(" %d",i);         printf("]\n");      }      i=0;      while (!valid[i] && i<nInputs) i++;      if (i<nInputs){         EditWordBuf(inbuf+i);         CopyWordBuf(inbuf+i,&outbuf);          i++;         if (mergeProns){            while (i<nInputs){               if (valid[i]){                  EditWordBuf(inbuf+i);                  AppendWordBuf(inbuf+i,&outbuf);               }               ++i;              }         }         RemDuplicates(&outbuf);         EditWordBuf(&outbuf);         RemDuplicates(&outbuf);         WriteDictWord(&outbuf, outfile, 0, TRUE);      } else {         if (isLogging){            if (numMissing==0){               fprintf(logF,"Missing Words\n");               fprintf(logF,"-------------\n");            }            fprintf(logF,"%s\n",required->name);         }         ++numMissing;         if (trace&T_WBUF)            printf("%s missing\n",required->name);      }   }   if (trace&T_TOP) {      if (!nullOutput)         printf("Dictionary %s created - ",outbuf.name);      printf("%d words processed, %d missing\n", numOut,numMissing);   }}/* ---------------------------------------------------------------- *//*                         END:  HDMan.c                            *//* ---------------------------------------------------------------- */

⌨️ 快捷键说明

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