00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "DecodeStepGeneration.h"
00023 #include "GenerationDictionary.h"
00024 #include "TranslationOption.h"
00025 #include "TranslationOptionCollection.h"
00026 #include "PartialTranslOptColl.h"
00027 #include "FactorCollection.h"
00028
00029 namespace Moses
00030 {
00031 using namespace std;
00032
00033 DecodeStepGeneration::DecodeStepGeneration(GenerationDictionary* dict,
00034 const DecodeStep* prev,
00035 const std::vector<FeatureFunction*> &features)
00036 : DecodeStep(dict, prev, features)
00037 {
00038 }
00039
00040
00041 typedef pair<Word, ScoreComponentCollection> WordPair;
00042 typedef list< WordPair > WordList;
00043
00044
00045 typedef list< WordPair >::const_iterator WordListIterator;
00046
00048 inline void IncrementIterators(vector< WordListIterator > &wordListIterVector
00049 , const vector< WordList > &wordListVector)
00050 {
00051 for (size_t currPos = 0 ; currPos < wordListVector.size() ; currPos++) {
00052 WordListIterator &iter = wordListIterVector[currPos];
00053 iter++;
00054 if (iter != wordListVector[currPos].end()) {
00055
00056 return;
00057 } else {
00058
00059 iter = wordListVector[currPos].begin();
00060 }
00061 }
00062 }
00063
00064 void DecodeStepGeneration::Process(const TranslationOption &inputPartialTranslOpt
00065 , const DecodeStep &decodeStep
00066 , PartialTranslOptColl &outputPartialTranslOptColl
00067 , TranslationOptionCollection *
00068 , bool ) const
00069 {
00070 if (inputPartialTranslOpt.GetTargetPhrase().GetSize() == 0) {
00071
00072
00073 TranslationOption *newTransOpt = new TranslationOption(inputPartialTranslOpt);
00074 outputPartialTranslOptColl.Add(newTransOpt);
00075
00076 return;
00077 }
00078
00079
00080 const GenerationDictionary* generationDictionary = decodeStep.GetGenerationDictionaryFeature();
00081
00082 const Phrase &targetPhrase = inputPartialTranslOpt.GetTargetPhrase();
00083 const InputPath &inputPath = inputPartialTranslOpt.GetInputPath();
00084 size_t targetLength = targetPhrase.GetSize();
00085
00086
00087 vector< WordList > wordListVector(targetLength);
00088
00089
00090 int wordListVectorPos = 0;
00091 for (size_t currPos = 0 ; currPos < targetLength ; currPos++) {
00092
00093 WordList &wordList = wordListVector[wordListVectorPos];
00094 const Word &word = targetPhrase.GetWord(currPos);
00095
00096
00097 const OutputWordCollection *wordColl = generationDictionary->FindWord(word);
00098
00099 if (wordColl == NULL) {
00100
00101
00102 return;
00103 } else {
00104
00105 OutputWordCollection::const_iterator iterWordColl;
00106 for (iterWordColl = wordColl->begin() ; iterWordColl != wordColl->end(); ++iterWordColl) {
00107 const Word &outputWord = (*iterWordColl).first;
00108 const ScoreComponentCollection& score = (*iterWordColl).second;
00109
00110 wordList.push_back(WordPair(outputWord, score));
00111 }
00112
00113 wordListVectorPos++;
00114 }
00115 }
00116
00117
00118
00119 size_t numIteration = 1;
00120 vector< WordListIterator > wordListIterVector(targetLength);
00121 vector< const Word* > mergeWords(targetLength);
00122 for (size_t currPos = 0 ; currPos < targetLength ; currPos++) {
00123 wordListIterVector[currPos] = wordListVector[currPos].begin();
00124 numIteration *= wordListVector[currPos].size();
00125 }
00126
00127
00128 for (size_t currIter = 0 ; currIter < numIteration ; currIter++) {
00129 ScoreComponentCollection generationScore;
00130
00131
00132 for (size_t currPos = 0 ; currPos < targetLength ; currPos++) {
00133 const WordPair &wordPair = *wordListIterVector[currPos];
00134 mergeWords[currPos] = &(wordPair.first);
00135 generationScore.PlusEquals(wordPair.second);
00136 }
00137
00138
00139 Phrase genPhrase( mergeWords);
00140
00141 if (IsFilteringStep()) {
00142 if (!inputPartialTranslOpt.IsCompatible(genPhrase, m_conflictFactors))
00143 continue;
00144 }
00145
00146 const TargetPhrase &inPhrase = inputPartialTranslOpt.GetTargetPhrase();
00147 TargetPhrase outPhrase(inPhrase);
00148 outPhrase.GetScoreBreakdown().PlusEquals(generationScore);
00149
00150 outPhrase.MergeFactors(genPhrase, m_newOutputFactors);
00151 outPhrase.EvaluateInIsolation(inputPath.GetPhrase(), m_featuresToApply);
00152
00153 const Range &sourceWordsRange = inputPartialTranslOpt.GetSourceWordsRange();
00154
00155 TranslationOption *newTransOpt = new TranslationOption(sourceWordsRange, outPhrase);
00156 assert(newTransOpt);
00157
00158 newTransOpt->SetInputPath(inputPath);
00159
00160 outputPartialTranslOptColl.Add(newTransOpt);
00161
00162
00163 IncrementIterators(wordListIterVector, wordListVector);
00164 }
00165 }
00166
00167 }
00168
00169