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 acmod_free(acmod_t *acmod) { if (acmod == NULL) return 0; if (--acmod->refcount > 0) return acmod->refcount; ckd_free(acmod->senone_scores); ckd_free(acmod->senone_active_vec); ckd_free(acmod->senone_active); if (acmod->mdef) bin_mdef_free(acmod->mdef); if (acmod->tmat) tmat_free(acmod->tmat); if (acmod->mgau) ps_mgau_free(acmod->mgau); featbuf_free(acmod->fb); feat_array_free(acmod->feat_buf); logmath_free(acmod->lmath); cmd_ln_free_r(acmod->config); ckd_free(acmod); 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; }
void Recognizer::cleanup() { if (decoder) ps_free(decoder); if (logmath) logmath_free(logmath); decoder = NULL; grammar_set = NULL; logmath = NULL; }
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 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[]) { 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; }
void s2_semi_mgau_free(s2_semi_mgau_t *ps) { s2_semi_mgau_t *s = (s2_semi_mgau_t *)ps; uint32 i; logmath_free(s->lmath); logmath_free(s->lmath_8b); if (s->sendump_mmap) { for (i = 0; i < s->n_feat; ++i) { ckd_free(s->mixw[i]); } ckd_free(s->mixw); mmio_file_unmap(s->sendump_mmap); } else { ckd_free_3d(s->mixw); } if (s->means) { for (i = 0; i < s->n_feat; ++i) { ckd_free(s->means[i]); } ckd_free(s->means); } if (s->vars) { for (i = 0; i < s->n_feat; ++i) { ckd_free(s->vars[i]); } ckd_free(s->vars); } for (i = 0; i < s->n_kdtrees; ++i) free_kd_tree(s->kdtrees[i]); ckd_free(s->kdtrees); ckd_free(s->veclen); ckd_free(s->topn_beam); ckd_free_2d(s->topn_hist_n); ckd_free_3d((void **)s->topn_hist); ckd_free_2d((void **)s->dets); ckd_free(s); }
int main(int argc, char *argv[]) { print_appl_info(argv[0]); cmd_ln_appl_enter(argc, argv, "default.arg", defn); unlimit(); config = cmd_ln_get(); logmath = logs3_init(cmd_ln_float64_r(config, "-logbase"), 1, cmd_ln_int32_r(config, "-log3table")); E_INFO("Value of base %f \n", cmd_ln_float32_r(config, "-logbase")); models_init(); ptmr_init(&tm_utt); if ((inmatchsegfp = fopen(cmd_ln_str_r(config, "-inhypseg"), "r")) == NULL) E_ERROR("fopen(%s,r) failed\n", cmd_ln_str_r(config, "-inhypseg")); if ((outconfmatchsegfp = fopen(cmd_ln_str_r(config, "-output"), "w")) == NULL) E_ERROR("fopen(%s,w) failed\n", cmd_ln_str_r(config, "-output")); if (cmd_ln_str_r(config, "-ctl")) { ctl_process(cmd_ln_str_r(config, "-ctl"), cmd_ln_str_r(config, "-ctl_lm"), NULL, cmd_ln_int32_r(config, "-ctloffset"), cmd_ln_int32_r(config, "-ctlcount"), utt_confidence, NULL); } else { E_FATAL("-ctl is not specified\n"); } #if (! WIN32) system("ps auxwww | grep s3dag"); #endif fclose(outconfmatchsegfp); fclose(inmatchsegfp); models_free(); logmath_free(logmath); 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 ps_free(ps_decoder_t *ps) { if (ps == NULL) return 0; if (--ps->refcount > 0) return ps->refcount; ps_free_searches(ps); dict_free(ps->dict); dict2pid_free(ps->d2p); acmod_free(ps->acmod); logmath_free(ps->lmath); cmd_ln_free_r(ps->config); ckd_free(ps); 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; }
int main(int32 argc, char *argv[]) { /* kb_t kb; ptmr_t tm; */ cmd_ln_appl_enter(argc, argv, "default.arg", defn); config = cmd_ln_get(); logmath = logs3_init(cmd_ln_float64_r(config, "-logbase"), 1, cmd_ln_int32_r(config, "-log3table")); /* Read in input databases */ models_init(); ptmr_init(&tm_utt); nbestdir = cmd_ln_str_r(config, "-nbestdir"); if (cmd_ln_str_r(config, "-ctl")) { ctl_process(cmd_ln_str_r(config, "-ctl"), cmd_ln_str_r(config, "-ctl_lm"), NULL, cmd_ln_int32_r(config, "-ctloffset"), cmd_ln_int32_r(config, "-ctlcount"), utt_astar, NULL); } else { E_FATAL("-ctl is not specified\n"); } models_free(); logmath_free(logmath); #if (! WIN32) system("ps aguxwww | grep s3astar"); #endif cmd_ln_free_r(config); 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 ps_free(ps_decoder_t *ps) { gnode_t *gn; if (ps == NULL) return 0; if (--ps->refcount > 0) return ps->refcount; for (gn = ps->searches; gn; gn = gnode_next(gn)) ps_search_free(gnode_ptr(gn)); glist_free(ps->searches); dict_free(ps->dict); dict2pid_free(ps->d2p); acmod_free(ps->acmod); logmath_free(ps->lmath); cmd_ln_free_r(ps->config); ckd_free(ps->uttid); ckd_free(ps); return 0; }
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[]) { 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); } if (lm == NULL) { E_FATAL("Failed to read the model from the file '%s'", cmd_ln_str_r(config, "-i")); } /* 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")); } /* 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); if (lmath) { logmath_free(lmath); } if (config) { cmd_ln_free_r(config); } return 0; error_out: ngram_model_free(lm); if (lmath) { logmath_free(lmath); } if (config) { cmd_ln_free_r(config); } 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; }
int32 main(int32 argc, char *argv[]) { FILE *fpout; mgau_model_t *mgau; int32 **subvec; int32 max_datarows, datarows, datacols, svqrows, svqcols; float32 **data, **vqmean; int32 *datamap, *vqmap; float64 sqerr; int32 stdev; int32 i, j, v, m, c; cmd_ln_t *config; logmath_t *logmath; print_appl_info(argv[0]); cmd_ln_appl_enter(argc, argv, "default.arg", arg); unlimit(); config = cmd_ln_get(); logmath = logs3_init(cmd_ln_float64_r(config, "-logbase"), 1, cmd_ln_int32_r(config, "-log3table")); /*Report Progress, use log table */ /* Load means/vars but DO NOT precompute variance inverses or determinants */ mgau = mgau_init(cmd_ln_str_r(config, "-mean"), cmd_ln_str_r(config, "-var"), 0.0 /* no varfloor */ , cmd_ln_str_r(config, "-mixw"), cmd_ln_float32_r(config, "-mixwfloor"), FALSE, /* No precomputation */ ".cont.", MIX_INT_FLOAT_COMP, logmath); /* Parse subvector spec argument; subvec is null terminated; subvec[x] is -1 terminated */ subvec = parse_subvecs(cmd_ln_str_r(config, "-svspec")); if (cmd_ln_str_r(config, "-subvq")) { if ((fpout = fopen(cmd_ln_str_r(config, "-subvq"), "w")) == NULL) { E_ERROR_SYSTEM("Failed to open output file '%s'", fpout); return 1; } } else fpout = stdout; /* Echo command line to output file */ for (i = 0; i < argc - 1; i++) fprintf(fpout, "# %s \\\n", argv[i]); fprintf(fpout, "# %s\n#\n", argv[argc - 1]); /* Print input and output configurations to output file */ for (v = 0; subvec[v]; v++); /* No. of subvectors */ svqrows = cmd_ln_int32_r(config, "-svqrows"); fprintf(fpout, "VQParam %d %d -> %d %d\n", mgau_n_mgau(mgau), mgau_max_comp(mgau), v, svqrows); for (v = 0; subvec[v]; v++) { for (i = 0; subvec[v][i] >= 0; i++); fprintf(fpout, "Subvector %d length %d ", v, i); for (i = 0; subvec[v][i] >= 0; i++) fprintf(fpout, " %2d", subvec[v][i]); fprintf(fpout, "\n"); } fflush(fpout); /* * datamap[] for identifying non-0 input vectors that take part in the clustering process: * datamap[m*max_mean + c] = row index of data[][] containing the copy. * vqmap[] for mapping vq input data to vq output. */ max_datarows = mgau_n_mgau(mgau) * mgau_max_comp(mgau); datamap = (int32 *) ckd_calloc(max_datarows, sizeof(int32)); vqmap = (int32 *) ckd_calloc(max_datarows, sizeof(int32)); stdev = cmd_ln_int32_r(config, "-stdev"); /* Copy and cluster each subvector */ for (v = 0; subvec[v]; v++) { E_INFO("Clustering subvector %d\n", v); for (datacols = 0; subvec[v][datacols] >= 0; datacols++); /* Input subvec length */ svqcols = datacols * 2; /* subvec length after concatenating mean + var */ /* Allocate input/output data areas */ data = (float32 **) ckd_calloc_2d(max_datarows, svqcols, sizeof(float32)); vqmean = (float32 **) ckd_calloc_2d(svqrows, svqcols, sizeof(float32)); /* Make a copy of the subvectors from the input data, and initialize maps */ for (i = 0; i < max_datarows; i++) datamap[i] = -1; datarows = 0; for (m = 0; m < mgau_n_mgau(mgau); m++) { /* For each mixture m */ for (c = 0; c < mgau_n_comp(mgau, m); c++) { /* For each component c in m */ if (vector_is_zero (mgau_var(mgau, m, c), mgau_veclen(mgau))) { E_INFO("Skipping mgau %d comp %d\n", m, c); continue; } for (i = 0; i < datacols; i++) { /* Copy specified dimensions, mean+var */ data[datarows][i * 2] = mgau->mgau[m].mean[c][subvec[v][i]]; data[datarows][i * 2 + 1] = (!stdev) ? mgau->mgau[m]. var[c][subvec[v][i]] : sqrt(mgau->mgau[m]. var[c][subvec[v][i]]); } datamap[m * mgau_max_comp(mgau) + c] = datarows++; } } E_INFO("Sanity check: input data[0]:\n"); vector_print(stderr, data[0], svqcols); for (i = 0; i < max_datarows; i++) vqmap[i] = -1; #if 0 { int32 **in; printf("Input data: %d x %d\n", datarows, svqcols); in = (int32 **) data; for (i = 0; i < datarows; i++) { printf("%8d:", i); for (j = 0; j < svqcols; j++) printf(" %08x", in[i][j]); printf("\n"); } for (i = 0; i < datarows; i++) { printf("%15d:", i); for (j = 0; j < svqcols; j++) printf(" %15.7e", data[i][j]); printf("\n"); } fflush(stdout); } #endif /* VQ the subvector copy built above */ sqerr = vector_vqgen(data, datarows, svqcols, svqrows, cmd_ln_float64_r(config, "-eps"), cmd_ln_int32_r(config, "-iter"), vqmean, vqmap, cmd_ln_int32_r(config, "-seed")); /* Output VQ */ fprintf(fpout, "Codebook %d Sqerr %e\n", v, sqerr); for (i = 0; i < svqrows; i++) { if (stdev) { /* Convert clustered stdev back to var */ for (j = 1; j < svqcols; j += 2) vqmean[i][j] *= vqmean[i][j]; } vector_print(fpout, vqmean[i], svqcols); } fprintf(fpout, "Map %d\n", v); for (i = 0; i < max_datarows; i += mgau_max_comp(mgau)) { for (j = 0; j < mgau_max_comp(mgau); j++) { if (datamap[i + j] < 0) fprintf(fpout, " -1"); else fprintf(fpout, " %d", vqmap[datamap[i + j]]); } fprintf(fpout, "\n"); } fflush(fpout); /* Cleanup */ ckd_free_2d((void **) data); ckd_free_2d((void **) vqmean); } subvecs_free(subvec); ckd_free(datamap); ckd_free(vqmap); mgau_free(mgau); fprintf(fpout, "End\n"); fclose(fpout); logmath_free(logmath); cmd_ln_free_r(config); exit(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; }
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; }