ps_nbest_t * ps_nbest(ps_decoder_t *ps) { ps_lattice_t *dag; ngram_model_t *lmset; ps_astar_t *nbest; float32 lwf; if (ps->search == NULL) return NULL; if ((dag = ps_get_lattice(ps)) == NULL) return NULL; /* FIXME: This is all quite specific to N-Gram search. Either we * should make N-best a method for each search module or it needs * to be abstracted to work for N-Gram and FSG. */ if (0 != strcmp(ps_search_type(ps->search), PS_SEARCH_TYPE_NGRAM)) { lmset = NULL; lwf = 1.0f; } else { lmset = ((ngram_search_t *)ps->search)->lmset; lwf = ((ngram_search_t *)ps->search)->bestpath_fwdtree_lw_ratio; } nbest = ps_astar_start(dag, lmset, lwf, 0, -1, -1, -1); nbest = ps_nbest_next(nbest); return (ps_nbest_t *)nbest; }
ps_nbest_t * ps_nbest(ps_decoder_t *ps, int sf, int ef, char const *ctx1, char const *ctx2) { ps_lattice_t *dag; ngram_model_t *lmset; ps_astar_t *nbest; float32 lwf; int32 w1, w2; if (ps->search == NULL) return NULL; if ((dag = ps_get_lattice(ps)) == NULL) return NULL; /* FIXME: This is all quite specific to N-Gram search. Either we * should make N-best a method for each search module or it needs * to be abstracted to work for N-Gram and FSG. */ if (0 != strcmp(ps_search_type(ps->search), PS_SEARCH_TYPE_NGRAM)) { lmset = NULL; lwf = 1.0f; } else { lmset = ((ngram_search_t *)ps->search)->lmset; lwf = ((ngram_search_t *)ps->search)->bestpath_fwdtree_lw_ratio; } w1 = ctx1 ? dict_wordid(ps_search_dict(ps->search), ctx1) : -1; w2 = ctx2 ? dict_wordid(ps_search_dict(ps->search), ctx2) : -1; nbest = ps_astar_start(dag, lmset, lwf, sf, ef, w1, w2); return (ps_nbest_t *)nbest; }
const char* ps_get_kws(ps_decoder_t *ps, const char* name) { ps_search_t *search = ps_find_search(ps, name); if (search && strcmp(PS_SEARCH_TYPE_KWS, ps_search_type(search))) return NULL; return search ? kws_search_get_keywords(search) : NULL; }
fsg_model_t * ps_get_fsg(ps_decoder_t *ps, const char *name) { ps_search_t *search = ps_find_search(ps, name); if (search && strcmp(PS_SEARCH_TYPE_FSG, ps_search_type(search))) return NULL; return search ? ((fsg_search_t *) search)->fsg : NULL; }
ngram_model_t * ps_get_lm(ps_decoder_t *ps, const char *name) { ps_search_t *search = ps_find_search(ps, name); if (search && strcmp(PS_SEARCH_TYPE_NGRAM, ps_search_type(search))) return NULL; return search ? ((ngram_search_t *) search)->lmset : NULL; }
int ps_set_search(ps_decoder_t *ps, const char *name) { ps_search_t *search = ps_find_search(ps, name); if (!search) return -1; ps->search = search; /* Set pl window depending on the search */ if (!strcmp(PS_SEARCH_TYPE_NGRAM, ps_search_type(search))) { ps->pl_window = cmd_ln_int32_r(ps->config, "-pl_window"); } else { ps->pl_window = 0; } return 0; }
int ps_set_search(ps_decoder_t *ps, const char *name) { ps_search_t *search; if (ps->acmod->state != ACMOD_ENDED && ps->acmod->state != ACMOD_IDLE) { E_ERROR("Cannot change search while decoding, end utterance first\n"); return -1; } if (!(search = ps_find_search(ps, name))) { return -1; } ps->search = search; /* Set pl window depending on the search */ if (!strcmp(PS_SEARCH_TYPE_NGRAM, ps_search_type(search))) { ps->pl_window = cmd_ln_int32_r(ps->config, "-pl_window"); } else { ps->pl_window = 0; } return 0; }
int ps_add_word(ps_decoder_t *ps, char const *word, char const *phones, int update) { int32 wid; s3cipid_t *pron; hash_iter_t *search_it; char **phonestr, *tmp; int np, i, rv; /* Parse phones into an array of phone IDs. */ tmp = ckd_salloc(phones); np = str2words(tmp, NULL, 0); phonestr = ckd_calloc(np, sizeof(*phonestr)); str2words(tmp, phonestr, np); pron = ckd_calloc(np, sizeof(*pron)); for (i = 0; i < np; ++i) { pron[i] = bin_mdef_ciphone_id(ps->acmod->mdef, phonestr[i]); if (pron[i] == -1) { E_ERROR("Unknown phone %s in phone string %s\n", phonestr[i], tmp); ckd_free(phonestr); ckd_free(tmp); ckd_free(pron); return -1; } } /* No longer needed. */ ckd_free(phonestr); ckd_free(tmp); /* Add it to the dictionary. */ if ((wid = dict_add_word(ps->dict, word, pron, np)) == -1) { ckd_free(pron); return -1; } /* No longer needed. */ ckd_free(pron); /* Now we also have to add it to dict2pid. */ dict2pid_add_word(ps->d2p, wid); /* TODO: we definitely need to refactor this */ for (search_it = hash_table_iter(ps->searches); search_it; search_it = hash_table_iter_next(search_it)) { ps_search_t *search = hash_entry_val(search_it->ent); if (!strcmp(PS_SEARCH_TYPE_NGRAM, ps_search_type(search))) { ngram_model_t *lmset = ((ngram_search_t *) search)->lmset; if (ngram_model_add_word(lmset, word, 1.0) == NGRAM_INVALID_WID) { hash_table_iter_free(search_it); return -1; } } if (update) { if ((rv = ps_search_reinit(search, ps->dict, ps->d2p) < 0)) { hash_table_iter_free(search_it); return rv; } } } /* Rebuild the widmap and search tree if requested. */ return wid; }