/* Write word segmentation output file */ static void write_wdseg(char *dir, align_wdseg_t * wdseg, char *uttid, char *ctlspec) { char str[1024]; FILE *fp; int32 uttscr; /* Attempt to write segmentation for this utt to a separate file */ build_output_uttfile(str, dir, uttid, ctlspec); strcat(str, ".wdseg"); E_INFO("Writing word segmentation to: %s\n", str); if ((fp = fopen(str, "w")) == NULL) { E_ERROR_SYSTEM("Failed to open file %s for writing", str); fp = stdout; /* Segmentations can be directed to stdout this way */ E_INFO("Word segmentation (%s):\n", uttid); dir = NULL; /* Flag to indicate fp shouldn't be closed at the end */ } if (!dir) { fprintf(fp, "WD:%s>", uttid); fflush(fp); } fprintf(fp, "\t%5s %5s %10s %s\n", "SFrm", "EFrm", "SegAScr", "Word"); fflush(fp); uttscr = 0; for (; wdseg; wdseg = wdseg->next) { if (!dir) { fprintf(fp, "wd:%s>", uttid); fflush(fp); } fprintf(fp, "\t%5d %5d %10d %s\n", wdseg->sf, wdseg->ef, wdseg->score, dict_wordstr(dict, wdseg->wid)); fflush(fp); uttscr += wdseg->score; } if (!dir) { fprintf(fp, "WD:%s>", uttid); fflush(fp); } fprintf(fp, " Total score: %11d\n", uttscr); fflush(fp); if (dir) fclose(fp); else { fprintf(fp, "\n"); fflush(fp); } }
/* Write phone segmentation output file */ static void write_phseg(char *dir, align_phseg_t * phseg, char *uttid, char *ctlspec) { char str[1024]; FILE *fp; int32 uttscr; /* Attempt to write segmentation for this utt to a separate file */ build_output_uttfile(str, dir, uttid, ctlspec); strcat(str, ".phseg"); E_INFO("Writing phone segmentation to: %s\n", str); if ((fp = fopen(str, "w")) == NULL) { E_ERROR_SYSTEM("Failed to open file %s for writing", str); fp = stdout; /* Segmentations can be directed to stdout this way */ E_INFO("Phone segmentation (%s):\n", uttid); dir = NULL; /* Flag to indicate fp shouldn't be closed at the end */ } if (!dir) { fprintf(fp, "PH:%s>", uttid); fflush(fp); } fprintf(fp, "\t%5s %5s %9s %s\n", "SFrm", "EFrm", "SegAScr", "Phone"); fflush(fp); uttscr = 0; for (; phseg; phseg = phseg->next) { mdef_phone_str(kbc->mdef, phseg->pid, str); if (!dir) { fprintf(fp, "ph:%s>", uttid); fflush(fp); } fprintf(fp, "\t%5d %5d %9d %s\n", phseg->sf, phseg->ef, phseg->score, str); fflush(fp); uttscr += (phseg->score); } if (!dir) { fprintf(fp, "PH:%s>", uttid); fflush(fp); } fprintf(fp, " Total score: %11d\n", uttscr); fflush(fp); if (dir) fclose(fp); else { fprintf(fp, "\n"); fflush(fp); } }
/* Write phone segmentation output file */ static void write_phseg (char *dir, align_phseg_t *phseg, char *uttid, char *ctlspec) { char str[1024]; FILE *fp; int32 uttscr, f, scale; /* Attempt to write segmentation for this utt to a separate file */ build_output_uttfile (str, dir, uttid, ctlspec); strcat (str, ".phseg"); E_INFO("Writing phone segmentation to: %s\n", str); if ((fp = fopen (str, "w")) == NULL) { E_ERROR("fopen(%s,w) failed\n", str); fp = stdout; /* Segmentations can be directed to stdout this way */ E_INFO ("Phone segmentation (%s):\n", uttid); dir = NULL; /* Flag to indicate fp shouldn't be closed at the end */ } if (! dir) fprintf (fp, "PH:%s>", uttid); fprintf (fp, "\t%5s %5s %9s %s\n", "SFrm", "EFrm", "SegAScr", "Phone"); uttscr = 0; for (; phseg; phseg = phseg->next) { mdef_phone_str (mdef, phseg->pid, str); /* Account for senone score scaling in each frame */ scale = 0; for (f = phseg->sf; f <= phseg->ef; f++) scale += senscale[f]; if (! dir) fprintf (fp, "ph:%s>", uttid); fprintf (fp, "\t%5d %5d %9d %s\n", phseg->sf, phseg->ef, phseg->score + scale, str); uttscr += (phseg->score + scale); } if (! dir) fprintf (fp, "PH:%s>", uttid); fprintf (fp, " Total score: %11d\n", uttscr); if (dir) fclose (fp); else fprintf (fp, "\n"); }
/* Write xlabel style phone segmentation output file */ static void write_phlab(char *dir, align_phseg_t * phseg, char *uttid, char *ctlspec, int32 fps) { char str[1024]; FILE *fp; /* Attempt to write segmentation for this utt to a separate file */ build_output_uttfile(str, dir, uttid, ctlspec); strcat(str, ".lab"); E_INFO("Writing xlabel style phone labels to: %s\n", str); if ((fp = fopen(str, "w")) == NULL) { E_ERROR_SYSTEM("Failed to open file %s for writing", str); fp = stdout; /* Segmentations can be directed to stdout this way */ E_INFO("Phone segmentation (%s):\n", uttid); dir = NULL; /* Flag to indicate fp shouldn't be closed at the end */ } if (!dir) { fprintf(fp, "PH:%s>", uttid); fflush(fp); } fprintf(fp, "#\n"); for (; phseg; phseg = phseg->next) { const char *name; name = mdef_ciphone_str(kbc->mdef, kbc->mdef->phone[phseg->pid].ci); if (!dir) { fprintf(fp, "ph:%s>", uttid); fflush(fp); } fprintf(fp, "%0.6f 125 %s\n", (double) phseg->ef / fps, name); fflush(fp); } if (dir) fclose(fp); else { fprintf(fp, "\n"); fflush(fp); } }
/* Write state segmentation output file */ static void write_stseg (char *dir, align_stseg_t *stseg, char *uttid, char *ctlspec) { char filename[1024]; FILE *fp; align_stseg_t *tmp; int32 i, k; s3cipid_t ci[3]; uint8 pos; char *str; word_posn_t wpos; build_output_uttfile (filename, dir, uttid, ctlspec); strcat (filename, ".stseg"); E_INFO("Writing state segmentation to: %s\n", filename); if ((fp = fopen (filename, "wb")) == NULL) { E_ERROR("fopen(%s,wb) failed\n", filename); return; } /* Write version no. */ if (fwrite ("0.1\n", sizeof(char), 4, fp) != 4) goto write_error; /* Write CI phone names */ for (k = 0; k < mdef->n_ciphone; k++) { str = mdef_ciphone_str (mdef, k); if (fwrite (str, sizeof(char), strlen(str), fp) != strlen(str)) goto write_error; if (fwrite (" ", sizeof(char), 1, fp) != 1) goto write_error; } str = WPOS_NAME; if (fwrite (str, sizeof(char), strlen(str), fp) != strlen(str)) goto write_error; /* Write format "description" */ str = "\nCI.8 LC.8 RC.8 POS.3(HI)-ST.5(LO) SCR(32)\n"; if (fwrite (str, sizeof(char), strlen(str), fp) != strlen(str)) goto write_error; /* Write binary comment string */ if (fwrite ("*end_comment*\n", sizeof(char), 14, fp) != 14) goto write_error; /* Write byte-ordering magic number */ k = BYTE_ORDER_MAGIC; if (fwrite (&k, sizeof(int32), 1, fp) != 1) goto write_error; /* Write #frames */ for (k = 0, tmp = stseg; tmp; k++, tmp = tmp->next); if (fwrite (&k, sizeof(int32), 1, fp) != 1) goto write_error; /* Write state segmentation for each frame */ for (i = 0; stseg; i++, stseg = stseg->next) { mdef_phone_components (mdef, stseg->pid, ci, &(ci[1]), &(ci[2]), &wpos); assert ((wpos >= 0) && (wpos < 8)); assert ((stseg->state >= 0) && (stseg->state < 32)); if (fwrite (ci, sizeof(s3cipid_t), 3, fp) != 3) goto write_error; pos = (wpos << 5) | (stseg->state & 0x001f); if (fwrite (&pos, sizeof(uint8), 1, fp) != 1) goto write_error; k = stseg->score + senscale[i]; if (fwrite (&k, sizeof(int32), 1, fp) != 1) goto write_error; } fclose (fp); return; write_error: E_ERROR("fwrite(%s) failed\n", filename); fclose (fp); }
/* * Write state segmentation in Sphinx-II format. (Must be written in BIG-ENDIAN * format!) */ static void write_s2stseg (char *dir, align_stseg_t *stseg, char *uttid, char *ctlspec) { char filename[1024]; FILE *fp; align_stseg_t *tmp; int32 k; s3cipid_t ci[3]; word_posn_t wpos; int16 s2_info; char buf[8]; static int32 byterev = -1; /* Whether to byte reverse output data */ build_output_uttfile (filename, dir, uttid, ctlspec); strcat (filename, ".v8_seg"); /* .v8_seg for compatibility */ E_INFO("Writing Sphinx-II format state segmentation to: %s\n", filename); if ((fp = fopen (filename, "wb")) == NULL) { E_ERROR("fopen(%s,wb) failed\n", filename); return; } if (byterev < 0) { /* Byte ordering of host machine unknown; first figure it out */ k = (int32) BYTE_ORDER_MAGIC; if (fwrite (&k, sizeof(int32), 1, fp) != 1) goto write_error; fclose (fp); if ((fp = fopen (filename, "rb")) == NULL) { E_ERROR ("fopen(%s,rb) failed\n", filename); return; } if (fread (buf, 1, sizeof(int32), fp) != sizeof(int32)) { E_ERROR ("fread(%s) failed\n", filename); return; } fclose (fp); /* If buf[0] == lsB of BYTE_ORDER_MAGIC, we are little-endian. Need to byterev */ byterev = (buf[0] == (BYTE_ORDER_MAGIC & 0x000000ff)) ? 1 : 0; if ((fp = fopen (filename, "wb")) == NULL) { E_ERROR("fopen(%s,wb) failed\n", filename); return; } } /* Write #frames */ for (k = 0, tmp = stseg; tmp; k++, tmp = tmp->next); if (byterev) SWAP_INT32(&k); if (fwrite (&k, sizeof(int32), 1, fp) != 1) goto write_error; /* Write state info for each frame */ for (; stseg; stseg = stseg->next) { mdef_phone_components (mdef, stseg->pid, ci, &(ci[1]), &(ci[2]), &wpos); s2_info = ci[0] * mdef->n_emit_state + stseg->state; if (stseg->start) s2_info |= 0x8000; if (byterev) SWAP_INT16(&s2_info); if (fwrite (&s2_info, sizeof(int16), 1, fp) != 1) goto write_error; } fclose (fp); return; write_error: E_ERROR("fwrite(%s) failed\n", filename); fclose (fp); }
/* Decode the given mfc file and write result to given directory */ static void utt_astar(void *data, utt_res_t * ur, int32 sf, int32 ef, char *uttid) { char dagfile[1024], nbestfile[1024]; const char *latdir; const char *latext; const char *nbestext; dag_t *dag; int32 nfrm; if (ur->lmname) lmset_set_curlm_wname(lmset, ur->lmname); latdir = cmd_ln_str_r(config, "-inlatdir"); latext = cmd_ln_str_r(config, "-latext"); nbestext = cmd_ln_str_r(config, "-nbestext"); if (latdir) { build_output_uttfile(dagfile, latdir, uttid, ur->uttfile); strcat(dagfile, "."); strcat(dagfile, latext); } else sprintf(dagfile, "%s.%s", uttid, latext); ptmr_reset(&tm_utt); ptmr_start(&tm_utt); nfrm = 0; if ((dag = dag_load(dagfile, cmd_ln_int32_r(config, "-maxedge"), cmd_ln_float32_r(config, "-logbase"), cmd_ln_int32_r(config, "-dagfudge"), dict, fpen, config, logmath)) != NULL) { if (dict_filler_word(dict, dag->end->wid)) dag->end->wid = dict->finishwid; dag_remove_unreachable(dag); if (dag_bypass_filler_nodes(dag, 1.0, dict, fpen) < 0) { E_ERROR("maxedge limit (%d) exceeded\n", dag->maxedge); goto search_done; } dag_compute_hscr(dag, dict, lmset->cur_lm, 1.0); dag_remove_bypass_links(dag); E_INFO("%5d frames, %6d nodes, %8d edges, %8d bypass\n", dag->nfrm, dag->nnode, dag->nlink, dag->nbypass); nfrm = dag->nfrm; build_output_uttfile(nbestfile, nbestdir, uttid, ur->uttfile); strcat(nbestfile, "."); strcat(nbestfile, nbestext); nbest_search(dag, nbestfile, uttid, 1.0, dict, lmset->cur_lm, fpen); lm_cache_stats_dump(lmset->cur_lm); lm_cache_reset(lmset->cur_lm); } else E_ERROR("Dag load (%s) failed\n", uttid); search_done: dag_destroy(dag); ptmr_stop(&tm_utt); printf("%s: TMR: %5d Frm", uttid, nfrm); if (nfrm > 0) { printf(" %6.2f xEl", tm_utt.t_elapsed * 100.0 / nfrm); printf(" %6.2f xCPU", tm_utt.t_cpu * 100.0 / nfrm); } printf("\n"); fflush(stdout); }