[Julius-cvs 608] CVS update: julius4/libsent/src/hmminfo

Back to archive index

sumom****@users***** sumom****@users*****
2011年 2月 13日 (日) 14:02:00 JST


Index: julius4/libsent/src/hmminfo/cdset.c
diff -u julius4/libsent/src/hmminfo/cdset.c:1.4 julius4/libsent/src/hmminfo/cdset.c:1.5
--- julius4/libsent/src/hmminfo/cdset.c:1.4	Tue Sep 30 12:58:18 2008
+++ julius4/libsent/src/hmminfo/cdset.c	Sun Feb 13 14:02:00 2011
@@ -60,7 +60,7 @@
  * @author Akinobu LEE
  * @date   Tue Feb 15 17:58:54 2005
  *
- * $Revision: 1.4 $
+ * $Revision: 1.5 $
  * 
  */
 /*
@@ -87,6 +87,7 @@
 static void
 cdset_init(HTK_HMM_INFO *hmminfo)
 {
+  hmminfo->cdset_info.binary_malloc = FALSE;
   hmminfo->cdset_info.cdtree = NULL;
 }
 
@@ -164,6 +165,7 @@
 {
   int i;
   CD_Set *a;
+  int j;
 
   a = ptr;
   printf("name: %s\n", a->name);
@@ -174,11 +176,13 @@
     } else {
       printf("\t[state %d]  %d variants\n", i, a->stateset[i].num);
     }
-    /*
-      for(j=0;j<a->stateset[i].num;j++) {
-        put_htk_state(stdout, a->stateset[i].s[j]);
+    for(j=0;j<a->stateset[i].num;j++) {
+      if (a->stateset[i].s[j]->name) {
+	printf("\t\t%s %d\n", a->stateset[i].s[j]->name, a->stateset[i].s[j]->id);
+      } else {
+	printf("\t\t(NULL) %d\n", a->stateset[i].s[j]->id);
       }
-    */
+    }
   }
 }
 
@@ -344,6 +348,7 @@
   }
 
   /* now that cdset is completely built */
+  hmminfo->cdset_info.binary_malloc = FALSE;
   
   return(TRUE);
 }
Index: julius4/libsent/src/hmminfo/init_phmm.c
diff -u julius4/libsent/src/hmminfo/init_phmm.c:1.4 julius4/libsent/src/hmminfo/init_phmm.c:1.5
--- julius4/libsent/src/hmminfo/init_phmm.c:1.4	Tue Sep 30 12:58:18 2008
+++ julius4/libsent/src/hmminfo/init_phmm.c	Sun Feb 13 14:02:00 2011
@@ -12,7 +12,7 @@
  * @author Akinobu LEE
  * @date   Tue Feb 15 23:05:33 2005
  *
- * $Revision: 1.4 $
+ * $Revision: 1.5 $
  * 
  */
 /*
@@ -96,8 +96,13 @@
 boolean
 hmminfo_free(HTK_HMM_INFO *hmm)
 {
-  /* cdset does not use bmalloc, so free them separately */
-  free_cdset(&(hmm->cdset_info.cdtree), &(hmm->cdset_root));
+  if (hmm->cdset_info.binary_malloc) {
+    /* cdset are allocated by bmalloc (in case read from binary hmmlist) */
+    if (hmm->cdset_root != NULL) mybfree2(&(hmm->cdset_root));
+  } else {
+    /* cdset does not use bmalloc, so free them separately */
+    free_cdset(&(hmm->cdset_info.cdtree), &(hmm->cdset_root));
+  }
 
   /* free all memory that has been allocated by bmalloc2() */
   if (hmm->mroot != NULL) mybfree2(&(hmm->mroot));
Index: julius4/libsent/src/hmminfo/rdhmmdef.c
diff -u julius4/libsent/src/hmminfo/rdhmmdef.c:1.4 julius4/libsent/src/hmminfo/rdhmmdef.c:1.5
--- julius4/libsent/src/hmminfo/rdhmmdef.c:1.4	Fri Feb 11 00:23:52 2011
+++ julius4/libsent/src/hmminfo/rdhmmdef.c	Sun Feb 13 14:02:00 2011
@@ -30,7 +30,7 @@
  * @author Akinobu LEE
  * @date   Wed Feb 16 00:17:18 2005
  *
