2014-05-20 22:51:46 +04:00
|
|
|
#include <vector>
|
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)
|
2015-04-29 22:16:52 +03:00
|
|
|
:StatelessFeatureFunction(line,true)
|
2014-05-29 01:51:26 +04:00
|
|
|
,m_hardConstraint(false)
|
|
|
|
{
|
|
|
|
ReadParameters();
|
|
|
|
}
|
|
|
|
|
2014-06-01 23:29:55 +04:00
|
|
|
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");
|
2014-06-01 23:29:55 +04:00
|
|
|
vector<float> ret(1, 1);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-10 01:35:59 +04:00
|
|
|
void NieceTerminal::EvaluateInIsolation(const Phrase &source
|
2015-01-14 14:07:42 +03:00
|
|
|
, const TargetPhrase &targetPhrase
|
|
|
|
, ScoreComponentCollection &scoreBreakdown
|
2015-11-04 18:10:45 +03:00
|
|
|
, ScoreComponentCollection &estimatedScores) const
|
2014-05-20 22:51:46 +04:00
|
|
|
{
|
|
|
|
targetPhrase.SetRuleSource(source);
|
|
|
|
}
|
|
|
|
|
2014-07-10 02:06:54 +04:00
|
|
|
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
|
2015-11-04 18:10:45 +03:00
|
|
|
, ScoreComponentCollection *estimatedScores) 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);
|
|
|
|
|
2015-10-17 23:43:03 +03:00
|
|
|
boost::unordered_set<Word> terms;
|
2014-05-20 22:51:46 +04:00
|
|
|
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);
|
2015-10-25 16:37:59 +03:00
|
|
|
const Range &ntRange = cell.GetCoverage();
|
2015-01-14 14:07:42 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-07-10 02:41:08 +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
|
|
|
{}
|
|
|
|
|
2014-07-10 02:54:16 +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-10-25 16:37:59 +03:00
|
|
|
const Range &ntRange,
|
2015-10-17 23:43:03 +03:00
|
|
|
const boost::unordered_set<Word> &terms) const
|
2014-05-20 22:51:46 +04:00
|
|
|
{
|
2015-10-17 23:43:03 +03:00
|
|
|
boost::unordered_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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|