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 HTK_HMM_Data * 00036 htk_hmmdata_new(HTK_HMM_INFO *hmminfo) 00037 { 00038 HTK_HMM_Data *new; 00039 00040 new = (HTK_HMM_Data *)mybmalloc2(sizeof(HTK_HMM_Data), &(hmminfo->mroot)); 00041 00042 new->name = NULL; 00043 new->state_num = 0; 00044 new->s = NULL; 00045 new->tr = NULL; 00046 new->next = NULL; 00047 00048 return(new); 00049 } 00050 00057 void 00058 htk_hmmdata_add(HTK_HMM_INFO *hmm, HTK_HMM_Data *new) 00059 { 00060 HTK_HMM_Data *match; 00061 /* link data structure */ 00062 new->next = hmm->start; 00063 hmm->start = new; 00064 00065 if (new->name == NULL) { 00066 /* HMM must have a name */ 00067 rderr("HMM has no name"); 00068 } else { 00069 /* add index to search index tree */ 00070 if (hmm->physical_root == NULL) { 00071 hmm->physical_root = aptree_make_root_node(new, &(hmm->mroot)); 00072 } else { 00073 match = aptree_search_data(new->name, hmm->physical_root); 00074 if (match != NULL && strmatch(match->name, new->name)) { 00075 /* HMM of the same name should not be defined */ 00076 jlog("Error: rdhmmdef_data: HMM \"%s\" is defined more than twice\n", new->name); 00077 rderr(NULL); 00078 } else { 00079 aptree_add_entry(new->name, new, match->name, &(hmm->physical_root), &(hmm->mroot)); 00080 } 00081 } 00082 } 00083 } 00084 00099 static HTK_HMM_Data * 00100 htk_hmmdata_read(FILE *fp, HTK_HMM_INFO *hmm) 00101 { 00102 HTK_HMM_Data *new; 00103 int i; 00104 short sid; 00105 00106 new = htk_hmmdata_new(hmm); 00107 00108 /* begin tag */ 00109 if (!currentis("BEGINHMM")) rderr("<BEGINHMM> not found"); 00110 read_token(fp); 00111 00112 /* read global opt if any */ 00113 /* read_global_opt(fp, &(new->opt)); */ 00114 00115 /* num of state */ 00116 if (!currentis("NUMSTATES")) rderr("<NUMSTATES> not found"); 00117 read_token(fp); 00118 NoTokErr("state num not found\n"); 00119 new->state_num = atoi(rdhmmdef_token); 00120 read_token(fp); 00121 00122 /* malloc state */ 00123 new->s = (HTK_HMM_State **)mybmalloc2(sizeof(HTK_HMM_State *) * new->state_num, &(hmm->mroot)); 00124 for(i=0;i<new->state_num;i++) { 00125 new->s[i] = NULL; 00126 } 00127 00128 /* read/set each state info */ 00129 for (;;) { 00130 if (!currentis("STATE")) break; 00131 read_token(fp); NoTokErr("STATE id not found"); 00132 sid = atoi(rdhmmdef_token) - 1; 00133 read_token(fp); 00134 new->s[sid] = get_state_data(fp, hmm); 00135 } 00136 00137 /* read/set transition info */ 00138 new->tr = get_trans_data(fp, hmm); 00139 if ((new->tr)->statenum != new->state_num) { 00140 rderr("# of transition != # of state"); 00141 } 00142 00143 /* read/set duration */ 00144 00145 /* end tag */ 00146 if (!currentis("ENDHMM")) rderr("<ENDHMM> not found"); 00147 read_token(fp); 00148 00149 return(new); 00150 } 00151 00159 void 00160 def_HMM(char *name, FILE *fp, HTK_HMM_INFO *hmm) 00161 { 00162 HTK_HMM_Data *new; 00163 00164 /* read in HMM model data from fp, and return newly malloced HTK_HMM_Data */ 00165 new = htk_hmmdata_read(fp, hmm); 00166 00167 /* set name and add the new data to the main structure */ 00168 new->name = name; 00169 htk_hmmdata_add(hmm, new); 00170 }