📄 utt.c
字号:
/* ==================================================================== * Copyright (c) 1999-2004 Carnegie Mellon University. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * This work was supported in part by funding from the Defense Advanced * Research Projects Agency and the National Science Foundation of the * United States of America, and the CMU Sphinx Speech Consortium. * * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * ==================================================================== * *//************************************************ * CMU ARPA Speech Project * * Copyright (c) 2000 Carnegie Mellon University. * ALL RIGHTS RESERVED. ************************************************ * * HISTORY * * 15-Jun-2004 Yitao Sun (yitao@cs.cmu.edu) at Carnegie Mellon University * Modified utt_end() to save hypothesis in the kb structure. * * 30-Dec-2000 Rita Singh (rsingh@cs.cmu.edu) at Carnegie Mellon University * Added utt_decode_block() to allow block-based decoding * and decoding of piped input. * * 30-Dec-2000 Rita Singh (rsingh@cs.cmu.edu) at Carnegie Mellon University * Moved all utt_*() routines into utt.c to make them independent * of main() during compilation * * 29-Feb-2000 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University * Modified to allow runtime choice between 3-state and 5-state * HMM topologies (instead of compile-time selection). * * 13-Aug-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University * Added -maxwpf. * * 10-May-1999 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University * Started. */#ifdef WIN32#include <direct.h> /* RAH, added */#endif#include "kb.h"#include "corpus.h"#include "utt.h"#include "logs3.h"#define _CHECKUNDERFLOW_ 1static int32 NO_UFLOW_ADD(int32 a, int32 b){ int32 c;#ifdef _CHECKUNDERFLOW_ c= a + b; c= (c>0 && a <0 && b <0) ? MAX_NEG_INT32 : c;#else c= a + b;#endif return c;}void matchseg_write (FILE *fp, kb_t *kb, glist_t hyp, char *hdr){ gnode_t *gn; hyp_t *h; int32 ascr, lscr; dict_t *dict; ascr = 0; lscr = 0; for (gn = hyp; gn; gn = gnode_next(gn)) { h = (hyp_t *) gnode_ptr (gn); ascr += h->ascr; lscr += h->lscr; } dict = kbcore_dict(kb->kbcore); fprintf (fp, "%s%s S 0 T %d A %d L %d", (hdr ? hdr : ""), kb->uttid, ascr+lscr, ascr, lscr); for (gn = hyp; gn && (gnode_next(gn)); gn = gnode_next(gn)) { h = (hyp_t *) gnode_ptr (gn); fprintf (fp, " %d %d %d %s", h->sf, h->ascr, h->lscr, dict_wordstr(dict, h->id)); } fprintf (fp, " %d\n", kb->nfr); fflush (fp);}void match_write (FILE *fp, kb_t *kb, glist_t hyp, char *hdr){ gnode_t *gn; hyp_t *h; dict_t *dict; int counter=0; dict = kbcore_dict(kb->kbcore); fprintf (fp, "%s ", (hdr ? hdr : "")); for (gn = hyp; gn && (gnode_next(gn)); gn = gnode_next(gn)) { h = (hyp_t *) gnode_ptr (gn); if((!dict_filler_word(dict,h->id)) && (h->id!=dict_finishwid(dict))) fprintf(fp,"%s ",dict_wordstr(dict, dict_basewid(dict,h->id))); counter++; } if(counter==0) fprintf(fp," "); fprintf (fp, "(%s)\n", kb->uttid); fflush (fp);}/* * Begin search at bigrams of <s>, backing off to unigrams; and fillers. * Update kb->lextree_next_active with the list of active lextrees. */void utt_begin (kb_t *kb){ kbcore_t *kbc; int32 n, pred; kbc = kb->kbcore; /* Insert initial <s> into vithist structure */ pred = vithist_utt_begin (kb->vithist, kbc); assert (pred == 0); /* Vithist entry ID for <s> */ /* Enter into unigram lextree[0] */ n = lextree_n_next_active(kb->ugtree[0]); assert (n == 0); lextree_enter (kb->ugtree[0], mdef_silphone(kbc->mdef), -1, 0, pred, kb->beam->hmm); /* Enter into filler lextree */ n = lextree_n_next_active(kb->fillertree[0]); assert (n == 0); lextree_enter (kb->fillertree[0], BAD_S3CIPID, -1, 0, pred, kb->beam->hmm); kb->n_lextrans = 1; kb_lextree_active_swap (kb);}void utt_end (kb_t *kb){ int32 id, ascr, lscr; glist_t hyp; gnode_t *gn; hyp_t *h; FILE *fp, *latfp; dict_t *dict; int32 i; fp = stderr; dict = kbcore_dict (kb->kbcore); if ((id = vithist_utt_end (kb->vithist, kb->kbcore)) >= 0) { if (cmd_ln_str("-bptbldir")) { char file[8192]; sprintf (file, "%s/%s.bpt", cmd_ln_str ("-bptbldir"), kb->uttid); if ((latfp = fopen (file, "w")) == NULL) { E_ERROR("fopen(%s,w) failed; using stdout\n", file); latfp = stdout; } vithist_dump (kb->vithist, -1, kb->kbcore, latfp); if (latfp != stdout) fclose (latfp); } hyp = vithist_backtrace (kb->vithist, id); /* Detailed backtrace */ fprintf (fp, "\nBacktrace(%s)\n", kb->uttid); fprintf (fp, "%6s %5s %5s %11s %8s %4s\n", "LatID", "SFrm", "EFrm", "AScr", "LScr", "Type"); ascr = 0; lscr = 0; for (gn = hyp; gn; gn = gnode_next(gn)) { h = (hyp_t *) gnode_ptr (gn); fprintf (fp, "%6d %5d %5d %11d %8d %4d %s\n", h->vhid, h->sf, h->ef, h->ascr, h->lscr, h->type, dict_wordstr(dict, h->id)); ascr += h->ascr; lscr += h->lscr; } fprintf (fp, " %5d %5d %11d %8d (Total)\n",0,kb->nfr,ascr,lscr); /* Match */ match_write(fp, kb, hyp, "\nFWDVIT: "); /* Matchseg */ if (kb->matchsegfp) matchseg_write (kb->matchsegfp, kb, hyp, NULL); matchseg_write (fp, kb, hyp, "FWDXCT: "); fprintf (fp, "\n"); if (kb->matchfp) match_write (kb->matchfp, kb, hyp, NULL); if (cmd_ln_str ("-outlatdir")) { char str[16384]; int32 ispipe; float64 logbase; sprintf (str, "%s/%s.%s", cmd_ln_str("-outlatdir"), kb->uttid, cmd_ln_str("-latext")); E_INFO("Writing lattice file: %s\n", str); if ((latfp = fopen_comp (str, "w", &ispipe)) == NULL) E_ERROR("fopen_comp (%s,w) failed\n", str); else { /* Write header info */ getcwd (str, sizeof(str)); fprintf (latfp, "# getcwd: %s\n", str); /* Print logbase first!! Other programs look for it early in the * DAG */ logbase = cmd_ln_float32 ("-logbase"); fprintf (latfp, "# -logbase %e\n", logbase); fprintf (latfp, "# -dict %s\n", cmd_ln_str ("-dict")); if (cmd_ln_str ("-fdict")) fprintf (latfp, "# -fdict %s\n", cmd_ln_str ("-fdict")); fprintf (latfp, "# -lm %s\n", cmd_ln_str ("-lm")); fprintf (latfp, "# -mdef %s\n", cmd_ln_str ("-mdef")); fprintf (latfp, "# -mean %s\n", cmd_ln_str ("-mean")); fprintf (latfp, "# -var %s\n", cmd_ln_str ("-var")); fprintf (latfp, "# -mixw %s\n", cmd_ln_str ("-mixw")); fprintf (latfp, "# -tmat %s\n", cmd_ln_str ("-tmat")); fprintf (latfp, "#\n"); fprintf (latfp, "Frames %d\n", kb->nfr); fprintf (latfp, "#\n"); vithist_dag_write (kb->vithist, hyp, dict, cmd_ln_int32("-outlatoldfmt"), latfp); fclose_comp (latfp, ispipe); } } /** free the list containing hyps */ for (gn = hyp; gn; gn = gnode_next(gn)) { ckd_free(gnode_ptr(gn)); } glist_free(hyp); } else E_ERROR("%s: No recognition\n\n", kb->uttid); /** do not print anything if nfr is 0 */ if (kb->nfr > 0) { E_INFO("%4d frm; %4d cdsen, %4d cisen, %5d cdgau %5d cigau/fr, Sen %4.2f, CPU %4.2f " "Clk [Ovrhd %4.2f CPU %4.2f Clk]; " "%5d hmm, %3d wd/fr, %4.2f CPU %4.2f Clk (%s)\n", kb->nfr, (kb->utt_sen_eval + (kb->nfr >> 1)) / kb->nfr, (kb->utt_cisen_eval + (kb->nfr >> 1)) / kb->nfr, (kb->utt_gau_eval + (kb->nfr >> 1)) / kb->nfr, (kb->utt_cigau_eval + (kb->nfr >> 1)) / kb->nfr, kb->tm_sen.t_cpu * 100.0 / kb->nfr, kb->tm_sen.t_elapsed * 100.0 / kb->nfr, kb->tm_ovrhd.t_cpu * 100.0 / kb->nfr, kb->tm_ovrhd.t_elapsed * 100.0 / kb->nfr, (kb->utt_hmm_eval + (kb->nfr >> 1)) / kb->nfr, (vithist_n_entry(kb->vithist) + (kb->nfr >> 1)) / kb->nfr, kb->tm_srch.t_cpu * 100.0 / kb->nfr, kb->tm_srch.t_elapsed * 100.0 / kb->nfr, kb->uttid); } { int32 j, k; for (j = kb->hmm_hist_bins-1; (j >= 0) && (kb->hmm_hist[j] == 0); --j); E_INFO("HMMHist[0..%d](%s):", j, kb->uttid); for (i = 0, k = 0; i <= j; i++) { k += kb->hmm_hist[i]; fprintf (stderr, " %d(%d)", kb->hmm_hist[i], (k*100)/kb->nfr); } fprintf (stderr, "\n"); fflush (stderr); } kb->tot_sen_eval += kb->utt_sen_eval; kb->tot_gau_eval += kb->utt_gau_eval; kb->tot_ci_sen_eval += kb->utt_cisen_eval; kb->tot_ci_gau_eval += kb->utt_cigau_eval; kb->tot_hmm_eval += kb->utt_hmm_eval; kb->tot_wd_exit += vithist_n_entry(kb->vithist); ptmr_reset (&(kb->tm_sen)); ptmr_reset (&(kb->tm_srch)); ptmr_reset (&(kb->tm_ovrhd));#if (!defined(WIN32)) if (! system("ps auxgw > /dev/null 2>&1")) { system ("ps aguxwww | grep /live | grep -v grep"); system ("ps aguxwww | grep /dec | grep -v grep"); }#endif for (i = 0; i < kb->n_lextree; i++) { lextree_utt_end (kb->ugtree[i], kb->kbcore); lextree_utt_end (kb->fillertree[i], kb->kbcore); } vithist_utt_reset (kb->vithist); lm_cache_stats_dump (kbcore_lm(kb->kbcore)); lm_cache_reset (kbcore_lm(kb->kbcore));}void utt_word_trans (kb_t *kb, int32 cf){ int32 k, th; vithist_t *vh; vithist_entry_t *ve; int32 vhid, le, n_ci, score; int32 maxpscore; int32 *bs = NULL, *bv = NULL, epl; s3wid_t wid; int32 p; dict_t *dict; mdef_t *mdef; maxpscore=MAX_NEG_INT32; vh = kb->vithist; th = kb->bestscore + kb->beam->hmm; /* Pruning threshold */ if (vh->bestvh[cf] < 0) return; dict = kbcore_dict(kb->kbcore); mdef = kbcore_mdef(kb->kbcore); n_ci = mdef_n_ciphone(mdef); /* Initialize best exit for each distinct word-final CIphone to NONE */ bs=kb->wordbestscore; bv=kb->wordbestexit; epl=kb->epl; for (p = 0; p < n_ci; p++) { bs[p] = MAX_NEG_INT32; bv[p] = -1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -