|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "DecodeStepGeneration.h" |
|
|
#include "GenerationDictionary.h" |
|
|
#include "TranslationOption.h" |
|
|
#include "TranslationOptionCollection.h" |
|
|
#include "PartialTranslOptColl.h" |
|
|
#include "FactorCollection.h" |
|
|
|
|
|
namespace Moses |
|
|
{ |
|
|
using namespace std; |
|
|
|
|
|
DecodeStepGeneration::DecodeStepGeneration(GenerationDictionary* dict, |
|
|
const DecodeStep* prev, |
|
|
const std::vector<FeatureFunction*> &features) |
|
|
: DecodeStep(dict, prev, features) |
|
|
{ |
|
|
} |
|
|
|
|
|
|
|
|
typedef pair<Word, ScoreComponentCollection> WordPair; |
|
|
typedef list< WordPair > WordList; |
|
|
|
|
|
|
|
|
typedef list< WordPair >::const_iterator WordListIterator; |
|
|
|
|
|
|
|
|
inline void IncrementIterators(vector< WordListIterator > &wordListIterVector |
|
|
, const vector< WordList > &wordListVector) |
|
|
{ |
|
|
for (size_t currPos = 0 ; currPos < wordListVector.size() ; currPos++) { |
|
|
WordListIterator &iter = wordListIterVector[currPos]; |
|
|
iter++; |
|
|
if (iter != wordListVector[currPos].end()) { |
|
|
|
|
|
return; |
|
|
} else { |
|
|
|
|
|
iter = wordListVector[currPos].begin(); |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
void DecodeStepGeneration::Process(const TranslationOption &inputPartialTranslOpt |
|
|
, const DecodeStep &decodeStep |
|
|
, PartialTranslOptColl &outputPartialTranslOptColl |
|
|
, TranslationOptionCollection * |
|
|
, bool ) const |
|
|
{ |
|
|
if (inputPartialTranslOpt.GetTargetPhrase().GetSize() == 0) { |
|
|
|
|
|
|
|
|
TranslationOption *newTransOpt = new TranslationOption(inputPartialTranslOpt); |
|
|
outputPartialTranslOptColl.Add(newTransOpt); |
|
|
|
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
const GenerationDictionary* generationDictionary = decodeStep.GetGenerationDictionaryFeature(); |
|
|
|
|
|
const Phrase &targetPhrase = inputPartialTranslOpt.GetTargetPhrase(); |
|
|
const InputPath &inputPath = inputPartialTranslOpt.GetInputPath(); |
|
|
size_t targetLength = targetPhrase.GetSize(); |
|
|
|
|
|
|
|
|
vector< WordList > wordListVector(targetLength); |
|
|
|
|
|
|
|
|
int wordListVectorPos = 0; |
|
|
for (size_t currPos = 0 ; currPos < targetLength ; currPos++) { |
|
|
|
|
|
WordList &wordList = wordListVector[wordListVectorPos]; |
|
|
const Word &word = targetPhrase.GetWord(currPos); |
|
|
|
|
|
|
|
|
const OutputWordCollection *wordColl = generationDictionary->FindWord(word); |
|
|
|
|
|
if (wordColl == NULL) { |
|
|
|
|
|
|
|
|
return; |
|
|
} else { |
|
|
|
|
|
OutputWordCollection::const_iterator iterWordColl; |
|
|
for (iterWordColl = wordColl->begin() ; iterWordColl != wordColl->end(); ++iterWordColl) { |
|
|
const Word &outputWord = (*iterWordColl).first; |
|
|
const ScoreComponentCollection& score = (*iterWordColl).second; |
|
|
|
|
|
wordList.push_back(WordPair(outputWord, score)); |
|
|
} |
|
|
|
|
|
wordListVectorPos++; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
size_t numIteration = 1; |
|
|
vector< WordListIterator > wordListIterVector(targetLength); |
|
|
vector< const Word* > mergeWords(targetLength); |
|
|
for (size_t currPos = 0 ; currPos < targetLength ; currPos++) { |
|
|
wordListIterVector[currPos] = wordListVector[currPos].begin(); |
|
|
numIteration *= wordListVector[currPos].size(); |
|
|
} |
|
|
|
|
|
|
|
|
for (size_t currIter = 0 ; currIter < numIteration ; currIter++) { |
|
|
ScoreComponentCollection generationScore; |
|
|
|
|
|
|
|
|
for (size_t currPos = 0 ; currPos < targetLength ; currPos++) { |
|
|
const WordPair &wordPair = *wordListIterVector[currPos]; |
|
|
mergeWords[currPos] = &(wordPair.first); |
|
|
generationScore.PlusEquals(wordPair.second); |
|
|
} |
|
|
|
|
|
|
|
|
Phrase genPhrase( mergeWords); |
|
|
|
|
|
if (IsFilteringStep()) { |
|
|
if (!inputPartialTranslOpt.IsCompatible(genPhrase, m_conflictFactors)) |
|
|
continue; |
|
|
} |
|
|
|
|
|
const TargetPhrase &inPhrase = inputPartialTranslOpt.GetTargetPhrase(); |
|
|
TargetPhrase outPhrase(inPhrase); |
|
|
outPhrase.GetScoreBreakdown().PlusEquals(generationScore); |
|
|
|
|
|
outPhrase.MergeFactors(genPhrase, m_newOutputFactors); |
|
|
outPhrase.EvaluateInIsolation(inputPath.GetPhrase(), m_featuresToApply); |
|
|
|
|
|
const Range &sourceWordsRange = inputPartialTranslOpt.GetSourceWordsRange(); |
|
|
|
|
|
TranslationOption *newTransOpt = new TranslationOption(sourceWordsRange, outPhrase); |
|
|
assert(newTransOpt); |
|
|
|
|
|
newTransOpt->SetInputPath(inputPath); |
|
|
|
|
|
outputPartialTranslOptColl.Add(newTransOpt); |
|
|
|
|
|
|
|
|
IncrementIterators(wordListIterVector, wordListVector); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|