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

📄 hutil.c

📁 实现HMM算法
💻 C
📖 第 1 页 / 共 3 页
字号:
{   HMMDef *hmm;   ILink h;   int s,j;   IntSet streams;   Keyword kw;      switch(kw=GetKey()) {   case MIX_KEY:   case STREAM_KEY:      if (hset->hsKind==TIEDHS || hset->hsKind==DISCRETEHS)         HError(7231,"PStatecomp: Cannot specify streams or mixes unless continuous");      streams = CreateSet(SMAX);      if(kw==STREAM_KEY) {         PIndex(streams);         SkipSpaces();         if (ch != '.')            EdError(". expected after stream spec");         ReadCh();         if (GetKey() != MIX_KEY)            EdError("Mix expected after Stream index");      } else          AddMember(streams,1);      SkipSpaces();      if (ch=='[')         PMix(models,ilist,type,states,streams,hset);      else {         ChkType('p',type);         for (h=models; h!=NULL; h=h->next) {            hmm = h->owner;            for (j=2; j<hmm->numStates; j++)               if (IsMember(states,j))                      for (s=1; s<=hset->swidth[0];s++)                        if (IsMember(streams,s)) { /* tie -> spdf */                        if (trace & T_ITM)                           printf(" %12s.state[%d].stream[%d]\n",                                  HMMPhysName(hset,hmm),j,s);                        AddItem(hmm,hmm->svec[j].info->pdf+s,ilist);                     }         }      }      FreeSet(streams);      break;   case DUR_KEY:      ChkType('d',type);      for (h=models; h!=NULL; h=h->next) {         hmm = h->owner;         for (j=2; j<hmm->numStates; j++)            if (IsMember(states,j)) {  /* tie ->dur */               if (trace & T_ITM)                   printf(" %12s.state[%d].dur\n",                         HMMPhysName(hset,hmm),j);               AddItem(hmm,hmm->svec[j].info,ilist);            }      }      break;   case WEIGHTS_KEY:      ChkType('w',type);      for (h=models; h!=NULL; h=h->next) {         hmm = h->owner;         for (j=2; j<hmm->numStates; j++)            if (IsMember(states,j)) { /* tie ->stream weights */               if (trace & T_ITM)                  printf(" %12s.state[%d].weights\n",                         HMMPhysName(hset,hmm),j);               AddItem(hmm,hmm->svec[j].info,ilist);            }      }      break;   default:      EdError("dur, weight, stream or mix expected");   }}/* PState: parse state and add all matches in models to ilist */static void PState(ILink models, ILink *ilist, char *type, HMMSet *hset){   IntSet states;   int j;   HMMDef *hmm;   ILink h;      states = CreateSet(maxStates);   PIndex(states);   SkipSpaces();   if (ch == '.') {      ReadCh();      PStatecomp(models,ilist,type,states,hset);   } else {      ChkType('s',type);      for (h=models; h!=NULL; h=h->next) {         hmm = h->owner;         for (j=2; j<hmm->numStates; j++)            if (IsMember(states,j)) {  /* tie ->info */               if (trace & T_ITM)                  printf(" %12s.state[%d]\n",                         HMMPhysName(hset,hmm),j);               AddItem(hmm,hmm->svec+j,ilist);            }      }   }     FreeSet(states);  }/* PHIdent: parse a hmm ident and do pattern match */static void PHIdent(ILink *models, HMMSet *hset){   char pattern[MAXSTRLEN];   int h;    MLink q;   LabId hmmId;   Boolean fullName=TRUE; /* are there wildcards in the name */   char *p;      SkipSpaces();   GetAlpha(pattern);   p = pattern; h=0;   while ((*p != '\0') && (h<MAXSTRLEN) && (fullName)) {     if ((*p=='*')||(*p=='?')||(*p=='%')) fullName=FALSE;     h++;      p = pattern+h;   }   if (fullName) { /* this is the name of the model */      hmmId = GetLabId(pattern,FALSE);      q = FindMacroName(hset,'l',hmmId);      if (q != NULL) {         if (trace & T_ITM)            printf("%s ",hmmId->name);         AddItem((HLink) q->structure, q->structure, models);      }   } else { /* need to search for all models that match */     for (h=0; h<MACHASHSIZE; h++)       for (q=hset->mtab[h]; q!=NULL; q=q->next)         if (((q->type=='h') && (parsePhysicalHMM)) || ((q->type=='l') && (!parsePhysicalHMM))) {	   if (DoMatch(q->id->name,pattern)) {	     if (trace & T_ITM)	       printf("%s ",q->id->name);	     AddItem((HLink) q->structure, q->structure, models);	   }         }   }}/* PHName: parse hname and return list of matching models */static void PHName(ILink *models,HMMSet *hset){   if (trace & T_ITM)      printf(" Models \n  ");      SkipSpaces();   if (ch == '(') {      ReadCh();      PHIdent(models,hset);      SkipSpaces();      while(ch == ',') {         ReadCh();         PHIdent(models,hset);         SkipSpaces();      }      if (ch != ')')         EdError(") expected");      ReadCh();   } else      PHIdent(models,hset);   if (trace & T_ITM) {      printf("\n  %d models in itemlist\n",NumItems(*models));      fflush(stdout);   }}/* PItemSet: parse and item set appending items to ilist */static void PItemSet(ILink *ilist, char *type, HMMSet *hset){   ILink models = NULL;         /* list of hmms in item set */   ILink p;      PHName(&models,hset);        /* parse hname and get list of models */   SkipSpaces();   if (ch == '.') {             /* look for subcomponents */      ReadCh();      switch (GetKey()) {      case TRANSP_KEY:          AddTransP(models,ilist,type);          break;      case STATE_KEY:           PState(models,ilist,type,hset);         break;      default:          EdError("State or TransP expected");      }      FreeItems(&models);   } else {                     /* append just the list of models to ilist */      ChkType('h',type);      p = *ilist;      if (p==NULL)         *ilist = models;      else {         while (p->next != NULL) p = p->next;         p->next = models;      }   }}/* EXPORT->PItemList: parse items in item list setting ilist and type */char *PItemList(ILink *ilist, char *type, HMMSet *hset,                Source *s, Boolean itrace){   int rtrace;   /* Initialise static variables */   rtrace=trace;   if (itrace)      trace|=T_ITM;   source = s;   maxMixes = MaxMixInSet(hset);   maxStates = MaxStatesInSet(hset);   position=pattern;   *position=0;   /* Parse item set */   ReadCh();   SkipSpaces();   if (ch != '{')      EdError("{ expected");   ReadCh();   PItemSet(ilist,type,hset);   SkipSpaces();   while (ch == ',') {      ReadCh();      PItemSet(ilist,type,hset);      SkipSpaces();   }   if (ch != '}')      EdError("} expected");   if (trace & T_ITM)      fflush(stdout);   trace=rtrace;      return(pattern);}/* ------------------- Generic macro handling ----------------- *//* EXPORT->GetMacroHook: Return value of hook field for any macro */Ptr GetMacroHook(MLink ml){   Ptr hook;   switch(ml->type) {   case 'l': /* HLink */   case 'h': /* HLink */      hook=((HLink)(ml->structure))->hook; break;   case 's': /* StateInfo * */      hook=((StateInfo *)(ml->structure))->hook; break;   case 'm': /* MixPDF * */      hook=((MixPDF *)(ml->structure))->hook; break;   case 'c': /* STriMat */   case 'i': /* STriMat */   case 'x': /* SMatrix */   case 't': /* SMatrix */   case 'u': /* SVector */   case 'd': /* SVector */   case 'w': /* SVector */   case 'v': /* SVector */      hook=GetHook(ml->structure); break;   case '*': /* deleted */      hook=NULL; break;   case 'o': /* fake */   default:      hook=NULL;      HError(7270,"GetMacroHook: Getting hook of non-existant macro");   }   return(hook);}/* EXPORT->SetMacroHook: Set value of hook field for any macro */void SetMacroHook(MLink ml,Ptr hook){   switch(ml->type) {   case 'l': /* HLink */   case 'h': /* HLink */      ((HLink)(ml->structure))->hook=hook; break;   case 's': /* StateInfo * */      ((StateInfo *)(ml->structure))->hook=hook; break;   case 'm': /* MixPDF * */      ((MixPDF *)(ml->structure))->hook=hook; break;   case 'c': /* STriMat */   case 'i': /* STriMat */   case 'x': /* SMatrix */   case 't': /* SMatrix */   case 'u': /* SVector */   case 'd': /* SVector */   case 'w': /* SVector */   case 'v': /* SVector */      SetHook(ml->structure,hook); break;   case 'r': /* fake */   case 'b':   case 'j':      break;   case '*': /* deleted */   case 'o': /* fake */   default:      HError(7270,"SetMacroHook: Setting hook of non-existant macro %s",             &(ml->type));   }}/* EXPORT->GetMacroUse: Return value of use field for any macro */int GetMacroUse(MLink ml){   int use;   switch(ml->type) {   case 'l': /* HLink */   case 'h': /* HLink */      use=((HLink)(ml->structure))->nUse; break;   case 's': /* StateInfo * */      use=((StateInfo *)(ml->structure))->nUse; break;   case 'm': /* MixPDF * */      use=((MixPDF *)(ml->structure))->nUse; break;   case 'j': /* InputXForm * */      use=((InputXForm *)(ml->structure))->nUse; break;   case 'c': /* STriMat */   case 'i': /* STriMat */   case 'x': /* SMatrix */   case 't': /* SMatrix */   case 'u': /* SVector */   case 'd': /* SVector */   case 'w': /* SVector */   case 'v': /* SVector */      use=GetUse(ml->structure); break;   case '*': /* deleted */   case 'b':   case 'r':          use=-1; break;   case 'o': /* fake */   default:      use=-1;      HError(7270,"GetMacroUse: Getting use of non-existant macro");   }   return(use);}/* EXPORT->SetMacroUse: Set value of use field for any macro */void SetMacroUse(MLink ml,int use){   switch(ml->type) {   case 'l': /* HLink */   case 'h': /* HLink */      ((HLink)(ml->structure))->nUse=use; break;   case 's': /* StateInfo * */      ((StateInfo *)(ml->structure))->nUse=use; break;   case 'm': /* MixPDF * */      ((MixPDF *)(ml->structure))->nUse=use; break;   case 'j': /* InputXForm * */      ((InputXForm *)(ml->structure))->nUse=use; break;   case 'c': /* STriMat */   case 'i': /* STriMat */   case 'x': /* SMatrix */   case 't': /* SMatrix */   case 'u': /* SVector */   case 'd': /* SVector */   case 'w': /* SVector */   case 'v': /* SVector */      SetUse(ml->structure,use); break;   case '*': /* deleted */   case 'o': /* fake */   case 'b':   case 'r':       break;   default:      HError(7270,"SetMacroUse: Setting use of non-existant macro");   }}/* EXPORT->ResetHooks: stores NULL in all hook fields of all current macros */void ResetHooks(HMMSet *hset,char *what){   int h;   MLink q;   for (h=0; h<MACHASHSIZE; h++)      for (q=hset->mtab[h]; q!=NULL; q=q->next) {         if (q->type=='*')              continue;         if (what==NULL || strchr(what,q->type))            SetMacroHook(q,NULL);      }}/* ------------------- Load Statistics File  --------------------- *//* EXPORT->LoadStatsFile: load the statistics file output by HERest */void LoadStatsFile(char *statfile,HMMSet *hset,Boolean otrace){   Source src;   char hname[256];   int i,idx,count,N,lnum = 0;   float x;   HMMDef *hmm;   MLink ml;   LabId hmmId;   double occSum = 0.0;   long occN = 0;   StateInfo *si;   Boolean bin=FALSE;   if(InitSource(statfile,&src,NoFilter)<SUCCESS)      HError(7210,"LoadStatsFile: Can't open file %s", statfile);   while(ReadInt(&src,&idx,1,bin)) {      ++lnum;      if (!ReadString(&src,hname) || !ReadInt(&src,&count,1,bin))         HError(7250,"LoadStatsFile: Format error in file %s line %d",                statfile,lnum);      /* look up hname and find num states N */      if ((hmmId = GetLabId(hname,FALSE))==NULL)         HError(7251,"LoadStatsFile: unknown name %s at line %d",                hname,lnum);      if ((ml = FindMacroName(hset,'l',hmmId))==NULL)         HError(7251,"LoadStatsFile: unknown model %s at line %d",                hname,lnum);      hmm = (HMMDef *) ml->structure;      N = hmm->numStates;      for (i=2; i<N; i++) {         if (!ReadFloat(&src,&x,1,bin))            HError(7250,"LoadStatsFile: Float format error file %s line %d\n",                   statfile,lnum);         si = hmm->svec[i].info;         si->stateCounter = count;/* load the # of times the state occurred */         memcpy(&(si->hook),&x,sizeof(float)); /* !! */         occSum += x; ++occN;      }   }   CloseSource(&src);   if (otrace || (trace & T_OCC)) {      printf("  Stats loaded for %d models\n",lnum);      printf("  Mean Occupation Count = %f\n",occSum/occN);      fflush(stdout);   }}/* ---------------------------- End of HUtil.c --------------------------- */

⌨️ 快捷键说明

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