static fsg_model_t * get_fsg(jsgf_t *grammar, const char *name) { logmath_t *lmath; fsg_model_t *fsg; jsgf_rule_t *rule; /* Take the -toprule if specified. */ if (name) { rule = jsgf_get_rule(grammar, name); if (rule == NULL) { E_ERROR("Start rule %s not found\n", name); return NULL; } } else { rule = jsgf_get_public_rule(grammar); if (rule == NULL) { E_ERROR("No public rules found in grammar %s\n", jsgf_grammar_name(grammar)); return NULL; } else { E_INFO("No -toprule was given; grabbing the first public rule: " "'%s' of the grammar '%s'.\n", jsgf_rule_name(rule), jsgf_grammar_name(grammar)); } } lmath = logmath_init(1.0001, 0, 0); fsg = jsgf_build_fsg_raw(grammar, rule, lmath, 1.0); return fsg; }
int main(int argc, char *argv[]) { logmath_t *lmath; ngram_model_t *model; /* Initialize a logmath object to pass to ngram_read */ lmath = logmath_init(1.0001, 0, 0); /* Read a language model */ model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_BIN, lmath); TEST_ASSERT(model); ngram_model_casefold(model, NGRAM_UPPER); TEST_EQUAL(0, strcmp("</s>", ngram_word(model, 5))); TEST_EQUAL(0, strcmp("BE", ngram_word(model, 42))); TEST_EQUAL(0, strcmp("FLOORED", ngram_word(model, 130))); TEST_EQUAL(0, strcmp("ZERO", ngram_word(model, 398))); TEST_EQUAL(0, strcmp("~", ngram_word(model, 399))); ngram_model_casefold(model, NGRAM_LOWER); TEST_EQUAL(0, strcmp("</s>", ngram_word(model, 5))); TEST_EQUAL(0, strcmp("be", ngram_word(model, 42))); TEST_EQUAL(0, strcmp("floored", ngram_word(model, 130))); TEST_EQUAL(0, strcmp("zero", ngram_word(model, 398))); TEST_EQUAL(0, strcmp("~", ngram_word(model, 399))); ngram_model_free(model); logmath_free(lmath); return 0; }
int main(int argc, char *argv[]) { logmath_t *lmath; cmd_ln_t *config; acmod_t *acmod; ps_mgau_t *ps; ptm_mgau_t *s; int i, lastcb; lmath = logmath_init(1.0001, 0, 0); config = cmd_ln_init(NULL, ps_args(), TRUE, "-compallsen", "yes", "-input_endian", "little", NULL); cmd_ln_parse_file_r(config, ps_args(), MODELDIR "/en-us/en-us/feat.params", FALSE); cmd_ln_set_str_extra_r(config, "_mdef", MODELDIR "/en-us/en-us/mdef"); cmd_ln_set_str_extra_r(config, "_mean", MODELDIR "/en-us/en-us/means"); cmd_ln_set_str_extra_r(config, "_var", MODELDIR "/en-us/en-us/variances"); cmd_ln_set_str_extra_r(config, "_tmat", MODELDIR "/en-us/en-us/transition_matrices"); cmd_ln_set_str_extra_r(config, "_sendump", MODELDIR "/en-us/en-us/sendump"); cmd_ln_set_str_extra_r(config, "_mixw", NULL); cmd_ln_set_str_extra_r(config, "_lda", NULL); cmd_ln_set_str_extra_r(config, "_senmgau", NULL); err_set_debug_level(3); TEST_ASSERT(config); TEST_ASSERT((acmod = acmod_init(config, lmath, NULL, NULL))); TEST_ASSERT((ps = acmod->mgau)); TEST_EQUAL(0, strcmp(ps->vt->name, "ptm")); s = (ptm_mgau_t *)ps; E_DEBUG(2,("PTM model loaded: %d codebooks, %d senones, %d frames of history\n", s->g->n_mgau, s->n_sen, s->n_fast_hist)); E_DEBUG(2,("Senone to codebook mappings:\n")); lastcb = s->sen2cb[0]; E_DEBUG(2,("\t%d: 0", lastcb)); for (i = 0; i < s->n_sen; ++i) { if (s->sen2cb[i] != lastcb) { lastcb = s->sen2cb[i]; E_DEBUGCONT(2,("-%d\n", i-1)); E_DEBUGCONT(2,("\t%d: %d", lastcb, i)); } } E_INFOCONT("-%d\n", i-1); run_acmod_test(acmod); #if 0 /* Replace it with ms_mgau. */ ptm_mgau_free(ps); cmd_ln_set_str_r(config, "-mixw", MODELDIR "/en-us/en-us/mixture_weights"); TEST_ASSERT((acmod->mgau = ms_mgau_init(acmod, lmath, acmod->mdef))); run_acmod_test(acmod); cmd_ln_free_r(config); #endif return 0; }
int main(int argc, char *argv[]) { logmath_t *lmath; fsg_model_t *fsg; /* Initialize a logmath object to pass to fsg_model_read */ lmath = logmath_init(1.0001, 0, 0); /* Read a FSG. */ fsg = fsg_model_readfile(LMDIR "/goforward.fsg", lmath, 7.5); TEST_ASSERT(fsg); TEST_ASSERT(fsg_model_add_silence(fsg, "<sil>", -1, 0.3)); TEST_ASSERT(fsg_model_add_silence(fsg, "++NOISE++", -1, 0.3)); TEST_ASSERT(fsg_model_add_alt(fsg, "FORWARD", "FORWARD(2)")); fsg_model_write(fsg, stdout); /* Test reference counting. */ TEST_ASSERT(fsg = fsg_model_retain(fsg)); TEST_EQUAL(1, fsg_model_free(fsg)); fsg_model_write(fsg, stdout); TEST_EQUAL(0, fsg_model_free(fsg)); logmath_free(lmath); return 0; }
int main(int argc, char *argv[]) { cmd_ln_t *config; logmath_t *lmath; acmod_t *acmod[5]; sbthread_t *thr[5]; featbuf_t *fb; FILE *raw; int16 buf[2048]; int nsamp; int i; config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", TESTDATADIR "/hub4wsj_sc_8k", "-lm", TESTDATADIR "/bn10000.3g.arpa", "-dict", TESTDATADIR "/bn10000.dic", "-compallsen", "yes", NULL); ps_init_defaults(config); fb = featbuf_init(config); TEST_ASSERT(fb); lmath = logmath_init(cmd_ln_float32_r(config, "-logbase"), 0, FALSE); acmod[0] = acmod_init(config, lmath, fb); TEST_ASSERT(acmod[0]); /* Create a couple threads to pull features out of it. */ for (i = 0; i < 5; ++i) { if (i != 0) acmod[i] = acmod_copy(acmod[0]); thr[i] = sbthread_start(NULL, consumer, acmod[i]); } /* Feed them some data. */ raw = fopen(TESTDATADIR "/chan3.raw", "rb"); featbuf_producer_start_utt(fb, "chan3"); while ((nsamp = fread(buf, 2, 2048, raw)) > 0) { int rv; rv = featbuf_producer_process_raw(fb, buf, nsamp, FALSE); printf("Producer processed %d samples\n", nsamp); TEST_ASSERT(rv > 0); } fclose(raw); printf("Waiting for consumers\n"); featbuf_producer_end_utt(fb); printf("Finished waiting\n"); /* Reap those threads. */ for (i = 0; i < 5; ++i) { sbthread_wait(thr[i]); sbthread_free(thr[i]); acmod_free(acmod[i]); printf("Reaped consumer %p\n", acmod[i]); } featbuf_free(fb); logmath_free(lmath); cmd_ln_free_r(config); return 0; }
int main(int argc, char *argv[]) { logmath_t *lmath; ngram_model_t *model; /* Initialize a logmath object to pass to ngram_read */ lmath = logmath_init(1.0001, 0, 0); /* Read a language model */ model = ngram_model_read(NULL, LMDIR "/turtle.ug.lm", NGRAM_ARPA, lmath); test_lm_ug_vals(model); TEST_EQUAL(0, ngram_model_free(model)); /* Read a language model */ model = ngram_model_read(NULL, LMDIR "/turtle.ug.lm.dmp", NGRAM_BIN, lmath); test_lm_ug_vals(model); TEST_EQUAL(0, ngram_model_free(model)); /* Read a language model with missing backoffs */ model = ngram_model_read(NULL, LMDIR "/104.lm.gz", NGRAM_ARPA, lmath); TEST_EQUAL(0, ngram_model_free(model)); /* Read corrupted language model, error expected */ model = ngram_model_read(NULL, LMDIR "/105.lm.gz", NGRAM_ARPA, lmath); TEST_EQUAL(NULL, model); /* Read corrupted language model, error expected */ model = ngram_model_read(NULL, LMDIR "/106.lm.gz", NGRAM_ARPA, lmath); TEST_EQUAL(NULL, model); /* Read a language model */ model = ngram_model_read(NULL, LMDIR "/100.lm.bz2", NGRAM_ARPA, lmath); test_lm_vals(model); TEST_EQUAL(0, ngram_model_free(model)); /* Read a language model */ model = ngram_model_read(NULL, LMDIR "/100.lm.bin", NGRAM_BIN, lmath); test_lm_vals(model); TEST_EQUAL(0, ngram_model_free(model)); /* Read a language model */ model = ngram_model_read(NULL, LMDIR "/100.lm.dmp", NGRAM_BIN, lmath); test_lm_vals(model); /* Test refcounting. */ model = ngram_model_retain(model); TEST_EQUAL(1, ngram_model_free(model)); TEST_EQUAL(ngram_score(model, "daines", "huggins", "david", NULL), -9452); TEST_EQUAL(0, ngram_model_free(model)); logmath_free(lmath); return 0; }
int main(int argc, char *argv[]) { ngram_trie_t *t; dict_t *dict; bin_mdef_t *mdef; logmath_t *lmath; cmd_ln_t *config; FILE *arpafh; config = cmd_ln_init(NULL, ps_args(), TRUE, "-hmm", TESTDATADIR "/hub4wsj_sc_8k", "-dict", TESTDATADIR "/bn10000.homos.dic", NULL); ps_init_defaults(config); lmath = logmath_init(cmd_ln_float32_r(config, "-logbase"), 0, FALSE); mdef = bin_mdef_read(config, cmd_ln_str_r(config, "-mdef")); dict = dict_init(config, mdef); t = ngram_trie_init(dict, lmath); arpafh = fopen(TESTDATADIR "/bn10000.3g.arpa", "r"); ngram_trie_read_arpa(t, arpafh); fclose(arpafh); /* Test 1, 2, 3-gram probs without backoff. */ test_lookups(t, lmath); arpafh = fopen("tmp.bn10000.3g.arpa", "w"); ngram_trie_write_arpa(t, arpafh); fclose(arpafh); ngram_trie_free(t); t = ngram_trie_init(dict, lmath); arpafh = fopen("tmp.bn10000.3g.arpa", "r"); ngram_trie_read_arpa(t, arpafh); fclose(arpafh); /* Test 1, 2, 3-gram probs without backoff. */ test_lookups(t, lmath); /* Test adding nodes. */ test_add_nodes(t, lmath); ngram_trie_free(t); dict_free(dict); logmath_free(lmath); bin_mdef_free(mdef); cmd_ln_free_r(config); return 0; }
int main(int argc, char *argv[]) { logmath_t *lmath; fsg_model_t *fsg; jsgf_t *jsgf; jsgf_rule_t *rule; lmath = logmath_init(1.0001, 0, 0); /* Test loading */ jsgf = jsgf_parse_file(LMDIR "/polite.gram", NULL); TEST_ASSERT(jsgf); rule = jsgf_get_rule(jsgf, "polite.startPolite"); TEST_ASSERT(rule); fsg = jsgf_build_fsg(jsgf, rule, lmath, 7.5); TEST_ASSERT(fsg); TEST_EQUAL_STRING("polite", jsgf_grammar_name(jsgf)); TEST_ASSERT(fsg_model_add_silence(fsg, "<sil>", -1, 0.3)); TEST_ASSERT(fsg_model_add_silence(fsg, "++NOISE++", -1, 0.3)); TEST_ASSERT(fsg_model_add_alt(fsg, "please", "please(2)")); jsgf_grammar_free(jsgf); fsg_model_write(fsg, stdout); fsg_model_free(fsg); /* Or do everything at once */ fsg = jsgf_read_file(LMDIR "/public.gram", lmath, 1.0); fsg_model_free(fsg); /* Test grammar with keywords inside */ jsgf = jsgf_parse_file(LMDIR "/public.gram", NULL); TEST_ASSERT(jsgf); jsgf_grammar_free(jsgf); jsgf = jsgf_parse_string("#JSGF V1.0; grammar test; public <choice> = yes | no;", NULL); TEST_ASSERT(jsgf); rule = jsgf_get_rule(jsgf, "test.choice"); TEST_ASSERT(rule); fsg = jsgf_build_fsg(jsgf, rule, lmath, 7.5); fsg_model_write(fsg, stdout); fsg_model_free(fsg); jsgf_grammar_free(jsgf); logmath_free(lmath); return 0; }
int jsgf_write_fsg(jsgf_t *grammar, jsgf_rule_t *rule, FILE *outfh) { fsg_model_t *fsg; logmath_t *lmath = logmath_init(1.0001, 0, 0); if ((fsg = jsgf_build_fsg_raw(grammar, rule, lmath, 1.0)) == NULL) goto error_out; fsg_model_write(fsg, outfh); logmath_free(lmath); return 0; error_out: logmath_free(lmath); return -1; }
int main(int argc, char *argv[]) { cmd_ln_t *config; ngram_model_t *lm = NULL; logmath_t *lmath; const char *lmfn, *probdefn, *lsnfn, *text; if ((config = cmd_ln_parse_r(NULL, defn, argc, argv, TRUE)) == NULL) return 1; verbose = cmd_ln_boolean_r(config, "-verbose"); /* Create log math object. */ if ((lmath = logmath_init (cmd_ln_float64_r(config, "-logbase"), 0, 0)) == NULL) { E_FATAL("Failed to initialize log math\n"); } /* Load the language model. */ lmfn = cmd_ln_str_r(config, "-lm"); if (lmfn == NULL || (lm = ngram_model_read(config, lmfn, NGRAM_AUTO, lmath)) == NULL) { E_FATAL("Failed to load language model from %s\n", cmd_ln_str_r(config, "-lm")); } if ((probdefn = cmd_ln_str_r(config, "-probdef")) != NULL) ngram_model_read_classdef(lm, probdefn); ngram_model_apply_weights(lm, cmd_ln_float32_r(config, "-lw"), cmd_ln_float32_r(config, "-wip"), cmd_ln_float32_r(config, "-uw")); /* Now evaluate some text. */ lsnfn = cmd_ln_str_r(config, "-lsn"); text = cmd_ln_str_r(config, "-text"); if (lsnfn) { evaluate_file(lm, lmath, lsnfn); } else if (text) { evaluate_string(lm, lmath, text); } return 0; }
int main(int argc, char *argv[]) { logmath_t *lmath; ngram_model_t *model; lmath = logmath_init(1.0001, 0, 0); model = ngram_model_read(NULL, LMDIR "/100.arpa.DMP", NGRAM_DMP, lmath); run_tests(model); ngram_model_free(model); model = ngram_model_read(NULL, LMDIR "/100.arpa.gz", NGRAM_ARPA, lmath); run_tests(model); ngram_model_free(model); logmath_free(lmath); return 0; }
fsg_model_t* ofxSphinxASR::get_fsg(jsgf_t *grammar, const char *name) { jsgf_rule_iter_t *itor; logmath_t *lmath = logmath_init(1.0001, 0, 0); fsg_model_t *fsg = NULL; for (itor = jsgf_rule_iter(grammar); itor; itor = jsgf_rule_iter_next(itor)) { jsgf_rule_t *rule = jsgf_rule_iter_rule(itor); char const *rule_name = jsgf_rule_name(rule); if ((name == NULL && jsgf_rule_public(rule)) || (name && strlen(rule_name)-2 == strlen(name) && 0 == strncmp(rule_name + 1, name, strlen(rule_name) - 2))) { fsg = jsgf_build_fsg_raw(grammar, rule, logmath_retain(lmath), 1.0); jsgf_rule_iter_free(itor); break; } } logmath_free(lmath); return fsg; }
int main() { logmath_t *logmath; ngram_model_t *model; FILE *fp; char *line = NULL, *grapheme, *phoneme, *predicted_phoneme; int32 different_word_count = 0, fit_count = 0; size_t len = 0; err_set_logfp(NULL); logmath = logmath_init(1.0001f, 0, 0); model = ngram_model_read(NULL, "cmudict-en-us.dmp", NGRAM_AUTO, logmath); fp = fopen("cmudict-en-us.dict", "r"); while (getline(&line, &len, fp) != -1) { grapheme = strtok(line, " "); phoneme = strtok(NULL, "\n"); if (strstr(grapheme, "(")) { grapheme = strtok(grapheme, "("); } else { different_word_count++; } predicted_phoneme = g2p(model, grapheme, 100); if (predicted_phoneme && strcmp(phoneme, predicted_phoneme) == 0) { fit_count++; } ckd_free(predicted_phoneme); } printf("%d %d %f\n", fit_count, different_word_count, fit_count * 1.0 / different_word_count); ckd_free(grapheme); fclose(fp); ngram_model_free(model); logmath_free(logmath); }
int main(int argc, char *argv[]) { dict_t *dict; dict2pid_t *d2p; bin_mdef_t *mdef; glextree_t *tree; hmm_context_t *ctx; logmath_t *lmath; tmat_t *tmat; int i; TEST_ASSERT(mdef = bin_mdef_read(NULL, MODELDIR "/hmm/en_US/hub4wsj_sc_8k/mdef")); TEST_ASSERT(dict = dict_init(cmd_ln_init(NULL, NULL, FALSE, "-dict", DATADIR "/turtle.dic", "-dictcase", "no", NULL), mdef)); TEST_ASSERT(d2p = dict2pid_build(mdef, dict)); lmath = logmath_init(1.0001, 0, TRUE); tmat = tmat_init(MODELDIR "/hmm/en_US/hub4wsj_sc_8k/transition_matrices", lmath, 1e-5, TRUE); ctx = hmm_context_init(bin_mdef_n_emit_state(mdef), tmat->tp, NULL, mdef->sseq); TEST_ASSERT(tree = glextree_build(ctx, dict, d2p, NULL, NULL)); /* Check that a path exists for all dictionary words. */ for (i = 0; i < dict_size(dict); ++i) TEST_ASSERT(glextree_has_word(tree, i)); dict_free(dict); dict2pid_free(d2p); bin_mdef_free(mdef); tmat_free(tmat); hmm_context_free(ctx); glextree_free(tree); return 0; }
ReturnType Recognizer::init(const Config& config) { parseStringList(HMM_FOLDERS, &acoustic_models, &default_acoustic_model); #ifdef LM_FILES parseStringList(LM_FILES, &language_models, &default_language_model); #endif /* LM_FILES */ #ifdef DICT_FILES parseStringList(DICT_FILES, &dictionaries, &default_dictionary); #endif /* DICT_FILES */ const arg_t cont_args_def[] = { POCKETSPHINX_OPTIONS, { "-argfile", ARG_STRING, NULL, "Argument file giving extra arguments." }, { "-adcdev", ARG_STRING, NULL, "Name of audio device to use for input." }, { "-infile", ARG_STRING, NULL, "Audio file to transcribe." }, { "-time", ARG_BOOLEAN, "no", "Print word times in file transcription." }, CMDLN_EMPTY_OPTION }; std::map<std::string, std::string> parameters; for (int i=0 ; i< config.size() ; ++i) parameters[config[i].key] = config[i].value; if ((parameters.find("-hmm") == parameters.end()) || (acoustic_models.find(parameters["-hmm"]) == acoustic_models.end())) parameters["-hmm"] = default_acoustic_model; if (parameters.find("-bestpath") == parameters.end()) parameters["-bestpath"] = "no"; if (parameters.find("-remove_noise") == parameters.end()) parameters["-remove_noise"] = "no"; int argc = 2 * parameters.size(); char ** argv = new char*[argc]; int index = 0; for (StringsMapIterator i = parameters.begin() ; i != parameters.end(); ++i) { if (isValidParameter(i->first, i->second)) { if (i->first == "-lm") is_fsg = false; argv[index++] = (char*) i->first.c_str(); argv[index++] = (char*) i->second.c_str(); } } cmd_ln_t * cmd_line = cmd_ln_parse_r(NULL, cont_args_def, argc, argv, FALSE); if (cmd_line == NULL) { delete [] argv; return RUNTIME_ERROR; } decoder = ps_init(cmd_line); delete [] argv; if (decoder == NULL) { return RUNTIME_ERROR; } logmath = logmath_init(1.0001, 0, 0); if (logmath == NULL) { return RUNTIME_ERROR; } return SUCCESS; }
int main(int argc, char *argv[]) { cmd_ln_t *config; ngram_model_t *lm = NULL; logmath_t *lmath; int itype, otype; char const *kase; if ((config = cmd_ln_parse_r(NULL, defn, argc, argv, TRUE)) == NULL) return 1; if (cmd_ln_boolean_r(config, "-help")) { usagemsg(argv[0]); } err_set_debug_level(cmd_ln_int32_r(config, "-debug")); /* Create log math object. */ if ((lmath = logmath_init (cmd_ln_float64_r(config, "-logbase"), 0, 0)) == NULL) { E_FATAL("Failed to initialize log math\n"); } if (cmd_ln_str_r(config, "-i") == NULL || cmd_ln_str_r(config, "-i") == NULL) { E_ERROR("Please specify both input and output models\n"); goto error_out; } /* Load the input language model. */ if (cmd_ln_str_r(config, "-ifmt")) { if ((itype = ngram_str_to_type(cmd_ln_str_r(config, "-ifmt"))) == NGRAM_INVALID) { E_ERROR("Invalid input type %s\n", cmd_ln_str_r(config, "-ifmt")); goto error_out; } lm = ngram_model_read(config, cmd_ln_str_r(config, "-i"), itype, lmath); } else { lm = ngram_model_read(config, cmd_ln_str_r(config, "-i"), NGRAM_AUTO, lmath); } /* Guess or set the output language model type. */ if (cmd_ln_str_r(config, "-ofmt")) { if ((otype = ngram_str_to_type(cmd_ln_str_r(config, "-ofmt"))) == NGRAM_INVALID) { E_ERROR("Invalid output type %s\n", cmd_ln_str_r(config, "-ofmt")); goto error_out; } } else { otype = ngram_file_name_to_type(cmd_ln_str_r(config, "-o")); } /* Recode the language model if desired. */ if (cmd_ln_str_r(config, "-ienc")) { if (ngram_model_recode(lm, cmd_ln_str_r(config, "-ienc"), cmd_ln_str_r(config, "-oenc")) != 0) { E_ERROR("Failed to recode language model from %s to %s\n", cmd_ln_str_r(config, "-ienc"), cmd_ln_str_r(config, "-oenc")); goto error_out; } } /* Case fold if requested. */ if ((kase = cmd_ln_str_r(config, "-case"))) { if (0 == strcmp(kase, "lower")) { ngram_model_casefold(lm, NGRAM_LOWER); } else if (0 == strcmp(kase, "upper")) { ngram_model_casefold(lm, NGRAM_UPPER); } else { E_ERROR("Unknown value for -case: %s\n", kase); goto error_out; } } /* Write the output language model. */ if (ngram_model_write(lm, cmd_ln_str_r(config, "-o"), otype) != 0) { E_ERROR("Failed to write language model in format %s to %s\n", ngram_type_to_str(otype), cmd_ln_str_r(config, "-o")); goto error_out; } /* That's all folks! */ ngram_model_free(lm); return 0; error_out: ngram_model_free(lm); return 1; }
int main(int argc, char *argv[]) { logmath_t *lmath; int32 rv; lmath = logmath_init(1.0001, 0, 1); TEST_ASSERT(lmath); printf("log(1e-150) = %d\n", logmath_log(lmath, 1e-150)); TEST_EQUAL_LOG(logmath_log(lmath, 1e-150), -3454050); printf("exp(log(1e-150)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-150))); TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-150)), 1e-150); printf("log(1e-48) = %d\n", logmath_log(lmath, 1e-48)); printf("exp(log(1e-48)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-48))); TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-48)), 1e-48); printf("log(42) = %d\n", logmath_log(lmath, 42)); TEST_EQUAL_LOG(logmath_log(lmath, 42), 37378); printf("exp(log(42)) = %f\n",logmath_exp(lmath, logmath_log(lmath, 42))); TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 42)), 42); printf("log(1e-3 + 5e-3) = %d l+ %d = %d\n", logmath_log(lmath, 1e-3), logmath_log(lmath, 5e-3), logmath_add(lmath, logmath_log(lmath, 1e-3), logmath_log(lmath, 5e-3))); printf("log(1e-3 + 5e-3) = %e + %e = %e\n", logmath_exp(lmath, logmath_log(lmath, 1e-3)), logmath_exp(lmath, logmath_log(lmath, 5e-3)), logmath_exp(lmath, logmath_add(lmath, logmath_log(lmath, 1e-3), logmath_log(lmath, 5e-3)))); TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48), logmath_log(lmath, 5e-48)), logmath_log(lmath, 6e-48)); TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48), logmath_log(lmath, 42)), logmath_log(lmath, 42)); rv = logmath_write(lmath, "tmp.logadd"); TEST_EQUAL(rv, 0); logmath_free(lmath); lmath = logmath_read("tmp.logadd"); TEST_ASSERT(lmath); printf("log(1e-150) = %d\n", logmath_log(lmath, 1e-150)); TEST_EQUAL_LOG(logmath_log(lmath, 1e-150), -3454050); printf("exp(log(1e-150)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-150))); TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-150)), 1e-150); printf("log(1e-48) = %d\n", logmath_log(lmath, 1e-48)); printf("exp(log(1e-48)) = %e\n",logmath_exp(lmath, logmath_log(lmath, 1e-48))); TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 1e-48)), 1e-48); printf("log(42) = %d\n", logmath_log(lmath, 42)); TEST_EQUAL_LOG(logmath_log(lmath, 42), 37378); printf("exp(log(42)) = %f\n",logmath_exp(lmath, logmath_log(lmath, 42))); TEST_EQUAL_FLOAT(logmath_exp(lmath, logmath_log(lmath, 42)), 41.99); printf("log(1e-3 + 5e-3) = %d l+ %d = %d\n", logmath_log(lmath, 1e-3), logmath_log(lmath, 5e-3), logmath_add(lmath, logmath_log(lmath, 1e-3), logmath_log(lmath, 5e-3))); printf("log(1e-3 + 5e-3) = %e + %e = %e\n", logmath_exp(lmath, logmath_log(lmath, 1e-3)), logmath_exp(lmath, logmath_log(lmath, 5e-3)), logmath_exp(lmath, logmath_add(lmath, logmath_log(lmath, 1e-3), logmath_log(lmath, 5e-3)))); TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48), logmath_log(lmath, 5e-48)), logmath_log(lmath, 6e-48)); TEST_EQUAL_LOG(logmath_add(lmath, logmath_log(lmath, 1e-48), logmath_log(lmath, 42)), logmath_log(lmath, 42)); 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 ps_reinit(ps_decoder_t *ps, cmd_ln_t *config) { const char *path; const char *keyphrase; int32 lw; if (config && config != ps->config) { cmd_ln_free_r(ps->config); ps->config = cmd_ln_retain(config); } err_set_debug_level(cmd_ln_int32_r(ps->config, "-debug")); ps->mfclogdir = cmd_ln_str_r(ps->config, "-mfclogdir"); ps->rawlogdir = cmd_ln_str_r(ps->config, "-rawlogdir"); ps->senlogdir = cmd_ln_str_r(ps->config, "-senlogdir"); /* Fill in some default arguments. */ ps_init_defaults(ps); /* Free old searches (do this before other reinit) */ ps_free_searches(ps); ps->searches = hash_table_new(3, HASH_CASE_YES); /* Free old acmod. */ acmod_free(ps->acmod); ps->acmod = NULL; /* Free old dictionary (must be done after the two things above) */ dict_free(ps->dict); ps->dict = NULL; /* Free d2p */ dict2pid_free(ps->d2p); ps->d2p = NULL; /* Logmath computation (used in acmod and search) */ if (ps->lmath == NULL || (logmath_get_base(ps->lmath) != (float64)cmd_ln_float32_r(ps->config, "-logbase"))) { if (ps->lmath) logmath_free(ps->lmath); ps->lmath = logmath_init ((float64)cmd_ln_float32_r(ps->config, "-logbase"), 0, cmd_ln_boolean_r(ps->config, "-bestpath")); } /* Acoustic model (this is basically everything that * uttproc.c, senscr.c, and others used to do) */ if ((ps->acmod = acmod_init(ps->config, ps->lmath, NULL, NULL)) == NULL) return -1; if (cmd_ln_int32_r(ps->config, "-pl_window") > 0) { /* Initialize an auxiliary phone loop search, which will run in * "parallel" with FSG or N-Gram search. */ if ((ps->phone_loop = phone_loop_search_init(ps->config, ps->acmod, ps->dict)) == NULL) return -1; hash_table_enter(ps->searches, ckd_salloc(ps_search_name(ps->phone_loop)), ps->phone_loop); } /* Dictionary and triphone mappings (depends on acmod). */ /* FIXME: pass config, change arguments, implement LTS, etc. */ if ((ps->dict = dict_init(ps->config, ps->acmod->mdef, ps->acmod->lmath)) == NULL) return -1; if ((ps->d2p = dict2pid_build(ps->acmod->mdef, ps->dict)) == NULL) return -1; lw = cmd_ln_float32_r(config, "-lw"); /* Determine whether we are starting out in FSG or N-Gram search mode. * If neither is used skip search initialization. */ /* Load KWS if one was specified in config */ if ((keyphrase = cmd_ln_str_r(config, "-keyphrase"))) { if (ps_set_keyphrase(ps, PS_DEFAULT_SEARCH, keyphrase)) return -1; ps_set_search(ps, PS_DEFAULT_SEARCH); } if ((path = cmd_ln_str_r(config, "-kws"))) { if (ps_set_kws(ps, PS_DEFAULT_SEARCH, path)) return -1; ps_set_search(ps, PS_DEFAULT_SEARCH); } /* Load an FSG if one was specified in config */ if ((path = cmd_ln_str_r(config, "-fsg"))) { fsg_model_t *fsg = fsg_model_readfile(path, ps->lmath, lw); if (!fsg) return -1; if (ps_set_fsg(ps, PS_DEFAULT_SEARCH, fsg)) return -1; ps_set_search(ps, PS_DEFAULT_SEARCH); } /* Or load a JSGF grammar */ if ((path = cmd_ln_str_r(config, "-jsgf"))) { if (ps_set_jsgf_file(ps, PS_DEFAULT_SEARCH, path) || ps_set_search(ps, PS_DEFAULT_SEARCH)) return -1; } if ((path = cmd_ln_str_r(ps->config, "-allphone"))) { if (ps_set_allphone_file(ps, PS_DEFAULT_SEARCH, path) || ps_set_search(ps, PS_DEFAULT_SEARCH)) return -1; } if ((path = cmd_ln_str_r(ps->config, "-lm")) && !cmd_ln_boolean_r(ps->config, "-allphone")) { if (ps_set_lm_file(ps, PS_DEFAULT_SEARCH, path) || ps_set_search(ps, PS_DEFAULT_SEARCH)) return -1; } if ((path = cmd_ln_str_r(ps->config, "-lmctl"))) { const char *name; ngram_model_t *lmset; ngram_model_set_iter_t *lmset_it; if (!(lmset = ngram_model_set_read(ps->config, path, ps->lmath))) { E_ERROR("Failed to read language model control file: %s\n", path); return -1; } for(lmset_it = ngram_model_set_iter(lmset); lmset_it; lmset_it = ngram_model_set_iter_next(lmset_it)) { ngram_model_t *lm = ngram_model_set_iter_model(lmset_it, &name); E_INFO("adding search %s\n", name); if (ps_set_lm(ps, name, lm)) { ngram_model_free(lm); ngram_model_set_iter_free(lmset_it); return -1; } ngram_model_free(lm); } name = cmd_ln_str_r(config, "-lmname"); if (name) ps_set_search(ps, name); else { E_ERROR("No default LM name (-lmname) for `-lmctl'\n"); return -1; } } /* Initialize performance timer. */ ps->perf.name = "decode"; ptmr_init(&ps->perf); return 0; }
s2_semi_mgau_t * s2_semi_mgau_init(cmd_ln_t *config, logmath_t *lmath, feat_t *fcb, mdef_t *mdef) { s2_semi_mgau_t *s; char const *sendump_path; float32 **fgau; int i; s = ckd_calloc(1, sizeof(*s)); s->config = config; s->lmath = logmath_retain(lmath); /* Log-add table. */ s->lmath_8b = logmath_init(logmath_get_base(lmath), SENSCR_SHIFT, TRUE); if (s->lmath_8b == NULL) { s2_semi_mgau_free(s); return NULL; } /* Ensure that it is only 8 bits wide so that fast_logmath_add() works. */ if (logmath_get_width(s->lmath_8b) != 1) { E_ERROR("Log base %f is too small to represent add table in 8 bits\n", logmath_get_base(s->lmath_8b)); s2_semi_mgau_free(s); return NULL; } /* Inherit stream dimensions from acmod, will be checked below. */ s->n_feat = feat_dimension1(fcb); s->veclen = ckd_calloc(s->n_feat, sizeof(int32)); for (i = 0; i < s->n_feat; ++i) s->veclen[i] = feat_dimension2(fcb, i); /* Read means and variances. */ if (s3_read_mgau(s, cmd_ln_str_r(s->config, "-mean"), &fgau) < 0) { s2_semi_mgau_free(s); return NULL; } s->means = (mfcc_t **)fgau; if (s3_read_mgau(s, cmd_ln_str_r(s->config, "-var"), &fgau) < 0) { s2_semi_mgau_free(s); return NULL; } s->vars = (mfcc_t **)fgau; /* Precompute (and fixed-point-ize) means, variances, and determinants. */ s->dets = (mfcc_t **)ckd_calloc_2d(s->n_feat, s->n_density, sizeof(**s->dets)); s3_precomp(s, s->lmath, cmd_ln_float32_r(s->config, "-varfloor")); /* Read mixture weights */ if ((sendump_path = cmd_ln_str_r(s->config, "-sendump"))) read_sendump(s, mdef, sendump_path); else read_mixw(s, cmd_ln_str_r(s->config, "-mixw"), cmd_ln_float32_r(s->config, "-mixwfloor")); s->ds_ratio = cmd_ln_int32_r(s->config, "-ds"); /* Determine top-N for each feature */ s->topn_beam = ckd_calloc(s->n_feat, sizeof(*s->topn_beam)); s->max_topn = cmd_ln_int32_r(s->config, "-topn"); split_topn(cmd_ln_str_r(s->config, "-topn_beam"), s->topn_beam, s->n_feat); E_INFO("Maximum top-N: %d ", s->max_topn); E_INFOCONT("Top-N beams:"); for (i = 0; i < s->n_feat; ++i) { E_INFOCONT(" %d", s->topn_beam[i]); } E_INFOCONT("\n"); /* Top-N scores from recent frames */ s->n_topn_hist = cmd_ln_int32_r(s->config, "-pl_window") + 2; s->topn_hist = (vqFeature_t ***) ckd_calloc_3d(s->n_topn_hist, s->n_feat, s->max_topn, sizeof(***s->topn_hist)); s->topn_hist_n = ckd_calloc_2d(s->n_topn_hist, s->n_feat, sizeof(**s->topn_hist_n)); for (i = 0; i < s->n_topn_hist; ++i) { int j; for (j = 0; j < s->n_feat; ++j) { int k; for (k = 0; k < s->max_topn; ++k) { s->topn_hist[i][j][k].score = WORST_DIST; s->topn_hist[i][j][k].codeword = k; } } } return s; }
int ps_reinit(ps_decoder_t *ps, cmd_ln_t *config) { char const *lmfile, *lmctl = NULL; if (config && config != ps->config) { cmd_ln_free_r(ps->config); ps->config = config; } #ifndef _WIN32_WCE /* Set up logging. */ if (cmd_ln_str_r(ps->config, "-logfn")) err_set_logfile(cmd_ln_str_r(ps->config, "-logfn")); #endif err_set_debug_level(cmd_ln_int32_r(ps->config, "-debug")); ps->mfclogdir = cmd_ln_str_r(ps->config, "-mfclogdir"); ps->rawlogdir = cmd_ln_str_r(ps->config, "-rawlogdir"); /* Fill in some default arguments. */ ps_init_defaults(ps); /* Free old searches (do this before other reinit) */ ps_free_searches(ps); /* Free old acmod. */ acmod_free(ps->acmod); ps->acmod = NULL; /* Free old dictionary (must be done after the two things above) */ dict_free(ps->dict); ps->dict = NULL; /* Logmath computation (used in acmod and search) */ if (ps->lmath == NULL || (logmath_get_base(ps->lmath) != (float64)cmd_ln_float32_r(ps->config, "-logbase"))) { if (ps->lmath) logmath_free(ps->lmath); ps->lmath = logmath_init ((float64)cmd_ln_float32_r(ps->config, "-logbase"), 0, cmd_ln_boolean_r(ps->config, "-bestpath")); } /* Acoustic model (this is basically everything that * uttproc.c, senscr.c, and others used to do) */ if ((ps->acmod = acmod_init(ps->config, ps->lmath, NULL, NULL)) == NULL) return -1; /* Make the acmod's feature buffer growable if we are doing two-pass search. */ if (cmd_ln_boolean_r(ps->config, "-fwdflat") && cmd_ln_boolean_r(ps->config, "-fwdtree")) acmod_set_grow(ps->acmod, TRUE); if ((ps->pl_window = cmd_ln_int32_r(ps->config, "-pl_window"))) { /* Initialize an auxiliary phone loop search, which will run in * "parallel" with FSG or N-Gram search. */ if ((ps->phone_loop = phone_loop_search_init(ps->config, ps->acmod, ps->dict)) == NULL) return -1; ps->searches = glist_add_ptr(ps->searches, ps->phone_loop); } /* Dictionary and triphone mappings (depends on acmod). */ /* FIXME: pass config, change arguments, implement LTS, etc. */ if ((ps->dict = dict_init(ps->config, ps->acmod->mdef)) == NULL) return -1; /* Determine whether we are starting out in FSG or N-Gram search mode. */ if (cmd_ln_str_r(ps->config, "-fsg") || cmd_ln_str_r(ps->config, "-jsgf")) { ps_search_t *fsgs; if ((ps->d2p = dict2pid_build(ps->acmod->mdef, ps->dict)) == NULL) return -1; if ((fsgs = fsg_search_init(ps->config, ps->acmod, ps->dict, ps->d2p)) == NULL) return -1; fsgs->pls = ps->phone_loop; ps->searches = glist_add_ptr(ps->searches, fsgs); ps->search = fsgs; } else if ((lmfile = cmd_ln_str_r(ps->config, "-lm")) || (lmctl = cmd_ln_str_r(ps->config, "-lmctl"))) { ps_search_t *ngs; if ((ps->d2p = dict2pid_build(ps->acmod->mdef, ps->dict)) == NULL) return -1; if ((ngs = ngram_search_init(ps->config, ps->acmod, ps->dict, ps->d2p)) == NULL) return -1; ngs->pls = ps->phone_loop; ps->searches = glist_add_ptr(ps->searches, ngs); ps->search = ngs; } /* Otherwise, we will initialize the search whenever the user * decides to load an FSG or a language model. */ else { if ((ps->d2p = dict2pid_build(ps->acmod->mdef, ps->dict)) == NULL) return -1; } /* Initialize performance timer. */ ps->perf.name = "decode"; ptmr_init(&ps->perf); return 0; }