static void do_search(ps_search_t *search, acmod_t *acmod) { FILE *rawfh; int16 buf[2048]; size_t nread; int16 const *bptr; int nfr; TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); ps_search_start(search); while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), 2048, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { while (acmod->n_feat_frame > 0) { ps_search_step(search, acmod->output_frame); acmod_advance(acmod); } } } ps_search_finish(search); TEST_ASSERT(acmod_end_utt(acmod) >= 0); fclose(rawfh); }
void run_acmod_test(acmod_t *acmod) { FILE *rawfh; int16 *buf; int16 const *bptr; size_t nread, nsamps; int nfr; int frame_counter; cmn_live_set(acmod->fcb->cmn_struct, cmninit); nsamps = 2048; frame_counter = 0; buf = ckd_calloc(nsamps, sizeof(*buf)); TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); printf("Incremental(2048):\n"); while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), nsamps, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { int16 best_score; int frame_idx = -1, best_senid; while (acmod->n_feat_frame > 0) { acmod_score(acmod, &frame_idx); acmod_advance(acmod); best_score = acmod_best_score(acmod, &best_senid); printf("Frame %d best senone %d score %d\n", frame_idx, best_senid, best_score); TEST_EQUAL(frame_counter, frame_idx); ++frame_counter; frame_idx = -1; } } } TEST_EQUAL(0, acmod_end_utt(acmod)); nread = 0; { int16 best_score; int frame_idx = -1, best_senid; while (acmod->n_feat_frame > 0) { acmod_score(acmod, &frame_idx); acmod_advance(acmod); best_score = acmod_best_score(acmod, &best_senid); printf("Frame %d best senone %d score %d\n", frame_idx, best_senid, best_score); TEST_EQUAL(frame_counter, frame_idx); ++frame_counter; frame_idx = -1; } } fclose(rawfh); }
int main(int argc, char *argv[]) { ps_decoder_t *ps; cmd_ln_t *config; acmod_t *acmod; ps_search_t *ngs, *pls; clock_t c; int32 score; int i; TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k", "-lm", MODELDIR "/lm/en_US/wsj0vp.5000.DMP", "-dict", MODELDIR "/lm/en_US/cmu07a.dic", "-fwdtree", "yes", "-fwdflat", "no", "-bestpath", "no", "-pl_window", "6", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); ngs = ps->search; pls = ps->phone_loop; acmod = ps->acmod; setbuf(stdout, NULL); c = clock(); for (i = 0; i < 5; ++i) { FILE *rawfh; int16 buf[2048]; size_t nread; int16 const *bptr; int nfr, n_searchfr; TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); ps_search_start(ngs); ps_search_start(pls); n_searchfr = 0; while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), 2048, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { while (acmod->n_feat_frame > 0) { ps_search_step(pls, n_searchfr); if (n_searchfr >= 6) ps_search_step(ngs, n_searchfr - 6); acmod_advance(acmod); ++n_searchfr; } } } for (nfr = n_searchfr - 6; nfr < n_searchfr; ++nfr) { ps_search_step(ngs, nfr); } ps_search_finish(pls); ps_search_finish(ngs); printf("%s\n", ps_search_hyp(ngs, &score)); TEST_ASSERT(acmod_end_utt(acmod) >= 0); fclose(rawfh); } printf("%s\n", ps_search_hyp(ngs, &score)); TEST_EQUAL(0, strcmp("go forward ten years", ps_search_hyp(ngs, &score))); c = clock() - c; printf("5 * fwdtree search in %.2f sec\n", (double)c / CLOCKS_PER_SEC); ps_free(ps); return 0; }
int main(int argc, char *argv[]) { ps_decoder_t *ps; cmd_ln_t *config; acmod_t *acmod; fsg_search_t *fsgs; ps_lattice_t *dag; ps_seg_t *seg; int32 score; TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", DATADIR "/tidigits/hmm", "-fsg", DATADIR "/tidigits/lm/tidigits.fsg", "-dict", DATADIR "/tidigits/lm/tidigits.dic", "-bestpath", "no", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); fsgs = (fsg_search_t *)ps->search; acmod = ps->acmod; setbuf(stdout, NULL); { FILE *rawfh; int16 buf[2048]; size_t nread; int16 const *bptr; char const *hyp; int nfr; TEST_ASSERT(rawfh = fopen(DATADIR "/numbers.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); fsg_search_start(ps_search_base(fsgs)); while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), 2048, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { while (acmod->n_feat_frame > 0) { fsg_search_step(ps_search_base(fsgs), acmod->output_frame); acmod_advance(acmod); } } } fsg_search_finish(ps_search_base(fsgs)); hyp = fsg_search_hyp(ps_search_base(fsgs), &score, NULL); printf("FSG: %s (%d)\n", hyp, score); TEST_ASSERT(acmod_end_utt(acmod) >= 0); fclose(rawfh); } for (seg = ps_seg_iter(ps); seg; seg = ps_seg_next(seg)) { char const *word; int sf, ef; int32 post, lscr, ascr, lback; word = ps_seg_word(seg); ps_seg_frames(seg, &sf, &ef); post = ps_seg_prob(seg, &ascr, &lscr, &lback); printf("%s (%d:%d) P(w|o) = %f ascr = %d lscr = %d lback = %d\n", word, sf, ef, logmath_exp(ps_get_logmath(ps), post), ascr, lscr, lback); } /* Now get the DAG and play with it. */ dag = ps_get_lattice(ps); ps_lattice_write(dag, "test_fsg3.lat"); printf("BESTPATH: %s\n", ps_lattice_hyp(dag, ps_lattice_bestpath(dag, NULL, 1.0, 15.0))); ps_lattice_posterior(dag, NULL, 15.0); ps_free(ps); cmd_ln_free_r(config); return 0; }
int main(int argc, char *argv[]) { ps_decoder_t *ps; cmd_ln_t *config; acmod_t *acmod; ngram_search_t *ngs; TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/en-us/en-us", "-lm", MODELDIR "/en-us/en-us.lm.dmp", "-dict", MODELDIR "/en-us/cmudict-en-us.dict", "-fwdtree", "no", "-fwdflat", "yes", "-bestpath", "no", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); ngs = (ngram_search_t *)ps->search; acmod = ps->acmod; setbuf(stdout, NULL); { FILE *rawfh, *senfh; int16 buf[2048]; size_t nread; int16 const *bptr; int nfr; TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); TEST_ASSERT(senfh = fopen("goforward.sen", "wb")); TEST_EQUAL(0, acmod_set_senfh(acmod, senfh)); ngram_fwdflat_start(ngs); while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), 2048, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { while (acmod->n_feat_frame > 0) { ngram_fwdflat_search(ngs, acmod->output_frame); acmod_advance(acmod); } } } ngram_fwdflat_finish(ngs); printf("%s\n", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL))); TEST_ASSERT(acmod_end_utt(acmod) >= 0); fclose(rawfh); TEST_EQUAL(0, strcmp("go forward ten meters", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL)))); TEST_EQUAL(0, acmod_set_senfh(acmod, NULL)); TEST_EQUAL(0, acmod_start_utt(acmod)); TEST_ASSERT(senfh = fopen("goforward.sen", "rb")); TEST_EQUAL(0, acmod_set_insenfh(acmod, senfh)); ngram_fwdflat_start(ngs); while ((nfr = acmod_read_scores(acmod)) > 0) { while (acmod->n_feat_frame > 0) { ngram_fwdflat_search(ngs, acmod->output_frame); acmod_advance(acmod); } } ngram_fwdflat_finish(ngs); printf("%s\n", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL))); TEST_ASSERT(acmod_end_utt(acmod) >= 0); TEST_EQUAL(0, strcmp("go forward ten meters", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL)))); fclose(senfh); } ps_free(ps); cmd_ln_free_r(config); return 0; }
int main(int argc, char *argv[]) { acmod_t *acmod; logmath_t *lmath; cmd_ln_t *config; FILE *rawfh; int16 *buf; int16 const *bptr; mfcc_t **cepbuf, **cptr; size_t nread, nsamps; int nfr; int frame_counter; int bestsen1[270]; lmath = logmath_init(1.0001, 0, 0); config = cmd_ln_init(NULL, ps_args(), TRUE, "-featparams", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/feat.params", "-mdef", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/mdef", "-mean", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/means", "-var", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/variances", "-tmat", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/transition_matrices", "-sendump", MODELDIR "/hmm/en_US/hub4wsj_sc_8k/sendump", "-compallsen", "true", "-cmn", "prior", "-tmatfloor", "0.0001", "-mixwfloor", "0.001", "-varfloor", "0.0001", "-mmap", "no", "-topn", "4", "-ds", "1", "-input_endian", "little", "-samprate", "16000", NULL); TEST_ASSERT(config); TEST_ASSERT(acmod = acmod_init(config, lmath, NULL, NULL)); cmn_prior_set(acmod->fcb->cmn_struct, prior); nsamps = 2048; frame_counter = 0; buf = ckd_calloc(nsamps, sizeof(*buf)); TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); E_INFO("Incremental(2048):\n"); while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), nsamps, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0 || nread > 0) { int16 const *senscr; int16 best_score; int frame_idx = -1, best_senid; while (acmod->n_feat_frame > 0) { senscr = acmod_score(acmod, &frame_idx); acmod_advance(acmod); best_score = acmod_best_score(acmod, &best_senid); E_INFO("Frame %d best senone %d score %d\n", frame_idx, best_senid, best_score); TEST_EQUAL(frame_counter, frame_idx); if (frame_counter < 190) bestsen1[frame_counter] = best_score; ++frame_counter; frame_idx = -1; } } } TEST_EQUAL(0, acmod_end_utt(acmod)); nread = 0; { int16 const *senscr; int16 best_score; int frame_idx = -1, best_senid; while (acmod->n_feat_frame > 0) { senscr = acmod_score(acmod, &frame_idx); acmod_advance(acmod); best_score = acmod_best_score(acmod, &best_senid); E_INFO("Frame %d best senone %d score %d\n", frame_idx, best_senid, best_score); if (frame_counter < 190) bestsen1[frame_counter] = best_score; TEST_EQUAL(frame_counter, frame_idx); ++frame_counter; frame_idx = -1; } } /* Now try to process the whole thing at once. */ E_INFO("Whole utterance:\n"); cmn_prior_set(acmod->fcb->cmn_struct, prior); nsamps = ftell(rawfh) / sizeof(*buf); clearerr(rawfh); fseek(rawfh, 0, SEEK_SET); buf = ckd_realloc(buf, nsamps * sizeof(*buf)); TEST_EQUAL(nsamps, fread(buf, sizeof(*buf), nsamps, rawfh)); bptr = buf; TEST_EQUAL(0, acmod_start_utt(acmod)); acmod_process_raw(acmod, &bptr, &nsamps, TRUE); TEST_EQUAL(0, acmod_end_utt(acmod)); { int16 const *senscr; int16 best_score; int frame_idx = -1, best_senid; frame_counter = 0; while (acmod->n_feat_frame > 0) { senscr = acmod_score(acmod, &frame_idx); acmod_advance(acmod); best_score = acmod_best_score(acmod, &best_senid); E_INFO("Frame %d best senone %d score %d\n", frame_idx, best_senid, best_score); if (frame_counter < 190) TEST_EQUAL_LOG(best_score, bestsen1[frame_counter]); TEST_EQUAL(frame_counter, frame_idx); ++frame_counter; frame_idx = -1; } } /* Now process MFCCs and make sure we get the same results. */ cepbuf = ckd_calloc_2d(frame_counter, fe_get_output_size(acmod->fe), sizeof(**cepbuf)); fe_start_utt(acmod->fe); nsamps = ftell(rawfh) / sizeof(*buf); bptr = buf; nfr = frame_counter; fe_process_frames(acmod->fe, &bptr, &nsamps, cepbuf, &nfr); fe_end_utt(acmod->fe, cepbuf[frame_counter-1], &nfr); E_INFO("Incremental(MFCC):\n"); cmn_prior_set(acmod->fcb->cmn_struct, prior); TEST_EQUAL(0, acmod_start_utt(acmod)); cptr = cepbuf; nfr = frame_counter; frame_counter = 0; while ((acmod_process_cep(acmod, &cptr, &nfr, FALSE)) > 0) { int16 const *senscr; int16 best_score; int frame_idx = -1, best_senid; while (acmod->n_feat_frame > 0) { senscr = acmod_score(acmod, &frame_idx); acmod_advance(acmod); best_score = acmod_best_score(acmod, &best_senid); E_INFO("Frame %d best senone %d score %d\n", frame_idx, best_senid, best_score); TEST_EQUAL(frame_counter, frame_idx); if (frame_counter < 190) TEST_EQUAL_LOG(best_score, bestsen1[frame_counter]); ++frame_counter; frame_idx = -1; } } TEST_EQUAL(0, acmod_end_utt(acmod)); nfr = 0; acmod_process_cep(acmod, &cptr, &nfr, FALSE); { int16 const *senscr; int16 best_score; int frame_idx = -1, best_senid; while (acmod->n_feat_frame > 0) { senscr = acmod_score(acmod, &frame_idx); acmod_advance(acmod); best_score = acmod_best_score(acmod, &best_senid); E_INFO("Frame %d best senone %d score %d\n", frame_idx, best_senid, best_score); TEST_EQUAL(frame_counter, frame_idx); if (frame_counter < 190) TEST_EQUAL_LOG(best_score, bestsen1[frame_counter]); ++frame_counter; frame_idx = -1; } } /* Note that we have to process the whole thing again because * !#@$@ s2mfc2feat modifies its argument (not for long) */ fe_start_utt(acmod->fe); nsamps = ftell(rawfh) / sizeof(*buf); bptr = buf; nfr = frame_counter; fe_process_frames(acmod->fe, &bptr, &nsamps, cepbuf, &nfr); fe_end_utt(acmod->fe, cepbuf[frame_counter-1], &nfr); E_INFO("Whole utterance (MFCC):\n"); cmn_prior_set(acmod->fcb->cmn_struct, prior); TEST_EQUAL(0, acmod_start_utt(acmod)); cptr = cepbuf; nfr = frame_counter; acmod_process_cep(acmod, &cptr, &nfr, TRUE); TEST_EQUAL(0, acmod_end_utt(acmod)); { int16 const *senscr; int16 best_score; int frame_idx = -1, best_senid; frame_counter = 0; while (acmod->n_feat_frame > 0) { senscr = acmod_score(acmod, &frame_idx); acmod_advance(acmod); best_score = acmod_best_score(acmod, &best_senid); E_INFO("Frame %d best senone %d score %d\n", frame_idx, best_senid, best_score); if (frame_counter < 190) TEST_EQUAL_LOG(best_score, bestsen1[frame_counter]); TEST_EQUAL(frame_counter, frame_idx); ++frame_counter; frame_idx = -1; } } E_INFO("Rewound (MFCC):\n"); TEST_EQUAL(0, acmod_rewind(acmod)); { int16 const *senscr; int16 best_score; int frame_idx = -1, best_senid; frame_counter = 0; while (acmod->n_feat_frame > 0) { senscr = acmod_score(acmod, &frame_idx); acmod_advance(acmod); best_score = acmod_best_score(acmod, &best_senid); E_INFO("Frame %d best senone %d score %d\n", frame_idx, best_senid, best_score); if (frame_counter < 190) TEST_EQUAL_LOG(best_score, bestsen1[frame_counter]); TEST_EQUAL(frame_counter, frame_idx); ++frame_counter; frame_idx = -1; } } /* Clean up, go home. */ ckd_free_2d(cepbuf); fclose(rawfh); ckd_free(buf); acmod_free(acmod); logmath_free(lmath); cmd_ln_free_r(config); return 0; }
int main(int argc, char *argv[]) { ps_decoder_t *ps; cmd_ln_t *config; acmod_t *acmod; ngram_search_t *ngs; clock_t c; TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k", "-lm", MODELDIR "/lm/en_US/wsj0vp.5000.DMP", "-dict", MODELDIR "/lm/en_US/cmu07a.dic", "-fwdtree", "no", "-fwdflat", "yes", "-bestpath", "no", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); ngs = (ngram_search_t *)ps->search; acmod = ps->acmod; setbuf(stdout, NULL); c = clock(); { FILE *rawfh; int16 buf[2048]; size_t nread; int16 const *bptr; int nfr; TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); ngram_fwdflat_start(ngs); while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), 2048, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { while (acmod->n_feat_frame > 0) { ngram_fwdflat_search(ngs,acmod->output_frame); acmod_advance(acmod); } } } ngram_fwdflat_finish(ngs); printf("%s\n", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL))); TEST_ASSERT(acmod_end_utt(acmod) >= 0); fclose(rawfh); } TEST_EQUAL(0, strcmp("go forward and users", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL)))); c = clock() - c; printf("2 * fwdflat search in %.2f sec\n", (double)c / CLOCKS_PER_SEC); ps_free(ps); return 0; }
int ps_start_utt(ps_decoder_t *ps) { int rv; char uttid[16]; if (ps->search == NULL) { E_ERROR("No search module is selected, did you forget to " "specify a language model or grammar?\n"); return -1; } ptmr_reset(&ps->perf); ptmr_start(&ps->perf); sprintf(uttid, "%09u", ps->uttno); ++ps->uttno; /* Remove any residual word lattice and hypothesis. */ ps_lattice_free(ps->search->dag); ps->search->dag = NULL; ps->search->last_link = NULL; ps->search->post = 0; ckd_free(ps->search->hyp_str); ps->search->hyp_str = NULL; if ((rv = acmod_start_utt(ps->acmod)) < 0) return rv; /* Start logging features and audio if requested. */ if (ps->mfclogdir) { char *logfn = string_join(ps->mfclogdir, "/", uttid, ".mfc", NULL); FILE *mfcfh; E_INFO("Writing MFCC log file: %s\n", logfn); if ((mfcfh = fopen(logfn, "wb")) == NULL) { E_ERROR_SYSTEM("Failed to open MFCC log file %s", logfn); ckd_free(logfn); return -1; } ckd_free(logfn); acmod_set_mfcfh(ps->acmod, mfcfh); } if (ps->rawlogdir) { char *logfn = string_join(ps->rawlogdir, "/", uttid, ".raw", NULL); FILE *rawfh; E_INFO("Writing raw audio log file: %s\n", logfn); if ((rawfh = fopen(logfn, "wb")) == NULL) { E_ERROR_SYSTEM("Failed to open raw audio log file %s", logfn); ckd_free(logfn); return -1; } ckd_free(logfn); acmod_set_rawfh(ps->acmod, rawfh); } if (ps->senlogdir) { char *logfn = string_join(ps->senlogdir, "/", uttid, ".sen", NULL); FILE *senfh; E_INFO("Writing senone score log file: %s\n", logfn); if ((senfh = fopen(logfn, "wb")) == NULL) { E_ERROR_SYSTEM("Failed to open senone score log file %s", logfn); ckd_free(logfn); return -1; } ckd_free(logfn); acmod_set_senfh(ps->acmod, senfh); } /* Start auxiliary phone loop search. */ if (ps->phone_loop) ps_search_start(ps->phone_loop); return ps_search_start(ps->search); }
int test_decode(ps_decoder_t *ps) { FILE *rawfh; int16 buf[2048]; size_t nread; int16 const *bptr; int nfr; ps_lattice_t *dag; acmod_t *acmod; ngram_search_t *ngs; int i, j; ps_latlink_t *link; ps_latnode_t *node; latlink_list_t *x; int32 norm, post; ngs = (ngram_search_t *)ps->search; acmod = ps->acmod; /* Decode stuff and build a DAG. */ TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); ngram_fwdtree_start(ngs); while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), 2048, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { while (acmod->n_feat_frame > 0) { ngram_fwdtree_search(ngs, acmod->output_frame); acmod_advance(acmod); } } } ngram_fwdtree_finish(ngs); printf("FWDTREE: %s\n", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL, NULL))); TEST_ASSERT(acmod_end_utt(acmod) >= 0); fclose(rawfh); dag = ngram_search_lattice(ps->search); if (dag == NULL) { E_ERROR("Failed to build DAG!\n"); return -1; } /* Write lattice to disk. */ TEST_EQUAL(0, ps_lattice_write(dag, "test_posterior.lat")); /* Do a bunch of checks on the DAG generation and traversal code: */ /* Verify that forward and backward iteration give the same number of edges. */ i = j = 0; for (link = ps_lattice_traverse_edges(dag, NULL, NULL); link; link = ps_lattice_traverse_next(dag, NULL)) { ++i; } for (link = ps_lattice_reverse_edges(dag, NULL, NULL); link; link = ps_lattice_reverse_next(dag, NULL)) { ++j; } printf("%d forward edges, %d reverse edges\n", i, j); TEST_EQUAL(i,j); /* Verify that the same links are reachable via entries and exits. */ for (node = dag->nodes; node; node = node->next) { for (x = node->exits; x; x = x->next) x->link->alpha = -42; } for (node = dag->nodes; node; node = node->next) { for (x = node->entries; x; x = x->next) TEST_EQUAL(x->link->alpha, -42); } /* Verify that forward iteration is properly ordered. */ for (link = ps_lattice_traverse_edges(dag, NULL, NULL); link; link = ps_lattice_traverse_next(dag, NULL)) { link->alpha = 0; for (x = link->from->entries; x; x = x->next) { TEST_EQUAL(x->link->alpha, 0); } } /* Verify that backward iteration is properly ordered. */ for (node = dag->nodes; node; node = node->next) { for (x = node->exits; x; x = x->next) x->link->alpha = -42; } for (link = ps_lattice_reverse_edges(dag, NULL, NULL); link; link = ps_lattice_reverse_next(dag, NULL)) { link->alpha = 0; for (x = link->to->exits; x; x = x->next) { TEST_EQUAL(x->link->alpha, 0); } } /* Find and print best path. */ link = ps_lattice_bestpath(dag, ngs->lmset, 1.0, 1.0/20.0); printf("BESTPATH: %s\n", ps_lattice_hyp(dag, link)); /* Calculate betas. */ post = ps_lattice_posterior(dag, ngs->lmset, 1.0/20.0); printf("Best path score: %d\n", link->path_scr + dag->final_node_ascr); printf("P(S|O) = %d\n", post); /* Verify that sum of final alphas and initial alphas+betas is * sufficiently similar. */ norm = logmath_get_zero(acmod->lmath); for (x = dag->start->exits; x; x = x->next) norm = logmath_add(acmod->lmath, norm, x->link->beta + x->link->alpha); E_INFO("Sum of final alphas+betas = %d\n", dag->norm); E_INFO("Sum of initial alphas+betas = %d\n", norm); TEST_EQUAL_LOG(dag->norm, norm); /* Print posterior probabilities for each link in best path. */ while (link) { printf("P(%s,%d) = %d = %f\n", dict_wordstr(ps->search->dict, link->from->wid), link->ef, link->alpha + link->beta - dag->norm, logmath_exp(acmod->lmath, link->alpha + link->beta - dag->norm)); link = link->best_prev; } return 0; }
int main(int argc, char *argv[]) { ps_decoder_t *ps; cmd_ln_t *config; acmod_t *acmod; ngram_search_t *ngs; ps_lattice_t *dag; clock_t c; int i; TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/en-us/en-us", "-lm", MODELDIR "/en-us/en-us.lm.bin", "-dict", MODELDIR "/en-us/cmudict-en-us.dict", "-fwdtree", "yes", "-fwdflat", "no", "-bestpath", "yes", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); ngs = (ngram_search_t *)ps->search; acmod = ps->acmod; acmod_set_grow(ps->acmod, TRUE); setbuf(stdout, NULL); c = clock(); for (i = 0; i < 5; ++i) { FILE *rawfh; int16 buf[2048]; size_t nread; int16 const *bptr; int nfr; ps_astar_t *nbest; ps_latpath_t *path; char *besthyp; const char *astar_besthyp = NULL; int32 astar_hyp_score; int i; /* PocketSphinx API would do this for us but we have to do it manually here. */ ps_lattice_free(ps->search->dag); ps->search->dag = NULL; TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); ngram_fwdtree_start(ngs); while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), 2048, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { while (acmod->n_feat_frame > 0) { ngram_fwdtree_search(ngs, acmod->output_frame); acmod_advance(acmod); } } } ngram_fwdtree_finish(ngs); printf("FWDTREE: %s\n", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL))); TEST_ASSERT(acmod_end_utt(acmod) >= 0); fclose(rawfh); dag = ngram_search_lattice(ps->search); if (dag == NULL) { E_ERROR("Failed to build DAG!\n"); return 1; } besthyp = ckd_salloc (ps_lattice_hyp(dag, ps_lattice_bestpath (dag, ngs->lmset, 9.5/6.5, 1.0))); printf("BESTPATH: %s\n", besthyp); TEST_ASSERT(nbest = ps_astar_start(dag, ngs->lmset, 9.5/6.5, 0, -1, -1, -1)); i = 0; astar_hyp_score = WORST_SCORE; while ((path = ps_astar_next(nbest))) { if (i < 10) printf("NBEST %d: %s (%d)\n", i, ps_astar_hyp(nbest, path), path->score); if (path->score > astar_hyp_score) { astar_hyp_score = path->score; astar_besthyp = ps_astar_hyp(nbest, path); } i++; } TEST_EQUAL(0, strcmp(besthyp, astar_besthyp)); ps_astar_finish(nbest); ckd_free(besthyp); } printf("%s\n", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL))); TEST_EQUAL(0, strcmp("go forward ten meters", ngram_search_bp_hyp(ngs, ngram_search_find_exit(ngs, -1, NULL)))); c = clock() - c; printf("5 * fwdtree + bestpath + N-best search in %.2f sec\n", (double)c / CLOCKS_PER_SEC); ps_free(ps); cmd_ln_free_r(config); return 0; }
int main(int argc, char *argv[]) { ps_decoder_t *ps; cmd_ln_t *config; acmod_t *acmod; fsg_search_t *fsgs; jsgf_t *jsgf; jsgf_rule_t *rule; fsg_model_t *fsg; ps_seg_t *seg; ps_lattice_t *dag; FILE *rawfh; char const *hyp, *uttid; int32 score, prob; clock_t c; int i; TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k", "-dict", MODELDIR "/lm/en/turtle.dic", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); jsgf = jsgf_parse_file(DATADIR "/goforward.gram", NULL); TEST_ASSERT(jsgf); rule = jsgf_get_rule(jsgf, "<goforward.move2>"); TEST_ASSERT(rule); fsg = jsgf_build_fsg(jsgf, rule, ps->lmath, 7.5); TEST_ASSERT(fsg); fsg_model_write(fsg, stdout); ps_set_fsg(ps, "<goforward.move2>", fsg); ps_set_search(ps, "<goforward.move2>"); acmod = ps->acmod; fsgs = (fsg_search_t *) fsg_search_init(fsg, config, acmod, ps->dict, ps->d2p); setbuf(stdout, NULL); c = clock(); for (i = 0; i < 5; ++i) { int16 buf[2048]; size_t nread; int16 const *bptr; int nfr; int is_final; TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); TEST_EQUAL(0, acmod_start_utt(acmod)); fsg_search_start(ps_search_base(fsgs)); is_final = FALSE; while (!feof(rawfh)) { nread = fread(buf, sizeof(*buf), 2048, rawfh); bptr = buf; while ((nfr = acmod_process_raw(acmod, &bptr, &nread, FALSE)) > 0) { while (acmod->n_feat_frame > 0) { fsg_search_step(ps_search_base(fsgs), acmod->output_frame); acmod_advance(acmod); } } hyp = fsg_search_hyp(ps_search_base(fsgs), &score, &is_final); printf("FSG: %s (%d) frame %d final %s\n", hyp, score, acmod->output_frame, is_final ? "FINAL" : ""); TEST_EQUAL (is_final, (acmod->output_frame > 170)); } fsg_search_finish(ps_search_base(fsgs)); hyp = fsg_search_hyp(ps_search_base(fsgs), &score, NULL); printf("FSG: %s (%d)\n", hyp, score); TEST_ASSERT(acmod_end_utt(acmod) >= 0); fclose(rawfh); } TEST_EQUAL(0, strcmp("go forward ten meters", fsg_search_hyp(ps_search_base(fsgs), &score, NULL))); ps->search = (ps_search_t *)fsgs; for (seg = ps_seg_iter(ps, &score); seg; seg = ps_seg_next(seg)) { char const *word; int sf, ef; word = ps_seg_word(seg); ps_seg_frames(seg, &sf, &ef); printf("%s %d %d\n", word, sf, ef); } c = clock() - c; printf("5 * fsg search in %.2f sec\n", (double)c / CLOCKS_PER_SEC); dag = ps_get_lattice(ps); ps_lattice_write(dag, "test_jsgf.lat"); jsgf_grammar_free(jsgf); fsg_search_free(ps_search_base(fsgs)); ps_free(ps); cmd_ln_free_r(config); TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k", "-dict", MODELDIR "/lm/en/turtle.dic", "-jsgf", DATADIR "/goforward.gram", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); ps_decode_raw(ps, rawfh, "goforward", -1); hyp = ps_get_hyp(ps, &score, &uttid); prob = ps_get_prob(ps, &uttid); printf("%s: %s (%d, %d)\n", uttid, hyp, score, prob); TEST_EQUAL(0, strcmp("go forward ten meters", hyp)); ps_free(ps); fclose(rawfh); cmd_ln_free_r(config); TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k", "-dict", MODELDIR "/lm/en/turtle.dic", "-jsgf", DATADIR "/goforward.gram", "-toprule", "goforward.move2", "-input_endian", "little", "-samprate", "16000", NULL)); TEST_ASSERT(ps = ps_init(config)); TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.raw", "rb")); ps_decode_raw(ps, rawfh, "goforward", -1); hyp = ps_get_hyp(ps, &score, &uttid); prob = ps_get_prob(ps, &uttid); printf("%s: %s (%d, %d)\n", uttid, hyp, score, prob); TEST_EQUAL(0, strcmp("go forward ten meters", hyp)); ps_free(ps); cmd_ln_free_r(config); fclose(rawfh); TEST_ASSERT(config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", MODELDIR "/hmm/en_US/hub4wsj_sc_8k", "-dict", MODELDIR "/lm/en/turtle.dic", "-jsgf", DATADIR "/defective.gram", NULL)); TEST_ASSERT(NULL == ps_init(config)); cmd_ln_free_r(config); return 0; }