Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

hmm_lookup.c

Go to the documentation of this file.
00001 
00039 /*
00040  * Copyright (c) 1991-2006 Kawahara Lab., Kyoto University
00041  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00042  * Copyright (c) 2005-2006 Julius project team, Nagoya Institute of Technology, Nagoya Institute of Technology
00043  * All rights reserved
00044  */
00045 
00046 /* physical HMMs ... already indexed when reading in hmmdefs */
00047 /* logical HMMs  ... already indexed when reading in HMMList */
00048 
00049 #include <sent/stddefs.h>
00050 #include <sent/htk_hmm.h>
00051 #include <sent/ptree.h>
00052 
00061 HTK_HMM_Data *
00062 htk_hmmdata_lookup_physical(HTK_HMM_INFO *hmminfo, char *keyname)
00063 {
00064   HTK_HMM_Data *tmp;
00065   tmp = aptree_search_data(keyname, hmminfo->physical_root);
00066   if (strmatch(tmp->name, keyname)) {
00067     return tmp;
00068   } else {
00069     return NULL;
00070   }
00071 }
00072 
00081 HMM_Logical *
00082 htk_hmmdata_lookup_logical(HTK_HMM_INFO *hmminfo, char *keyname)
00083 {
00084   HMM_Logical *tmp;
00085   tmp = aptree_search_data(keyname, hmminfo->logical_root);
00086   if (strmatch(tmp->name, keyname)) {
00087     return tmp;
00088   } else {
00089     return NULL;
00090   }
00091 }
00092 
00098 static void
00099 hmm_count_logical_num(HTK_HMM_INFO *hmminfo)
00100 {
00101   HMM_Logical *lg;
00102   int n;
00103 
00104   n = 0;
00105   for (lg = hmminfo->lgstart; lg; lg = lg->next) n++;
00106   hmminfo->totallogicalnum = n;
00107 }
00108 
00118 void
00119 hmm_add_physical_to_logical(HTK_HMM_INFO *hmminfo)
00120 {
00121   HMM_Logical *new, *match = NULL;
00122   HTK_HMM_Data *ph;
00123 
00124   for (ph = hmminfo->start; ph; ph = ph->next) {
00125 
00126     /* check if same name already exist */
00127     if (hmminfo->logical_root != NULL) {
00128       match = aptree_search_data(ph->name, hmminfo->logical_root);
00129       if (strmatch(match->name, ph->name)) {
00130         /* the physcal name was already mapped to other HMMs in HMMList */
00131         j_printerr("Warning: \"%s\" is defined in hmmdefs, but \"%s\" will be used instead\n", ph->name, (match->body.defined)->name);
00132         continue;
00133       }
00134     }
00135     /* create new HMM_Logical */
00136     /* body refers to the physical HMM */
00137     new = (HMM_Logical *)mybmalloc(sizeof(HMM_Logical));
00138     new->name = (char *)mybmalloc(strlen(ph->name) + 1);
00139     strcpy(new->name, ph->name);
00140     new->is_pseudo = FALSE;
00141     new->body.defined = ph;
00142     new->next = hmminfo->lgstart;
00143     hmminfo->lgstart = new;
00144     if (hmminfo->logical_root == NULL) {
00145       hmminfo->logical_root = aptree_make_root_node(new);
00146     } else {
00147       aptree_add_entry(new->name, new, match->name, &(hmminfo->logical_root));
00148     }
00149   }
00150 
00151   /* re-count total number */
00152   hmm_count_logical_num(hmminfo);
00153 }
00154 
00155 
00156 static int add_count;           
00157 
00168 static void
00169 hmm_add_pseudo_phones_sub(HTK_HMM_INFO *hmminfo, char *name)
00170 {
00171   HMM_Logical *new, *match;
00172 
00173   /* check if already exist */
00174   match = aptree_search_data(name, hmminfo->logical_root);
00175   if (strmatch(match->name, name)) {
00176     /* already exist in list */
00177     /*    if (! match->is_pseudo) {*/
00178       /* this pseudo-HMM is already defined as real HMM in hmmdefs or in HMMList */
00179     /*designated_count++;
00180       }*/
00181   } else {
00182     /* create new HMM_Logical with pseudo body */
00183     new = (HMM_Logical *)mybmalloc(sizeof(HMM_Logical));
00184     new->name = (char *)mybmalloc(strlen(name) + 1);
00185     strcpy(new->name, name);
00186     new->is_pseudo = TRUE;
00187     new->body.pseudo = cdset_lookup(hmminfo, name);
00188     if (new->body.pseudo == NULL) {     /* should never happen */
00189       j_error("InternalError: tried to add pseudo phone \"%s\" to logical HMM, but no corresponding CD_Set found.  Why??\n");
00190     }
00191     new->next = hmminfo->lgstart;
00192     hmminfo->lgstart = new;
00193     if (hmminfo->logical_root == NULL) {
00194       hmminfo->logical_root = aptree_make_root_node(new);
00195     } else {
00196       aptree_add_entry(new->name, new, match->name, &(hmminfo->logical_root));
00197     }
00198     add_count++;
00199   }
00200 }
00201     
00208 void
00209 hmm_add_pseudo_phones(HTK_HMM_INFO *hmminfo)
00210 {
00211   HMM_Logical *lg;
00212   static char buf[MAX_HMMNAME_LEN];
00213 
00214   add_count = 0;
00215   /* add pseudo monophone */
00216   for (lg = hmminfo->lgstart; lg; lg = lg->next) {
00217     if (lg->is_pseudo) continue;
00218     hmm_add_pseudo_phones_sub(hmminfo, center_name(lg->name, buf));
00219   }
00220   /* add pseudo biphone, i.e. "a-k" etc. */
00221   for (lg = hmminfo->lgstart; lg; lg = lg->next) {
00222     if (lg->is_pseudo) continue;
00223     hmm_add_pseudo_phones_sub(hmminfo, leftcenter_name(lg->name, buf));
00224   }
00225   /* add pseudo biphone, i.e. "k+e" etc. */
00226   for (lg = hmminfo->lgstart; lg; lg = lg->next) {
00227     if (lg->is_pseudo) continue;
00228     hmm_add_pseudo_phones_sub(hmminfo, rightcenter_name(lg->name, buf));
00229   }
00230   j_printerr("%d added as logical...", add_count);
00231 
00232   hmminfo->totalpseudonum = add_count;
00233   /* re-count total number */
00234   hmm_count_logical_num(hmminfo);
00235 }
00236 
00244 int
00245 hmm_logical_state_num(HMM_Logical *lg)
00246 {
00247   int len;
00248   if (lg->is_pseudo) len = lg->body.pseudo->state_num;
00249   else len = lg->body.defined->state_num;
00250   return(len);
00251 }
00252 
00260 HTK_HMM_Trans *
00261 hmm_logical_trans(HMM_Logical *lg)
00262 {
00263   HTK_HMM_Trans *tr;
00264   if (lg->is_pseudo) tr = lg->body.pseudo->tr;
00265   else tr = lg->body.defined->tr;
00266   return(tr);
00267 }

Generated on Tue Mar 28 16:01:39 2006 for Julius by  doxygen 1.4.2