mosesdecoder/moses/FF/NieceTerminal.cpp

112 lines
3.0 KiB
C++
Raw Normal View History

2014-05-20 22:51:46 +04:00
#include <vector>
#include <set>
2014-05-20 23:02:49 +04:00
#include "NieceTerminal.h"
2014-05-20 22:51:46 +04:00
#include "moses/ScoreComponentCollection.h"
#include "moses/TargetPhrase.h"
#include "moses/ChartCellLabel.h"
2014-05-21 16:43:32 +04:00
#include "moses/InputType.h"
2014-05-20 22:51:46 +04:00
using namespace std;
namespace Moses
{
2014-05-29 01:51:26 +04:00
NieceTerminal::NieceTerminal(const std::string &line)
:StatelessFeatureFunction(line)
,m_hardConstraint(false)
{
ReadParameters();
}
std::vector<float> NieceTerminal::DefaultWeights() const
{
UTIL_THROW_IF2(m_numScoreComponents != 1,
2015-01-14 14:07:42 +03:00
"NieceTerminal must only have 1 score");
vector<float> ret(1, 1);
return ret;
}
void NieceTerminal::EvaluateInIsolation(const Phrase &source
2015-01-14 14:07:42 +03:00
, const TargetPhrase &targetPhrase
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection &estimatedFutureScore) const
2014-05-20 22:51:46 +04:00
{
targetPhrase.SetRuleSource(source);
}
void NieceTerminal::EvaluateWithSourceContext(const InputType &input
2015-01-14 14:07:42 +03:00
, const InputPath &inputPath
, const TargetPhrase &targetPhrase
, const StackVec *stackVec
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection *estimatedFutureScore) const
2014-05-20 22:51:46 +04:00
{
2014-05-29 01:51:26 +04:00
assert(stackVec);
2014-05-20 22:51:46 +04:00
const Phrase *ruleSource = targetPhrase.GetRuleSource();
assert(ruleSource);
std::set<Word> terms;
for (size_t i = 0; i < ruleSource->GetSize(); ++i) {
2015-01-14 14:07:42 +03:00
const Word &word = ruleSource->GetWord(i);
if (!word.IsNonTerminal()) {
terms.insert(word);
}
2014-05-20 22:51:46 +04:00
}
2014-05-29 01:51:26 +04:00
for (size_t i = 0; i < stackVec->size(); ++i) {
2015-01-14 14:07:42 +03:00
const ChartCellLabel &cell = *stackVec->at(i);
const WordsRange &ntRange = cell.GetCoverage();
bool containTerm = ContainTerm(input, ntRange, terms);
if (containTerm) {
//cerr << "ruleSource=" << *ruleSource << " ";
//cerr << "ntRange=" << ntRange << endl;
// non-term contains 1 of the terms in the rule.
float score = m_hardConstraint ? - std::numeric_limits<float>::infinity() : 1;
scoreBreakdown.PlusEquals(this, score);
return;
}
2014-05-20 22:51:46 +04:00
}
}
void NieceTerminal::EvaluateWhenApplied(const Hypothesis& hypo,
2015-01-14 14:07:42 +03:00
ScoreComponentCollection* accumulator) const
2014-05-20 22:51:46 +04:00
{}
void NieceTerminal::EvaluateWhenApplied(const ChartHypothesis &hypo,
2014-05-20 22:51:46 +04:00
ScoreComponentCollection* accumulator) const
{}
2014-05-21 16:43:32 +04:00
bool NieceTerminal::ContainTerm(const InputType &input,
2015-01-14 14:07:42 +03:00
const WordsRange &ntRange,
const std::set<Word> &terms) const
2014-05-20 22:51:46 +04:00
{
2015-01-14 14:07:42 +03:00
std::set<Word>::const_iterator iter;
2014-05-20 22:51:46 +04:00
2015-01-14 14:07:42 +03:00
for (size_t pos = ntRange.GetStartPos(); pos <= ntRange.GetEndPos(); ++pos) {
const Word &word = input.GetWord(pos);
iter = terms.find(word);
2014-05-20 22:51:46 +04:00
2015-01-14 14:07:42 +03:00
if (iter != terms.end()) {
return true;
}
}
return false;
2014-05-20 22:51:46 +04:00
}
2014-05-29 01:51:26 +04:00
void NieceTerminal::SetParameter(const std::string& key, const std::string& value)
{
if (key == "hard-constraint") {
2015-01-14 14:07:42 +03:00
m_hardConstraint = Scan<bool>(value);
2014-05-29 01:51:26 +04:00
} else {
StatelessFeatureFunction::SetParameter(key, value);
}
2014-05-20 22:51:46 +04:00
}
2014-05-29 01:51:26 +04:00
}