diff options
author | ccorbett <ccorbett@1f5c12ca-751b-0410-a591-d2e778427230> | 2006-08-02 00:49:21 +0400 |
---|---|---|
committer | ccorbett <ccorbett@1f5c12ca-751b-0410-a591-d2e778427230> | 2006-08-02 00:49:21 +0400 |
commit | cc7397802bffb26b3d335b27327398700ad83ab1 (patch) | |
tree | 8064c0c55a0be86f50935005a74c1918cd96344d | |
parent | c754c6c6b06d6470a5ea23c04952ed9359aac694 (diff) |
finished integrating new score infrastructure into LexicalReordering. Regression tests passed, but LexicalReordering's functionality in the new infrastructure remains to be tested.
git-svn-id: https://mosesdecoder.svn.sourceforge.net/svnroot/mosesdecoder/trunk@454 1f5c12ca-751b-0410-a591-d2e778427230
-rwxr-xr-x | moses/src/Hypothesis.cpp | 5 | ||||
-rwxr-xr-x | moses/src/LexicalReordering.cpp | 93 | ||||
-rwxr-xr-x | moses/src/LexicalReordering.h | 8 | ||||
-rwxr-xr-x | moses/src/StaticData.h | 5 |
4 files changed, 52 insertions, 59 deletions
diff --git a/moses/src/Hypothesis.cpp b/moses/src/Hypothesis.cpp index 874ec0f03..7e3dcd06a 100755 --- a/moses/src/Hypothesis.cpp +++ b/moses/src/Hypothesis.cpp @@ -359,10 +359,7 @@ void Hypothesis::CalcScore(const StaticData& staticData, const SquareMatrix &fut //LEXICAL REORDERING COST LexicalReordering *m_lexReorder = staticData.GetLexReorder(); if (m_lexReorder) { - std::vector<float> lroScores; - lroScores.push_back(m_lexReorder->CalcScore(this,LexReorderType::Forward)); - lroScores.push_back(m_lexReorder->CalcScore(this,LexReorderType::Backward)); - m_scoreBreakdown.PlusEquals(m_lexReorder, lroScores); + m_scoreBreakdown.PlusEquals(m_lexReorder, m_lexReorder->CalcScore(this)); } // TOTAL diff --git a/moses/src/LexicalReordering.cpp b/moses/src/LexicalReordering.cpp index 2e7fe2683..347007e0c 100755 --- a/moses/src/LexicalReordering.cpp +++ b/moses/src/LexicalReordering.cpp @@ -3,6 +3,7 @@ #include <iostream> #include <limits> #include <assert.h> +#include <vector> #include "LexicalReordering.h" #include "InputFileStream.h" #include "DistortionOrientation.h" @@ -17,11 +18,22 @@ using namespace std; */ LexicalReordering::LexicalReordering(const std::string &filename, int orientation, int direction, - int condition, const std::vector<float> weights) : - m_orientation(orientation), m_direction(direction), m_condition(condition), m_weights(weights), - m_filename(filename) + int condition, const std::vector<float>& weights) : + m_orientation(orientation), m_condition(condition), m_filename(filename), m_numberscores(weights.size()) { + //add score producer const_cast<ScoreIndexManager&>(StaticData::Instance()->GetScoreIndexManager()).AddScoreProducer(this); + //manage the weights by SetWeightsForScoreProducer method of static data. + if(direction == LexReorderType::Bidirectional) + { + m_direction.push_back(LexReorderType::Forward); + m_direction.push_back(LexReorderType::Backward); + } + else + { + m_direction.push_back(direction); + } + const_cast<StaticData*>(StaticData::Instance())->SetWeightsForScoreProducer(this, weights); // Load the file LoadFile(); PrintTable(); @@ -95,12 +107,13 @@ void LexicalReordering::PrintTable() } } -float LexicalReordering::CalcScore(Hypothesis *hypothesis, int direction) +std::vector<float> LexicalReordering::CalcScore(Hypothesis *hypothesis) { - if(m_direction==LexReorderType::Bidirectional || m_direction==direction){ - vector<float> val; - //this phrase declaration is to get around const mumbo jumbo and let me call a - //"convert to a string" method + std::vector<float> score(m_numberscores, 0); + vector<float> val; + for(int i=0; i < m_direction.size(); i++) + { + int direction = m_direction[i]; int orientation = DistortionOrientation::GetOrientation(hypothesis, direction); if(m_condition==LexReorderType::Fe) { @@ -115,41 +128,33 @@ float LexicalReordering::CalcScore(Hypothesis *hypothesis, int direction) //this key string is F from the hypothesis val=m_orientation_table[hypothesis->GetTargetPhrase().GetStringRep(hypothesis->GetCurrTargetWordsRange())]; } - //will tell us where to look in the table for the probability we need - int index = 0; - //the weight will tell us what to multiply the probability we fetch by - float weight = 1; + //the forward_offset is only applicable if we have a bidirectional model + //as the forward weights/scores come after the backward in this model, we need to offset by this amount. int forward_offset = 0; - //the weight vector will be longer if this LexicalReordering is bidirectional, - //containing backward weights then forward weights. - //by probing its size we can see if the LexicalReordering is bidirectional, - //(meaning we need to access the forward weights midvector) and - //if it is Monotone or MSD (which changes where midvector is) - if(m_weights.size()==4){ - forward_offset = 2; + //we know we have a bidirectional model if the number of scores is 4 or 6, not 2 or 3. + if(m_numberscores==4) + { + forward_offset=2; + } + else if(m_numberscores==6) + { + forward_offset=3; } - else if(m_weights.size()==6){ - forward_offset = 3; - } - if(m_orientation==DistortionOrientationType::Msd) { if(direction==LexReorderType::Backward) { if(orientation==DistortionOrientationType::MONO) { - index=BACK_M; - weight=m_weights[0]; + score[BACK_M] = val[BACK_M]; } else if(orientation==DistortionOrientationType::SWAP) { - index=BACK_S; - weight=m_weights[1]; + score[BACK_S] = val[BACK_S]; } else { - index=BACK_D; - weight=m_weights[2]; + score[BACK_D] = val[BACK_D]; } } @@ -157,18 +162,15 @@ float LexicalReordering::CalcScore(Hypothesis *hypothesis, int direction) { if(orientation==DistortionOrientationType::MONO) { - index=FOR_M; - weight=m_weights[0+forward_offset]; + score[FOR_M+forward_offset] = val[FOR_M+forward_offset]; } else if(orientation==DistortionOrientationType::SWAP) { - index=FOR_S; - weight=m_weights[1+forward_offset]; + score[FOR_S+forward_offset] = val[FOR_S+forward_offset]; } else { - index=FOR_D; - weight=m_weights[2+forward_offset]; + score[FOR_D+forward_offset] = val[FOR_D+forward_offset]; } } } @@ -178,41 +180,34 @@ float LexicalReordering::CalcScore(Hypothesis *hypothesis, int direction) { if(orientation==DistortionOrientationType::MONO) { - index=BACK_MONO; - weight=m_weights[0]; + score[BACK_MONO] = val[BACK_MONO]; } else { - index=BACK_NONMONO; - weight=m_weights[1]; + score[BACK_NONMONO] = val[BACK_NONMONO]; } } else { if(orientation==DistortionOrientationType::MONO) { - index=FOR_MONO; - weight=m_weights[0+forward_offset]; + score[FOR_MONO+forward_offset] = val[FOR_MONO+forward_offset]; } else { - index=FOR_NONMONO; - weight=m_weights[1+forward_offset]; + score[FOR_NONMONO+forward_offset] = val[FOR_NONMONO+forward_offset]; } } } - return val[index] * weight; - } - else - { - return 0; + } + return score; } unsigned int LexicalReordering::GetNumScoreComponents() const { - return m_weights.size(); + return m_numberscores; } const std::string LexicalReordering::GetScoreProducerDescription() const diff --git a/moses/src/LexicalReordering.h b/moses/src/LexicalReordering.h index 496d7c69b..20d97538f 100755 --- a/moses/src/LexicalReordering.h +++ b/moses/src/LexicalReordering.h @@ -71,9 +71,9 @@ private: static const int MONO_NUM_PROBS = 4; int m_orientation; // msd or monotone - int m_direction; // forward, backward, or bidirectional + std::vector<int> m_direction; // contains forward, backward, or both (bidirectional) int m_condition; // fe or f - std::vector<float> m_weights; //weights to give the LR scores. + int m_numberscores; //2, 3, 4 or 6 std::string m_filename; // probability table location ORIENTATION_TABLE m_orientation_table; // probability table @@ -86,13 +86,13 @@ public: // orientation probability table, orientation is one of {MSD, MONO}, // direction is one of {FOR,BACK,BI}, and condition is one of {F,FE}. LexicalReordering(const std::string &filename, int orientation, int direction, - int condition, const std::vector<float> weights); + int condition, const std::vector<float>& weights); // Descructor ~LexicalReordering(void) {} // Compute and return a score for a hypothesis - float CalcScore(Hypothesis *curr_hypothesis, int direction); + std::vector<float> CalcScore(Hypothesis *curr_hypothesis); // Print the orientation probability table void PrintTable(void); diff --git a/moses/src/StaticData.h b/moses/src/StaticData.h index d95ec3a12..36e0be369 100755 --- a/moses/src/StaticData.h +++ b/moses/src/StaticData.h @@ -101,8 +101,7 @@ protected: int m_useDistortionFutureCosts; - //! Sets the global score vector weights for a given ScoreProducer. - void SetWeightsForScoreProducer(const ScoreProducer* sp, const std::vector<float>& weights); + public: StaticData(); @@ -278,6 +277,8 @@ public: { m_weightWordPenalty = weightWordPenalty; } +//! Sets the global score vector weights for a given ScoreProducer. + void SetWeightsForScoreProducer(const ScoreProducer* sp, const std::vector<float>& weights); void SetWeightTransModel(const std::vector<float> &weight); void SetWeightLM(const std::vector<float> &weight); void SetWeightGeneration(const std::vector<float> &weight); |