mosesdecoder/contrib/other-builds/moses2/FeatureFunctions.cpp

118 lines
2.9 KiB
C++
Raw Normal View History

2015-10-27 19:54:15 +03:00
/*
* FeatureFunctions.cpp
*
* Created on: 27 Oct 2015
* Author: hieu
*/
#include <boost/foreach.hpp>
#include "FeatureFunctions.h"
#include "StatefulFeatureFunction.h"
#include "System.h"
2015-10-28 19:11:12 +03:00
#include "SkeletonStatelessFF.h"
2015-10-28 00:01:23 +03:00
#include "SkeletonStatefulFF.h"
2015-10-28 19:45:52 +03:00
#include "PhraseTableMemory.h"
2015-10-28 20:12:02 +03:00
#include "UnknownWordPenalty.h"
2015-10-28 19:33:08 +03:00
#include "WordPenalty.h"
2015-10-27 19:54:15 +03:00
using namespace std;
FeatureFunctions::FeatureFunctions(System &system)
:m_system(system)
,m_ffStartInd(0)
{
// TODO Auto-generated constructor stub
}
FeatureFunctions::~FeatureFunctions() {
Moses::RemoveAllInColl(m_featureFunctions);
}
void FeatureFunctions::LoadFeatureFunctions()
{
const Moses::Parameter &params = m_system.GetParameter();
const Moses::PARAM_VEC *ffParams = params.GetParam("feature");
UTIL_THROW_IF2(ffParams == NULL, "Must have [feature] section");
BOOST_FOREACH(const std::string &line, *ffParams) {
cerr << "line=" << line << endl;
FeatureFunction *ff = Create(line);
m_featureFunctions.push_back(ff);
StatefulFeatureFunction *sfff = dynamic_cast<StatefulFeatureFunction*>(ff);
if (sfff) {
m_statefulFeatureFunctions.push_back(sfff);
}
PhraseTable *pt = dynamic_cast<PhraseTable*>(ff);
if (pt) {
2015-10-28 20:12:02 +03:00
pt->SetPtInd(m_phraseTables.size());
2015-10-27 19:54:15 +03:00
m_phraseTables.push_back(pt);
}
m_ffStartInd += ff->GetNumScores();
}
// load, everything but pts
BOOST_FOREACH(const FeatureFunction *ff, m_featureFunctions) {
FeatureFunction *nonConstFF = const_cast<FeatureFunction*>(ff);
PhraseTable *pt = dynamic_cast<PhraseTable*>(nonConstFF);
if (pt) {
// do nothing. load pt last
}
else {
2015-10-28 19:33:08 +03:00
cerr << "Loading " << nonConstFF->GetName() << endl;
2015-10-27 19:54:15 +03:00
nonConstFF->Load(m_system);
2015-10-28 19:33:08 +03:00
cerr << "Finished loading " << nonConstFF->GetName() << endl;
2015-10-27 19:54:15 +03:00
}
}
// load pt
BOOST_FOREACH(const PhraseTable *pt, m_phraseTables) {
PhraseTable *nonConstPT = const_cast<PhraseTable*>(pt);
2015-10-28 19:33:08 +03:00
cerr << "Loading " << nonConstPT->GetName() << endl;
2015-10-27 19:54:15 +03:00
nonConstPT->Load(m_system);
2015-10-28 19:33:08 +03:00
cerr << "Finished loading " << nonConstPT->GetName() << endl;
2015-10-27 19:54:15 +03:00
}
}
FeatureFunction *FeatureFunctions::Create(const std::string &line)
{
vector<string> toks = Moses::Tokenize(line);
FeatureFunction *ret;
if (toks[0] == "PhraseDictionaryMemory") {
2015-10-28 19:45:52 +03:00
ret = new PhraseTableMemory(m_ffStartInd, line);
2015-10-27 19:54:15 +03:00
}
2015-10-28 20:12:02 +03:00
else if (toks[0] == "UnknownWordPenalty") {
ret = new UnknownWordPenalty(m_ffStartInd, line);
}
2015-10-28 19:33:08 +03:00
else if (toks[0] == "WordPenalty") {
ret = new WordPenalty(m_ffStartInd, line);
}
2015-10-28 00:01:23 +03:00
else {
2015-10-28 19:11:12 +03:00
//ret = new SkeletonStatefulFF(m_ffStartInd, line);
ret = new SkeletonStatelessFF(m_ffStartInd, line);
2015-10-28 00:01:23 +03:00
}
2015-10-27 19:54:15 +03:00
m_ffStartInd += ret->GetNumScores();
2015-10-28 00:01:23 +03:00
return ret;
2015-10-27 19:54:15 +03:00
}
2015-10-27 20:35:42 +03:00
const FeatureFunction &FeatureFunctions::FindFeatureFunction(const std::string &name)
{
BOOST_FOREACH(const FeatureFunction *ff, m_featureFunctions) {
if (ff->GetName() == name) {
return *ff;
}
}
UTIL_THROW2(name << " not found");
}