source phrase is a pointer in translation options.

This commit is contained in:
Hieu Hoang 2013-08-07 10:28:28 +01:00
parent 7a808a2edb
commit 66d4c2b0be
11 changed files with 47 additions and 39 deletions

View File

@ -348,7 +348,7 @@ void OutputBestHypo(const std::vector<Word>& mbrBestHypo, long /*translationId*
void OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo) void OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo)
{ {
if (hypo->GetPrevHypo()) { if (hypo->GetPrevHypo()) {
OutputInput(map, hypo->GetPrevHypo()); OutputInput(map, hypo->GetPrevHypo());
map[hypo->GetCurrSourceWordsRange().GetStartPos()] = &hypo->GetTranslationOption().GetSourcePhrase(); map[hypo->GetCurrSourceWordsRange().GetStartPos()] = &hypo->GetTranslationOption().GetSourcePhrase();
} }
} }

View File

@ -7,6 +7,15 @@
namespace Moses 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, Search *Search::CreateSearch(Manager& manager, const InputType &source,
SearchAlgorithm searchAlgorithm, const TranslationOptionCollection &transOptColl) SearchAlgorithm searchAlgorithm, const TranslationOptionCollection &transOptColl)
{ {

View File

@ -3,6 +3,8 @@
#include <vector> #include <vector>
#include "TypeDef.h" #include "TypeDef.h"
#include "TranslationOption.h"
#include "Phrase.h"
namespace Moses namespace Moses
{ {
@ -29,7 +31,7 @@ public:
//! Decode the sentence according to the specified search algorithm. //! Decode the sentence according to the specified search algorithm.
virtual void ProcessSentence() = 0; virtual void ProcessSentence() = 0;
explicit Search(Manager& manager) : m_manager(manager) {} explicit Search(Manager& manager);
virtual ~Search() {} virtual ~Search() {}
// Factory method // Factory method
@ -39,6 +41,8 @@ public:
protected: protected:
const Phrase *m_constraint; const Phrase *m_constraint;
Manager& m_manager; Manager& m_manager;
Phrase m_sourcePhrase; // for initial hypo
TranslationOption m_initialTransOpt; /**< used to seed 1st hypo */
}; };
} }

View File

