2014-05-16 15:47:43 +04:00
|
|
|
#include "RuleScope.h"
|
2014-04-11 13:22:03 +04:00
|
|
|
#include "moses/StaticData.h"
|
|
|
|
#include "moses/Word.h"
|
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
2014-05-16 15:47:43 +04:00
|
|
|
RuleScope::RuleScope(const std::string &line)
|
2014-04-11 13:22:03 +04:00
|
|
|
:StatelessFeatureFunction(1, line)
|
|
|
|
,m_sourceSyntax(true)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsAmbiguous(const Word &word, bool sourceSyntax)
|
|
|
|
{
|
|
|
|
const Word &inputDefaultNonTerminal = StaticData::Instance().GetInputDefaultNonTerminal();
|
|
|
|
return word.IsNonTerminal() && (!sourceSyntax || word == inputDefaultNonTerminal);
|
|
|
|
}
|
|
|
|
|
2014-07-10 01:35:59 +04:00
|
|
|
void RuleScope::EvaluateInIsolation(const Phrase &source
|
2014-04-11 13:22:03 +04:00
|
|
|
, const TargetPhrase &targetPhrase
|
|
|
|
, ScoreComponentCollection &scoreBreakdown
|
|
|
|
, ScoreComponentCollection &estimatedFutureScore) const
|
|
|
|
{
|
2014-05-16 15:47:43 +04:00
|
|
|
// adjacent non-term count as 1 ammbiguity, rather than 2 as in rule scope
|
2014-04-11 13:22:03 +04:00
|
|
|
// source can't be empty, right?
|
|
|
|
float score = 0;
|
|
|
|
|
|
|
|
int count = 0;
|
|
|
|
for (size_t i = 0; i < source.GetSize() - 0; ++i) {
|
|
|
|
const Word &word = source.GetWord(i);
|
|
|
|
bool ambiguous = IsAmbiguous(word, m_sourceSyntax);
|
|
|
|
if (ambiguous) {
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (count > 0) {
|
|
|
|
score += count;
|
|
|
|
}
|
|
|
|
count = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1st & last always adjacent to ambiguity
|
|
|
|
++count;
|
|
|
|
if (count > 0) {
|
|
|
|
score += count;
|
|
|
|
}
|
|
|
|
|
|
|
|
scoreBreakdown.PlusEquals(this, score);
|
|
|
|
}
|
|
|
|
|
2014-05-16 15:47:43 +04:00
|
|
|
void RuleScope::SetParameter(const std::string& key, const std::string& value)
|
2014-04-11 13:22:03 +04:00
|
|
|
{
|
|
|
|
if (key == "source-syntax") {
|
|
|
|
m_sourceSyntax = Scan<bool>(value);
|
|
|
|
} else {
|
|
|
|
StatelessFeatureFunction::SetParameter(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|