1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
|
// Copyright (C) 2003--2004 Darren Moore (moore@idiap.ch)
//
// This file is part of Torch 3.1.
//
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// 3. The name of the author may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
// NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
// THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "Allocator.h"
#include "BeamSearchDecoder.h"
namespace Torch {
BeamSearchDecoder::BeamSearchDecoder( LinearLexicon *lexicon_ , LanguageModel *lang_model_ ,
real log_word_entrance_penalty_ , real word_int_beam_ ,
real word_end_beam_ , bool delayed_lm_ , bool verbose_mode_ )
{
if ( lexicon_ == NULL )
error("BeamSearchDecoder::BeamSearchDecoder - no lexicon defined\n") ;
lexicon = lexicon_ ;
vocabulary = lexicon->lex_info->vocabulary ;
lang_model = lang_model_ ;
phone_models = lexicon->phone_models ;
n_frames = 0 ;
log_word_entrance_penalty = log_word_entrance_penalty_ ;
verbose_mode = verbose_mode_ ;
delayed_lm = delayed_lm_ ;
word_state_hyps_1 = (DecodingHypothesis ***)Allocator::sysAlloc( lexicon->n_models *
sizeof(DecodingHypothesis **) ) ;
word_state_hyps_2 = (DecodingHypothesis ***)Allocator::sysAlloc( lexicon->n_models *
sizeof(DecodingHypothesis **) ) ;
word_end_hyps_1 = (DecodingHypothesis **)Allocator::sysAlloc( lexicon->n_models *
sizeof(DecodingHypothesis *) ) ;
word_end_hyps_2 = (DecodingHypothesis **)Allocator::sysAlloc( lexicon->n_models *
sizeof(DecodingHypothesis *) ) ;
word_entry_hyps_1 = (DecodingHypothesis **)Allocator::sysAlloc( lexicon->n_models *
sizeof(DecodingHypothesis *) ) ;
word_entry_hyps_2 = (DecodingHypothesis **)Allocator::sysAlloc( lexicon->n_models *
sizeof(DecodingHypothesis *) ) ;
curr_word_hyps = word_state_hyps_1 ;
prev_word_hyps = word_state_hyps_2 ;
curr_word_end_hyps = word_end_hyps_1 ;
prev_word_end_hyps = word_end_hyps_2 ;
curr_word_entry_hyps = word_entry_hyps_1 ;
prev_word_entry_hyps = word_entry_hyps_2 ;
for ( int w=0 ; w<lexicon->n_models ; w++ )
{
word_state_hyps_1[w] = (DecodingHypothesis **)Allocator::sysAlloc(
lexicon->nStatesInModel(w) * sizeof(DecodingHypothesis *) ) ;
word_state_hyps_2[w] = (DecodingHypothesis **)Allocator::sysAlloc(
lexicon->nStatesInModel(w) * sizeof(DecodingHypothesis *) ) ;
for ( int s=0 ; s<lexicon->nStatesInModel(w) ; s++ )
{
word_state_hyps_1[w][s] = new DecodingHypothesis() ;
word_state_hyps_1[w][s]->initHyp( w , s ) ;
word_state_hyps_2[w][s] = new DecodingHypothesis() ;
word_state_hyps_2[w][s]->initHyp( w , s ) ;
}
word_end_hyps_1[w] = word_state_hyps_1[w][lexicon->nStatesInModel(w)-1] ;
word_end_hyps_2[w] = word_state_hyps_2[w][lexicon->nStatesInModel(w)-1] ;
word_entry_hyps_1[w] = word_state_hyps_1[w][0] ;
word_entry_hyps_2[w] = word_state_hyps_2[w][0] ;
}
sent_start_index = lexicon->lex_info->sent_start_index ;
sent_end_index = lexicon->lex_info->sent_end_index ;
max_interior_score = LOG_ZERO ;
best_word_end_hyp = NULL ;
if ( word_int_beam_ > 0.0 )
word_int_beam = word_int_beam_ ;
else
word_int_beam = -LOG_ZERO ;
if ( word_end_beam_ > 0.0 )
word_end_beam = word_end_beam_ ;
else
word_end_beam = -LOG_ZERO ;
}
BeamSearchDecoder::~BeamSearchDecoder()
{
resetHypotheses() ;
if ( word_state_hyps_1 != NULL )
{
for ( int w=0 ; w<lexicon->n_models ; w++ )
{
for ( int s=0 ; s<lexicon->nStatesInModel(w) ; s++ )
delete word_state_hyps_1[w][s] ;
free( word_state_hyps_1[w] ) ;
}
free( word_state_hyps_1 ) ;
}
if ( word_state_hyps_2 != NULL )
{
for ( int w=0 ; w<lexicon->n_models ; w++ )
{
for ( int s=0 ; s<lexicon->nStatesInModel(w) ; s++ )
delete word_state_hyps_2[w][s] ;
free( word_state_hyps_2[w] ) ;
}
free( word_state_hyps_2 ) ;
}
if ( word_end_hyps_1 != NULL )
free( word_end_hyps_1 ) ;
if ( word_end_hyps_2 != NULL )
free( word_end_hyps_2 ) ;
if ( word_entry_hyps_1 != NULL )
free( word_entry_hyps_1 ) ;
if ( word_entry_hyps_2 != NULL )
free( word_entry_hyps_2 ) ;
}
void BeamSearchDecoder::decode( real **input_data , int n_frames_ , int *num_result_words ,
int **result_words , int **result_words_times )
{
int temp_words[5000] , temp_times[5000] ;
DecodingHypothesis *curr_hyp ;
real score ;
#ifdef DEBUG
if ( (num_result_words==NULL) || (result_words==NULL) || (result_words_times==NULL) )
error("BeamSearchDecoder::decode - Result variables are NULL\n") ;
#endif
n_frames = n_frames_ ;
// Initialise the hypothesis buffers and queues.
init() ;
// process the inputs
for ( int t=0 ; t<n_frames ; t++ )
{
if ( verbose_mode == true )
{
fprintf( stderr , "\r \r") ;
fprintf( stderr , "Frame %d of %d",t+1,n_frames) ; fflush(stderr) ;
}
// Swap hypothesis buffers
swapHypBuffers() ;
// Pass the new input vector to the phone set - it knows whether the inputs
// are emission probabilities or features and how to handle each.
phone_models->setInputVector( input_data[t] ) ;
// Process the transitions between states inside words.
processWordInteriorStates() ;
if ( t == (n_frames-1) )
{
// We've reached the end of the input data - no need to evaluate word transitions.
if ( verbose_mode == true )
fprintf( stderr , "\r \r") ;
break ;
}
// If there is a language model, then tune the word-end hypotheses (that remain
// after pruning) using the language model.
// After that, evaluate word transitions.
if ( lang_model != NULL )
{
if ( delayed_lm == false )
processWordTransitionsLM( t ) ;
else
{
applyLMProbs() ;
processWordTransitionsNoLM( t ) ;
}
}
else
processWordTransitionsNoLM( t ) ;
// We now have hypotheses for the initial states of all possible next words.
// These hypotheses cannot remain in the (non-emitting) initial states.
// We have to consider transitions from each initial state to all possible
// (emitting) successor states and see if the word entry hypothesis score is better
// than the current hypothesis in the successor state.
// If so, we update the hypothesis in the successor state using the word entry
// hypothesis.
processWordEntryHypotheses() ;
}
if ( sent_end_index >= 0 )
{
// We look at the hypothesis that is in the final state of the sentence end pronunciation.
curr_hyp = curr_word_end_hyps[sent_end_index] ;
}
else
{
if ( (sent_start_index >= 0) && (best_word_end_hyp==curr_word_end_hyps[sent_start_index]) )
{
// Cannot have start word as end of sentence word.
// Find the next best.
score = LOG_ZERO ;
curr_hyp = NULL ;
for ( int i=0 ; i<lexicon->n_models ; i++ )
{
if ( i == sent_start_index )
continue ;
if ( curr_word_end_hyps[i]->score > score )
{
score = curr_word_end_hyps[i]->score ;
curr_hyp = curr_word_end_hyps[i] ;
}
}
}
else
curr_hyp = best_word_end_hyp ;
}
if ( (curr_hyp == NULL) || (curr_hyp->score <= LOG_ZERO) )
{
// There is no hypothesis that is in the final state of the sentence end model.
*num_result_words = 0 ;
*result_words = NULL ;
*result_words_times = NULL ;
return ;
}
// Allocate memory for the result array (ie. array of indices corresponding to words
// in the lexicon).
WordChainElem *temp_elem = curr_hyp->word_level_info ;
*num_result_words = 0 ;
while ( temp_elem != NULL )
{
temp_words[*num_result_words] = temp_elem->word ;
temp_times[*num_result_words] = temp_elem->word_start_frame ;
temp_elem = temp_elem->prev_elem ;
(*num_result_words)++ ;
}
*result_words = (int *)Allocator::sysAlloc( (*num_result_words) * sizeof(int) ) ;
*result_words_times = (int *)Allocator::sysAlloc( (*num_result_words) * sizeof(int) ) ;
for ( int w=0 ; w<(*num_result_words) ; w++ )
{
(*result_words)[w] = temp_words[(*num_result_words)-w-1] ;
(*result_words_times)[w] = temp_times[(*num_result_words)-w-1] ;
}
}
void BeamSearchDecoder::resetHypotheses()
{
// Reset the scores of the new state hypotheses buffers
// If the decoder has already been used, only the curr_word_hyps will
// contain active hypotheses that need to be deactivated.
for ( int w=0 ; w<lexicon->n_models ; w++ )
{
for ( int s=0 ; s<(lexicon->nStatesInModel(w)) ; s++ )
{
curr_word_hyps[w][s]->deactivate() ;
// word_state_hyps_1[w][s]->deactivate() ;
// word_state_hyps_2[w][s]->deactivate() ;
}
}
}
void BeamSearchDecoder::swapHypBuffers()
{
// Swap buffers
if ( curr_word_hyps == word_state_hyps_1 )
{
curr_word_hyps = word_state_hyps_2 ;
prev_word_hyps = word_state_hyps_1 ;
curr_word_end_hyps = word_end_hyps_2 ;
prev_word_end_hyps = word_end_hyps_1 ;
curr_word_entry_hyps = word_entry_hyps_2 ;
prev_word_entry_hyps = word_entry_hyps_1 ;
}
else
{
curr_word_hyps = word_state_hyps_1 ;
prev_word_hyps = word_state_hyps_2 ;
curr_word_end_hyps = word_end_hyps_1 ;
prev_word_end_hyps = word_end_hyps_2 ;
curr_word_entry_hyps = word_entry_hyps_1 ;
prev_word_entry_hyps = word_entry_hyps_2 ;
}
if ( verbose_mode == true )
{
fprintf( stderr , ": " ) ; fflush(stderr) ;
}
}
void BeamSearchDecoder::processWordInteriorStates()
{
DecodingHypothesis *prev_hyp ;
real emission_prob , new_score , *suc_log_trans_probs , int_prune_thresh ;
real temp_int_prune_thresh , temp_end_prune_thresh , max_end_score ;
int n_processed=0 , n_states_minus_one ;
short n_sucs , *sucs ;
// Process the interior state hypotheses for the "normal" lexicon words.
int_prune_thresh = max_interior_score - word_int_beam ;
max_interior_score = LOG_ZERO ;
max_end_score = LOG_ZERO ;
temp_int_prune_thresh = LOG_ZERO ;
temp_end_prune_thresh = LOG_ZERO ;
best_word_end_hyp = NULL ;
for ( int w=0 ; w<lexicon->n_models ; w++ )
{
n_states_minus_one = lexicon->nStatesInModel(w) - 1 ;
for ( int s=1 ; s<n_states_minus_one ; s++ ) // for all emitting states
{
prev_hyp = prev_word_hyps[w][s] ;
if ( prev_hyp->score <= LOG_ZERO )
continue ;
#ifdef DEBUG
// We assume from this point on that the word/state field in the hypothesis
// correspond to the indices in the nested loops (s & w). Check that this is so.
if ( (prev_hyp->word != w) || (prev_hyp->state != s) )
error("BeamSearchDecoder::processWordIntStates - word-state index mismatch\n") ;
#endif
if ( w == sent_end_index )
{
// We don't want to prune any of the sentence end hypotheses.
emission_prob = lexicon->calcEmissionProb( w , s ) ;
lexicon->getSuccessorInfo( w , s , &n_sucs , &sucs , &suc_log_trans_probs ) ;
for ( int suc=0 ; suc<n_sucs ; suc++ )
{
new_score = prev_hyp->score + emission_prob + suc_log_trans_probs[suc] ;
if ( new_score > curr_word_hyps[w][sucs[suc]]->score )
curr_word_hyps[w][sucs[suc]]->extendState( prev_hyp , new_score ) ;
}
}
else if ( prev_hyp->score >= int_prune_thresh )
{
n_processed++ ;
// Retrieve/calculate the emission probability for the current state.
emission_prob = lexicon->calcEmissionProb( w , s ) ;
// The hypothesis we've just retrieved is for a particular word, w,
// and state, sprev.
// See if a path through (w,sprev) improves the current hypothesis for
// every (next) state, s, of word w.
lexicon->getSuccessorInfo( w , s , &n_sucs , &sucs , &suc_log_trans_probs ) ;
for ( int suc=0 ; suc<n_sucs ; suc++ )
{
new_score = prev_hyp->score + emission_prob + suc_log_trans_probs[suc] ;
if ( sucs[suc] == n_states_minus_one )
{
// The final state is a special case. If we have a language model,
// then we want to prune word end hyps before we apply LM probs.
// If we don't have a language model, then we only need to keep
// track of the most likely word end.
if ( lang_model != NULL )
{
if ( (new_score >= temp_end_prune_thresh) &&
(new_score > curr_word_hyps[w][n_states_minus_one]->score ) )
{
if ( new_score > max_end_score )
{
best_word_end_hyp = curr_word_hyps[w][n_states_minus_one] ;
max_end_score = new_score ;
temp_end_prune_thresh = new_score - word_end_beam ;
}
curr_word_hyps[w][n_states_minus_one]->extendState( prev_hyp ,
new_score ) ;
}
}
else
{
if ( new_score > max_end_score )
{
if ( best_word_end_hyp != NULL )
best_word_end_hyp->deactivate() ;
best_word_end_hyp = curr_word_hyps[w][n_states_minus_one] ;
max_end_score = new_score ;
curr_word_hyps[w][n_states_minus_one]->extendState( prev_hyp ,
new_score ) ;
}
}
}
else
{
if ( new_score > curr_word_hyps[w][sucs[suc]]->score )
{
if ( new_score >= temp_int_prune_thresh )
{
if ( new_score > max_interior_score )
{
max_interior_score = new_score ;
temp_int_prune_thresh = new_score - word_int_beam ;
}
curr_word_hyps[w][sucs[suc]]->extendState( prev_hyp , new_score );
}
}
}
}
}
// We've finished with this hypothesis, so deactivate it.
prev_hyp->deactivate() ;
}
}
if ( verbose_mode == true )
{
fprintf( stderr , "%d," , n_processed ) ; fflush(stderr) ;
}
}
void BeamSearchDecoder::applyLMProbs()
{
real temp_end_prune_thresh , best_score , score ;
if ( best_word_end_hyp != NULL )
temp_end_prune_thresh = best_word_end_hyp->score - word_end_beam ;
else
temp_end_prune_thresh = LOG_ZERO ;
best_word_end_hyp = NULL ;
best_score = LOG_ZERO ;
for ( int i=0 ; i<lexicon->n_models ; i++ )
{
if ( i == sent_end_index )
{
curr_word_end_hyps[i]->deactivate() ;
continue ;
}
score = curr_word_end_hyps[i]->score ;
if ( score > LOG_ZERO )
{
if ( score < temp_end_prune_thresh )
curr_word_end_hyps[i]->deactivate() ;
else
{
score += lang_model->calcLMProb( curr_word_end_hyps[i] ) ;
if ( score > best_score )
{
curr_word_end_hyps[i]->score = score ;
best_score = score ;
if ( best_word_end_hyp != NULL )
best_word_end_hyp->deactivate() ;
best_word_end_hyp = curr_word_end_hyps[i] ;
}
else
curr_word_end_hyps[i]->deactivate() ;
}
}
}
}
void BeamSearchDecoder::processWordTransitionsLM( int curr_frame )
{
real prob ;
int *pronuns , n_pronuns , n_processed ;
WordChainElem *next_word_chain_elem ;
real temp_end_prune_thresh ;
if ( best_word_end_hyp != NULL )
temp_end_prune_thresh = best_word_end_hyp->score - word_end_beam ;
else
temp_end_prune_thresh = LOG_ZERO ;
n_processed=0 ;
for ( int i=0 ; i<lexicon->n_models ; i++ )
{
if ( i == sent_end_index )
{
curr_word_end_hyps[i]->deactivate() ;
continue ;
}
if ( curr_word_end_hyps[i]->score <= LOG_ZERO )
continue ;
if ( curr_word_end_hyps[i]->score < temp_end_prune_thresh )
{
curr_word_end_hyps[i]->deactivate() ;
continue ;
}
n_processed++ ;
for ( int w=0 ; w<vocabulary->n_words ; w++ )
{
if ( (w == vocabulary->sent_end_index) && (i == sent_start_index) )
continue ;
prob = log_word_entrance_penalty + curr_word_end_hyps[i]->score +
lang_model->calcLMProb( curr_word_end_hyps[i] , w ) ;
pronuns = lexicon->lex_info->vocab_to_lex_map[w].pronuns ;
n_pronuns = lexicon->lex_info->vocab_to_lex_map[w].n_pronuns ;
next_word_chain_elem = DecodingHypothesis::word_chain_elem_pool.getElem( w ,
curr_word_end_hyps[i]->word_level_info , curr_frame ) ;
for ( int p=0 ; p<n_pronuns ; p++ )
{
if ( pronuns[p] == sent_start_index )
continue ;
if ( prob > curr_word_entry_hyps[pronuns[p]]->score )
curr_word_entry_hyps[pronuns[p]]->extendWord( prob , next_word_chain_elem ) ;
}
if ( next_word_chain_elem->n_connected <= 0 )
DecodingHypothesis::word_chain_elem_pool.returnElem( next_word_chain_elem ) ;
}
curr_word_end_hyps[i]->deactivate() ;
}
if ( verbose_mode == true )
{
fprintf( stderr , "%d " , n_processed ) ; fflush(stderr) ;
}
}
void BeamSearchDecoder::processWordTransitionsNoLM( int curr_frame )
{
// The best_word_end_hyp member points to the best word end hypothesis.
int *pronuns , n_pronuns ;
WordChainElem *next_word_chain_elem ;
real score ;
if ( verbose_mode == true )
{
fprintf( stderr , ":" ) ; fflush(stderr) ;
}
// Now extend the best word end hypothesis to the initial states of all
// words and the initial state of the sentence end word.
// If the best word end hyp was the final state of the sentence end hypothesis
// then we don't extend it to any other words.
if ( best_word_end_hyp != NULL )
{
score = best_word_end_hyp->score + log_word_entrance_penalty ;
for ( int w=0 ; w<vocabulary->n_words ; w++ )
{
if ( (w == vocabulary->sent_end_index) && (sent_start_index >= 0) &&
(best_word_end_hyp == curr_word_end_hyps[sent_start_index]) )
continue ; // A start-to-end transition is invalid
pronuns = lexicon->lex_info->vocab_to_lex_map[w].pronuns ;
n_pronuns = lexicon->lex_info->vocab_to_lex_map[w].n_pronuns ;
#ifdef DEBUG
if ( n_pronuns == 0 )
error("BeamSearchDecoder::processWordTransNoLM - voc word %d has no pronuns\n",w);
#endif
next_word_chain_elem = DecodingHypothesis::word_chain_elem_pool.getElem( w ,
best_word_end_hyp->word_level_info , curr_frame) ;
for ( int p=0 ; p<n_pronuns ; p++ )
{
if ( pronuns[p] == sent_start_index )
continue ; // Cannot make a transition to the sentence start word.
#ifdef DEBUG
// If the score of each word entry hyp at this point is not LOG_ZERO then we
// have a problem.
if ( curr_word_entry_hyps[pronuns[p]]->score > LOG_ZERO )
error("BeamSearchDecoder::processWordTransNoLM - word entry hyp not reset\n") ;
#endif
curr_word_entry_hyps[pronuns[p]]->extendWord( score , next_word_chain_elem ) ;
}
if ( next_word_chain_elem->n_connected <= 0 )
DecodingHypothesis::word_chain_elem_pool.returnElem( next_word_chain_elem ) ;
}
}
// Deactivate the best word-end hypotheses and the sentence end word-end hypothesis.
if ( best_word_end_hyp != NULL )
best_word_end_hyp->deactivate() ;
best_word_end_hyp = NULL ;
if ( sent_end_index >= 0 )
curr_word_end_hyps[sent_end_index]->deactivate() ;
}
void BeamSearchDecoder::processWordEntryHypotheses()
{
DecodingHypothesis *curr_hyp ;
short n_sucs , *sucs ;
real *suc_log_trans_probs , new_score , temp_prune_thresh ;
temp_prune_thresh = max_interior_score - word_int_beam ;
for ( int w=0 ; w<lexicon->n_models ; w++ )
{
curr_hyp = curr_word_entry_hyps[w] ;
if ( curr_hyp->score <= LOG_ZERO )
{
curr_hyp->deactivate() ;
continue ;
}
// For each successor state, s, for the initial state of word, w, is our hyposthesis
// improved if we consider the best word boundary hypothesis ?
// (ie. Is there a better path ending in state s that comes in through a word boundary?)
lexicon->getSuccessorInfo( w , 0 , &n_sucs , &sucs , &suc_log_trans_probs ) ;
for ( int s=0 ; s<n_sucs ; s++ )
{
new_score = curr_hyp->score + suc_log_trans_probs[s] ;
if ( new_score > curr_word_hyps[w][sucs[s]]->score )
{
if ( w == sent_end_index )
{
// We don't want to prune sentence end hypotheses.
curr_word_hyps[w][sucs[s]]->extendState( curr_hyp , new_score ) ;
}
else if ( new_score >= temp_prune_thresh )
{
if ( new_score > max_interior_score )
{
max_interior_score = new_score ;
temp_prune_thresh = max_interior_score - word_int_beam ;
}
curr_word_hyps[w][sucs[s]]->extendState( curr_hyp , new_score ) ;
}
}
}
curr_hyp->deactivate() ;
}
}
void BeamSearchDecoder::init()
{
short n_sucs , *sucs ;
int n_pronuns , *pronuns ;
real *suc_log_trans_probs ;
real new_score ;
WordChainElem *next_word_chain_elem ;
// Reset all hypotheses.
resetHypotheses() ;
max_interior_score = LOG_ZERO ;
best_word_end_hyp = NULL ;
// If there is a sentence start word defined, initialise just the initial state
// of the sentence start pronun.
if ( sent_start_index >= 0 )
{
next_word_chain_elem = DecodingHypothesis::word_chain_elem_pool.getElem(
vocabulary->sent_start_index , NULL , 0) ;
curr_word_hyps[sent_start_index][0]->extendWord( 0.0 , next_word_chain_elem ) ;
// Extend to the successor states of the initial state of the sentence start pronun.
lexicon->getSuccessorInfo( sent_start_index , 0 , &n_sucs , &sucs , &suc_log_trans_probs ) ;
for ( int s=0 ; s<n_sucs ; s++ )
{
new_score = curr_word_hyps[sent_start_index][0]->score + suc_log_trans_probs[s] ;
curr_word_hyps[sent_start_index][sucs[s]]->extendState(
curr_word_hyps[sent_start_index][0] , new_score ) ;
}
curr_word_hyps[sent_start_index][0]->deactivate() ;
}
else
{
// There is no sentence start pronunciation defined.
// Initialise hypotheses for the initial states of all models
// in the lexicon (except the sent end word if defined).
for ( int w=0 ; w<vocabulary->n_words ; w++ )
{
next_word_chain_elem = DecodingHypothesis::word_chain_elem_pool.getElem( w, NULL, 0 ) ;
n_pronuns = lexicon->lex_info->vocab_to_lex_map[w].n_pronuns ;
pronuns = lexicon->lex_info->vocab_to_lex_map[w].pronuns ;
for ( int p=0 ; p<n_pronuns ; p++ )
{
if ( pronuns[p] == sent_end_index )
continue ;
curr_word_hyps[pronuns[p]][0]->extendWord( 0.0 , next_word_chain_elem ) ;
}
if ( next_word_chain_elem->n_connected <= 0 )
DecodingHypothesis::word_chain_elem_pool.returnElem( next_word_chain_elem ) ;
}
// Now go through all models and extend the intial state hypotheses.
for ( int m=0 ; m<lexicon->n_models ; m++ )
{
if ( m == sent_end_index )
continue ;
lexicon->getSuccessorInfo( m , 0 , &n_sucs , &sucs , &suc_log_trans_probs ) ;
for ( int s=0 ; s<n_sucs ; s++ )
{
new_score = curr_word_hyps[m][0]->score + suc_log_trans_probs[s] ;
curr_word_hyps[m][sucs[s]]->extendState( curr_word_hyps[m][0] , new_score ) ;
}
curr_word_hyps[m][0]->deactivate() ;
}
}
}
}
|