Julius 4.2
|
00001 00018 /* 00019 * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University 00020 * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology 00021 * Copyright (c) 2005-2011 Julius project team, Nagoya Institute of Technology 00022 * All rights reserved 00023 */ 00024 00025 #include <sent/stddefs.h> 00026 #include <sent/htk_hmm.h> 00027 00028 extern char *rdhmmdef_token; 00029 00035 static HTK_HMM_State * 00036 state_new(HTK_HMM_INFO *hmm) 00037 { 00038 HTK_HMM_State *new; 00039 int i; 00040 00041 new = (HTK_HMM_State *)mybmalloc2(sizeof(HTK_HMM_State), &(hmm->mroot)); 00042 new->name = NULL; 00043 new->nstream = hmm->opt.stream_info.num; 00044 new->w = NULL; 00045 new->pdf = (HTK_HMM_PDF **)mybmalloc2(sizeof(HTK_HMM_PDF *) * new->nstream, &(hmm->mroot)); 00046 for(i=0;i<new->nstream;i++) { 00047 new->pdf[i] = NULL; 00048 } 00049 new->id = 0; 00050 new->next = NULL; 00051 00052 return(new); 00053 } 00054 00061 void 00062 state_add(HTK_HMM_INFO *hmm, HTK_HMM_State *new) 00063 { 00064 HTK_HMM_State *match; 00065 00066 /* link data structure */ 00067 new->next = hmm->ststart; 00068 hmm->ststart = new; 00069 00070 if (new->name != NULL) { 00071 /* add index to search index tree */ 00072 if (hmm->st_root == NULL) { 00073 hmm->st_root = aptree_make_root_node(new, &(hmm->mroot)); 00074 } else { 00075 match = aptree_search_data(new->name, hmm->st_root); 00076 if (match != NULL && strmatch(match->name, new->name)) { 00077 jlog("Error: rdhmmdef_state: ~s \"%s\" is already defined\n", new->name); 00078 rderr(NULL); 00079 } else { 00080 aptree_add_entry(new->name, new, match->name, &(hmm->st_root), &(hmm->mroot)); 00081 } 00082 } 00083 } 00084 00085 } 00086 00095 HTK_HMM_State * 00096 state_lookup(HTK_HMM_INFO *hmm, char *keyname) 00097 { 00098 HTK_HMM_State *s; 00099 00100 s = aptree_search_data(keyname, hmm->st_root); 00101 if (s != NULL && strmatch(s->name, keyname)) { 00102 return s; 00103 } else { 00104 return NULL; 00105 } 00106 } 00107 00122 static HTK_HMM_State * 00123 state_read(FILE *fp, HTK_HMM_INFO *hmm) 00124 { 00125 HTK_HMM_State *new; 00126 int s, k; 00127 boolean no_nummixes; 00128 00129 new = state_new(hmm); 00130 00131 if (currentis("NUMMIXES")) { 00132 if (hmm->tmp_mixnum == NULL) { 00133 hmm->tmp_mixnum = (int *)mybmalloc2(sizeof(int) * hmm->opt.stream_info.num, &(hmm->mroot)); 00134 } 00135 for(s=0;s<new->nstream;s++) { 00136 read_token(fp); 00137 NoTokErr("missing NUMMIXES value"); 00138 hmm->tmp_mixnum[s] = atoi(rdhmmdef_token); 00139 } 00140 read_token(fp); 00141 no_nummixes = FALSE; 00142 } else { 00143 no_nummixes = TRUE; 00144 } 00145 00146 if (currentis("SWEIGHTS") || currentis("~w")) { 00147 new->w = get_streamweight_data(fp, hmm); 00148 if (new->w == NULL) { 00149 rderr("error reading stream weights"); 00150 } 00151 } 00152 00153 for(k = 0; k < new->nstream; k++) { 00154 00155 if (currentis("STREAM")) { 00156 read_token(fp); 00157 NoTokErr("missing STREAM value"); 00158 s = atoi(rdhmmdef_token) - 1; 00159 read_token(fp); 00160 } else { 00161 s = 0; 00162 if (k != 0) { /* not a first time */ 00163 rderr("a state does not has mixture for all streams"); 00164 } 00165 } 00166 00167 new->pdf[s] = get_mpdf_data(fp, hmm, no_nummixes ? -1 : hmm->tmp_mixnum[s], s); 00168 00169 } 00170 return (new); 00171 } 00172 00186 HTK_HMM_State * 00187 get_state_data(FILE *fp, HTK_HMM_INFO *hmm) 00188 { 00189 HTK_HMM_State *tmp; 00190 00191 if (currentis("NUMMIXES")||currentis("SWEIGHTS")||currentis("~w")||currentis("STREAM")||currentis("MIXTURE")||currentis("TMIX")||currentis("MEAN")||currentis("~m")||currentis("RCLASS")) { 00192 /* definition: define state data, and return the pointer */ 00193 tmp = state_read(fp, hmm); 00194 tmp->name = NULL; /* no name */ 00195 state_add(hmm, tmp); 00196 return tmp; 00197 } else if (currentis("~s")) { 00198 /* macro reference: lookup and return the pointer */ 00199 read_token(fp); 00200 NoTokErr("missing state macro name"); 00201 tmp = state_lookup(hmm, rdhmmdef_token); 00202 if (tmp == NULL) { 00203 jlog("Error: rdhmmdef_state: ~s \"%s\" not defined\n", rdhmmdef_token); 00204 rderr(NULL); 00205 } 00206 read_token(fp); 00207 return tmp; 00208 } else { 00209 rderr("no state data"); 00210 return NULL; 00211 } 00212 } 00213 00214 00215 00223 void 00224 def_state_macro(char *name, FILE *fp, HTK_HMM_INFO *hmm) 00225 { 00226 HTK_HMM_State *new; 00227 00228 /* read in data and return newly malloced data */ 00229 new = state_read(fp, hmm); 00230 00231 /* register it to the grobal HMM structure */ 00232 new->name = name; 00233 state_add(hmm, new); 00234 }