📄 lextree.c
字号:
ckd_free ((void *) lextree->next_active); if (k != lextree->n_node) E_ERROR("#Nodes allocated(%d) != #nodes freed(%d)\n", lextree->n_node, k); ckd_free (lextree);}void lextree_ci_active (lextree_t *lextree, bitvec_t ci_active){ lextree_node_t **list, *ln; int32 i; list = lextree->active; for (i = 0; i < lextree->n_active; i++) { ln = list[i]; bitvec_set (ci_active, ln->ci); }}void lextree_ssid_active (lextree_t *lextree, int32 *ssid, int32 *comssid){ lextree_node_t **list, *ln; int32 i; list = lextree->active; for (i = 0; i < lextree->n_active; i++) { ln = list[i]; if (ln->composite) comssid[ln->ssid] = 1; else ssid[ln->ssid] = 1; }}void lextree_utt_end (lextree_t *l, kbcore_t *kbc){ mdef_t *mdef; lextree_node_t *ln; int32 i; mdef = kbcore_mdef (kbc); for (i = 0; i < l->n_active; i++) { /* The inactive ones should already be reset */ ln = l->active[i]; ln->frame = -1; hmm_clear (&(ln->hmm), mdef_n_emit_state(mdef)); } l->n_active = 0; l->n_next_active = 0;}static void lextree_node_print (lextree_node_t *ln, dict_t *dict, FILE *fp){ fprintf (fp, "wid(%d)pr(%d)com(%d)ss(%d)", ln->wid, ln->prob, ln->composite, ln->ssid); if (IS_S3WID(ln->wid)) fprintf (fp, "%s", dict_wordstr(dict, ln->wid)); fprintf (fp, "\n");}static void lextree_subtree_print (lextree_node_t *ln, int32 level, dict_t *dict, FILE *fp){ int32 i; gnode_t *gn; for (i = 0; i < level; i++) fprintf (fp, " "); lextree_node_print (ln, dict, fp); for (gn = ln->children; gn; gn = gnode_next(gn)) { ln = (lextree_node_t *) gnode_ptr (gn); lextree_subtree_print (ln, level+1, dict, fp); }}void lextree_dump (lextree_t *lextree, dict_t *dict, FILE *fp){ gnode_t *gn; lextree_node_t *ln; int32 i; for (gn = lextree->root; gn; gn = gnode_next(gn)) { ln = (lextree_node_t *) gnode_ptr (gn); lextree_subtree_print (ln, 0, dict, fp); } if (lextree->n_lc > 0) { for (i = 0; i < lextree->n_lc; i++) { fprintf (fp, "lcroot %d\n", lextree->lcroot[i].lc); for (gn = lextree->lcroot[i].root; gn; gn = gnode_next(gn)) { ln = (lextree_node_t *) gnode_ptr(gn); lextree_node_print (ln, dict, fp); } } }}void lextree_enter (lextree_t *lextree, s3cipid_t lc, int32 cf, int32 inscore, int32 inhist, int32 thresh){ glist_t root; gnode_t *gn; lextree_node_t *ln; int32 nf, scr; int32 i, n; hmm_t *hmm; nf = cf+1; /* Locate root nodes list */ if (lextree->n_lc == 0) { assert (NOT_S3CIPID(lc)); root = lextree->root; } else { for (i = 0; (i < lextree->n_lc) && (lextree->lcroot[i].lc != lc); i++); assert (i < lextree->n_lc); root = lextree->lcroot[i].root; } /* Enter root nodes */ n = lextree->n_next_active; for (gn = root; gn; gn = gnode_next(gn)) { ln = (lextree_node_t *) gnode_ptr (gn); hmm = &(ln->hmm); scr = inscore + ln->prob; if ((scr >= thresh) && (hmm->in.score < scr)) { hmm->in.score = scr; hmm->in.history = inhist; if (ln->frame != nf) { ln->frame = nf; lextree->next_active[n++] = ln; } } /* else it is activated separately */ } lextree->n_next_active = n;}void lextree_active_swap (lextree_t *lextree){ lextree_node_t **t; t = lextree->active; lextree->active = lextree->next_active; lextree->next_active = t; lextree->n_active = lextree->n_next_active; lextree->n_next_active = 0;}int32 lextree_hmm_eval (lextree_t *lextree, kbcore_t *kbc, ascr_t *ascr, int32 frm, FILE *fp){ int32 best, wbest, n_st; int32 i, k; lextree_node_t **list, *ln; mdef_t *mdef; dict2pid_t *d2p; mdef = kbc->mdef; d2p = kbc->dict2pid; n_st = mdef_n_emit_state (mdef); list = lextree->active; best = MAX_NEG_INT32; wbest = MAX_NEG_INT32; if (fp) { for (i = 0; i < lextree->n_active; i++) { ln = list[i]; assert (ln->frame == frm); lextree_node_print (ln, kbc->dict, fp); if (! ln->composite) k = hmm_dump_vit_eval (&(ln->hmm), n_st, mdef->sseq[ln->ssid], ascr->sen, fp); else k = hmm_dump_vit_eval (&(ln->hmm), n_st, d2p->comsseq[ln->ssid], ascr->comsen, fp); if (best < k) best = k; if (IS_S3WID(ln->wid)) { if (wbest < k) wbest = k; } } } else { if (n_st == 3) { for (i = 0; i < lextree->n_active; i++) { ln = list[i]; assert (ln->frame == frm); if (! ln->composite) { k = hmm_vit_eval_3st (&(ln->hmm), mdef->sseq[ln->ssid], ascr->sen); } else { k = hmm_vit_eval_3st (&(ln->hmm), d2p->comsseq[ln->ssid], ascr->comsen); } if (best < k) best = k; if (IS_S3WID(ln->wid)) { if (wbest < k) wbest = k; } } } else if (n_st == 5) { for (i = 0; i < lextree->n_active; i++) { ln = list[i]; assert (ln->frame == frm); if (! ln->composite) k = hmm_vit_eval_5st (&(ln->hmm), mdef->sseq[ln->ssid], ascr->sen); else k = hmm_vit_eval_5st (&(ln->hmm), d2p->comsseq[ln->ssid], ascr->comsen); if (best < k) best = k; if (IS_S3WID(ln->wid)) { if (wbest < k) wbest = k; } } } else E_FATAL("#State= %d unsupported\n", n_st); } lextree->best = best; lextree->wbest = wbest; if (fp) { fprintf (fp, "Fr %d #active %d best %d wbest %d\n", frm, lextree->n_active, best, wbest); fflush (fp); } return best;}void lextree_hmm_histbin (lextree_t *lextree, int32 bestscr, int32 *bin, int32 nbin, int32 bw){ lextree_node_t **list, *ln; hmm_t *hmm; int32 i, k; glist_t *binln; gnode_t *gn; binln = (glist_t *) ckd_calloc (nbin, sizeof(glist_t)); list = lextree->active; for (i = 0; i < lextree->n_active; i++) { ln = list[i]; hmm = &(ln->hmm); k = (bestscr - hmm->bestscore) / bw; if (k >= nbin) k = nbin-1; assert (k >= 0); bin[k]++; binln[k] = glist_add_ptr (binln[k], (void *) ln); } /* Reorder the active lexnodes in APPROXIMATELY descending scores */ k = 0; for (i = 0; i < nbin; i++) { for (gn = binln[i]; gn; gn = gnode_next(gn)) { ln = (lextree_node_t *) gnode_ptr (gn); list[k++] = ln; } glist_free (binln[i]); } assert (k == lextree->n_active); ckd_free ((void *) binln);}void lextree_hmm_propagate (lextree_t *lextree, kbcore_t *kbc, vithist_t *vh, int32 cf, int32 th, int32 pth, int32 wth,int32 *phn_heur_list,int32 heur_beam, int32 heur_type){ mdef_t *mdef; int32 nf, newscore, newHeurScore; lextree_node_t **list, *ln, *ln2; hmm_t *hmm, *hmm2; gnode_t *gn; int32 i, n;
int32 hth ;
/* Code for heursitic score */ kbc->maxNewHeurScore=MAX_NEG_INT32; kbc->lastfrm=-1; hth = 0; mdef = kbcore_mdef(kbc); nf = cf+1; list = lextree->active; n = lextree->n_next_active; assert (n == 0); for (i = 0; i < lextree->n_active; i++) { ln = list[i]; hmm = &(ln->hmm); if (ln->frame < nf) { if (hmm->bestscore >= th) { /* Active in next frm */ ln->frame = nf; lextree->next_active[n++] = ln; } else { /* Deactivate */ ln->frame = -1; hmm_clear (hmm, mdef_n_emit_state(mdef)); } } if (NOT_S3WID(ln->wid)) { /* Not a leaf node */#if 0 if (((cf % 3) == 0) || (hmm->out.score < pth)) continue; /* HMM exit score not good enough */#else if (hmm->out.score < pth) continue; /* HMM exit score not good enough */#endif if(heur_type >0){ if (cf!=kbc->lastfrm) { kbc->lastfrm=cf; kbc->maxNewHeurScore=MAX_NEG_INT32; } for (gn = ln->children; gn; gn = gnode_next(gn)) { ln2 = gnode_ptr(gn); newHeurScore = hmm->out.score + (ln2->prob - ln->prob) + phn_heur_list[(int32)ln2->ci]; if (kbc->maxNewHeurScore < newHeurScore) kbc->maxNewHeurScore = newHeurScore; } hth = kbc->maxNewHeurScore + heur_beam; } /* Transition to each child */ for (gn = ln->children; gn; gn = gnode_next(gn)) { ln2 = gnode_ptr(gn); hmm2 = &(ln2->hmm); newscore = hmm->out.score + (ln2->prob - ln->prob); newHeurScore = newscore + phn_heur_list[(int32)ln2->ci];#if 0 E_INFO("Newscore %d, Heurscore %d, hth %d, CI phone ID %d, CI phone Str %s\n",newscore, newHeurScore, hth, ln2->ci,mdef_ciphone_str(mdef,ln2->ci));#endif if (((heur_type==0)|| /*If the heuristic type is 0, by-pass heuristic score OR */ (heur_type>0 && newHeurScore >= hth)) && /*If the heuristic type is other and if the heur score is within threshold*/ (newscore >= th) && /*If the score is smaller than the phone score, prune away*/ (hmm2->in.score < newscore) /*Just the Viterbi Update */ ) { hmm2->in.score = newscore; hmm2->in.history = hmm->out.history; if (ln2->frame != nf) { ln2->frame = nf; lextree->next_active[n++] = ln2; } } } } else { /* Leaf node; word exit */ if (hmm->out.score < wth) continue; /* Word exit score not good enough */ if(hmm->out.history==-1) E_ERROR("Hmm->out.history equals to -1 with score %d and active idx %d, lextree->type\n",hmm->out.score,i,lextree->type); /* Rescore the LM prob for this word wrt all possible predecessors */ vithist_rescore (vh, kbc, ln->wid, cf, hmm->out.score - ln->prob, hmm->out.history, lextree->type); } } lextree->n_next_active = n;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -