diff --git a/moses-cmd/IOWrapper.cpp b/moses-cmd/IOWrapper.cpp index daebcebdf..37398128a 100644 --- a/moses-cmd/IOWrapper.cpp +++ b/moses-cmd/IOWrapper.cpp @@ -348,7 +348,7 @@ void OutputBestHypo(const std::vector& mbrBestHypo, long /*translationId* void OutputInput(std::vector& map, const Hypothesis* hypo) { if (hypo->GetPrevHypo()) { - OutputInput(map, hypo->GetPrevHypo()); + OutputInput(map, hypo->GetPrevHypo()); map[hypo->GetCurrSourceWordsRange().GetStartPos()] = &hypo->GetTranslationOption().GetSourcePhrase(); } } diff --git a/moses/Search.cpp b/moses/Search.cpp index bfeec677b..d3403461b 100644 --- a/moses/Search.cpp +++ b/moses/Search.cpp @@ -7,6 +7,15 @@ namespace Moses { +Search::Search(Manager& manager) +: m_manager(manager) +,m_sourcePhrase(0) +,m_initialTransOpt() +{ + m_initialTransOpt.SetSourcePhrase(m_sourcePhrase); +} + + Search *Search::CreateSearch(Manager& manager, const InputType &source, SearchAlgorithm searchAlgorithm, const TranslationOptionCollection &transOptColl) { diff --git a/moses/Search.h b/moses/Search.h index ed1775fe4..71f70e243 100644 --- a/moses/Search.h +++ b/moses/Search.h @@ -3,6 +3,8 @@ #include #include "TypeDef.h" +#include "TranslationOption.h" +#include "Phrase.h" namespace Moses { @@ -29,7 +31,7 @@ public: //! Decode the sentence according to the specified search algorithm. virtual void ProcessSentence() = 0; - explicit Search(Manager& manager) : m_manager(manager) {} + explicit Search(Manager& manager); virtual ~Search() {} // Factory method @@ -39,6 +41,8 @@ public: protected: const Phrase *m_constraint; Manager& m_manager; + Phrase m_sourcePhrase; // for initial hypo + TranslationOption m_initialTransOpt; /**< used to seed 1st hypo */ }; } diff --git a/moses/SearchCubePruning.cpp b/moses/SearchCubePruning.cpp index e7ecdf82b..e3f47ee43 100644 --- a/moses/SearchCubePruning.cpp +++ b/moses/SearchCubePruning.cpp @@ -41,7 +41,6 @@ SearchCubePruning::SearchCubePruning(Manager& manager, const InputType &source, :Search(manager) ,m_source(source) ,m_hypoStackColl(source.GetSize() + 1) - ,m_initialTransOpt() ,m_start(clock()) ,m_transOptColl(transOptColl) { diff --git a/moses/SearchCubePruning.h b/moses/SearchCubePruning.h index 3c3971405..e00ae4409 100644 --- a/moses/SearchCubePruning.h +++ b/moses/SearchCubePruning.h @@ -20,7 +20,6 @@ protected: const InputType &m_source; std::vector < HypothesisStack* > m_hypoStackColl; /**< stacks to store hypotheses (partial translations) */ // no of elements = no of words in source + 1 - TranslationOption m_initialTransOpt; /**< used to seed 1st hypo */ clock_t m_start; /**< used to track time spend on translation */ const TranslationOptionCollection &m_transOptColl; /**< pre-computed list of translation options for the phrases in this sentence */ diff --git a/moses/SearchNormal.cpp b/moses/SearchNormal.cpp index b1eda987e..eb7d8710a 100644 --- a/moses/SearchNormal.cpp +++ b/moses/SearchNormal.cpp @@ -16,7 +16,6 @@ SearchNormal::SearchNormal(Manager& manager, const InputType &source, const Tran :Search(manager) ,m_source(source) ,m_hypoStackColl(source.GetSize() + 1) - ,m_initialTransOpt() ,m_start(clock()) ,interrupted_flag(0) ,m_transOptColl(transOptColl) diff --git a/moses/SearchNormal.h b/moses/SearchNormal.h index e17efec1a..c55caa58d 100644 --- a/moses/SearchNormal.h +++ b/moses/SearchNormal.h @@ -6,7 +6,6 @@ #include "HypothesisStackNormal.h" #include "TranslationOptionCollection.h" #include "Timer.h" -#include "TranslationOption.h" namespace Moses { @@ -24,7 +23,6 @@ protected: const InputType &m_source; std::vector < HypothesisStack* > m_hypoStackColl; /**< stacks to store hypotheses (partial translations) */ // no of elements = no of words in source + 1 - TranslationOption m_initialTransOpt; /**< used to seed 1st hypo */ clock_t m_start; /**< starting time, used for logging */ size_t interrupted_flag; /**< flag indicating that decoder ran out of time (see switch -time-out) */ HypothesisStackNormal* actual_hypoStack; /**actual (full expanded) stack of hypotheses*/ diff --git a/moses/SentenceStats.cpp b/moses/SentenceStats.cpp index 28183e2bc..247ff9350 100644 --- a/moses/SentenceStats.cpp +++ b/moses/SentenceStats.cpp @@ -44,7 +44,8 @@ void SentenceStats::AddDeletedWords(const Hypothesis& hypo) //don't check either a null pointer or the empty initial hypothesis (if we were given the empty hypo, the null check will save us) if(hypo.GetPrevHypo() != NULL && hypo.GetPrevHypo()->GetCurrSourceWordsRange().GetNumWordsCovered() > 0) AddDeletedWords(*hypo.GetPrevHypo()); - if(hypo.GetCurrTargetWordsRange().GetNumWordsCovered() == 0) { + if(hypo.GetPrevHypo() && hypo.GetCurrTargetWordsRange().GetNumWordsCovered() == 0) { + m_deletedWords.push_back(&hypo.GetTranslationOption().GetSourcePhrase()); } } diff --git a/moses/TranslationOption.cpp b/moses/TranslationOption.cpp index ff77216be..42761f01b 100644 --- a/moses/TranslationOption.cpp +++ b/moses/TranslationOption.cpp @@ -34,6 +34,7 @@ namespace Moses TranslationOption::TranslationOption() :m_targetPhrase() +,m_sourcePhrase(NULL) ,m_sourceWordsRange(NOT_FOUND, NOT_FOUND) { } @@ -42,19 +43,12 @@ TranslationOption::TranslationOption() TranslationOption::TranslationOption(const WordsRange &wordsRange , const TargetPhrase &targetPhrase) : m_targetPhrase(targetPhrase) + , m_sourcePhrase(NULL) , m_sourceWordsRange(wordsRange) , m_futureScore(targetPhrase.GetFutureScore()) { } -TranslationOption::TranslationOption(const TranslationOption ©, const WordsRange &sourceWordsRange) - : m_targetPhrase(copy.m_targetPhrase) -//, m_sourcePhrase(new Phrase(*copy.m_sourcePhrase)) // TODO use when confusion network trans opt for confusion net properly implemented - , m_sourceWordsRange(sourceWordsRange) - , m_futureScore(copy.m_futureScore) - , m_lexReorderingScores(copy.m_lexReorderingScores) -{} - bool TranslationOption::IsCompatible(const Phrase& phrase, const std::vector& featuresToCheck) const { if (featuresToCheck.size() == 1) { @@ -83,6 +77,13 @@ void TranslationOption::Evaluate(const InputType &source) m_targetPhrase.Evaluate(source); } +const Phrase &TranslationOption::GetSourcePhrase() const +{ + CHECK(m_sourcePhrase); + return *m_sourcePhrase; +} + + TO_STRING_BODY(TranslationOption); // friend diff --git a/moses/TranslationOption.h b/moses/TranslationOption.h index 3279759c9..e7125a3dd 100644 --- a/moses/TranslationOption.h +++ b/moses/TranslationOption.h @@ -66,6 +66,7 @@ class TranslationOption protected: TargetPhrase m_targetPhrase; /*< output phrase when using this translation option */ + const Phrase *m_sourcePhrase; const WordsRange m_sourceWordsRange; /*< word position in the input that are covered by this translation option */ float m_futureScore; /*< estimate of total cost when using this translation option, includes language model probabilities */ @@ -73,16 +74,12 @@ protected: _ScoreCacheMap m_lexReorderingScores; public: - explicit TranslationOption(); // For initial hypo that does translate anything + explicit TranslationOption(); // For initial hypo that does translate nothing /** constructor. Used by initial translation step */ TranslationOption(const WordsRange &wordsRange , const TargetPhrase &targetPhrase); - /** copy constructor, but change words range. used by caching */ - TranslationOption(const TranslationOption ©, const WordsRange &sourceWordsRange); - - /** returns true if all feature types in featuresToCheck are compatible between the two phrases */ bool IsCompatible(const Phrase& phrase, const std::vector& featuresToCheck) const; @@ -97,13 +94,11 @@ public: } /** returns source phrase */ - const Phrase &GetSourcePhrase() const { - return m_targetPhrase.GetSourcePhrase(); - } + const Phrase &GetSourcePhrase() const; void SetSourcePhrase(const Phrase &sourcePhrase) { - // TODO + m_sourcePhrase = &sourcePhrase; } /** whether source span overlaps with those of a hypothesis */ diff --git a/moses/TranslationOptionCollection.cpp b/moses/TranslationOptionCollection.cpp index cf14e7e27..e4c5ec522 100644 --- a/moses/TranslationOptionCollection.cpp +++ b/moses/TranslationOptionCollection.cpp @@ -226,13 +226,7 @@ void TranslationOptionCollection::ProcessOneUnknownWord(const Word &sourceWord,s // modify the starting bitmap } - Phrase* m_unksrc = new Phrase(1); - m_unksrc->AddWord() = sourceWord; - m_unksrcs.push_back(m_unksrc); - - TranslationOption *transOpt; TargetPhrase targetPhrase; - targetPhrase.SetSourcePhrase(*m_unksrc); if (!(staticData.GetDropUnknown() || isEpsilon) || isDigit) { // add to dictionary @@ -266,9 +260,17 @@ void TranslationOptionCollection::ProcessOneUnknownWord(const Word &sourceWord,s targetPhrase.SetInputScore(*inputScores); } - targetPhrase.Evaluate(*m_unksrc); + // source phrase + Phrase *unksrc = new Phrase(1); + unksrc->AddWord() = sourceWord; + m_unksrcs.push_back(unksrc); - transOpt = new TranslationOption(WordsRange(sourcePos, sourcePos + length - 1), targetPhrase); + targetPhrase.Evaluate(*unksrc); + + WordsRange range(sourcePos, sourcePos + length - 1); + + TranslationOption *transOpt = new TranslationOption(range, targetPhrase); + transOpt->SetSourcePhrase(*unksrc); Add(transOpt); @@ -517,16 +519,17 @@ void TranslationOptionCollection::CreateTranslationOptionsForRange( void TranslationOptionCollection::AddInputScore(const InputPath &inputPath, PartialTranslOptColl &oldPtoc) { const ScoreComponentCollection *inputScore = inputPath.GetInputScore(); - - if (inputScore == NULL) { - return; - } + const Phrase &sourcePhrase = inputPath.GetPhrase(); const std::vector &transOpts = oldPtoc.GetList(); for (size_t i = 0; i < transOpts.size(); ++i) { TranslationOption &transOpt = *transOpts[i]; - ScoreComponentCollection &scores = transOpt.GetScoreBreakdown(); - scores.PlusEquals(*inputScore); + transOpt.SetSourcePhrase(sourcePhrase); + + if (inputScore) { + ScoreComponentCollection &scores = transOpt.GetScoreBreakdown(); + scores.PlusEquals(*inputScore); + } } }