hmodel.c

来自「隐马尔科夫模型工具箱」· C语言 代码 · 共 2,112 行 · 第 1/5 页

C
2,112
字号
         if (VectorSize(si->weights) != S){            HMError(src,"Incorrect number of stream weights");            return(NULL);         }      }      if((GetStream(hset,src,tok,si->pdf,nMix))<SUCCESS){         HMError(src,"GetStream failed");         return(NULL);      }      while(tok->sym==STREAM)         if((GetStream(hset,src,tok,si->pdf,nMix))<SUCCESS){            HMError(src,"GetStream failed");            return(NULL);         }      if (tok->sym==DURATION || (tok->sym==MACRO && tok->macroType=='d')){         if((si->dur = GetDuration(hset,src,tok))==NULL){            HMError(src,"GetDuration failed");            return(NULL);         }      }      else         si->dur = NULL;   }   if (S>1 && si->weights == NULL) {      si->weights = CreateSVector(hset->hmem,S);      for (i=1;i<=S;i++)         si->weights[i] = 1.0;   }   return si;  }/* GetTransMat: parse src and return Transition Matrix structure */static SMatrix GetTransMat(HMMSet *hset, Source *src, Token *tok){   SMatrix m;   int i,j;   short size;   Vector v;   float rSum;      if (trace&T_PAR) printf("HModel: GetTransMat\n");   if (tok->sym == TRANSP) {      if (!ReadShort(src,&size,1,tok->binForm)){         HMError(src,"Size of Transition matrix expected");         return(NULL);      }      if (size < 1){         HRError(7031,"GetTransMat: Bad size of transition matrix: %d\n", size);         return(NULL);      }      m = CreateSMatrix(hset->hmem,size,size);      if (!ReadMatrix(src,m,tok->binForm)){         HMError(src,"Transition Matrix expected");         return(NULL);      }      if (!hset->allowTMods && (m[1][size] > 0.0)) { /* kill teeModel */         rSum = 0.0; v = m[1]; v[size] = 0.0;         for (j=1;j<size;j++) rSum += v[j];         for (j=1;j<size;j++) v[j] /= rSum;      }           for (i=1;i<size;i++){                       /* convert to logs */         v=m[i]; rSum = 0.0;         for (j=1;j<=size;j++){            rSum += v[j];            v[j] = (v[j]<=MINLARG) ? LZERO : log(v[j]);         }         if (rSum<0.99 || rSum>1.01){            HRError(7031,"GetTransMat: Bad Trans Mat Sum in Row %d\n",i);            return(NULL);         }      }      v = m[size];      for (j=1;j<=size;j++)         v[j] =  LZERO;   } else {      if((m = (SMatrix)GetStructure(hset,src,'t'))==NULL){         HMError(src,"GetStructure failed");         return(NULL);      }      IncUse(m);   }   if(GetToken(src,tok)<SUCCESS){      HMError(src,"GetToken failed");      return(NULL);   }   return m;}/* GetHMMDef: get a hmm def from given source */static ReturnStatus GetHMMDef(HMMSet *hset, Source *src, Token *tok,                              HLink hmm, int nState){   short state;   int N=0;   StateElem *se;   char buf[MAXSTRLEN];      if (trace&T_PAR) printf("HModel: GetHMMDef\n");   if (tok->sym != BEGINHMM){      HMError(src,"<BeginHMM> symbol expected");      return(FAIL);   }   if(GetToken(src,tok)<SUCCESS){      HMError(src,"GetHMMDef: GetToken failed");      return(FAIL);   }   if (tok->sym == USEMAC){      /* V1 style USE clause */      if (!ReadString(src,buf)){         HRError(7013,"GetHMMDef: cannot read USE macro name");         return(FAIL);      }      if(GetToken(src,tok)<SUCCESS){         HMError(src,"GetHMMDEf: GetToken failed");         return(FAIL);      }   }   while (tok->sym != STATE) {      if(GetOption(hset,src,tok, &N)<SUCCESS){         HMError(src,"GetHMMDef: GetOption failed");         return(FAIL);      }      if (N>nState) nState = N;   }   FreezeOptions(hset);   if (nState==0){      HMError(src,"NumStates not set");      return(FAIL);   }   hmm->numStates = N = nState;   se = (StateElem *)New(hset->hmem,(N-2)*sizeof(StateElem));   hmm->svec = se - 2;   while (tok->sym == STATE) {      if (!ReadShort(src,&state,1,tok->binForm)){         HMError(src,"States index expected");         return(FAIL);      }      if (state<2 || state >= N){         HMError(src,"State index out of range");         return(FAIL);      }      if(GetToken(src,tok)<SUCCESS){         HMError(src,"GetHMMDef: GetToken failed");         return(FAIL);      }      se = hmm->svec+state;      if((se->info = GetStateInfo(hset,src,tok))==NULL){         HMError(src,"GetStateInfo failed");         return(FAIL);      }        }   if (tok->sym==TRANSP || (tok->sym==MACRO && tok->macroType=='t')){      if((hmm->transP = GetTransMat(hset,src,tok))==NULL){         HMError(src,"GetTransMat failed");         return(FAIL);      }           if (NumRows(hmm->transP) != N ||          NumCols(hmm->transP) != N){         HRError(7030,"GetHMMDef: Trans Mat Dimensions not %d x %d",N,N);         return(FAIL);      }   }   else{      HMError(src,"Transition Matrix Missing");      return(FAIL);   }   if (tok->sym==DURATION || (tok->sym==MACRO && tok->macroType=='d')){      if((hmm->dur = GetDuration(hset,src,tok))==NULL){         HMError(src,"GetDuration Failed");         return(FAIL);      }   }   else      hmm->dur = NULL;   if (tok->sym!=ENDHMM){      HMError(src,"<EndHMM> symbol expected");      return(FAIL);   }   if(GetToken(src,tok)<SUCCESS){      HMError(src,"GetHMMDef: GetToken failed");      return(FAIL);   }   return(SUCCESS);}/* GetBias: parse src and return Bias structure */static SVector GetBias(HMMSet *hset, Source *src, Token *tok){   SVector m = NULL;   MemHeap *hmem;   short size;      if (trace&T_PAR) printf("HModel: GetBias\n");   if (tok->sym==BIAS) {            if (hset==NULL) hmem = &xformStack;      else hmem = hset->hmem;      if (!ReadShort(src,&size,1,tok->binForm)){         HMError(src,"Size of Bias Vector expected");         return(NULL);      }      m = CreateSVector(hmem,size);      if (!ReadVector(src,m,tok->binForm)){         HMError(src,"Bias Vector expected");         return(NULL);      }   }      else if ((tok->sym==MACRO && tok->macroType=='y') && (hset != NULL)) {      if((m=(SVector)GetStructure(hset,src,'y'))==NULL){         HMError(src,"GetStructure Failed");         return(NULL);      }      IncUse(m);   } else{      HMError(src,"<BIAS> symbol expected in GetBias");      return(NULL);   }   if(GetToken(src,tok)<SUCCESS){      HMError(src,"GetToken failed");      return(NULL);   }   return m;}/* GetLinXForm: get a linear transformations */static LinXForm* GetLinXForm(HMMSet *hset, Source *src, Token *tok){   LinXForm *xf;   MemHeap *hmem;   int i,b;   int numBlocks;   if (trace&T_PAR) printf("HModel: GetLinXForm\n");   if (tok->sym == VECSIZE) {      if (hset==NULL) hmem = &xformStack;      else hmem = hset->hmem;      xf = (LinXForm *)New(hmem,sizeof(LinXForm));      if (!ReadInt(src,&(xf->vecSize),1,tok->binForm)){         HRError(7013,"GetLinXForm: cannot read vector size");         return(NULL);      }      if(GetToken(src,tok)<SUCCESS){         HMError(src,"GetToken failed");         return(NULL);      }      if (tok->sym == OFFSET){         if(GetToken(src,tok)<SUCCESS){            HMError(src,"GetToken failed");            return(NULL);         }         xf->bias = GetBias(hset,src,tok);      } else {         xf->bias = NULL;      }      if (tok->sym!=BLOCKINFO){         HMError(src,"<BLOCKINFO> symbol expected");         return(NULL);      }      if (!ReadInt(src,&numBlocks,1,tok->binForm)){         HMError(src,"Number of transform blocks expected");         return(NULL);      }      xf->blockSize = CreateIntVec(hmem,numBlocks);      if (!ReadInt(src,xf->blockSize+1,numBlocks,tok->binForm)){         HMError(src,"Size of blocks expected");         return(NULL);      }          xf->xform = (Matrix *)New(hmem,(numBlocks+1)*sizeof(Matrix));      if(GetToken(src,tok)<SUCCESS){         HMError(src,"GetToken failed");         return(NULL);      }      for (i=1;i<=numBlocks;i++) {         if (tok->sym != BLOCK){            HMError(src,"<NUMXFORMS> symbol expected");            return(NULL);         }         if (!ReadInt(src,&b,1,tok->binForm)){            HMError(src,"Weight class expected");            return(NULL);         }               if (b != i) {            HMError(src,"Inconsistency in transform definition");            return(NULL);         }         if(GetToken(src,tok)<SUCCESS){            HMError(src,"GetToken failed");            return(NULL);         }         xf->xform[i] = GetTransform(hset,src,tok);      }      xf->nUse = 0;   }   else if ((tok->sym==MACRO && tok->macroType=='f') && (hset != NULL)){      if((xf=(LinXForm *)GetStructure(hset,src,'f'))==NULL){         HMError(src,"GetStructure Failed");         return(NULL);      }      xf->nUse++;      if(GetToken(src,tok)<SUCCESS){         HMError(src,"GetToken failed");         return(NULL);      }   } else{      HMError(src,"<VECSIZE> symbol expected in GetXFormSet");      return(NULL);   }   return xf;}/* GetInputXForm: get the input linear transformations */static InputXForm* GetInputXForm(HMMSet *hset, Source *src, Token *tok){   char buf[MAXSTRLEN];   MemHeap *hmem;   InputXForm *xf;   if (trace&T_PAR) printf("HModel: GetXForm\n");   if (tok->sym == MMFIDMASK) {       if (hset==NULL) hmem = &xformStack;      else hmem = hset->hmem;      xf = (InputXForm *)New(hmem,sizeof(InputXForm));      xf->fname = CopyString(hmem,src->name);      xf->xformName = NULL;      if (!ReadString(src,buf)){         HRError(7013,"GetInputXForm: cannot read MMFIDMASK");         return(NULL);      }      xf->mmfIdMask = CopyString(hmem,buf);      if(GetToken(src,tok)<SUCCESS){         HMError(src,"GetToken failed");         return(NULL);      }      if (tok->sym != PARMKIND) {         HMError(src,"parameter kind symbol expected");         return(NULL);      }      xf->pkind = tok->pkind;      if(GetToken(src,tok)<SUCCESS){         HMError(src,"GetToken failed");         return(NULL);      }      if (tok->sym == PREQUAL) {         xf->preQual = TRUE;         if(GetToken(src,tok)<SUCCESS){            HMError(src,"GetToken failed");            return(NULL);         }      } else          xf->preQual = FALSE;      if (tok->sym != LINXFORM){         HMError(src,"<LINXFORM> symbol expected");         return(NULL);      }      if(GetToken(src,tok)<SUCCESS){         HMError(src,"GetToken failed");         return(NULL);      }      xf->xform = GetLinXForm(hset,src,tok);      xf->nUse=0;   }   else if ((tok->sym==MACRO && tok->macroType=='j') && (hset != NULL)){      if((xf=(InputXForm *)GetStructure(hset,src,'j'))==NULL){         HMError(src,"GetStructure Failed");         return(NULL);      }      xf->nUse++;      if(GetToken(src,tok)<SUCCESS){         HMError(src,"GetToken failed");         return(NULL);      }   } else{      HMError(src,"<MMFIDMASK> symbol expected in GetInputXForm");      return(NULL);   }   return xf;}/* ---------------------- Symbol Output Routine ------------------------ *//* PutSymbol: output symbol to f in ascii or binary form */static void PutSymbol(FILE *f, Symbol sym, Boolean binary){   if (binary){      fputc(':',f);fputc(sym,f);   } else      fprintf(f,"<%s>",symNames[sym]);}/* ------------- Special Tied Mixture Output Routines ------------------ */static Boolean putWtActive;   /* false until a weight is output *//* PutTiedWeight: output mix weight via 1-item buffer */void PutTiedWeight(FILE *f, short repeatLast, float w, Boolean binary){   static float putWtCurrent;  /* the current output weight */      if (putWtActive)  {        /* not 1st time so output last w */      if (binary){         if (repeatLast>0) {            putWtCurrent = putWtCurrent-2.0;            WriteFloat(f,&putWtCurrent,1,binary);            fputc(repeatLast,f);         } else            WriteFloat(f,&putWtCurrent,1,binary);      } else {         fprintf(f," %e",putWtCurrent);         if (repeatLast>0)             fprintf(f,"*%d",repeatLast);      }   } else {      putWtActive = TRUE;   }   putWtCurrent = w;   if (!binary && w<0) /* all done */      fprintf(f,"\n");}/* PutTiedWeights: output mixture weights in compact format */void PutTiedWeights(FILE *f, StreamElem *se, Bool

⌨️ 快捷键说明

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