Julius 4.2
libsent/src/hmminfo/init_phmm.c
説明を見る。
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 
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   if (hmm->cdset_info.binary_malloc) {
00100     /* cdset are allocated by bmalloc (in case read from binary hmmlist) */
00101     if (hmm->cdset_root != NULL) mybfree2(&(hmm->cdset_root));
00102   } else {
00103     /* cdset does not use bmalloc, so free them separately */
00104     free_cdset(&(hmm->cdset_info.cdtree), &(hmm->cdset_root));
00105   }
00106 
00107   /* free all memory that has been allocated by bmalloc2() */
00108   if (hmm->mroot != NULL) mybfree2(&(hmm->mroot));
00109   if (hmm->lroot != NULL) mybfree2(&(hmm->lroot));
00110 
00111   /* free whole */
00112   free(hmm);
00113 
00114   return(TRUE);
00115 }
00116 
00127 boolean
00128 init_hmminfo(HTK_HMM_INFO *hmminfo, char *hmmfilename, char *namemapfile, Value *para)
00129 {
00130   FILE *fp;
00131   boolean ok_p;
00132   boolean binary;
00133 
00134   ok_p = FALSE;
00135 
00136   /* read hmmdef file */
00137   jlog("Stat: init_phmm: Reading in HMM definition\n");
00138   /* first, try ascii format */
00139   if ((fp = fopen_readfile(hmmfilename)) == NULL) {
00140     jlog("Error: init_phmm: failed to open %s\n",hmmfilename);
00141     return FALSE;
00142   }
00143   if (rdhmmdef(fp, hmminfo) == TRUE) {
00144     ok_p = TRUE;
00145   }
00146   if (fclose_readfile(fp) < 0) {
00147     jlog("Error: init_phmm: failed to close %s\n", hmmfilename);
00148     return FALSE;
00149   }
00150   if (ok_p == FALSE) {
00151     /* second, try binary format */
00152     if ((fp = fopen_readfile(hmmfilename)) == NULL) {
00153       jlog("Error: init_phmm: failed to open %s\n",hmmfilename);
00154       return FALSE;
00155     }
00156     if (read_binhmm(fp, hmminfo, TRUE, para) == TRUE) {
00157       ok_p = TRUE;
00158     }
00159     if (fclose_readfile(fp) < 0) {
00160       jlog("Error: init_phmm: failed to close %s\n", hmmfilename);
00161       return FALSE;
00162     }
00163   }
00164   if (ok_p == FALSE) {
00165     jlog("Error: init_phmm: failed to read %s\n", hmmfilename);
00166     return FALSE;
00167   }
00168 
00169   jlog("Stat: init_phmm: defined HMMs: %5d\n", hmminfo->totalhmmnum);
00170 
00171   /* make mapping from logically named HMM to real defined HMM name */
00172   if (namemapfile != NULL) {
00173     /* use HMMList map file */
00174     if ((fp = fopen_readfile(namemapfile)) == NULL) {
00175       jlog("Error: init_phmm: failed to open %s\n",namemapfile);
00176       return FALSE;
00177     }
00178     /* detect binary / ascii by the first 4 bytes */
00179     {
00180       int x;
00181       if (myfread(&x, sizeof(int), 1, fp) < 1) {
00182         jlog("Error: init_phmm: failed to read %s\n", namemapfile);
00183         return FALSE;
00184       }
00185       if (x == 0) {
00186         binary = TRUE;
00187       } else {
00188         binary = FALSE;
00189         myfrewind(fp);
00190       }
00191     }
00192     if (binary) {
00193       /* binary format */
00194       jlog("Stat: init_phmm: loading binary hmmlist\n");
00195       if (load_hmmlist_bin(fp, hmminfo) == FALSE) {
00196         jlog("Error: init_phmm: HMMList \"%s\" read error\n",namemapfile);
00197         return FALSE;
00198       }
00199     } else {
00200       /* ascii format */
00201       jlog("Stat: init_phmm: loading ascii hmmlist\n");
00202       if (rdhmmlist(fp, hmminfo) == FALSE) {
00203         jlog("Error: init_phmm: HMMList \"%s\" read error\n",namemapfile);
00204         return FALSE;
00205       }
00206     }
00207     if (fclose_readfile(fp) < 0) {
00208       jlog("Error: init_phmm: failed to close %s\n", namemapfile);
00209       return FALSE;
00210     }
00211     jlog("Stat: init_phmm: logical names: %5d in HMMList\n", hmminfo->totallogicalnum);
00212 
00213   } else {
00214     /* add all names of physical HMMs as logical names */
00215     hmm_add_physical_to_logical(hmminfo);
00216     jlog("Stat: init_phmm: logical names: %5d\n", hmminfo->totallogicalnum);
00217   }
00218 
00219   /* extract basephone */
00220   make_hmm_basephone_list(hmminfo);
00221   jlog("Stat: init_phmm: base phones: %5d used in logical\n", hmminfo->basephone.num);
00222 
00223   /* Guess we need to handle context dependency */
00224   /* (word-internal CD is done statically, cross-word CD dynamically */
00225   if (guess_if_cd_hmm(hmminfo)) {
00226     hmminfo->is_triphone = TRUE;
00227   } else {
00228     hmminfo->is_triphone = FALSE;
00229   }
00230 
00231   jlog("Stat: init_phmm: finished reading HMM definitions\n");
00232 
00233   return TRUE;
00234 }
00235 
00242 void
00243 htk_hmm_set_pause_model(HTK_HMM_INFO *hmminfo, char *spmodel_name)
00244 {
00245   HMM_Logical *l;
00246 
00247   l = htk_hmmdata_lookup_logical(hmminfo, spmodel_name);
00248   if (l == NULL) {
00249     jlog("Warning: init_phmm: no model named as \"%s\", no short pause model assigned\n", spmodel_name);
00250   }
00251   hmminfo->sp = l;
00252 }