From 9a68ff8da4b54ae30802c66140ad96974e450f63 Mon Sep 17 00:00:00 2001 From: Hieu Hoang Date: Thu, 29 Oct 2015 18:15:12 +0000 Subject: [PATCH] fixing scores --- contrib/other-builds/moses2/Distortion.cpp | 3 ++ .../other-builds/moses2/FeatureFunctions.cpp | 7 +++-- .../other-builds/moses2/FeatureFunctions.h | 5 ++-- contrib/other-builds/moses2/Hypothesis.cpp | 3 ++ contrib/other-builds/moses2/Scores.cpp | 7 +++-- contrib/other-builds/moses2/System.cpp | 4 ++- .../moses2/UnknownWordPenalty.cpp | 3 +- contrib/other-builds/moses2/Weights.cpp | 28 ++++++++++++++++++- contrib/other-builds/moses2/Weights.h | 8 +++++- 9 files changed, 57 insertions(+), 11 deletions(-) diff --git a/contrib/other-builds/moses2/Distortion.cpp b/contrib/other-builds/moses2/Distortion.cpp index 598d58378..a5dc79b8d 100644 --- a/contrib/other-builds/moses2/Distortion.cpp +++ b/contrib/other-builds/moses2/Distortion.cpp @@ -11,6 +11,8 @@ #include "moses/Range.h" #include "moses/Bitmap.h" +using namespace std; + struct DistortionState_traditional : public Moses::FFState { Moses::Range range; int first_gap; @@ -79,6 +81,7 @@ Moses::FFState* Distortion::EvaluateWhenApplied(const Manager &mgr, prev.range, hypo.GetRange(), prev.first_gap); + //cerr << "distortionScore=" << distortionScore << endl; score.PlusEquals(mgr.GetSystem(), *this, distortionScore); diff --git a/contrib/other-builds/moses2/FeatureFunctions.cpp b/contrib/other-builds/moses2/FeatureFunctions.cpp index ffe3ab6ae..c8cfad53c 100644 --- a/contrib/other-builds/moses2/FeatureFunctions.cpp +++ b/contrib/other-builds/moses2/FeatureFunctions.cpp @@ -31,7 +31,7 @@ FeatureFunctions::~FeatureFunctions() { Moses::RemoveAllInColl(m_featureFunctions); } -void FeatureFunctions::LoadFeatureFunctions() +void FeatureFunctions::Create() { const Moses::Parameter ¶ms = m_system.GetParameter(); @@ -56,7 +56,10 @@ void FeatureFunctions::LoadFeatureFunctions() m_phraseTables.push_back(pt); } } +} +void FeatureFunctions::Load() +{ // load, everything but pts BOOST_FOREACH(const FeatureFunction *ff, m_featureFunctions) { FeatureFunction *nonConstFF = const_cast(ff); @@ -107,7 +110,7 @@ FeatureFunction *FeatureFunctions::Create(const std::string &line) return ret; } -const FeatureFunction &FeatureFunctions::FindFeatureFunction(const std::string &name) +const FeatureFunction &FeatureFunctions::FindFeatureFunction(const std::string &name) const { BOOST_FOREACH(const FeatureFunction *ff, m_featureFunctions) { if (ff->GetName() == name) { diff --git a/contrib/other-builds/moses2/FeatureFunctions.h b/contrib/other-builds/moses2/FeatureFunctions.h index 7721375a8..ad03319dd 100644 --- a/contrib/other-builds/moses2/FeatureFunctions.h +++ b/contrib/other-builds/moses2/FeatureFunctions.h @@ -36,9 +36,10 @@ public: size_t GetNumScores() const { return m_ffStartInd; } - void LoadFeatureFunctions(); + void Create(); + void Load(); - const FeatureFunction &FindFeatureFunction(const std::string &name); + const FeatureFunction &FindFeatureFunction(const std::string &name) const; virtual void EvaluateInIsolation(const System &system, diff --git a/contrib/other-builds/moses2/Hypothesis.cpp b/contrib/other-builds/moses2/Hypothesis.cpp index 5b065fa9b..21e84376e 100644 --- a/contrib/other-builds/moses2/Hypothesis.cpp +++ b/contrib/other-builds/moses2/Hypothesis.cpp @@ -13,6 +13,8 @@ #include "Scores.h" #include "StatefulFeatureFunction.h" +using namespace std; + Hypothesis::Hypothesis(Manager &mgr, const TargetPhrase &tp, const Moses::Range &range, @@ -135,5 +137,6 @@ void Hypothesis::EvaluateWhenApplied() m_ffStates[statefulInd] = state; } + cerr << *this << endl; } diff --git a/contrib/other-builds/moses2/Scores.cpp b/contrib/other-builds/moses2/Scores.cpp index dd36414c5..8f2971504 100644 --- a/contrib/other-builds/moses2/Scores.cpp +++ b/contrib/other-builds/moses2/Scores.cpp @@ -51,6 +51,7 @@ void Scores::PlusEquals(const System &system, SCORE incrScore = scores[i]; m_scores[ffStartInd + i] += incrScore; + cerr << "ffStartInd=" << ffStartInd << " " << i << endl; SCORE weight = weights[ffStartInd + i]; m_total += incrScore * weight; } @@ -72,13 +73,13 @@ void Scores::PlusEquals(const System &system, } -void Scores::PlusEquals(const System &system, const Scores &scores) +void Scores::PlusEquals(const System &system, const Scores &other) { size_t numScores = system.GetFeatureFunctions().GetNumScores(); for (size_t i = 0; i < numScores; ++i) { - m_scores[i] += scores.m_scores[i]; + m_scores[i] += other.m_scores[i]; } - m_total += scores.m_total; + m_total += other.m_total; } diff --git a/contrib/other-builds/moses2/System.cpp b/contrib/other-builds/moses2/System.cpp index c52518958..e42903147 100644 --- a/contrib/other-builds/moses2/System.cpp +++ b/contrib/other-builds/moses2/System.cpp @@ -18,8 +18,9 @@ System::System(const Moses::Parameter ¶ms) :m_featureFunctions(*this) ,m_params(params) { - m_featureFunctions.LoadFeatureFunctions(); + m_featureFunctions.Create(); LoadWeights(); + m_featureFunctions.Load(); } System::~System() { @@ -30,6 +31,7 @@ void System::LoadWeights() const Moses::PARAM_VEC *weightParams = m_params.GetParam("weight"); UTIL_THROW_IF2(weightParams == NULL, "Must have [weight] section"); + m_weights.Init(m_featureFunctions); BOOST_FOREACH(const std::string &line, *weightParams) { m_weights.CreateFromString(m_featureFunctions, line); } diff --git a/contrib/other-builds/moses2/UnknownWordPenalty.cpp b/contrib/other-builds/moses2/UnknownWordPenalty.cpp index b7c60efa4..52dcaba29 100644 --- a/contrib/other-builds/moses2/UnknownWordPenalty.cpp +++ b/contrib/other-builds/moses2/UnknownWordPenalty.cpp @@ -45,7 +45,8 @@ TargetPhrases::shared_const_ptr UnknownWordPenalty::Lookup(const Manager &mgr, I //const Moses::Factor *factor = fc.AddFactor("SSS", false); word[0] = factor; - // TODO - set score + Scores &scores = target->GetScores(); + scores.PlusEquals(mgr.GetSystem(), *this, -100); system.GetFeatureFunctions().EvaluateInIsolation(system, source, *target, target->GetScores(), NULL); diff --git a/contrib/other-builds/moses2/Weights.cpp b/contrib/other-builds/moses2/Weights.cpp index 2473d2b0c..7a0c1b3ca 100644 --- a/contrib/other-builds/moses2/Weights.cpp +++ b/contrib/other-builds/moses2/Weights.cpp @@ -7,13 +7,15 @@ #include #include #include +#include "FeatureFunction.h" #include "FeatureFunctions.h" #include "Weights.h" #include "moses/Util.h" using namespace std; -Weights::Weights() { +Weights::Weights() +{ // TODO Auto-generated constructor stub } @@ -22,6 +24,22 @@ Weights::~Weights() { // TODO Auto-generated destructor stub } +void Weights::Init(const FeatureFunctions &ffs) +{ + size_t totalNumScores = ffs.GetNumScores(); + cerr << "totalNumScores=" << totalNumScores << endl; + m_weights.resize(totalNumScores, 1); +} + +void Weights::Debug(std::ostream &out, const FeatureFunctions &ffs) const +{ + size_t numScores = ffs.GetNumScores(); + for (size_t i = 0; i < numScores; ++i) { + out << m_weights[i] << " "; + } + +} + std::ostream& operator<<(std::ostream &out, const Weights &obj) { @@ -39,5 +57,13 @@ void Weights::CreateFromString(const FeatureFunctions &ffs, const std::string &l ffName = ffName.substr(0, ffName.size() - 1); cerr << "ffName=" << ffName << endl; + const FeatureFunction &ff = ffs.FindFeatureFunction(ffName); + size_t startInd = ff.GetStartInd(); + size_t numScores = ff.GetNumScores(); + assert(numScores == toks.size() -1); + for (size_t i = 0; i < numScores; ++i) { + SCORE score = Moses::Scan(toks[i+1]); + m_weights[i + startInd] = score; + } } diff --git a/contrib/other-builds/moses2/Weights.h b/contrib/other-builds/moses2/Weights.h index 9a62da971..bb6224fe5 100644 --- a/contrib/other-builds/moses2/Weights.h +++ b/contrib/other-builds/moses2/Weights.h @@ -9,6 +9,7 @@ #define WEIGHTS_H_ #include +#include #include "TypeDef.h" class FeatureFunctions; @@ -18,13 +19,18 @@ class Weights { public: Weights(); virtual ~Weights(); + void Init(const FeatureFunctions &ffs); SCORE operator[](size_t ind) const { - return 444.5f; + return m_weights[ind]; } + void Debug(std::ostream &out, const FeatureFunctions &ffs) const; + void CreateFromString(const FeatureFunctions &ffs, const std::string &line); +protected: + std::vector m_weights; }; #endif /* WEIGHTS_H_ */