ReturnType Recognizer::addGrammar(Integers& id, const Grammar& grammar) { if (decoder == NULL) return BAD_STATE; std::ostringstream grammar_name; grammar_name << grammar_index; grammar_names.push_back(grammar_name.str()); current_grammar = fsg_model_init(grammar_names.back().c_str(), logmath, 1.0, grammar.numStates); if (current_grammar == NULL) return RUNTIME_ERROR; current_grammar->start_state = grammar.start; current_grammar->final_state = grammar.end; for (int i=0;i<grammar.transitions.size();i++) { if (grammar.transitions.at(i).word.size() == 0) fsg_model_null_trans_add(current_grammar, grammar.transitions.at(i).from, grammar.transitions.at(i).to, grammar.transitions.at(i).logp); else fsg_model_trans_add(current_grammar, grammar.transitions.at(i).from, grammar.transitions.at(i).to, grammar.transitions.at(i).logp, fsg_model_word_add(current_grammar, grammar.transitions.at(i).word.c_str())); } fsg_model_add_silence(current_grammar, "<sil>", -1, 1.0); if (current_grammar != fsg_set_add(grammar_set, grammar_names.back().c_str(), current_grammar)) { return RUNTIME_ERROR; } if (id.size() == 0) id.push_back(grammar_index); else id.at(0) = grammar_index; grammar_index++; ps_update_fsgset(decoder); fsg_model_t * fsg = fsg_set_select(grammar_set, grammar_names.back().c_str()); if (fsg == NULL) return RUNTIME_ERROR; if (ps_update_fsgset(decoder) == NULL) return RUNTIME_ERROR; return SUCCESS; }
void gst_sphinx_sink_set_grammar (GstSphinxSink *sink, GSList *phrases) { fsg_set_t *fsgs; if (sink->ad.listening) return; sink->fsg = gst_sphinx_construct_fsg (sink, phrases); fsgs = ps_get_fsgset(sink->decoder); fsg_set_remove_byname(fsgs, "desktop-control"); fsg_set_add(fsgs, fsg_model_name(sink->fsg), sink->fsg); if (!fsg_set_select(fsgs, fsg_model_name(sink->fsg))) { #if DEBUG g_message("Failed to configure grammar"); #endif exit(1); } ps_update_fsgset (sink->decoder); #if DEBUG g_message ("New fsg is set"); #endif }
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)); fsgs = (fsg_search_t *)fsg_search_init(config, ps->acmod, ps->dict, ps->d2p); acmod = ps->acmod; 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); TEST_ASSERT(fsg_set_add(fsgs, "<goforward.move2>", fsg)); TEST_ASSERT(fsg_set_select(fsgs, "<goforward.move2>")); fsg_search_reinit(ps_search_base(fsgs), 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; TEST_ASSERT(rawfh = fopen(DATADIR "/goforward.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); 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))); 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"); ps_free(ps); jsgf_grammar_free(jsgf); fsg_search_free(ps_search_base(fsgs)); 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); 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); return 0; }