00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "DecodeStepTranslation.h"
00023 #include "TranslationOption.h"
00024 #include "TranslationOptionCollection.h"
00025 #include "PartialTranslOptColl.h"
00026 #include "FactorCollection.h"
00027 #include "util/exception.hh"
00028
00029 using namespace std;
00030
00031 namespace Moses
00032 {
00033 DecodeStepTranslation::DecodeStepTranslation(PhraseDictionary* pdf,
00034 const DecodeStep* prev,
00035 const std::vector<FeatureFunction*> &features)
00036 : DecodeStep(pdf, prev, features)
00037 {
00038
00039
00040 const std::vector<FeatureFunction*> &pdfFeatures = pdf->GetFeaturesToApply();
00041 for (size_t i = 0; i < pdfFeatures.size(); ++i) {
00042 FeatureFunction *ff = pdfFeatures[i];
00043 RemoveFeature(ff);
00044 }
00045 }
00046
00047 void DecodeStepTranslation::Process(const TranslationOption &inputPartialTranslOpt
00048 , const DecodeStep &decodeStep
00049 , PartialTranslOptColl &outputPartialTranslOptColl
00050 , TranslationOptionCollection *toc
00051 , bool adhereTableLimit
00052 , TargetPhraseCollection::shared_ptr phraseColl) const
00053 {
00054 if (inputPartialTranslOpt.GetTargetPhrase().GetSize() == 0) {
00055
00056 outputPartialTranslOptColl.Add(new TranslationOption(inputPartialTranslOpt));
00057 return;
00058 }
00059
00060
00061 const Range &sourceWordsRange = inputPartialTranslOpt.GetSourceWordsRange();
00062 const InputPath &inputPath = inputPartialTranslOpt.GetInputPath();
00063 const PhraseDictionary* phraseDictionary =
00064 decodeStep.GetPhraseDictionaryFeature();
00065 const TargetPhrase &inPhrase = inputPartialTranslOpt.GetTargetPhrase();
00066 const size_t currSize = inPhrase.GetSize();
00067 const size_t tableLimit = phraseDictionary->GetTableLimit();
00068
00069 if (phraseColl != NULL) {
00070 TargetPhraseCollection::const_iterator iterTargetPhrase, iterEnd;
00071 iterEnd = (!adhereTableLimit || tableLimit == 0 || phraseColl->GetSize() < tableLimit) ? phraseColl->end() : phraseColl->begin() + tableLimit;
00072
00073 for (iterTargetPhrase = phraseColl->begin(); iterTargetPhrase != iterEnd; ++iterTargetPhrase) {
00074 const TargetPhrase& targetPhrase = **iterTargetPhrase;
00075
00076
00077 if (targetPhrase.GetSize() != currSize) continue;
00078
00079 TargetPhrase outPhrase(inPhrase);
00080
00081 if (IsFilteringStep()) {
00082 if (!inputPartialTranslOpt.IsCompatible(targetPhrase, m_conflictFactors))
00083 continue;
00084 }
00085
00086 outPhrase.Merge(targetPhrase, m_newOutputFactors);
00087 outPhrase.EvaluateInIsolation(inputPath.GetPhrase(), m_featuresToApply);
00088
00089 TranslationOption *newTransOpt = new TranslationOption(sourceWordsRange, outPhrase);
00090 assert(newTransOpt != NULL);
00091
00092 newTransOpt->SetInputPath(inputPath);
00093
00094 outputPartialTranslOptColl.Add(newTransOpt );
00095
00096 }
00097 } else if (sourceWordsRange.GetNumWordsCovered() == 1) {
00098
00099
00100 }
00101 }
00102
00103 void
00104 DecodeStepTranslation::
00105 ProcessInitialTranslation(InputType const& source,
00106 PartialTranslOptColl &outputPartialTranslOptColl,
00107 size_t startPos, size_t endPos,
00108 bool adhereTableLimit,
00109 InputPath const& inputPath,
00110 TargetPhraseCollection::shared_ptr phraseColl) const
00111 {
00112 const PhraseDictionary* phraseDictionary = GetPhraseDictionaryFeature();
00113 const size_t tableLimit = phraseDictionary->GetTableLimit();
00114
00115 const Range range(startPos, endPos);
00116
00117 if (phraseColl != NULL) {
00118 IFVERBOSE(3) {
00119 if(source.GetType() == SentenceInput)
00120 TRACE_ERR("[" << source.GetSubString(range) << "; "
00121 << startPos << "-" << endPos << "]\n");
00122 else
00123 TRACE_ERR("[" << startPos << "-" << endPos << "]" << std::endl);
00124 }
00125
00126 TargetPhraseCollection::const_iterator iterTargetPhrase, iterEnd;
00127 iterEnd = (!adhereTableLimit || tableLimit == 0 || phraseColl->GetSize() < tableLimit) ? phraseColl->end() : phraseColl->begin() + tableLimit;
00128
00129 for (iterTargetPhrase = phraseColl->begin() ; iterTargetPhrase != iterEnd ; ++iterTargetPhrase) {
00130 const TargetPhrase &targetPhrase = **iterTargetPhrase;
00131 TranslationOption *transOpt = new TranslationOption(range, targetPhrase);
00132
00133 transOpt->SetInputPath(inputPath);
00134
00135 outputPartialTranslOptColl.Add (transOpt);
00136
00137 VERBOSE(3,"\t" << targetPhrase << "\n");
00138 }
00139 VERBOSE(3,std::endl);
00140 }
00141 }
00142
00143 void
00144 DecodeStepTranslation::
00145 ProcessInitialTransLEGACY(InputType const& source,
00146 PartialTranslOptColl &outputPartialTranslOptColl,
00147 size_t startPos, size_t endPos,
00148 bool adhereTableLimit,
00149 InputPathList const& inputPathList) const
00150 {
00151 const PhraseDictionary* phraseDictionary = GetPhraseDictionaryFeature();
00152 const size_t tableLimit = phraseDictionary->GetTableLimit();
00153
00154 const Range range(startPos, endPos);
00155 TargetPhraseCollectionWithSourcePhrase::shared_ptr phraseColl
00156 = phraseDictionary->GetTargetPhraseCollectionLEGACY(source,range);
00157
00158 if (phraseColl != NULL) {
00159 IFVERBOSE(3) {
00160 if(source.GetType() == SentenceInput)
00161 TRACE_ERR("[" << source.GetSubString(range) << "; "
00162 << startPos << "-" << endPos << "]\n");
00163 else
00164 TRACE_ERR("[" << startPos << "-" << endPos << "]" << std::endl);
00165 }
00166
00167 const std::vector<Phrase> &sourcePhrases = phraseColl->GetSourcePhrases();
00168
00169 TargetPhraseCollection::const_iterator iterTargetPhrase, iterEnd;
00170 std::vector<Phrase>::const_iterator iterSourcePhrase;
00171 iterEnd = (!adhereTableLimit || tableLimit == 0 || phraseColl->GetSize() < tableLimit) ? phraseColl->end() : phraseColl->begin() + tableLimit;
00172
00173 for (iterTargetPhrase = phraseColl->begin(), iterSourcePhrase = sourcePhrases.begin()
00174 ; iterTargetPhrase != iterEnd
00175 ; ++iterTargetPhrase, ++iterSourcePhrase) {
00176 assert(iterSourcePhrase != sourcePhrases.end());
00177
00178 const TargetPhrase &targetPhrase = **iterTargetPhrase;
00179 const Phrase &sourcePhrase = *iterSourcePhrase;
00180
00181 const InputPath &inputPath = GetInputPathLEGACY(targetPhrase, sourcePhrase, inputPathList);
00182
00183 TranslationOption *transOpt = new TranslationOption(range, targetPhrase);
00184 transOpt->SetInputPath(inputPath);
00185
00186 outputPartialTranslOptColl.Add (transOpt);
00187
00188 VERBOSE(3,"\t" << targetPhrase << "\n");
00189 }
00190 VERBOSE(3,std::endl);
00191 }
00192 }
00193
00194 const InputPath &DecodeStepTranslation::GetInputPathLEGACY(
00195 const TargetPhrase targetPhrase,
00196 const Phrase sourcePhrase,
00197 const InputPathList &inputPathList) const
00198 {
00199 const Word &wordFromPt = sourcePhrase.GetWord(0);
00200
00201 InputPathList::const_iterator iter;
00202 for (iter = inputPathList.begin(); iter != inputPathList.end(); ++iter) {
00203 const InputPath &inputPath = **iter;
00204 const Phrase &phraseFromIP = inputPath.GetPhrase();
00205
00206 const Word *wordIP = NULL;
00207 for (size_t i = 0; i < phraseFromIP.GetSize(); ++i) {
00208 const Word &tempWord = phraseFromIP.GetWord(i);
00209 if (!tempWord.IsEpsilon()) {
00210 wordIP = &tempWord;
00211 break;
00212 }
00213 }
00214
00215
00216
00217 if (wordIP && *wordIP == wordFromPt) {
00218 return inputPath;
00219 }
00220 }
00221
00222 UTIL_THROW(util::Exception, "Input path not found");
00223 }
00224
00225 void
00226 DecodeStepTranslation::
00227 ProcessLEGACY(TranslationOption const& in,
00228 DecodeStep const& decodeStep,
00229 PartialTranslOptColl &out,
00230 TranslationOptionCollection *toc,
00231 bool adhereTableLimit) const
00232 {
00233 if (in.GetTargetPhrase().GetSize() == 0) {
00234
00235 out.Add(new TranslationOption(in));
00236 return;
00237 }
00238
00239
00240 Range const& srcRange = in.GetSourceWordsRange();
00241 InputPath const& inputPath = in.GetInputPath();
00242 PhraseDictionary const* pdict = decodeStep.GetPhraseDictionaryFeature();
00243 TargetPhrase const& inPhrase = in.GetTargetPhrase();
00244 size_t const currSize = inPhrase.GetSize();
00245 size_t const tableLimit = pdict->GetTableLimit();
00246
00247 TargetPhraseCollectionWithSourcePhrase::shared_ptr phraseColl
00248 = pdict->GetTargetPhraseCollectionLEGACY(toc->GetSource(),srcRange);
00249
00250 if (phraseColl != NULL) {
00251 TargetPhraseCollection::const_iterator iterTargetPhrase, iterEnd;
00252 iterEnd = ((adhereTableLimit && tableLimit && phraseColl->GetSize() >= tableLimit)
00253 ? phraseColl->begin() + tableLimit : phraseColl->end());
00254
00255 for (iterTargetPhrase = phraseColl->begin();
00256 iterTargetPhrase != iterEnd;
00257 ++iterTargetPhrase) {
00258 TargetPhrase const& targetPhrase = **iterTargetPhrase;
00259 if (targetPhrase.GetSize() != currSize ||
00260 (IsFilteringStep() && !in.IsCompatible(targetPhrase, m_conflictFactors)))
00261 continue;
00262
00263 TargetPhrase outPhrase(inPhrase);
00264 outPhrase.Merge(targetPhrase, m_newOutputFactors);
00265 outPhrase.EvaluateInIsolation(inputPath.GetPhrase(), m_featuresToApply);
00266
00267 TranslationOption *newTransOpt = new TranslationOption(srcRange, outPhrase);
00268 assert(newTransOpt != NULL);
00269
00270 newTransOpt->SetInputPath(inputPath);
00271
00272 out.Add(newTransOpt);
00273
00274 }
00275 }
00276 }
00277 }
00278
00279
00280