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"
|
2009-02-06 18:43:06 +03:00
|
|
|
|
2012-09-21 14:56:01 +04:00
|
|
|
#include "ChartHypothesis.h"
|
|
|
|
#include "ChartManager.h"
|
2012-09-19 21:00:53 +04:00
|
|
|
#include "FeatureFunction.h"
|
|
|
|
#include "Hypothesis.h"
|
|
|
|
#include "Manager.h"
|
|
|
|
#include "TranslationOption.h"
|
|
|
|
|
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
|
|
|
|
2012-09-19 21:00:53 +04:00
|
|
|
PhraseBasedFeatureContext::PhraseBasedFeatureContext(const Hypothesis* hypothesis) :
|
|
|
|
m_hypothesis(hypothesis),
|
|
|
|
m_translationOption(m_hypothesis->GetTranslationOption()),
|
|
|
|
m_source(m_hypothesis->GetManager().GetSource()) {}
|
|
|
|
|
|
|
|
PhraseBasedFeatureContext::PhraseBasedFeatureContext
|
|
|
|
(const TranslationOption& translationOption, const InputType& source) :
|
|
|
|
m_hypothesis(NULL),
|
|
|
|
m_translationOption(translationOption),
|
|
|
|
m_source(source) {}
|
|
|
|
|
|
|
|
const TranslationOption& PhraseBasedFeatureContext::GetTranslationOption() const
|
|
|
|
{
|
|
|
|
return m_translationOption;
|
|
|
|
}
|
|
|
|
|
|
|
|
const InputType& PhraseBasedFeatureContext::GetSource() const
|
|
|
|
{
|
|
|
|
return m_source;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TargetPhrase& PhraseBasedFeatureContext::GetTargetPhrase() const
|
|
|
|
{
|
|
|
|
return m_translationOption.GetTargetPhrase();
|
|
|
|
}
|
|
|
|
|
|
|
|
const WordsBitmap& PhraseBasedFeatureContext::GetWordsBitmap() const
|
|
|
|
{
|
|
|
|
if (!m_hypothesis) {
|
|
|
|
throw std::logic_error("Coverage vector not available during pre-calculation");
|
|
|
|
}
|
|
|
|
return m_hypothesis->GetWordsBitmap();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-09-21 14:56:01 +04:00
|
|
|
ChartBasedFeatureContext::ChartBasedFeatureContext
|
|
|
|
(const ChartHypothesis* hypothesis):
|
|
|
|
m_hypothesis(hypothesis),
|
|
|
|
m_targetPhrase(hypothesis->GetCurrTargetPhrase()),
|
|
|
|
m_source(hypothesis->GetManager().GetSource()) {}
|
|
|
|
|
|
|
|
ChartBasedFeatureContext::ChartBasedFeatureContext(
|
|
|
|
const TargetPhrase& targetPhrase,
|
|
|
|
const InputType& source):
|
|
|
|
m_hypothesis(NULL),
|
|
|
|
m_targetPhrase(targetPhrase),
|
|
|
|
m_source(source) {}
|
|
|
|
|
|
|
|
const InputType& ChartBasedFeatureContext::GetSource() const
|
|
|
|
{
|
|
|
|
return m_source;
|
|
|
|
}
|
|
|
|
|
|
|
|
const TargetPhrase& ChartBasedFeatureContext::GetTargetPhrase() const
|
|
|
|
{
|
|
|
|
return m_targetPhrase;
|
|
|
|
}
|
|
|
|
|
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-02-03 22:27:55 +04:00
|
|
|
FeatureFunction::FeatureFunction(const std::string& description, const std::string &line)
|
2013-05-15 14:37:21 +04:00
|
|
|
: m_tuneable(true)
|
2013-02-03 22:27:55 +04:00
|
|
|
{
|
2013-02-22 00:03:35 +04:00
|
|
|
ParseLine(description, line);
|
|
|
|
|
2013-05-09 14:48:12 +04:00
|
|
|
if (m_description == "") {
|
|
|
|
// not been given a name. Make a unique name
|
2013-03-06 16:39:41 +04:00
|
|
|
size_t index = description_counts.count(description);
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2013-03-06 16:39:41 +04:00
|
|
|
ostringstream dstream;
|
|
|
|
dstream << description;
|
|
|
|
dstream << index;
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2013-03-06 16:39:41 +04:00
|
|
|
description_counts.insert(description);
|
|
|
|
m_description = dstream.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
ScoreComponentCollection::RegisterScoreProducer(this);
|
2013-02-03 22:27:55 +04:00
|
|
|
m_producers.push_back(this);
|
|
|
|
}
|
|
|
|
|
2013-02-02 00:23:36 +04:00
|
|
|
FeatureFunction::FeatureFunction(const std::string& description, size_t numScoreComponents, const std::string &line)
|
2013-05-15 14:37:21 +04:00
|
|
|
: m_numScoreComponents(numScoreComponents)
|
2013-05-09 15:05:19 +04:00
|
|
|
, m_tuneable(true)
|
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
|
|
|
|
|
|
|
if (m_description == "") {
|
2013-03-06 16:39:41 +04:00
|
|
|
size_t index = description_counts.count(description);
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2013-03-06 16:39:41 +04:00
|
|
|
ostringstream dstream;
|
|
|
|
dstream << description;
|
|
|
|
dstream << index;
|
2013-02-22 00:03:35 +04:00
|
|
|
|
2013-03-06 16:39:41 +04:00
|
|
|
description_counts.insert(description);
|
|
|
|
m_description = dstream.str();
|
|
|
|
}
|
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
|
|
|
|
2012-09-19 21:00:53 +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);
|
|
|
|
|
|
|
|
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-05-09 14:48:12 +04:00
|
|
|
if (args[0] == "num-features") {
|
|
|
|
m_numScoreComponents = Scan<size_t>(args[1]);
|
2013-02-22 00:03:35 +04:00
|
|
|
}
|
2013-05-09 14:48:12 +04:00
|
|
|
else if (args[0] == "name") {
|
|
|
|
m_description = args[1];
|
|
|
|
}
|
2013-05-09 15:05:19 +04:00
|
|
|
else if (args[0] == "tuneable") {
|
|
|
|
m_tuneable = Scan<bool>(args[1]);
|
|
|
|
}
|
2013-05-09 14:48:12 +04:00
|
|
|
else {
|
|
|
|
m_args.push_back(args);
|
2013-03-06 16:39:41 +04:00
|
|
|
}
|
|
|
|
}
|
2013-02-22 00:03:35 +04:00
|
|
|
}
|
|
|
|
|
2013-02-04 05:09:15 +04:00
|
|
|
StatelessFeatureFunction::StatelessFeatureFunction(const std::string& description, const std::string &line)
|
|
|
|
:FeatureFunction(description, line)
|
|
|
|
{
|
|
|
|
m_statelessFFs.push_back(this);
|
|
|
|
}
|
|
|
|
|
2013-02-02 00:23:36 +04:00
|
|
|
StatelessFeatureFunction::StatelessFeatureFunction(const std::string& description, size_t numScoreComponents, const std::string &line)
|
|
|
|
:FeatureFunction(description, numScoreComponents, line)
|
2012-12-30 23:42:53 +04:00
|
|
|
{
|
2012-12-31 04:57:21 +04:00
|
|
|
m_statelessFFs.push_back(this);
|
2012-12-30 23:42:53 +04:00
|
|
|
}
|
|
|
|
|
2013-02-03 22:27:55 +04:00
|
|
|
StatefulFeatureFunction::StatefulFeatureFunction(const std::string& description, const std::string &line)
|
|
|
|
: FeatureFunction(description, line)
|
|
|
|
{
|
|
|
|
m_statefulFFs.push_back(this);
|
|
|
|
}
|
|
|
|
|
2013-02-02 00:23:36 +04:00
|
|
|
StatefulFeatureFunction::StatefulFeatureFunction(const std::string& description, size_t numScoreComponents, const std::string &line)
|
|
|
|
: FeatureFunction(description,numScoreComponents, line)
|
2012-12-30 23:42:53 +04:00
|
|
|
{
|
2012-12-31 04:57:21 +04:00
|
|
|
m_statefulFFs.push_back(this);
|
2012-12-30 23:42:53 +04:00
|
|
|
}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
bool StatefulFeatureFunction::IsStateless() const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-02-06 18:43:06 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|