2012-09-19 21:00:53 +04:00
|
|
|
#include <stdexcept>
|
2009-02-06 18:43:06 +03:00
|
|
|
|
2011-11-18 16:07:41 +04:00
|
|
|
#include "util/check.hh"
|
2013-06-10 19:29:07 +04:00
|
|
|
#include "util/exception.hh"
|
2009-02-06 18:43:06 +03:00
|
|
|
|
2012-09-19 21:00:53 +04:00
|
|
|
#include "FeatureFunction.h"
|
2013-05-24 21:02:49 +04:00
|
|
|
#include "moses/Hypothesis.h"
|
|
|
|
#include "moses/Manager.h"
|
|
|
|
#include "moses/TranslationOption.h"
|
2012-09-19 21:00:53 +04:00
|
|
|
|
2013-02-22 00:03:35 +04:00
|
|
|
using namespace std;
|
2012-09-19 21:00:53 +04:00
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
namespace Moses
|
|
|
|
{
|
2009-02-06 18:43:06 +03:00
|
|
|
|
2013-02-22 00:03:35 +04:00
|
|
|
multiset<string> FeatureFunction::description_counts;
|
|
|
|
|
2013-01-18 22:22:06 +04:00
|
|
|
std::vector<FeatureFunction*> FeatureFunction::m_producers;
|
2012-12-31 04:57:21 +04:00
|
|
|
std::vector<const StatelessFeatureFunction*> StatelessFeatureFunction::m_statelessFFs;
|
|
|
|
std::vector<const StatefulFeatureFunction*> StatefulFeatureFunction::m_statefulFFs;
|
|
|
|
|
2013-06-07 20:32:01 +04:00
|
|
|
FeatureFunction &FeatureFunction::FindFeatureFunction(const std::string& name)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_producers.size(); ++i) {
|
2013-06-10 21:11:55 +04:00
|
|
|
FeatureFunction &ff = *m_producers[i];
|
|
|
|
if (ff.GetScoreProducerDescription() == name) {
|
|
|
|
return ff;
|
|
|
|
}
|
2013-06-07 20:32:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
throw "Unknown feature " + name;
|
|
|
|
}
|
|
|
|
|
2013-02-03 22:27:55 +04:00
|
|
|
FeatureFunction::FeatureFunction(const std::string& description, const std::string &line)
|
2013-05-29 21:16:15 +04:00
|
|
|
: m_tuneable(true)
|
2013-02-03 22:27:55 +04:00
|
|
|
{
|
2013-06-10 19:29:07 +04:00
|
|
|
Initialize(description, line);
|
2013-02-03 22:27:55 +04:00
|
|
|
}
|
|
|
|
|
2013-02-02 00:23:36 +04:00
|
|
|
FeatureFunction::FeatureFunction(const std::string& description, size_t numScoreComponents, const std::string &line)
|
2013-05-29 21:16:15 +04:00
|
|
|
: m_numScoreComponents(numScoreComponents)
|
|
|
|
, m_tuneable(true)
|
2013-06-10 19:29:07 +04:00
|
|
|
{
|
|
|
|
Initialize(description, line);
|
|
|
|
}
|
|
|
|
|
|
|
|
void FeatureFunction::Initialize(const std::string& description, const std::string &line)
|
2012-12-30 23:42:53 +04:00
|
|
|
{
|
2013-02-22 00:03:35 +04:00
|
|
|
ParseLine(description, line);
|
2013-05-09 14:48:12 +04:00
|
|
|
|
2013-06-10 19:29:07 +04:00
|
|
|
size_t ind = 0;
|
|
|
|
while (ind < m_args.size()) {
|
2013-06-10 21:11:55 +04:00
|
|
|
vector<string> &args = m_args[ind];
|
2013-06-11 03:05:12 +04:00
|
|
|
bool consumed = SetParameter(args[0], args[1]);
|
2013-06-10 19:29:07 +04:00
|
|
|
if (consumed) {
|
2013-06-10 21:11:55 +04:00
|
|
|
m_args.erase(m_args.begin() + ind);
|
|
|
|
} else {
|
|
|
|
++ind;
|
2013-06-10 19:29:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-09 14:48:12 +04:00
|
|
|
if (m_description == "") {
|
2013-06-10 21:11:55 +04:00
|
|
|
size_t index = description_counts.count(description);
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2013-06-10 21:11:55 +04:00
|
|
|
ostringstream dstream;
|
|
|
|
dstream << description;
|
|
|
|
dstream << index;
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2013-06-10 21:11:55 +04:00
|
|
|
description_counts.insert(description);
|
|
|
|
m_description = dstream.str();
|
2013-03-06 16:39:41 +04:00
|
|
|
}
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2013-05-08 18:34:56 +04:00
|
|
|
ScoreComponentCollection::RegisterScoreProducer(this);
|
2012-12-31 04:57:21 +04:00
|
|
|
m_producers.push_back(this);
|
2012-12-30 23:42:53 +04:00
|
|
|
}
|
2012-09-21 14:56:01 +04:00
|
|
|
|
2009-02-06 18:43:06 +03:00
|
|
|
FeatureFunction::~FeatureFunction() {}
|
|
|
|
|
2013-02-22 00:03:35 +04:00
|
|
|
void FeatureFunction::ParseLine(const std::string& description, const std::string &line)
|
|
|
|
{
|
|
|
|
vector<string> toks = Tokenize(line);
|
2013-06-11 05:41:06 +04:00
|
|
|
set<string> keys;
|
2013-02-22 00:03:35 +04:00
|
|
|
|
|
|
|
CHECK(toks.size());
|
|
|
|
//CHECK(toks[0] == description);
|
|
|
|
|
|
|
|
for (size_t i = 1; i < toks.size(); ++i) {
|
2013-03-14 23:06:01 +04:00
|
|
|
vector<string> args = Tokenize(toks[i], "=");
|
2013-02-22 00:03:35 +04:00
|
|
|
CHECK(args.size() == 2);
|
2013-06-11 05:41:06 +04:00
|
|
|
|
|
|
|
pair<set<string>::iterator,bool> ret = keys.insert(args[0]);
|
|
|
|
UTIL_THROW_IF(!ret.second, util::Exception, "Duplicate key in line " << line);
|
|
|
|
|
2013-06-10 19:29:07 +04:00
|
|
|
m_args.push_back(args);
|
2013-03-06 16:39:41 +04:00
|
|
|
}
|
2013-02-22 00:03:35 +04:00
|
|
|
}
|
|
|
|
|
2013-06-11 03:05:12 +04:00
|
|
|
bool FeatureFunction::SetParameter(const std::string& key, const std::string& value)
|
2013-06-07 20:32:01 +04:00
|
|
|
{
|
2013-06-10 21:11:55 +04:00
|
|
|
if (key == "num-features") {
|
|
|
|
m_numScoreComponents = Scan<size_t>(value);
|
|
|
|
} else if (key == "name") {
|
|
|
|
m_description = value;
|
|
|
|
} else if (key == "tuneable") {
|
|
|
|
m_tuneable = Scan<bool>(value);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2013-06-07 20:32:01 +04:00
|
|
|
}
|
|
|
|
|
2013-06-11 05:41:06 +04:00
|
|
|
void FeatureFunction::OverrideParameter(const std::string& key, const std::string& value)
|
|
|
|
{
|
|
|
|
bool ret = SetParameter(key, value);
|
|
|
|
UTIL_THROW_IF(!ret, util::Exception, "Unknown argument" << key);
|
|
|
|
}
|
|
|
|
|
2009-02-06 18:43:06 +03:00
|
|
|
}
|
|
|
|
|