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

📄 hmodel.c

📁 隐马尔科夫模型工具箱
💻 C
📖 第 1 页 / 共 5 页
字号:
{   int i;   for (i=0;i<=NULLSYM;i++) symNames[i]="";   for (i=0;i<NUMSYM;i++) symNames[symMap[i].sym]=symMap[i].name;}/* InitScanner: initialise scanner for new source */ReturnStatus InitScanner(char *fname, Source *src, Token *tok, HMMSet *hset){   if(InitSource(fname, src, HMMDefFilter)<SUCCESS){      return(FAIL);   }   tok->sym = NULLSYM; tok->macroType = ' ';    tok->binForm = FALSE;   return(SUCCESS);}/* TermScanner: terminate scanner for given source */static void TermScanner(Source *src){   CloseSource(src);}/* HMError: report a HMM definition error */static void HMError(Source *src, char *message){   char buf[MAXSTRLEN];      fflush(stdout);   fprintf(stderr,"HMM Def Error: %s at %s\n",           message,SrcPosition(*src,buf));   HRError(7050,"HMError:");}/* GetToken: put next symbol from given source into token */static ReturnStatus GetToken(Source *src, Token *tok){   char buf[MAXSYMLEN],tmp[MAXSTRLEN];   int i,c,imax,sym;      tok->binForm = FALSE;   while (isspace(c=GetCh(src)));     /* Look for symbol or Macro */   if (c != '<' && c != ':' && c != '~'  && c != '.' && c != '#') {      if (c == EOF) {         if (trace&T_TOK) printf("HModel:   tok=<EOF>\n");         tok->sym=EOFSYM; return(SUCCESS);      }      HMError(src,"GetToken: Symbol expected");      return(FAIL);   }   if (c == '~'){                    /* If macro sym return immediately */      c = tolower(GetCh(src));      if (c!='s' && c!='m' && c!='u' && c!='x' && c!='d' && c!='c' &&          c!='r' && c!='a' && c!='b' && c!='g' && c!='f' && c!='y' && c!='j' &&          c!='v' && c!='i' && c!='t' && c!='w' && c!='h' && c!='o')         {            HMError(src,"GetToken: Illegal macro type");            return(FAIL);         }      tok->macroType = c; tok->sym = MACRO;      if (trace&T_TOK) printf("HModel:   MACRO ~%c\n",c);      return(SUCCESS);   }   i=0; imax = MAXSYMLEN-1;   if (c=='#') {           /* if V1 mmf header convert to ~h */      while ((c=GetCh(src)) != '#' && i<imax)         buf[i++] = c;      buf[i] = '\0';      if (strcmp(buf,"!MMF!") != 0){         HMError(src,"GetToken: expecting V1 style MMF header #!MMF!#");         return(FAIL);      }      tok->sym = MACRO; tok->macroType = 'h';      if (trace&T_TOK) printf("HModel:   MACRO ~h (#!MMF!#)\n");      return(SUCCESS);   }   if (c=='.'){            /* if . and not EOF convert to ~h */      while (isspace(c=GetCh(src)));      if (c == EOF) {         if (trace&T_TOK) printf("HModel:   tok=.<EOF>\n");         tok->sym=EOFSYM;         return(SUCCESS);      }      UnGetCh(c,src);      tok->sym = MACRO; tok->macroType = 'h';      if (trace&T_TOK) printf("HModel:   MACRO ~h (.)\n");      return(SUCCESS);        }     if (c=='<') {                 /* Read verbose symbol string into buf */      while ((c=GetCh(src)) != '>' && i<imax)         buf[i++] = islower(c)?toupper(c):c;      buf[i] = '\0';      if (c != '>'){         HMError(src,"GetToken: > missing in symbol");         return(FAIL);      }      for (sym=0; sym<NUMSYM; sym++) /* Look symbol up in symMap */         if (strcmp(symMap[sym].name,buf) == 0) {            tok->sym = symMap[sym].sym;            if (trace&T_TOK) printf("HModel:   tok=<%s>\n",buf);            return(SUCCESS);                                /* and return */           }   } else {      /* Read binary symbol into buf */      tok->binForm = TRUE;      sym = GetCh(src);      if (sym>=BEGINHMM && sym<PARMKIND) {         if (trace&T_TOK) printf("HModel:   tok=:%s\n",symNames[sym]);         tok->sym = (Symbol) sym;         return(SUCCESS);                                /* and return */        }        }            /* if symbol not in symMap then it may be a sampkind */   if ((tok->pkind = Str2ParmKind(buf)) != ANON){      tok->sym = PARMKIND;      if (trace&T_TOK) printf("HModel:   tok=SK[%s]\n",buf);      return(SUCCESS);   }   strcpy(tmp,"GetToken: Unknown symbol ");   HMError(src,strcat(tmp,buf));   return(FAIL);}/*  Looks for the macroname in the current directory. If it is   notfound search through the input transform directories  in order. FOpen is used to allow optional generation of  Error messages using T_XFD*/static char *InitXFormScanner(HMMSet *hset, char *macroname, char *fname,			      Source *src, Token *tok){   static char buf[MAXSTRLEN];   XFDirLink p;   Boolean isPipe;   FILE *f;   if ((fname==NULL) || ((f=FOpen(fname,NoFilter,&isPipe)) == NULL)) {      if ((trace&T_XFD) && (fname!=NULL))         HRError(7010,"InitXFormScanner: Cannot open source file %s",fname);      p = xformDirNames;      while ((p!=NULL) &&              ((f=FOpen(MakeFN(macroname,p->dirName,NULL,buf),NoFilter,&isPipe)) == NULL)) {         if (trace&T_XFD)             HRError(7010,"InitXFormScanner: Cannot open source file %s",buf);         p = p->next;      }      if (p==NULL) { /* Failed to find macroname */         HError(7035,"Failed to find macroname %s",macroname);      } else { /* Close file and initialise scanner */         FClose(f,isPipe);         InitScanner(buf,src,tok,hset);      }      if (trace&T_TOP) printf("Loading macro file %s\n",buf);      return buf;   } else {      FClose(f,isPipe);      if (trace&T_TOP) printf("Loading macro file %s\n",fname);      InitScanner(fname,src,tok,hset);      return fname;   }}/* ------------------- HMM 'option' handling ----------------------- */static void OWarn(HMMSet *hset,Boolean equal,char *opt){   if (!equal && hset->optSet)      HRError(-7032,"OWarn: change HMM Set %s",opt);}/* GetOption: read a HMM option specifier - value set in nState pointer */static ReturnStatus GetOption(HMMSet *hset, Source *src, Token *tok, int *nState){   DurKind dk;   char buf[MAXSTRLEN];   short vs,sw[SMAX],nSt=0;   int i;   Boolean ntok=TRUE;   static InputXForm* GetInputXForm(HMMSet *hset, Source *src, Token *tok);   switch (tok->sym) {   case NUMSTATES:      if (!ReadShort(src,&nSt,1,tok->binForm)){         HMError(src,"NumStates Expected");         return(FAIL);      }      *nState=nSt;      break;   case PARMKIND:          OWarn(hset,hset->pkind==tok->pkind,"parmKind");      hset->pkind = tok->pkind;      break;   case NDUR:   case PDUR:   case GDUR:   case RELDUR:   case GENDUR:      dk = (DurKind) (NULLD + (tok->sym-NDUR));      OWarn(hset,hset->dkind==dk,"durKind");      hset->dkind = dk;       break;   case HMMSETID:      if (!ReadString(src,buf)){         HMError(src,"HMM identifier expected");         return(FAIL);      }      hset->hmmSetId=CopyString(hset->hmem,buf);      SkipWhiteSpace(src);      break;   case INPUTXFORM:      if(GetToken(src,tok)<SUCCESS){         HMError(src,"GetOption: GetToken failed");         return(FAIL);           }      if (tok->sym==MACRO && tok->macroType=='j') {         if (!ReadString(src,buf))            HError(7013,"GetOption: cannot read input xform macro name");         hset->xf = LoadInputXForm(hset,buf,NULL);	      } else {         hset->xf = GetInputXForm(hset,src,tok);         hset->xf->xformName = CopyString(hset->hmem,src->name);         ntok = FALSE;      }           break;   case VECSIZE:      if (!ReadShort(src,&vs,1,tok->binForm)){         HMError(src,"Vector Size Expected");         return(FAIL);      }      OWarn(hset,hset->vecSize==vs,"vecSize");      hset->vecSize = vs;      break;   case STREAMINFO:      if (!ReadShort(src,sw,1,tok->binForm)){         HMError(src,"Num Streams Expected");         return(FAIL);      }      if (sw[0] >= SMAX){         HMError(src,"Stream limit exceeded");         return(FAIL);      }      if (!ReadShort(src,sw+1,sw[0],tok->binForm)){         HMError(src,"Stream Widths Expected");         return(FAIL);      }      OWarn(hset,hset->swidth[0]==sw[0],"swidth[0]");             for (i=0; i<=sw[0]; i++) hset->swidth[i] = sw[i];      break;   case DIAGCOV:      OWarn(hset,hset->ckind==DIAGC,"covKind");      hset->ckind = DIAGC;       break;   case FULLCOV:      OWarn(hset,hset->ckind==FULLC,"covKind");      hset->ckind = FULLC;       break;   case XFORMCOV:      OWarn(hset,hset->ckind==XFORMC,"covKind");      hset->ckind = XFORMC;       break;   case INVDIAGCOV:      OWarn(hset,hset->ckind==INVDIAGC,"covKind");      hset->ckind = INVDIAGC;       break;   case LLTCOV:      OWarn(hset,hset->ckind==LLTC,"covKind");      hset->ckind = LLTC;       break;   default:       HMError(src,"GetOption: Ilegal Option Symbol");      return(FAIL);   }   if (ntok && (GetToken(src,tok)<SUCCESS)){      HMError(src,"GetOption: GetToken failed");      return(FAIL);        }      return(SUCCESS);}/* FreezeOptions: freeze the global options in HMM set */static ReturnStatus FreezeOptions(HMMSet *hset){   int i;      if (hset->optSet) return(SUCCESS);   if (hset->vecSize == 0) {      if (hset->swidth[0] > 0 && hset->swidth[1] > 0)         for (i=1; i<=hset->swidth[0]; i++)            hset->vecSize += hset->swidth[i];      else{         HRError(7032,"FreezeOptions: vecSize not set");         return(FAIL);      }   }   if (hset->swidth[0] == 0) {      hset->swidth[0] = 1; hset->swidth[1] = hset->vecSize;   }   if (hset->pkind == 0){      HRError(7032,"FreezeOptions: parmKind not set");      return(FAIL);   }   hset->optSet = TRUE;   return(SUCCESS);}/* CheckOptions: check that options are set in given HMM set */static ReturnStatus CheckOptions(HMMSet *hset){   if (!hset->optSet){      HRError(7032,"CheckOptions: options not set in HMM Set");      return(FAIL);   }   return(SUCCESS);}/* ---------------------- Input XForm Directory Handling ---------------------- *//* EXPORT->AddInXFormDir: Add given file name to set *//* Doesn't check for repeated specification! */void AddInXFormDir(HMMSet *hset, char *dirname){  XFDirLink p,q;     p = (XFDirLink)New(hset->hmem,sizeof(XFDirInfo));  p->next = NULL;  p->dirName = CopyString(hset->hmem,dirname);  if (xformDirNames == NULL)    xformDirNames = p;  else {  /* store in order of arrival */    for (q=xformDirNames; q->next != NULL; q=q->next);    q->next = p;  }}/* ---------------------- MMF File Name Handling ---------------------- *//* FindMMF: find given file name in HMM set */static MILink FindMMF(HMMSet *hset, char *fname, Boolean ignorePath){   MILink p;   char buf1[MAXSTRLEN],buf2[MAXSTRLEN];      for (p=hset->mmfNames; p!=NULL; p=p->next){      if (ignorePath){         if (strcmp(NameOf(fname,buf1),NameOf(p->fName,buf2)) == 0 )             return p;      }else{         if (strcmp(fname,p->fName) == 0 )            return p;      }   }   return NULL;}/* EXPORT->AddMMF: Add given file name to set */MILink AddMMF(HMMSet *hset, char *fname){   MILink p,q;      if ((p = FindMMF(hset,fname,FALSE)) != NULL)       return(p);   p = (MILink)New(hset->hmem,sizeof(MMFInfo));   ++hset->numFiles;   p->isLoaded = FALSE; p->next = NULL;   p->fName = CopyString(hset->hmem,fname);   p->fidx = hset->numFiles;   if (hset->mmfNames == NULL)      hset->mmfNames = p;   else {  /* store in order of arrival */      for (q=hset->mmfNames; q->next != NULL; q=q->next);      q->next = p;   }   return p;}/* -------------- Special Discrete/Tied Mixture Input Routines ------------ *//*    Run-length encoding for verbose external form uses the notation w*n   to mean that w is repeated n times.  For binary form, all weights   are stored as unsigned shorts.  The msb is set to indicate that a   repeat count follows.  Repeat counts are stored as unsigned chars.*//* GetTiedWeights: parse src and get compact mixture weight def */ReturnStatus GetTiedWeights(Source *src, Token *tok, int M, Vector tpdf){   float weight=0.0;   short repCount=0;      /* repeat counter for weights */   int c,m;      if (trace&T_PAR) printf("HModel: GetTiedWeights: M=%d\n",M);   for (m=1; m<=M; m++) {      if (repCount>0)         /* get mixture weight */         --repCount;      else {         if (tok->binForm) {            if (!ReadFloat(src,&weight,1,TRUE)){               HMError(src,"Tied Weight expected");               return(FAIL);            }            if (weight<0.0) {               repCount=GetCh(src);               --repCount; weight = weight+2.0;            }         } else {            if (!ReadFloat(src,&weight,1,FALSE)){               HMError(src,"Discrete Weight expected");               return(FAIL);            }            c=GetCh(src);            if (c == '*') {               if (!ReadShort(src,&repCount,1,FALSE)){                  HMError(src,"Discrete Repeat Count expected");                  return(FAIL);               }

⌨️ 快捷键说明

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