int srch_FLAT_FWD_end(void *srch) { srch_FLAT_FWD_graph_t *fwg; srch_t *s; kbcore_t *kbc; dict_t *dict; stat_t *st; whmm_t *h, *nexth; s3wid_t w; lm_t *lm; s = (srch_t *) srch; fwg = (srch_FLAT_FWD_graph_t *) s->grh->graph_struct; kbc = s->kbc; dict = kbcore_dict(kbc); st = s->stat; lm = s->kbc->lmset->cur_lm; fwg->lathist->frm_latstart[fwg->n_frm] = fwg->lathist->n_lat_entry; /* Add sentinel */ pctr_increment(fwg->ctr_latentry, fwg->lathist->n_lat_entry); /* Free whmm search structures */ for (w = 0; w < dict->n_word; w++) { for (h = fwg->whmm[w]; h; h = nexth) { nexth = h->next; whmm_free(h); } fwg->whmm[w] = NULL; } if (fwg->n_word_cand > 0) { word_cand_free(fwg->word_cand); fwg->n_word_cand = 0; } lm_cache_stats_dump(lm); lm_cache_reset(lm); fwd_timing_dump(fwg); return SRCH_SUCCESS; }
/* Decode the given mfc file and write result to matchfp and matchsegfp */ static void decode_utt (char *uttid, FILE *matchfp, FILE *matchsegfp) { char dagfile[1024]; hyp_t *h, *hyp; char *latdir, *latext; int32 nfrm, ascr, lscr; timing_reset (tm_utt); timing_start (tm_utt); latdir = (char *) cmd_ln_access ("-inlatdir"); latext = (char *) cmd_ln_access ("-latext"); if (latdir) sprintf (dagfile, "%s/%s.%s", latdir, uttid, latext); else sprintf (dagfile, "%s.%s", uttid, latext); if ((nfrm = dag_load (dagfile)) >= 0) { hyp = dag_search (uttid); if ( *((int32 *) cmd_ln_access("-backtrace")) ) log_hyp_detailed (stdout, hyp, uttid, "BP", "bp"); /* Total scaled acoustic score and LM score */ ascr = lscr = 0; for (h = hyp; h; h = h->next) { ascr += h->ascr; lscr += h->lscr; } printf ("BSTPTH: "); log_hypstr (stdout, hyp, uttid, ascr+lscr); printf ("BSTXCT: "); log_hypseg (uttid, stdout, hyp, nfrm); lm_cache_stats_dump (); lm_cache_reset (); } else { E_ERROR("DAG search (%s) failed\n", uttid); hyp = NULL; } /* Log recognition output to the standard match and matchseg files */ if (matchfp) log_hypstr (matchfp, hyp, uttid, 0); if (matchsegfp) log_hypseg (uttid, matchsegfp, hyp, nfrm); dag_destroy (); timing_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); tot_nfr += nfrm; }
int main(int argc, char *argv[]) { char *lm_file; char *args_file; char *ngrams_file; char *lmLoadTimer = "LM Load"; char *lmLookupTimer = "LM Lookup"; char *ngrams[MAX_NGRAMS]; float64 lw, wip, uw, logbase; int i, n, score; int32 *nwdptr; int32 nwords[MAX_NGRAMS]; int scores[MAX_NGRAMS]; lm_t *lm; s3lmwid_t wid[MAX_NGRAMS][MAX_WORDS_PER_NGRAM]; FILE* fp; if (argc < 3) { E_FATAL("USAGE: %s <lm_file> <args_file> <ngrams_file>\n", argv[0]); } args_file = argv[1]; lm_file = argv[2]; ngrams_file = argv[3]; parse_args_file(args_file); lw = cmd_ln_float32("-lw"); wip = cmd_ln_float32("-wip"); uw = cmd_ln_float32("-uw"); logbase = cmd_ln_float32("-logbase"); logs3_init(logbase); metricsStart(lmLoadTimer); /* initialize the language model */ lm = lm_read(lm_file, lw, wip, uw); metricsStop(lmLoadTimer); if ((fp = fopen(ngrams_file, "r")) == NULL) { E_FATAL("Unable to open N-gram file %s\n", ngrams_file); } while (has_more_utterances(fp)) { /* read in all the N-grams */ n = read_ngrams(fp, ngrams, wid, nwords, MAX_NGRAMS, lm); metricsStart(lmLookupTimer); /* scores the N-grams */ for (i = 0; i < n; i++) { scores[i] = score_ngram(wid[i], nwords[i], lm); printf("%-10d %s\n", scores[i], ngrams[i]); /* printf("%-10d %s %d %d %d\n", scores[i], ngrams[i], wid[i][0], wid[i][1], wid[i][2]); */ } /* reset cache if <END_UTT> was reached */ if (n != MAX_NGRAMS) { lm_cache_reset(lm); } metricsStop(lmLookupTimer); } printf("Bigram misses: %d \n", lm->n_bg_bo); printf("Trigram misses: %d \n", lm->n_tg_bo); fflush(stdout); metricsPrint(); }
static void decode_utt (void *data, char *uttfile, int32 sf, int32 ef, char *uttid) { kb_t *kb; acoustic_t *am; int32 featwin, nfr, min_utt_frames, n_vithist; char cepfile[4096], latfile[4096]; vithist_t *finalhist; int32 i, f; glist_t hyplist; FILE *latfp; printf ("\n"); fflush (stdout); E_INFO("Utterance %s\n", uttid); kb = (kb_t *)data; am = kb->am; featwin = feat_window_size(am->fcb); /* Build complete cepfile name and read cepstrum data; check for min length */ ctl_infile (cepfile, cmd_ln_str("-cepdir"), cmd_ln_str("-cepext"), uttfile); if ((nfr = s2mfc_read (cepfile, sf, ef, featwin, am->mfc, S3_MAX_FRAMES)) < 0) { E_ERROR("%s: MFC read failed\n", uttid); return; } E_INFO("%s: %d frames\n", uttid, nfr-(featwin<<1)); ptmr_reset (kb->tm); ptmr_reset (kb->tm_search); ptmr_start (kb->tm); min_utt_frames = (featwin<<1) + 1; if (nfr < min_utt_frames) { E_ERROR("%s: Utterance shorter than %d frames; ignored\n", uttid, min_utt_frames, nfr); return; } /* CMN/AGC */ if (strcmp (cmd_ln_str("-cmn"), "current") == 0) cmn (am->mfc, nfr, feat_cepsize(am->fcb)); if (strcmp (cmd_ln_str("-agc"), "max") == 0) agc_max (am->mfc, nfr); /* Process utterance */ lextree_vit_start (kb, uttid); for (i = featwin, f = 0; i < nfr-featwin; i++, f++) { am->senscale[f] = acoustic_eval (am, i); ptmr_start (kb->tm_search); lextree_vit_frame (kb, f, uttid); printf (" %d,%d,%d", f, glist_count (kb->vithist[f]), glist_count (kb->lextree_active)); fflush (stdout); ptmr_stop (kb->tm_search); } printf ("\n"); finalhist = lextree_vit_end (kb, f, uttid); hyplist = vithist_backtrace (finalhist, kb->am->senscale); hyp_log (stdout, hyplist, _dict_wordstr, (void *)kb->dict); hyp_myfree (hyplist); printf ("\n"); /* Log the entire Viterbi word lattice */ sprintf (latfile, "%s.lat", uttid); if ((latfp = fopen(latfile, "w")) == NULL) { E_ERROR("fopen(%s,w) failed; using stdout\n", latfile); latfp = stdout; } n_vithist = vithist_log (latfp, kb->vithist, f, _dict_wordstr, (void *)kb->dict); if (latfp != stdout) fclose (latfp); else { printf ("\n"); fflush (stdout); } ptmr_stop (kb->tm); if (f > 0) { printf("TMR(%s): %5d frames; %.1fs CPU, %.2f xRT; %.1fs CPU(search), %.2f xRT; %.1fs Elapsed, %.2f xRT\n", uttid, f, kb->tm->t_cpu, kb->tm->t_cpu * 100.0 / f, kb->tm_search->t_cpu, kb->tm_search->t_cpu * 100.0 / f, kb->tm->t_elapsed, kb->tm->t_elapsed * 100.0 / f); printf("CTR(%s): %5d frames; %d Sen (%.1f/fr); %d HMM (%.1f/fr); %d Words (%.1f/fr)\n", uttid, f, kb->n_sen_eval, ((float64)kb->n_sen_eval) / f, kb->n_hmm_eval, ((float64)kb->n_hmm_eval) / f, n_vithist, ((float64) n_vithist) / f); } /* Cleanup */ glist_free (kb->lextree_active); kb->lextree_active = NULL; for (; f >= -1; --f) { /* I.e., including dummy START_WORD node at frame -1 */ glist_myfree (kb->vithist[f], sizeof(vithist_t)); kb->vithist[f] = NULL; } lm_cache_reset (kb->lm); }
/* 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); }