00001
00065
00066
00067
00068
00069
00070
00071
00072 #include <sent/stddefs.h>
00073 #include <sent/htk_param.h>
00074 #include <sent/htk_hmm.h>
00075
00077
00078
00079 #define CD_STATE_SET_STEP 10
00080
00081
00086 static void
00087 cdset_init(HTK_HMM_INFO *hmminfo)
00088 {
00089 hmminfo->cdset_info.cdtree = NULL;
00090 }
00091
00097 static CD_Set *
00098 cdset_new()
00099 {
00100 return((CD_Set *)mybmalloc(sizeof(CD_Set)));
00101 }
00102
00111 CD_Set *
00112 cdset_lookup(HTK_HMM_INFO *hmminfo, char *cdstr)
00113 {
00114 CD_Set *cd;
00115 cd = aptree_search_data(cdstr, hmminfo->cdset_info.cdtree);
00116 if (strmatch(cdstr, cd->name)) {
00117 return cd;
00118 } else {
00119 return NULL;
00120 }
00121 }
00122
00131 CD_Set *
00132 lcdset_lookup_by_hmmname(HTK_HMM_INFO *hmminfo, char *hmmname)
00133 {
00134 static char buf[MAX_HMMNAME_LEN];
00135
00136 return(cdset_lookup(hmminfo, leftcenter_name(hmmname, buf)));
00137 }
00138
00147 CD_Set *
00148 rcdset_lookup_by_hmmname(HTK_HMM_INFO *hmminfo, char *hmmname)
00149 {
00150 static char buf[MAX_HMMNAME_LEN];
00151
00152 return(cdset_lookup(hmminfo, rightcenter_name(hmmname, buf)));
00153 }
00154
00155
00161 static void
00162 put_cdset(void *ptr)
00163 {
00164 int i;
00165 CD_Set *a;
00166
00167 a = ptr;
00168 printf("name: %s\n", a->name);
00169
00170 for(i=0;i<a->state_num;i++) {
00171 if (a->stateset[i].num == 0) {
00172 printf("\t[state %d] not exist\n", i);
00173 } else {
00174 printf("\t[state %d] %d variants\n", i, a->stateset[i].num);
00175 }
00176
00177
00178
00179
00180
00181 }
00182 }
00183
00189 void
00190 put_all_cdinfo(HTK_HMM_INFO *hmminfo)
00191 {
00192 aptree_traverse_and_do(hmminfo->cdset_info.cdtree, put_cdset);
00193 }
00194
00195
00205 boolean
00206 regist_cdset(APATNODE **root, HTK_HMM_Data *d, char *cdname)
00207 {
00208 boolean need_new;
00209 CD_State_Set *tmp;
00210 CD_Set *lset = NULL, *lmatch = NULL;
00211 int j,n;
00212 boolean changed = FALSE;
00213
00214 if (strlen(cdname) >= MAX_HMMNAME_LEN) {
00215 j_error("Error: HMM name exceeds limit (%d): %s!\n", MAX_HMMNAME_LEN, cdname);
00216 }
00217
00218
00219 need_new = TRUE;
00220 if (*root != NULL) {
00221 lmatch = aptree_search_data(cdname, *root);
00222 if (strmatch(lmatch->name, cdname)) {
00223
00224 lset = lmatch;
00225 need_new = FALSE;
00226
00227 if (d->state_num > lset->state_num) {
00228 lset->stateset = (CD_State_Set *)myrealloc(lset->stateset, sizeof(CD_State_Set) * d->state_num);
00229
00230
00231
00232
00233
00234 for(j = lset->state_num - 1; j < d->state_num - 1; j++) {
00235 lset->stateset[j].maxnum = CD_STATE_SET_STEP;
00236 lset->stateset[j].s = (HTK_HMM_State **)mymalloc(sizeof(HTK_HMM_State *) * lset->stateset[j].maxnum);
00237 lset->stateset[j].num = 0;
00238 }
00239 lset->stateset[d->state_num-1].s = NULL;
00240 lset->stateset[d->state_num-1].num = 0;
00241 lset->stateset[d->state_num-1].maxnum = 0;
00242
00243 lset->state_num = d->state_num;
00244
00245
00246 lset->tr = d->tr;
00247
00248 changed = TRUE;
00249 }
00250 }
00251 }
00252
00253 if (need_new) {
00254
00255 lset = cdset_new();
00256 lset->name = mybstrdup(cdname);
00257 lset->state_num = d->state_num;
00258 lset->stateset = (CD_State_Set *)mymalloc(sizeof(CD_State_Set) * lset->state_num);
00259
00260 lset->stateset[0].s = lset->stateset[lset->state_num-1].s = NULL;
00261 lset->stateset[0].num = lset->stateset[lset->state_num-1].num = 0;
00262 lset->stateset[0].maxnum = lset->stateset[lset->state_num-1].maxnum = 0;
00263 for(j=1;j<lset->state_num-1; j++) {
00264
00265 lset->stateset[j].maxnum = CD_STATE_SET_STEP;
00266 lset->stateset[j].s = (HTK_HMM_State **)mymalloc(sizeof(HTK_HMM_State *) * lset->stateset[j].maxnum);
00267 lset->stateset[j].num = 0;
00268 }
00269
00270 lset->tr = d->tr;
00271
00272 if (*root == NULL) {
00273 *root = aptree_make_root_node(lset);
00274 } else {
00275 aptree_add_entry(lset->name, lset, lmatch->name, root);
00276 }
00277
00278 changed = TRUE;
00279 }
00280
00281
00282
00283 for (j=1;j<d->state_num-1;j++) {
00284 tmp = &(lset->stateset[j]);
00285
00286 for(n = 0; n < tmp->num ; n++) {
00287 if (tmp->s[n] == d->s[j]) {
00288
00289 break;
00290 }
00291 }
00292 if (n < tmp->num ) continue;
00293
00294
00295 if (tmp->num >= tmp->maxnum) {
00296 tmp->maxnum += CD_STATE_SET_STEP;
00297 tmp->s = (HTK_HMM_State **)myrealloc(tmp->s, sizeof(HTK_HMM_State *) * tmp->maxnum);
00298 }
00299
00300 tmp->s[tmp->num] = d->s[j];
00301 tmp->num++;
00302
00303 changed = TRUE;
00304 }
00305
00306 return(changed);
00307 }
00308
00317 boolean
00318 remove_cdset(HTK_HMM_INFO *hmminfo, char *cdname)
00319 {
00320 CD_Set *lmatch;
00321
00322 if (hmminfo->cdset_info.cdtree == NULL) return TRUE;
00323
00324 lmatch = aptree_search_data(cdname, hmminfo->cdset_info.cdtree);
00325 if (strmatch(lmatch->name, cdname)) {
00326 printf("[%s] found\n", lmatch->name);
00327
00328
00329
00330
00331
00332
00333
00334 aptree_remove_entry(cdname, &(hmminfo->cdset_info.cdtree));
00335 } else {
00336 return FALSE;
00337 }
00338 return TRUE;
00339 }
00340
00341
00350 boolean
00351 make_cdset(HTK_HMM_INFO *hmminfo)
00352 {
00353 HMM_Logical *lg;
00354 static char buf[MAX_HMMNAME_LEN];
00355
00356 cdset_init(hmminfo);
00357
00358
00359
00360 for(lg = hmminfo->lgstart; lg; lg = lg->next) {
00361 if (lg->is_pseudo) continue;
00362 regist_cdset(&(hmminfo->cdset_info.cdtree), lg->body.defined, leftcenter_name(lg->name, buf));
00363 }
00364
00365
00366 for(lg = hmminfo->lgstart; lg; lg = lg->next) {
00367 if (lg->is_pseudo) continue;
00368 regist_cdset(&(hmminfo->cdset_info.cdtree), lg->body.defined, rightcenter_name(lg->name, buf));
00369 }
00370
00371
00372 for(lg = hmminfo->lgstart; lg; lg = lg->next) {
00373 if (lg->is_pseudo) continue;
00374 regist_cdset(&(hmminfo->cdset_info.cdtree), lg->body.defined, center_name(lg->name, buf));
00375 }
00376
00377
00378
00379
00380 hmm_add_pseudo_phones(hmminfo);
00381
00382 return(TRUE);
00383 }
00384