- * $Revision: 1.4 $
+ * $Revision: 1.5 $
  * 
  */
 /*
@@ -349,6 +349,15 @@
     }
     hmm->totalpdfnum = n;
   }
+  /* assign ID number for all HTK_HMM_Trans */
+  {
+    HTK_HMM_Trans *ttmp;
+    int n = 0;
+    for (ttmp = hmm->trstart; ttmp; ttmp = ttmp->next) {
+      ttmp->id = n++;
+    }
+    hmm->totaltransnum = n;
+  }
 #ifdef ENABLE_MSD
   /* check if MSD-HMM */
   htk_hmm_check_msd(hmm);
Index: julius4/libsent/src/hmminfo/read_binhmm.c
diff -u julius4/libsent/src/hmminfo/read_binhmm.c:1.5 julius4/libsent/src/hmminfo/read_binhmm.c:1.6
--- julius4/libsent/src/hmminfo/read_binhmm.c:1.5	Sun Jul 20 01:51:35 2008
+++ julius4/libsent/src/hmminfo/read_binhmm.c	Sun Feb 13 14:02:00 2011
@@ -22,7 +22,7 @@
  * @author Akinobu LEE
  * @date   Wed Feb 16 05:23:59 2005
  *
- * $Revision: 1.5 $
+ * $Revision: 1.6 $
  * 
  */
 /*
@@ -850,6 +850,24 @@
     hmm->totalpdfnum = n;
   }
 
+  /* re-number state id */
+  {
+    HTK_HMM_State *stmp;
+    int n = 0;
+    for (stmp = hmm->ststart; stmp; stmp = stmp->next) {
+      stmp->id = n++;
+    }
+  }
+  /* assign ID number for all HTK_HMM_Trans */
+  {
+    HTK_HMM_Trans *ttmp;
+    int n = 0;
+    for (ttmp = hmm->trstart; ttmp; ttmp = ttmp->next) {
+      ttmp->id = n++;
+    }
+    hmm->totaltransnum = n;
+  }
+
   /* determine whether this model needs multi-path handling */
   hmm->need_multipath = htk_hmm_has_several_arc_on_edge(hmm);
   if (hmm->need_multipath) {
Index: julius4/libsent/src/hmminfo/read_binhmmlist.c
diff -u julius4/libsent/src/hmminfo/read_binhmmlist.c:1.1 julius4/libsent/src/hmminfo/read_binhmmlist.c:1.2
--- julius4/libsent/src/hmminfo/read_binhmmlist.c:1.1	Tue Sep 30 12:58:18 2008
+++ julius4/libsent/src/hmminfo/read_binhmmlist.c	Sun Feb 13 14:02:00 2011
@@ -41,7 +41,7 @@
  * @author Akinobu LEE
  * @date   Wed Feb 16 04:04:23 2005
  *
- * $Revision: 1.1 $
+ * $Revision: 1.2 $
  * 
  */
 /*
@@ -55,6 +55,15 @@
 #include <sent/htk_hmm.h>
 #include <sent/ptree.h>
 
+/** 
+ * Callback to read hmmlist data from file.
+ * 
+ * @param data_p [out] pointer to index tree node to store the leaf content
+ * @param data [in] user data
+ * @param fp [in] file pointer to read
+ * 
+ * @return TRUE on success, FALSE on failure.
+ */
 static boolean
 load_hmmlist_callback(void **data_p, void *data, FILE *fp)
 {
@@ -90,16 +99,105 @@
   return TRUE;
 }
 
+typedef struct {
+  HTK_HMM_Trans **tr;
+  HTK_HMM_State **st;
+} ListData;
+
+/** 
+ * Callback to read cdset data from file.
+ * 
+ * @param data_p [out] pointer to index tree node to store the leaf content
+ * @param data [in] user data
+ * @param fp [in] file pointer to read
+ * 
+ * @return TRUE on success, FALSE on failure.
+ */
+static boolean
+load_cdset_callback(void **data_p, void *data, FILE *fp)
+{
+  HTK_HMM_INFO *hmminfo = data;
+  ListData *ld = (ListData *)hmminfo->hook;
+  CD_Set *new;
+  int len;
+  int id;
+  int i, j;
+  HTK_HMM_Trans *ttmp;
+
+  new = (CD_Set *)mybmalloc2(sizeof(CD_Set), &(hmminfo->cdset_root));
+  if (myfread(&len, sizeof(int), 1, fp) < 1) return FALSE;
+  new->name = (char *)mybmalloc2(len, &(hmminfo->cdset_root));
+  if (myfread(new->name, len, 1, fp) < 1) return FALSE;
+  if (myfread(&id, sizeof(int), 1, fp) < 1) return FALSE;
+  new->tr = ld->tr[id];
+  if (myfread(&(new->state_num), sizeof(unsigned short), 1, fp) < 1) return FALSE;
+  new->stateset = (CD_State_Set *)mybmalloc2(sizeof(CD_State_Set) * new->state_num, &(hmminfo->cdset_root));
+  for(i=0;i<new->state_num;i++) {
+    if (myfread(&(new->stateset[i].num), sizeof(unsigned short), 1, fp) < 1) return FALSE;
+    new->stateset[i].maxnum = new->stateset[i].num;
+    new->stateset[i].s = (HTK_HMM_State **)mybmalloc2(sizeof(HTK_HMM_State *) * new->stateset[i].num, &(hmminfo->cdset_root));
+    for(j=0;j<new->stateset[i].num;j++) {
+      if (myfread(&id, sizeof(int), 1, fp) < 1) return FALSE;
+      new->stateset[i].s[j] = ld->st[id];
+    }
+  }  
+
+  *data_p = new;
+  
+  return TRUE;
+}
+
+/** 
+ * Load HMMList and CDSet data from binary file.
+ * 
+ * @param fp [in] file pointer to read
+ * @param hmminfo [in] HMM definition
+ * 
+ * @return TRUE on success, FALSE on failure.
+ */
 boolean
 load_hmmlist_bin(FILE *fp, HTK_HMM_INFO *hmminfo)
 {
   HMM_Logical *l;
   int n;
+  HTK_HMM_Trans *t;
+  HTK_HMM_State *s;
+  ListData ld;
+
+  /* build id->entity list */
+  ld.tr = (HTK_HMM_Trans **)mymalloc(sizeof(HTK_HMM_Trans *) * hmminfo->totaltransnum);
+  n = 0;
+  for (t = hmminfo->trstart; t; t = t->next) {
+    ld.tr[n++] = t;
+  }
+  ld.st = (HTK_HMM_State **)mymalloc(sizeof(HTK_HMM_State *) * hmminfo->totalstatenum);
+  n = 0;
+  for (s = hmminfo->ststart; s; s = s->next) {
+    ld.st[n++] = s;
+  }
+  /* set it to hook */
+  hmminfo->hook = &ld;
 
+  /* load hmmlist */
+  jlog("Stat: load_hmmlist_bin: reading hmmlist\n");
   if (aptree_read(fp, &(hmminfo->logical_root), &(hmminfo->lroot), hmminfo, load_hmmlist_callback) == FALSE) {
     jlog("Error: load_hmmlist_bin: failed to read hmmlist from binary file\n");
     return FALSE;
   }
+  /* load cdset */
+  jlog("Stat: load_hmmlist_bin: reading pseudo phone set\n");
+  if (aptree_read(fp, &(hmminfo->cdset_info.cdtree), &(hmminfo->cdset_root), hmminfo, load_cdset_callback) == FALSE) {
+    jlog("Warning: load_hmmlist_bin: cdset not in binary file, skip reading\n");
+  } else {
+    hmminfo->cdset_info.binary_malloc = TRUE;
+  }
+
+  /* remove data */
+  free(ld.st);
+  free(ld.tr);
+  hmminfo->hook = NULL;
+
+  /* form other data */
   n = 0;
   for(l=hmminfo->lgstart;l;l=l->next) n++;
   hmminfo->totallogicalnum = n;
Index: julius4/libsent/src/hmminfo/write_binhmmlist.c
diff -u julius4/libsent/src/hmminfo/write_binhmmlist.c:1.1 julius4/libsent/src/hmminfo/write_binhmmlist.c:1.2
--- julius4/libsent/src/hmminfo/write_binhmmlist.c:1.1	Tue Sep 30 12:58:18 2008
+++ julius4/libsent/src/hmminfo/write_binhmmlist.c	Sun Feb 13 14:02:00 2011
@@ -13,7 +13,7 @@
  * @author Akinobu LEE
  * @date   Wed Feb 16 04:04:23 2005
  *
- * $Revision: 1.1 $
+ * $Revision: 1.2 $
  * 
  */
 /*
@@ -27,6 +27,14 @@
 #include <sent/htk_hmm.h>
 #include <sent/ptree.h>
 
+/** 
+ * Callback to write hmmlist data into file.
+ * 
+ * @param data [in] hmmlist node data
+ * @param fp [in] file pointer to write
+ * 
+ * @return TRUE on success, FALSE on failure.
+ */
 static boolean
 save_hmmlist_callback(void *data, FILE *fp)
 {
@@ -50,18 +58,65 @@
   return TRUE;
 }
 
+/** 
+ * Callback to write cdset data into file.
+ * 
+ * @param data [in] cdset node data
+ * @param fp [in] file pointer to write
+ * 
+ * @return TRUE on success, FALSE on failure.
+ */
+static boolean
+save_cdset_callback(void *data, FILE *fp)
+{
+  CD_Set *cd = data;
+  int len;
+  int i, j;
+
+  len = strlen(cd->name) + 1;
+  if (myfwrite(&len, sizeof(int), 1, fp) < 1) return FALSE;
+  if (myfwrite(cd->name, len, 1, fp) < 1) return FALSE;
+  if (myfwrite(&(cd->tr->id), sizeof(int), 1, fp) < 1) return FALSE;
+  if (myfwrite(&(cd->state_num), sizeof(unsigned short), 1, fp) < 1) return FALSE;
+  for(i=0;i<cd->state_num;i++) {
+    if (myfwrite(&(cd->stateset[i].num), sizeof(unsigned short), 1, fp) < 1) return FALSE;
+    for(j=0;j<cd->stateset[i].num;j++) {
+      if (myfwrite(&(cd->stateset[i].s[j]->id), sizeof(int), 1, fp) < 1) return FALSE;
+      
+    }
+  }
+  
+  return TRUE;
+}
+
+/** 
+ * Write hmmlist (logical-to-physical mapping table) and
+ * cdset (pseudo phone set) to file.
+ * 
+ * @param fp [in] file pointer to write
+ * @param hmminfo [in] HMM definition data
+ * 
+ * @return TRUE on success, FALSE on failure.
+ */
 boolean
 save_hmmlist_bin(FILE *fp, HTK_HMM_INFO *hmminfo)
 {
-  /* set mark */
+  /* write 4 byte as NULL to auto detect file format at read time */
+  /* this mark will be read in init_hmminfo() */
   int x = 0;
   if (myfwrite(&x, sizeof(int), 1, fp) < 1) {
     jlog("Error: save_hmmlist_bin: failed to write hmmlist to binary file\n");
     return FALSE;
   }
+  /* write hmmlist */
   if (aptree_write(fp, hmminfo->logical_root, save_hmmlist_callback) == FALSE) {
     jlog("Error: save_hmmlist_bin: failed to write hmmlist to binary file\n");
     return FALSE;
   }
+  /* write cdset */
+  if (aptree_write(fp, hmminfo->cdset_info.cdtree, save_cdset_callback) == FALSE) {
+    jlog("Error: save_hmmlist_bin: failed to write cdset to binary file\n");
+    return FALSE;
+  }
   return TRUE;
 }



Julius-cvs メーリングリストの案内
Back to archive index