43 #include "EST_viterbi.h"
45 static void init_paths_array(
EST_VTPoint *n,
int num_states);
49 EST_VTPoint::~EST_VTPoint()
53 if (paths != 0)
delete paths;
56 for (i=0; i<num_states; i++)
61 if (cands != 0)
delete cands;
62 if (next != 0)
delete next;
66 : vit_a_big_number(1.0e10)
75 overall_path_pruning_envelope_width = -1;
76 candidate_pruning_envelope_width = -1;
84 : vit_a_big_number(1.0e10)
92 overall_path_pruning_envelope_width = -1;
93 candidate_pruning_envelope_width = -1;
103 EST_Viterbi_Decoder::~EST_Viterbi_Decoder()
114 for (i=p->
head(); i != 0; i=i->next())
119 init_paths_array(n,num_states);
129 init_paths_array(n,num_states);
134 if (num_states == -1)
135 init_paths_array(timeline,1);
143 static void init_paths_array(
EST_VTPoint *n,
int num_states)
148 n->num_states = num_states;
150 for (j=0;j<num_states;j++)
154 const int EST_Viterbi_Decoder::betterthan(
const float a,
const float b)
const
175 for (i=0, c=cands; c != 0; c=c->next,i++)
177 init_paths_array(p,i);
187 overall_path_pruning_envelope_width = beam;
188 candidate_pruning_envelope_width = ob_beam;
192 void EST_Viterbi_Decoder::turn_on_debug()
197 void EST_Viterbi_Decoder::turn_on_trace()
211 double best_score=0.0,score_cutoff=0.0;
212 double best_candidate_score=0.0,candidate_cutoff=0;
214 int cand_count=0, cands_considered=0;
216 for (p=timeline; p->next != 0; p=p->next)
219 p->cands = (*user_clist)(p->s,
f);
221 prune_initialize(p,best_score,best_candidate_score,
222 score_cutoff,candidate_cutoff,
226 if (num_states == -1)
227 init_dynamic_states(p->next,p->cands);
230 for (i=0; i<p->num_states; i++)
233 if (((p == timeline) && i==0) || (p->st_paths[i] != 0))
234 for (c=p->cands; c != 0; c=c->next)
239 betterthan(c->score,candidate_cutoff))
243 np = (*user_npath)(p->st_paths[i],c,
f);
244 if (debug) debug_output_1(p,c,np,i);
245 if (do_pruning && betterthan(np->score,best_score))
247 best_score = np->score;
249 score_cutoff = best_score
250 - overall_path_pruning_envelope_width;
252 score_cutoff = best_score
253 + overall_path_pruning_envelope_width;
259 if(!do_pruning||betterthan(np->score,score_cutoff))
260 vit_add_paths(p->next,np);
271 best_score - overall_path_pruning_envelope_width;
274 best_score + overall_path_pruning_envelope_width;
277 cerr <<
"Considered " << cands_considered <<
" of ";
278 cerr << cand_count*p->num_states <<
" candidate paths" << endl;
279 cerr <<
"FRAME: best score " << best_score;
280 cerr <<
" score cutoff " << score_cutoff << endl;
281 cerr <<
" best candidate score " << best_candidate_score;
282 cerr <<
" candidate cutoff " << candidate_cutoff << endl;
286 for (i=0; i<p->next->num_states; i++)
287 if(p->next->st_paths[i] != 0)
290 if(!betterthan(p->next->st_paths[i]->score,
293 delete p->next->st_paths[i];
294 p->next->st_paths[i] = 0;
300 cerr <<
"Pruned " << dcount <<
" of " << pcount;
301 cerr <<
" paths" << endl << endl;
306 for (t=p->paths; t != 0; t=t->next)
308 for (c=p->cands; c != 0; c=c->next)
310 np = (*user_npath)(t,c,
f);
311 add_path(p->next,np);
314 if (debug) fprintf(stdout,
"\n");
319 void EST_Viterbi_Decoder::
321 double &best_score,
double &best_candidate_score,
322 double &score_cutoff,
double &candidate_cutoff,
331 candidate_cutoff = - candidate_pruning_envelope_width;
338 candidate_cutoff = candidate_pruning_envelope_width;
341 for (cand_count=0,c=p->cands; c; c=c->next,cand_count++)
342 if (betterthan(c->score,best_candidate_score))
343 best_candidate_score = c->score;
344 candidate_cutoff += best_candidate_score;
350 printf(
"%s: ",(
const char *)p->s->name());
352 printf(
" %1.3f B %1.3f (%1.3f) st %d s %1.3f ",
354 (np->c->score==0 ? 0 :
355 ((
float)np->f(
"lscore"))/np->c->score),
356 (float)np->f(
"lscore"),np->state,
358 if (p->st_paths[i] == 0)
359 cout <<
"(I)" << endl;
361 cout << p->st_paths[i]->c->name << endl;
369 for (p=np; p != 0; p=next_p)
383 if ((np->state < 0) || (np->state > p->num_states))
385 cerr <<
"EST_Viterbi: state too big (" << np->state <<
")" << endl;
387 else if ((p->st_paths[np->state] == 0) ||
388 (betterthan(np->score,p->st_paths[np->state]->score)))
391 if (p->st_paths[np->state] != 0)
392 delete p->st_paths[np->state];
393 p->st_paths[np->state] = np;
399 bool EST_Viterbi_Decoder::vit_prune_path(
double path_score,
double score_cutoff)
406 if(!betterthan(path_score,score_cutoff))
420 cerr <<
"Viterbi: tried to add path to NULL point\n";
423 if ((beam_width == 0) ||
424 (p->num_paths < beam_width) ||
425 (betterthan(np->score,p->paths->score)))
431 for (a=p->paths; ; a=a->next)
433 if ((a == 0) || (betterthan(a->score,np->score)))
443 if ((beam_width > 0) &&
444 (p->num_paths > beam_width))
473 numcands = allcands->pos;
475 if ((cand_width == 0) ||
476 (numcands < cand_width) ||
477 (betterthan(newcand->score,allcands->score)))
482 for (a=newlist; ; a=a->next)
484 if ((a == 0) || (betterthan(a->score,newcand->score)))
494 if ((cand_width > 0) &&
495 (numcands > cand_width))
507 newlist->pos = numcands;
517 if ((timeline == 0) || (timeline->next == 0))
523 for (; p != 0; p=p->from)
528 p->c->s->
set_val(n,p->c->name);
529 p->c->s->
set(n+
"_score",p->f.
F(
"lscore",0.0));
540 if ((timeline == 0) || (timeline->next == 0))
543 *bestPathEnd = find_best_end();
545 if (*bestPathEnd == 0)
561 for (; p != 0; p=p->from)
564 if ((p->c != 0) && (p->f.
present(n)))
569 EST_VTPath *EST_Viterbi_Decoder::find_best_end()
const
584 for (i=0,t=timeline; t->next != 0; t=t->next,i++)
586 if ((t->num_states == 0) && (t->num_paths == 0))
588 cerr <<
"No paths at frame " << i <<
" " << t->s->name() << endl;
594 for (i=0; i<t->num_states; i++)
596 if ((t->st_paths[i] != 0) &&
597 (betterthan(t->st_paths[i]->score,best)))
599 best = t->st_paths[i]->score;
600 best_p = t->st_paths[i];
604 for (p=t->paths; p!=0; p=p->next)
606 if (betterthan(p->score,best))
617 cerr <<
"Failed to find path" << endl;
618 cout <<
"Best score is " << best << endl;