diff options
author | Hieu Hoang <hieuhoang@gmail.com> | 2016-06-28 12:22:46 +0300 |
---|---|---|
committer | Hieu Hoang <hieuhoang@gmail.com> | 2016-06-28 12:28:19 +0300 |
commit | cef30724d5fc48c0a4258cf67c95f22912603b00 (patch) | |
tree | 9931646a12bd24d7d0d36cd44fdea5dcf85abeb3 /moses/TranslationModel | |
parent | aca2db894cd7180ae95836ef6baa729e75047574 (diff) |
move Moses classes out of OnDiskPt - TargetPhrase
Diffstat (limited to 'moses/TranslationModel')
-rw-r--r-- | moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.cpp | 71 | ||||
-rw-r--r-- | moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.h | 8 |
2 files changed, 78 insertions, 1 deletions
diff --git a/moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.cpp b/moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.cpp index 9ca2c9b0b..06c3bd262 100644 --- a/moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.cpp +++ b/moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.cpp @@ -230,7 +230,7 @@ PhraseDictionaryOnDisk::ConvertToMoses( for (size_t i = 0; i < targetPhrasesOnDisk->GetSize(); ++i) { const OnDiskPt::TargetPhrase &tp = targetPhrasesOnDisk->GetTargetPhrase(i); Moses::TargetPhrase *mosesPhrase - = tp.ConvertToMoses(inputFactors, outputFactors, vocab, + = ConvertToMoses(tp, inputFactors, outputFactors, vocab, phraseDict, weightT, isSyntax); /* @@ -248,6 +248,75 @@ PhraseDictionaryOnDisk::ConvertToMoses( return ret; } +Moses::TargetPhrase *PhraseDictionaryOnDisk::ConvertToMoses(const OnDiskPt::TargetPhrase &targetPhraseOnDisk + , const std::vector<Moses::FactorType> &inputFactors + , const std::vector<Moses::FactorType> &outputFactors + , const OnDiskPt::Vocab &vocab + , const Moses::PhraseDictionary &phraseDict + , const std::vector<float> &weightT + , bool isSyntax) const +{ + Moses::TargetPhrase *ret = new Moses::TargetPhrase(&phraseDict); + + // words + size_t phraseSize = targetPhraseOnDisk.GetSize(); + UTIL_THROW_IF2(phraseSize == 0, "Target phrase cannot be empty"); // last word is lhs + if (isSyntax) { + --phraseSize; + } + + for (size_t pos = 0; pos < phraseSize; ++pos) { + targetPhraseOnDisk.GetWord(pos).ConvertToMoses(outputFactors, vocab, ret->AddWord()); + } + + // alignments + // int index = 0; + Moses::AlignmentInfo::CollType alignTerm, alignNonTerm; + std::set<std::pair<size_t, size_t> > alignmentInfo; + const OnDiskPt::PhrasePtr sp = targetPhraseOnDisk.GetSourcePhrase(); + for (size_t ind = 0; ind < targetPhraseOnDisk.GetAlign().size(); ++ind) { + const std::pair<size_t, size_t> &entry = targetPhraseOnDisk.GetAlign()[ind]; + alignmentInfo.insert(entry); + size_t sourcePos = entry.first; + size_t targetPos = entry.second; + + if (targetPhraseOnDisk.GetWord(targetPos).IsNonTerminal()) { + alignNonTerm.insert(std::pair<size_t,size_t>(sourcePos, targetPos)); + } else { + alignTerm.insert(std::pair<size_t,size_t>(sourcePos, targetPos)); + } + + } + ret->SetAlignTerm(alignTerm); + ret->SetAlignNonTerm(alignNonTerm); + + if (isSyntax) { + Moses::Word *lhsTarget = new Moses::Word(true); + targetPhraseOnDisk.GetWord(targetPhraseOnDisk.GetSize() - 1).ConvertToMoses(outputFactors, vocab, *lhsTarget); + ret->SetTargetLHS(lhsTarget); + } + + // set source phrase + Moses::Phrase mosesSP(Moses::Input); + for (size_t pos = 0; pos < sp->GetSize(); ++pos) { + sp->GetWord(pos).ConvertToMoses(inputFactors, vocab, mosesSP.AddWord()); + } + + // scores + ret->GetScoreBreakdown().Assign(&phraseDict, targetPhraseOnDisk.GetScores()); + + // sparse features + ret->GetScoreBreakdown().Assign(&phraseDict, targetPhraseOnDisk.GetSparseFeatures()); + + // property + ret->SetProperties(targetPhraseOnDisk.GetProperty()); + + ret->EvaluateInIsolation(mosesSP, phraseDict.GetFeaturesToApply()); + + return ret; + +} + void PhraseDictionaryOnDisk::SetParameter(const std::string& key, const std::string& value) { if (key == "max-span-default") { diff --git a/moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.h b/moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.h index 7a0c06c4a..f3c923b72 100644 --- a/moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.h +++ b/moses/TranslationModel/RuleTable/PhraseDictionaryOnDisk.h @@ -64,6 +64,14 @@ protected: void GetTargetPhraseCollectionBatch(InputPath &inputPath) const; + Moses::TargetPhrase *ConvertToMoses(const OnDiskPt::TargetPhrase &targetPhraseOnDisk + , const std::vector<Moses::FactorType> &inputFactors + , const std::vector<Moses::FactorType> &outputFactors + , const OnDiskPt::Vocab &vocab + , const Moses::PhraseDictionary &phraseDict + , const std::vector<float> &weightT + , bool isSyntax) const; + public: PhraseDictionaryOnDisk(const std::string &line); ~PhraseDictionaryOnDisk(); |