fixing scores

This commit is contained in:
Hieu Hoang 2015-10-29 18:15:12 +00:00
parent 82a2129eb4
commit 9a68ff8da4
9 changed files with 57 additions and 11 deletions

View File

@ -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);

View File

@ -31,7 +31,7 @@ FeatureFunctions::~FeatureFunctions() {
Moses::RemoveAllInColl(m_featureFunctions);
}
void FeatureFunctions::LoadFeatureFunctions()
void FeatureFunctions::Create()
{
const Moses::Parameter &params = 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<FeatureFunction*>(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) {

View File

@ -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,

View File

@ -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;
}

View File

@ -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;
}

View File

@ -18,8 +18,9 @@ System::System(const Moses::Parameter &params)
: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);
}

View File

@ -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);

View File

@ -7,13 +7,15 @@
#include <cassert>
#include <string>
#include <vector>
#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<SCORE>(toks[i+1]);
m_weights[i + startInd] = score;
}
}

View File

@ -9,6 +9,7 @@
#define WEIGHTS_H_
#include <iostream>
#include <vector>
#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<SCORE> m_weights;
};
#endif /* WEIGHTS_H_ */