Julius 4.2
|
00001 00016 /* 00017 * Copyright (c) 1991-2011 Kawahara Lab., Kyoto University 00018 * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology 00019 * Copyright (c) 2005-2011 Julius project team, Nagoya Institute of Technology 00020 * All rights reserved 00021 */ 00022 00023 /* currenty cannot treat other sub macros (~u,~i,~x) */ 00024 00025 #include <sent/stddefs.h> 00026 #include <sent/htk_hmm.h> 00027 00028 extern char *rdhmmdef_token; 00029 00035 static HTK_HMM_StreamWeight * 00036 sw_new(HTK_HMM_INFO *hmm) 00037 { 00038 HTK_HMM_StreamWeight *new; 00039 00040 new = (HTK_HMM_StreamWeight *)mybmalloc2(sizeof(HTK_HMM_StreamWeight), &(hmm->mroot)); 00041 00042 new->name = NULL; 00043 new->weight = NULL; 00044 new->len = 0; 00045 new->next = NULL; 00046 00047 return(new); 00048 } 00049 00056 void 00057 sw_add(HTK_HMM_INFO *hmm, HTK_HMM_StreamWeight *new) 00058 { 00059 HTK_HMM_StreamWeight *match; 00060 00061 /* link data structure */ 00062 new->next = hmm->swstart; 00063 hmm->swstart = new; 00064 00065 if (new->name != NULL) { 00066 /* add index to search index tree */ 00067 if (hmm->sw_root == NULL) { 00068 hmm->sw_root = aptree_make_root_node(new, &(hmm->mroot)); 00069 } else { 00070 match = aptree_search_data(new->name, hmm->sw_root); 00071 if (match != NULL && strmatch(match->name, new->name)) { 00072 jlog("Error: rdhmmdef_streamweight: ~w \"%s\" is already defined\n", new->name); 00073 rderr(NULL); 00074 } else { 00075 aptree_add_entry(new->name, new, match->name, &(hmm->sw_root), &(hmm->mroot)); 00076 } 00077 } 00078 } 00079 00080 } 00081 00090 static HTK_HMM_StreamWeight * 00091 sw_lookup(HTK_HMM_INFO *hmm, char *keyname) 00092 { 00093 HTK_HMM_StreamWeight *sw; 00094 00095 sw = aptree_search_data(keyname, hmm->sw_root); 00096 if (sw != NULL && strmatch(sw->name, keyname)) { 00097 return sw; 00098 } else { 00099 return NULL; 00100 } 00101 } 00102 00117 static HTK_HMM_StreamWeight * 00118 sw_read(FILE *fp, HTK_HMM_INFO *hmm) 00119 { 00120 HTK_HMM_StreamWeight *new; 00121 int i; 00122 00123 new = sw_new(hmm); 00124 /* read stream weight */ 00125 if (!currentis("SWEIGHTS")) { 00126 jlog("Error: rdhmmdef_streamweight: failed to read stream weight: \"%s\"\n", rdhmmdef_token); 00127 rderr(NULL); 00128 } else { 00129 read_token(fp); 00130 NoTokErr("missing SWEIGHTS vector length"); 00131 new->len = atoi(rdhmmdef_token); 00132 read_token(fp); 00133 new->weight = (VECT *)mybmalloc2(sizeof(VECT) * new->len, &(hmm->mroot)); 00134 /* needs conversion if integerized */ 00135 for (i=0;i<new->len;i++) { 00136 NoTokErr("missing some SWEIGHTS element"); 00137 new->weight[i] = (VECT)atof(rdhmmdef_token); 00138 read_token(fp); 00139 } 00140 } 00141 00142 return (new); 00143 } 00144 00158 HTK_HMM_StreamWeight * 00159 get_streamweight_data(FILE *fp, HTK_HMM_INFO *hmm) 00160 { 00161 HTK_HMM_StreamWeight *tmp; 00162 00163 if (currentis("~w")) { 00164 /* macro reference: lookup and return the pointer */ 00165 read_token(fp); 00166 NoTokErr("missing SWEIGHTS macro name"); 00167 tmp = sw_lookup(hmm, rdhmmdef_token); 00168 if (tmp == NULL) { 00169 jlog("Error: rdhmmdef_streamweight: ~w \"%s\" not defined\n", rdhmmdef_token); 00170 rderr(NULL); 00171 } 00172 read_token(fp); 00173 return tmp; 00174 } else if (currentis("SWEIGHTS")){ 00175 /* definition: define stream weight data, and return the pointer */ 00176 tmp = sw_read(fp, hmm); 00177 tmp->name = NULL; /* no name */ 00178 sw_add(hmm, tmp); 00179 return tmp; 00180 } else { 00181 rderr("no stream weights data"); 00182 return NULL; 00183 } 00184 } 00185 00193 void 00194 def_streamweight_macro(char *name, FILE *fp, HTK_HMM_INFO *hmm) 00195 { 00196 HTK_HMM_StreamWeight *new; 00197 00198 00199 /* read in data and return newly malloced data */ 00200 new = sw_read(fp, hmm); 00201 00202 /* register it to the grobal HMM structure */ 00203 new->name = name; 00204 sw_add(hmm, new); 00205 } 00206 00207 /* end of file */