📄 hmodel.c
字号:
{ "RELD", RELDUR }, { "GEND", GENDUR }, { "DIAGC", DIAGCOV }, { "FULLC", FULLCOV }, { "XFORMC", XFORMCOV }, { "STATE", STATE }, { "TMIX", TMIX }, { "MIXTURE", MIXTURE }, { "STREAM", STREAM }, { "SWEIGHTS", SWEIGHTS }, { "MEAN", MEAN }, { "VARIANCE", VARIANCE }, { "INVCOVAR", INVCOVAR }, { "XFORM", XFORM }, { "GCONST" , GCONST }, { "DURATION", DURATION }, { "INVDIAGC", INVDIAGCOV }, { "TRANSP", TRANSP }, { "DPROB", DPROB }, { "LLTC", LLTCOV }, { "LLTCOVAR", LLTCOVAR }, { "RCLASS", RCLASS }, { "REGTREE", REGTREE }, { "NODE", NODE }, { "TNODE", TNODE }, { "HMMSETID", HMMSETID }, { "PARMKIND", PARMKIND }, { "MACRO", MACRO }, { "EOF", EOFSYM }, /* Transformation symbols */ {"XFORMKIND", XFORMKIND } , {"PARENTXFORM", PARENTXFORM }, {"NUMXFORMS", NUMXFORMS }, {"XFORMSET", XFORMSET }, {"LINXFORM", LINXFORM }, {"OFFSET", OFFSET }, {"BIAS", BIAS }, {"LOGDET", LOGDET},{"BLOCKINFO", BLOCKINFO }, {"BLOCK", BLOCK }, {"BASECLASS", BASECLASS }, {"CLASS", CLASS }, {"XFORMWGTSET", XFORMWGTSET }, {"CLASSXFORM", CLASSXFORM }, {"MMFIDMASK", MMFIDMASK }, {"PARAMETERS", PARAMETERS }, {"NUMCLASSES", NUMCLASSES }, {"ADAPTKIND", ADAPTKIND }, {"PREQUAL", PREQUAL }, {"INPUTXFORM", INPUTXFORM }, { "", NULLSYM }};/* Reverse lookup table for above */static char *symNames[NULLSYM+1];#define NUMSYM (sizeof(symMap)/sizeof(symMap[0]))typedef struct { /* Token returned by the scanner */ Symbol sym; /* the current input symbol */ Boolean binForm; /* binary form of keyword symbol */ ParmKind pkind; /* samp kind when sym==PARMKIND */ char macroType; /* current macro type if sym==MACRO */} Token; void InitSymNames(void){ 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); } /* This is tacky and has to be fixed*/ 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);}/* ------------------- 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);}/* Str2BaseClassKind: parse the string into the correct baseclass */BaseClassKind Str2BaseClassKind(char *str){ BaseClassKind bkind = MIXBASE; if (!(strcmp(str,"MIXBASE"))) bkind = MIXBASE; else if (!(strcmp(str,"MEANBASE"))) bkind = MEANBASE; else if (!(strcmp(str,"COVBASE"))) bkind = COVBASE; else HError(999,"Unknown BaseClass kind"); return bkind;}/* Str2XFormKind: parse the string into the correct xform kind */XFormKind Str2XFormKind(char *str){ XFormKind xkind = MLLRMEAN; if (!(strcmp(str,"MLLRMEAN"))) xkind = MLLRMEAN; else if (!(strcmp(str,"MLLRCOV"))) xkind = MLLRCOV; else if (!(strcmp(str,"MLLRVAR"))) xkind = MLLRVAR; else if (!(strcmp(str,"CMLLR"))) xkind = CMLLR; else HError(999,"Unknown XForm Class kind"); return xkind;}/* Str2XFormKind: parse the string into the correct xform kind */AdaptKind Str2AdaptKind(char *str){ AdaptKind akind = TREE; if (!(strcmp(str,"TREE"))) akind = TREE; else if (!(strcmp(str,"BASE"))) akind = BASE; else HError(999,"Unknown Adapt kind"); return akind;}/* ---------------------- 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 ------------ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -