int32 live_get_partialhyp(int32 endutt) { int32 id, nwds; glist_t hyp; gnode_t *gn; hyp_t *h; dict_t *dict; dict = kbcore_dict (kb->kbcore); if (endutt) id = vithist_utt_end(kb->vithist, kb->kbcore); else id = vithist_partialutt_end(kb->vithist, kb->kbcore); if (id > 0) { hyp = vithist_backtrace(kb->vithist,id); for (gn = hyp,nwds=0; gn; gn = gnode_next(gn),nwds++) { h = (hyp_t *) gnode_ptr (gn); if (parthyp[nwds].word != NULL) { ckd_free(parthyp[nwds].word); parthyp[nwds].word = NULL; } parthyp[nwds].word = strdup(dict_wordstr(dict, h->id)); parthyp[nwds].sf = h->sf; parthyp[nwds].ef = h->ef; parthyp[nwds].ascr = h->ascr; parthyp[nwds].lscr = h->lscr; } if (parthyp[nwds].word != NULL){ ckd_free(parthyp[nwds].word); parthyp[nwds].word = NULL; } /* Free hyplist */ for (gn = hyp; gn && (gnode_next(gn)); gn = gnode_next(gn)) { h = (hyp_t *) gnode_ptr (gn); ckd_free ((void *) h); } glist_free (hyp); } else { nwds = 0; if (parthyp[nwds].word != NULL) { ckd_free(parthyp[nwds].word); parthyp[nwds].word = NULL; } } return(nwds); }
int32 vithist_utt_end(vithist_t * vh, ngram_model_t *lm, s3dict_t *dict, dict2pid_t *dict2pid, fillpen_t *fp) { int32 f, i; int32 sv, nsv, scr, bestscore, bestvh, vhid; vithist_entry_t *ve, *bestve = 0; int32 endwid = NGRAM_INVALID_WID; bestscore = MAX_NEG_INT32; bestvh = -1; /* Find last frame with entries in vithist table */ /* by ARCHAN 20050525, it is possible that the last frame will not be reached in decoding */ for (f = vh->n_frm - 1; f >= 0; --f) { sv = vh->frame_start[f]; /* First vithist entry in frame f */ nsv = vh->frame_start[f + 1]; /* First vithist entry in next frame (f+1) */ if (sv < nsv) break; } if (f < 0) return -1; if (f != vh->n_frm - 1) E_WARN("No word exit in frame %d, using exits from frame %d\n", vh->n_frm - 1, f); /* Terminate in a final </s> node (make this optional?) */ endwid = ngram_wid(lm, S3_FINISH_WORD); for (i = sv; i < nsv; i++) { int n_used; ve = vithist_id2entry(vh, i); scr = ve->path.score; scr += ngram_tg_score(lm, endwid, ve->lmstate.lm3g.lwid[0], ve->lmstate.lm3g.lwid[1], &n_used); if (bestscore < scr) { bestscore = scr; bestvh = i; bestve = ve; } } assert(bestvh >= 0); if (f != vh->n_frm - 1) { E_ERROR("No word exit in frame %d, using exits from frame %d\n", vh->n_frm - 1, f); /* Add a dummy silwid covering the remainder of the utterance */ assert(vh->frame_start[vh->n_frm - 1] == vh->frame_start[vh->n_frm]); vh->n_frm -= 1; vithist_rescore(vh, lm, dict, dict2pid, fp, s3dict_silwid(dict), vh->n_frm, bestve->path.score, bestvh, -1, -1); vh->n_frm += 1; vh->frame_start[vh->n_frm] = vh->n_entry; return vithist_utt_end(vh, lm, dict, dict2pid, fp); } /* vithist_dump(vh,-1,kbc,stdout); */ /* Create an </s> entry */ ve = vithist_entry_alloc(vh); ve->wid = s3dict_finishwid(dict); ve->sf = (bestve->ef == BAD_S3FRMID) ? 0 : bestve->ef + 1; ve->ef = vh->n_frm; ve->ascr = 0; ve->lscr = bestscore - bestve->path.score; ve->path.score = bestscore; ve->path.pred = bestvh; ve->type = 0; ve->valid = 1; ve->lmstate.lm3g.lwid[0] = endwid; ve->lmstate.lm3g.lwid[1] = ve->lmstate.lm3g.lwid[0]; vhid = vh->n_entry - 1; /* vithist_dump(vh,-1,kbc,stdout); */ return vhid; }
int32 vithist_utt_end (vithist_t *vh, kbcore_t *kbc) { int32 f, i, b, l; int32 sv, nsv, scr, bestscore, bestvh, vhid; vithist_entry_t *ve, *bestve=0; s3lmwid_t endwid; lm_t *lm; dict_t *dict; /* Find last frame with entries in vithist table */ for (f = vh->n_frm-1; f >= 0; --f) { sv = vh->frame_start[f]; /* First vithist entry in frame f */ nsv = vh->frame_start[f+1]; /* First vithist entry in next frame (f+1) */ if (sv < nsv) break; } if (f < 0) return -1; if (f != vh->n_frm-1) E_ERROR("No word exit in frame %d, using exits from frame %d\n", vh->n_frm-1, f); /* Terminate in a final </s> node (make this optional?) */ lm = kbcore_lm (kbc); endwid = lm_finishwid (lm); bestscore = MAX_NEG_INT32; bestvh = -1; for (i = sv; i < nsv; i++) { b = VITHIST_ID2BLK (i); l = VITHIST_ID2BLKOFFSET (i); ve = vh->entry[b] + l; scr = ve->score; scr += lm_tg_score (lm, ve->lmstate.lm3g.lwid[1], ve->lmstate.lm3g.lwid[0], endwid); if (bestscore < scr) { bestscore = scr; bestvh = i; bestve = ve; } } assert (bestvh >= 0); dict = kbcore_dict (kbc); if (f != vh->n_frm-1) { E_ERROR("No word exit in frame %d, using exits from frame %d\n", vh->n_frm-1, f); /* Add a dummy silwid covering the remainder of the utterance */ assert (vh->frame_start[vh->n_frm-1] == vh->frame_start[vh->n_frm]); vh->n_frm -= 1; vithist_rescore (vh, kbc, dict_silwid (dict), vh->n_frm, bestve->score, bestvh, -1); vh->n_frm += 1; vh->frame_start[vh->n_frm] = vh->n_entry; return vithist_utt_end (vh, kbc); } /* Create an </s> entry */ vhid = vh->n_entry; ve = vithist_entry_alloc (vh); ve->wid = dict_finishwid (dict); ve->sf = (bestve->ef == BAD_S3FRMID) ? 0 : bestve->ef + 1; ve->ef = vh->n_frm; ve->ascr = 0; ve->lscr = bestscore - bestve->score; ve->score = bestscore; ve->pred = bestvh; ve->type = 0; ve->valid = 1; ve->lmstate.lm3g.lwid[0] = endwid; ve->lmstate.lm3g.lwid[1] = ve->lmstate.lm3g.lwid[0]; return vhid; }
int32 live_get_partialhyp(int32 endutt) { int32 id, nwds; glist_t hyp; gnode_t *gn; hyp_t *h; dict_t *dict; dict = kbcore_dict (kb->kbcore); if (endutt) id = vithist_utt_end(kb->vithist, kb->kbcore); else id = vithist_partialutt_end(kb->vithist, kb->kbcore); if (id > 0) { hyp = vithist_backtrace(kb->vithist,id); for (gn = hyp,nwds=0; gn; gn = gnode_next(gn),nwds++) { h = (hyp_t *) gnode_ptr (gn); if (parthyp[nwds].word != NULL) { ckd_free(parthyp[nwds].word); parthyp[nwds].word = NULL; } /* 20040905 L Galescu <*****@*****.**> * Report noise? If not, replace with silence word. */ if ((cmd_ln_int32("-reportfill") == 0) && dict_filler_word(dict, h->id)) parthyp[nwds].word = strdup(dict_wordstr(dict, dict->silwid)); else parthyp[nwds].word = strdup(dict_wordstr(dict, h->id)); /* 20040901 L Galescu <*****@*****.**> * Choice to report alternative pronunciations or not. */ if (cmd_ln_int32("-reportpron") == 0) dict_word2basestr(parthyp[nwds].word); parthyp[nwds].sf = h->sf; parthyp[nwds].ef = h->ef; parthyp[nwds].ascr = h->ascr; parthyp[nwds].lscr = h->lscr; } if (parthyp[nwds].word != NULL){ ckd_free(parthyp[nwds].word); parthyp[nwds].word = NULL; } parthyplen = nwds; /* Free hyplist */ for (gn = hyp; gn && (gnode_next(gn)); gn = gnode_next(gn)) { h = (hyp_t *) gnode_ptr (gn); ckd_free ((void *) h); } glist_free (hyp); } else if (id == 0) { nwds = 0; if (parthyp[nwds].word != NULL) { ckd_free(parthyp[nwds].word); parthyp[nwds].word = NULL; } parthyplen = nwds; } else { /* lgalescu 2004/10/13: nothing changed; we return the same hyp that was there before */ } return(parthyplen); }
int ld_record_hyps(live_decoder_t * _decoder, int _end_utt) { int32 id; int32 i = 0; glist_t hyp_list; gnode_t *node; srch_hyp_t *hyp; char *hyp_strptr = 0; char *hyp_str = 0; srch_hyp_t **hyp_segs = 0; int hyp_seglen = 0; int hyp_strlen = 0; int finish_wid = 0; kb_t *kb = 0; dict_t *dict; int rv; assert(_decoder != NULL); ld_free_hyps(_decoder); kb = &_decoder->kb; dict = kbcore_dict(_decoder->kbcore); id = _end_utt ? vithist_utt_end(kb->vithist, _decoder->kbcore) : vithist_partialutt_end(kb->vithist, _decoder->kbcore); if (id < 0) { E_WARN("Failed to retrieve viterbi history.\n"); return LD_ERROR_INTERNAL; } /** record the segment length and the overall string length */ hyp_list = vithist_backtrace(kb->vithist, id, dict); finish_wid = dict_finishwid(dict); for (node = hyp_list; node != NULL; node = gnode_next(node)) { hyp = (srch_hyp_t *) gnode_ptr(node); hyp_seglen++; if (!dict_filler_word(dict, hyp->id) && hyp->id != finish_wid) { hyp_strlen += strlen(dict_wordstr(dict, dict_basewid(dict, hyp->id))) + 1; } } if (hyp_strlen == 0) { hyp_strlen = 1; } /** allocate array to hold the segments and/or decoded string */ hyp_str = (char *) ckd_calloc(hyp_strlen, sizeof(char)); hyp_segs = (srch_hyp_t **) ckd_calloc(hyp_seglen + 1, sizeof(srch_hyp_t *)); if (hyp_segs == NULL || hyp_str == NULL) { E_WARN("Failed to allocate storage for hypothesis.\n"); rv = LD_ERROR_OUT_OF_MEMORY; goto ld_record_hyps_cleanup; } /** iterate thru to fill in the array of segments and/or decoded string */ i = 0; hyp_strptr = hyp_str; for (node = hyp_list; node != NULL; node = gnode_next(node), i++) { hyp = (srch_hyp_t *) gnode_ptr(node); hyp_segs[i] = hyp; hyp->word = dict_wordstr(dict, dict_basewid(dict, hyp->id)); if (!dict_filler_word(dict, hyp->id) && hyp->id != finish_wid) { strcat(hyp_strptr, dict_wordstr(dict, dict_basewid(dict, hyp->id))); hyp_strptr += strlen(hyp_strptr); *hyp_strptr = ' '; hyp_strptr += 1; } } glist_free(hyp_list); hyp_str[hyp_strlen - 1] = '\0'; hyp_segs[hyp_seglen] = 0; _decoder->hyp_frame_num = _decoder->num_frames_decoded; _decoder->hyp_segs = hyp_segs; _decoder->hyp_str = hyp_str; return LD_SUCCESS; ld_record_hyps_cleanup: if (hyp_segs != NULL) { ckd_free(hyp_segs); } if (hyp_str != NULL) { ckd_free(hyp_segs); } if (hyp_list != NULL) { for (node = hyp_list; node != NULL; node = gnode_next(node)) { if ((hyp = (srch_hyp_t *) gnode_ptr(node)) != NULL) { ckd_free(hyp); } } } return rv; }