src/Generic/names/IdFDecoder.cpp
/**
 * Walks backward through sentence, doing a beam search. Final results end up in 
 * _currentTheories. Pruning governed by BACKWARD_PRUNING_THRESHOLD and 
 * BACKWARD_BEAM_WIDTH.
 */
Walks backward through sentence, doing a beam search. Final results end up in
_currentTheories.
Walks backward through sentence doing a beam search . Final results end up in _currentTheories .
void IdFDecoder::walkBackwardThroughSentence(IdFSentenceTokens* sentenceTokens) {
	double alpha_score, word_prob, tag_prob;
	int tag, tag_plus_one;
	Symbol word, word_plus_one;
	_previousTheories[0] = new IdFSentenceTheory(sentenceTokens->getLength(),
		_nameClassTags->getNoneStartTagIndex());
	_numPreviousTheories = 1;
	word = sentenceTokens->getWord(sentenceTokens->getLength() - 1);
	if (!_model->wordIsInVocab(word)) {
		word = _model->getWordFeatures(word, false, _model->normalizedWordIsInVocab(word));
	}
	for (tag = 0; tag < _nameClassTags->getNumRegularTags(); tag++) {
		alpha_score = _alphaProbs[tag][sentenceTokens->getLength() - 1];
		if (alpha_score <= _model->LOG_OF_ZERO)
			continue;
		tag_prob = _model->getTagProbability(_nameClassTags->getSentenceEndIndex(), 
			tag, word);
		addCurrentTheory(_previousTheories[0], sentenceTokens->getLength() - 1, 
			tag, alpha_score, tag_prob);
	}
	transferCurrentTheoriesToPrevious();
	word_plus_one = word;
	for (int sent_index = sentenceTokens->getLength() - 2; sent_index >= 0; sent_index--) {
		word_plus_one = word;
		word = sentenceTokens->getWord(sent_index);
		if (!_model->wordIsInVocab(word)) {
			word = _model->getWordFeatures(word, sent_index == 0, 
				_model->normalizedWordIsInVocab(word));
		}		
		for (int t = 0; t < _numPreviousTheories; t++) {
			tag_plus_one = _previousTheories[t]->getTag(sent_index + 1);
			for (tag = 0; tag < _nameClassTags->getNumRegularTags(); tag++) {
				alpha_score = _alphaProbs[tag][sent_index];
				if (alpha_score <= _model->LOG_OF_ZERO)
					continue;
				word_prob = _model->getWordProbability(word_plus_one,
					tag_plus_one, tag, word);
				tag_prob = _model->getTagProbability(tag_plus_one, tag, word);
				addCurrentTheory(_previousTheories[t], 
					sent_index, tag, alpha_score, word_prob + tag_prob);
			}
		}
		transferCurrentTheoriesToPrevious();
	}
	for (int i = 0; i < _numPreviousTheories; i++) {
		IdFSentenceTheory *theory = new IdFSentenceTheory(_previousTheories[i]);
		word_prob = _model->getWordProbability(word,
					_previousTheories[i]->getTag(0),
					_nameClassTags->getSentenceStartIndex(),
					_nameClassTags->getSentenceStartTag());
		tag_prob = _model->getTagProbability(_previousTheories[i]->getTag(0),
					_nameClassTags->getSentenceStartIndex(),
					_nameClassTags->getSentenceStartTag());
		theory->incrementBetaScore(word_prob + tag_prob);
		theory->setBestPossibleScore(theory->getBetaScore());
		_currentTheories[_numCurrentTheories] = theory;
		_numCurrentTheories++;
		delete _previousTheories[i];
		_previousTheories[i] = 0;
	}
	_numPreviousTheories = 0;
}
void IdFDecoder :: walkBackwardThroughSentence ( IdFSentenceTokens * sentenceTokens ) { double alpha_score , word_prob , tag_prob ; int tag , tag_plus_one ; Symbol word , word_plus_one ; _previousTheories [ 0 ] = new IdFSentenceTheory ( sentenceTokens -> getLength ( ) , _nameClassTags -> getNoneStartTagIndex ( ) ) ; _numPreviousTheories = 1 ; word = sentenceTokens -> getWord ( sentenceTokens -> getLength ( ) - 1 ) ; if ( ! _model -> wordIsInVocab ( word ) ) { word = _model -> getWordFeatures ( word , false , _model -> normalizedWordIsInVocab ( word ) ) ; } for ( tag = 0 ; tag < _nameClassTags -> getNumRegularTags ( ) ; tag ++ ) { alpha_score = _alphaProbs [ tag ] [ sentenceTokens -> getLength ( ) - 1 ] ; if ( alpha_score <= _model -> LOG_OF_ZERO ) continue ; tag_prob = _model -> getTagProbability ( _nameClassTags -> getSentenceEndIndex ( ) , tag , word ) ; addCurrentTheory ( _previousTheories [ 0 ] , sentenceTokens -> getLength ( ) - 1 , tag , alpha_score , tag_prob ) ; } transferCurrentTheoriesToPrevious ( ) ; word_plus_one = word ; for ( int sent_index = sentenceTokens -> getLength ( ) - 2 ; sent_index >= 0 ; sent_index -- ) { word_plus_one = word ; word = sentenceTokens -> getWord ( sent_index ) ; if ( ! _model -> wordIsInVocab ( word ) ) { word = _model -> getWordFeatures ( word , sent_index == 0 , _model -> normalizedWordIsInVocab ( word ) ) ; } for ( int t = 0 ; t < _numPreviousTheories ; t ++ ) { tag_plus_one = _previousTheories [ t ] -> getTag ( sent_index + 1 ) ; for ( tag = 0 ; tag < _nameClassTags -> getNumRegularTags ( ) ; tag ++ ) { alpha_score = _alphaProbs [ tag ] [ sent_index ] ; if ( alpha_score <= _model -> LOG_OF_ZERO ) continue ; word_prob = _model -> getWordProbability ( word_plus_one , tag_plus_one , tag , word ) ; tag_prob = _model -> getTagProbability ( tag_plus_one , tag , word ) ; addCurrentTheory ( _previousTheories [ t ] , sent_index , tag , alpha_score , word_prob + tag_prob ) ; } } transferCurrentTheoriesToPrevious ( ) ; } for ( int i = 0 ; i < _numPreviousTheories ; i ++ ) { IdFSentenceTheory * theory = new IdFSentenceTheory ( _previousTheories [ i ] ) ; word_prob = _model -> getWordProbability ( word , _previousTheories [ i ] -> getTag ( 0 ) , _nameClassTags -> getSentenceStartIndex ( ) , _nameClassTags -> getSentenceStartTag ( ) ) ; tag_prob = _model -> getTagProbability ( _previousTheories [ i ] -> getTag ( 0 ) , _nameClassTags -> getSentenceStartIndex ( ) , _nameClassTags -> getSentenceStartTag ( ) ) ; theory -> incrementBetaScore ( word_prob + tag_prob ) ; theory -> setBestPossibleScore ( theory -> getBetaScore ( ) ) ; _currentTheories [ _numCurrentTheories ] = theory ; _numCurrentTheories ++ ; delete _previousTheories [ i ] ; _previousTheories [ i ] = 0 ; } _numPreviousTheories = 0 ; }
Walks backward through sentence, doing a beam search.
Walks backward through sentence doing a beam search .
// generate end tag
 // we're going backwards
 // generate each word and its tag