@ -41,7 +41,6 @@ SearchCubePruning::SearchCubePruning(Manager& manager, const InputType &source,
:Search(manager) :Search(manager)
,m_source(source) ,m_source(source)
,m_hypoStackColl(source.GetSize() + 1) ,m_hypoStackColl(source.GetSize() + 1)
,m_initialTransOpt()
,m_start(clock()) ,m_start(clock())
,m_transOptColl(transOptColl) ,m_transOptColl(transOptColl)
{ {

View File

@ -20,7 +20,6 @@ protected:
const InputType &m_source; const InputType &m_source;
std::vector < HypothesisStack* > m_hypoStackColl; /**< stacks to store hypotheses (partial translations) */ std::vector < HypothesisStack* > m_hypoStackColl; /**< stacks to store hypotheses (partial translations) */
// no of elements = no of words in source + 1 // 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 */ 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 */ const TranslationOptionCollection &m_transOptColl; /**< pre-computed list of translation options for the phrases in this sentence */

View File

@ -16,7 +16,6 @@ SearchNormal::SearchNormal(Manager& manager, const InputType &source, const Tran
:Search(manager) :Search(manager)
,m_source(source) ,m_source(source)
,m_hypoStackColl(source.GetSize() + 1) ,m_hypoStackColl(source.GetSize() + 1)
,m_initialTransOpt()
,m_start(clock()) ,m_start(clock())
,interrupted_flag(0) ,interrupted_flag(0)
,m_transOptColl(transOptColl) ,m_transOptColl(transOptColl)

View File

@ -6,7 +6,6 @@
#include "HypothesisStackNormal.h" #include "HypothesisStackNormal.h"
#include "TranslationOptionCollection.h" #include "TranslationOptionCollection.h"
#include "Timer.h" #include "Timer.h"
#include "TranslationOption.h"
namespace Moses namespace Moses
{ {
@ -24,7 +23,6 @@ protected:
const InputType &m_source; const InputType &m_source;
std::vector < HypothesisStack* > m_hypoStackColl; /**< stacks to store hypotheses (partial translations) */ std::vector < HypothesisStack* > m_hypoStackColl; /**< stacks to store hypotheses (partial translations) */
// no of elements = no of words in source + 1 // 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 */ 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) */ 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*/ HypothesisStackNormal* actual_hypoStack; /**actual (full expanded) stack of hypotheses*/

View File

@ -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) //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.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()); m_deletedWords.push_back(&hypo.GetTranslationOption().GetSourcePhrase());
} }
} }

View File

@ -34,6 +34,7 @@ namespace Moses
TranslationOption::TranslationOption() TranslationOption::TranslationOption()
:m_targetPhrase() :m_targetPhrase()
,m_sourcePhrase(NULL)
,m_sourceWordsRange(NOT_FOUND, NOT_FOUND) ,m_sourceWordsRange(NOT_FOUND, NOT_FOUND)
{ {
} }
@ -42,19 +43,12 @@ TranslationOption::TranslationOption()
TranslationOption::TranslationOption(const WordsRange &wordsRange TranslationOption::TranslationOption(const WordsRange &wordsRange
, const TargetPhrase &targetPhrase) , const TargetPhrase &targetPhrase)
: m_targetPhrase(targetPhrase) : m_targetPhrase(targetPhrase)
, m_sourcePhrase(NULL)
, m_sourceWordsRange(wordsRange) , m_sourceWordsRange(wordsRange)
, m_futureScore(targetPhrase.GetFutureScore()) , m_futureScore(targetPhrase.GetFutureScore())
{ {
} }
TranslationOption::TranslationOption(const TranslationOption &copy, 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<FactorType>& featuresToCheck) const bool TranslationOption::IsCompatible(const Phrase& phrase, const std::vector<FactorType>& featuresToCheck) const
{ {
if (featuresToCheck.size() == 1) { if (featuresToCheck.size() == 1) {
@ -83,6 +77,13 @@ void TranslationOption::Evaluate(const InputType &source)
m_targetPhrase.Evaluate(source); m_targetPhrase.Evaluate(source);
} }
const Phrase &TranslationOption::GetSourcePhrase() const
{
CHECK(m_sourcePhrase);
return *m_sourcePhrase;
}
TO_STRING_BODY(TranslationOption); TO_STRING_BODY(TranslationOption);
// friend // friend

View File

@ -66,6 +66,7 @@ class TranslationOption
protected: protected:
TargetPhrase m_targetPhrase; /*< output phrase when using this translation option */ 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 */ 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 */ float m_futureScore; /*< estimate of total cost when using this translation option, includes language model probabilities */
@ -73,16 +74,12 @@ protected:
_ScoreCacheMap m_lexReorderingScores; _ScoreCacheMap m_lexReorderingScores;
public: 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 */ /** constructor. Used by initial translation step */
TranslationOption(const WordsRange &wordsRange TranslationOption(const WordsRange &wordsRange
, const TargetPhrase &targetPhrase); , const TargetPhrase &targetPhrase);
/** copy constructor, but change words range. used by caching */
TranslationOption(const TranslationOption &copy, const WordsRange &sourceWordsRange);
/** returns true if all feature types in featuresToCheck are compatible between the two phrases */ /** returns true if all feature types in featuresToCheck are compatible between the two phrases */
bool IsCompatible(const Phrase& phrase, const std::vector<FactorType>& featuresToCheck) const; bool IsCompatible(const Phrase& phrase, const std::vector<FactorType>& featuresToCheck) const;
@ -97,13 +94,11 @@ public:
} }
/** returns source phrase */ /** returns source phrase */
const Phrase &GetSourcePhrase() const { const Phrase &GetSourcePhrase() const;
return m_targetPhrase.GetSourcePhrase();
}
void SetSourcePhrase(const Phrase &sourcePhrase) void SetSourcePhrase(const Phrase &sourcePhrase)
{ {
// TODO m_sourcePhrase = &sourcePhrase;
} }
/** whether source span overlaps with those of a hypothesis */ /** whether source span overlaps with those of a hypothesis */

View File

@ -226,13 +226,7 @@ void TranslationOptionCollection::ProcessOneUnknownWord(const Word &sourceWord,s
// modify the starting bitmap // 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 targetPhrase;
targetPhrase.SetSourcePhrase(*m_unksrc);
if (!(staticData.GetDropUnknown() || isEpsilon) || isDigit) { if (!(staticData.GetDropUnknown() || isEpsilon) || isDigit) {
// add to dictionary // add to dictionary
@ -266,9 +260,17 @@ void TranslationOptionCollection::ProcessOneUnknownWord(const Word &sourceWord,s
targetPhrase.SetInputScore(*inputScores); 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); Add(transOpt);
@ -517,16 +519,17 @@ void TranslationOptionCollection::CreateTranslationOptionsForRange(
void TranslationOptionCollection::AddInputScore(const InputPath &inputPath, PartialTranslOptColl &oldPtoc) void TranslationOptionCollection::AddInputScore(const InputPath &inputPath, PartialTranslOptColl &oldPtoc)
{ {
const ScoreComponentCollection *inputScore = inputPath.GetInputScore(); const ScoreComponentCollection *inputScore = inputPath.GetInputScore();
const Phrase &sourcePhrase = inputPath.GetPhrase();
if (inputScore == NULL) {
return;
}
const std::vector<TranslationOption*> &transOpts = oldPtoc.GetList(); const std::vector<TranslationOption*> &transOpts = oldPtoc.GetList();
for (size_t i = 0; i < transOpts.size(); ++i) { for (size_t i = 0; i < transOpts.size(); ++i) {
TranslationOption &transOpt = *transOpts[i]; TranslationOption &transOpt = *transOpts[i];
ScoreComponentCollection &scores = transOpt.GetScoreBreakdown(); transOpt.SetSourcePhrase(sourcePhrase);
scores.PlusEquals(*inputScore);
if (inputScore) {
ScoreComponentCollection &scores = transOpt.GetScoreBreakdown();
scores.PlusEquals(*inputScore);
}
} }
} }