Julius 4.1.5
libsent/src/hmminfo/init_phmm.c
説明を見る。
00001 
00018 /*
00019  * Copyright (c) 1991-2007 Kawahara Lab., Kyoto University
00020  * Copyright (c) 2000-2005 Shikano Lab., Nara Institute of Science and Technology
00021  * Copyright (c) 2005-2007 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 
00033 HTK_HMM_INFO *
00034 hmminfo_new()
00035 {
00036   HTK_HMM_INFO *new;
00037 
00038   new = (HTK_HMM_INFO *)mymalloc(sizeof(HTK_HMM_INFO));
00039 
00040   new->mroot = NULL;
00041   new->lroot = NULL;
00042   new->cdset_root = NULL;
00043   new->tmp_mixnum = NULL;
00044 
00045   new->opt.stream_info.num = 0;
00046   new->opt.cov_type = C_DIAG_C;
00047   new->opt.dur_type = D_NULL;
00048 
00049   new->trstart = NULL;
00050   new->vrstart = NULL;
00051   new->swstart = NULL;
00052   new->ststart = NULL;
00053   new->dnstart = NULL;
00054   new->pdfstart = NULL;
00055   new->start   = NULL;
00056   new->lgstart = NULL;
00057   new->physical_root = NULL;
00058   new->logical_root = NULL;
00059   new->tr_root = NULL;
00060   new->vr_root = NULL;
00061   new->sw_root = NULL;
00062   new->dn_root = NULL;
00063   new->pdf_root = NULL;
00064   new->st_root = NULL;
00065   new->codebooknum = 0;
00066   new->codebook_root = NULL;
00067   new->maxcodebooksize = 0;
00068   new->totalmixnum = 0;
00069   new->totalstatenum = 0;
00070   new->totalhmmnum = 0;
00071   new->totallogicalnum = 0;
00072   new->is_triphone = FALSE;
00073   new->is_tied_mixture = FALSE;
00074   new->cdset_method = IWCD_NBEST;
00075   new->cdmax_num = 3;
00076   new->totalpseudonum = 0;
00077   new->sp = NULL;
00078   new->basephone.root = NULL;
00079   new->cdset_info.cdtree = NULL;
00080   new->variance_inversed = FALSE;
00081 
00082 #ifdef ENABLE_MSD
00083   new->has_msd = FALSE;
00084 #endif
00085 
00086   return(new);
00087 }
00088 
00096 boolean
00097 hmminfo_free(HTK_HMM_INFO *hmm)
00098 {
00099   /* cdset does not use bmalloc, so free them separately */
00100   free_cdset(&(hmm->cdset_info.cdtree), &(hmm->cdset_root));
00101 
00102   /* free all memory that has been allocated by bmalloc2() */
00103   if (hmm->mroot != NULL) mybfree2(&(hmm->mroot));
00104   if (hmm->lroot != NULL) mybfree2(&(hmm->lroot));
00105 
00106   /* free whole */
00107   free(hmm);
00108 
00109   return(TRUE);
00110 }
00111 
00122 boolean
00123 init_hmminfo(HTK_HMM_INFO *hmminfo, char *hmmfilename, char *namemapfile, Value *para)
00124 {
00125   FILE *fp;
00126   boolean ok_p;
00127   boolean binary;
00128 
00129   ok_p = FALSE;
00130 
00131   /* read hmmdef file */
00132   jlog("Stat: init_phmm: Reading in HMM definition\n");
00133   /* first, try ascii format */
00134   if ((fp = fopen_readfile(hmmfilename)) == NULL) {
00135     jlog("Error: init_phmm: failed to open %s\n",hmmfilename);
00136     return FALSE;
00137   }
00138   if (rdhmmdef(fp, hmminfo) == TRUE) {
00139     ok_p = TRUE;
00140   }
00141   if (fclose_readfile(fp) < 0) {
00142     jlog("Error: init_phmm: failed to close %s\n", hmmfilename);
00143     return FALSE;
00144   }
00145   if (ok_p == FALSE) {
00146     /* second, try binary format */
00147     if ((fp = fopen_readfile(hmmfilename)) == NULL) {
00148       jlog("Error: init_phmm: failed to open %s\n",hmmfilename);
00149       return FALSE;
00150     }
00151     if (read_binhmm(fp, hmminfo, TRUE, para) == TRUE) {
00152       ok_p = TRUE;
00153     }
00154     if (fclose_readfile(fp) < 0) {
00155       jlog("Error: init_phmm: failed to close %s\n", hmmfilename);
00156       return FALSE;
00157     }
00158   }
00159   if (ok_p == FALSE) {
00160     jlog("Error: init_phmm: failed to read %s\n", hmmfilename);
00161     return FALSE;
00162   }
00163 
00164   jlog("Stat: init_phmm: defined HMMs: %5d\n", hmminfo->totalhmmnum);
00165 
00166   /* make mapping from logically named HMM to real defined HMM name */
00167   if (namemapfile != NULL) {
00168     /* use HMMList map file */
00169     if ((fp = fopen_readfile(namemapfile)) == NULL) {
00170       jlog("Error: init_phmm: failed to open %s\n",namemapfile);
00171       return FALSE;
00172     }
00173     /* detect binary / ascii by the first 4 bytes */
00174     {
00175       int x;
00176       if (myfread(&x, sizeof(int), 1, fp) < 1) {
00177         jlog("Error: init_phmm: failed to read %s\n", namemapfile);
00178         return FALSE;
00179       }
00180       if (x == 0) {
00181         binary = TRUE;
00182       } else {
00183         binary = FALSE;
00184         myfrewind(fp);
00185       }
00186     }
00187     if (binary) {
00188       /* binary format */
00189       jlog("Stat: init_phmm: loading binary hmmlist\n");
00190       if (load_hmmlist_bin(fp, hmminfo) == FALSE) {
00191         jlog("Error: init_phmm: HMMList \"%s\" read error\n",namemapfile);
00192         return FALSE;
00193       }
00194     } else {
00195       /* ascii format */
00196       jlog("Stat: init_phmm: loading ascii hmmlist\n");
00197       if (rdhmmlist(fp, hmminfo) == FALSE) {
00198         jlog("Error: init_phmm: HMMList \"%s\" read error\n",namemapfile);
00199         return FALSE;
00200       }
00201     }
00202     if (fclose_readfile(fp) < 0) {
00203       jlog("Error: init_phmm: failed to close %s\n", namemapfile);
00204       return FALSE;
00205     }
00206     jlog("Stat: init_phmm: logical names: %5d in HMMList\n", hmminfo->totallogicalnum);
00207 
00208   } else {
00209     /* add all names of physical HMMs as logical names */
00210     hmm_add_physical_to_logical(hmminfo);
00211     jlog("Stat: init_phmm: logical names: %5d\n", hmminfo->totallogicalnum);
00212   }
00213 
00214   /* extract basephone */
00215   make_hmm_basephone_list(hmminfo);
00216   jlog("Stat: init_phmm: base phones: %5d used in logical\n", hmminfo->basephone.num);
00217 
00218   /* Guess we need to handle context dependency */
00219   /* (word-internal CD is done statically, cross-word CD dynamically */
00220   if (guess_if_cd_hmm(hmminfo)) {
00221     hmminfo->is_triphone = TRUE;
00222   } else {
00223     hmminfo->is_triphone = FALSE;
00224   }
00225 
00226   jlog("Stat: init_phmm: finished reading HMM definitions\n");
00227 
00228   return TRUE;
00229 }
00230 
00237 void
00238 htk_hmm_set_pause_model(HTK_HMM_INFO *hmminfo, char *spmodel_name)
00239 {
00240   HMM_Logical *l;
00241 
00242   l = htk_hmmdata_lookup_logical(hmminfo, spmodel_name);
00243   if (l == NULL) {
00244     jlog("Warning: init_phmm: no model named as \"%s\", no short pause model assigned\n", spmodel_name);
00245   }
00246   hmminfo->sp = l;
00247 }