add FF RuleAmbiguity

This commit is contained in:
Hieu Hoang 2014-04-11 10:22:03 +01:00
parent 1686686e65
commit 9644a30858
5 changed files with 125 additions and 0 deletions

View File

@ -1261,6 +1261,16 @@
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/moses/FF/ReferenceComparison.h</locationURI>
</link>
<link>
<name>FF/RuleAmbiguity.cpp</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/moses/FF/RuleAmbiguity.cpp</locationURI>
</link>
<link>
<name>FF/RuleAmbiguity.h</name>
<type>1</type>
<locationURI>PARENT-3-PROJECT_LOC/moses/FF/RuleAmbiguity.h</locationURI>
</link>
<link>
<name>FF/SetSourcePhrase.cpp</name>
<type>1</type>

View File

@ -40,6 +40,7 @@
#include "moses/FF/SetSourcePhrase.h"
#include "CountNonTerms.h"
#include "ReferenceComparison.h"
#include "RuleAmbiguity.h"
#include "moses/FF/SkeletonStatelessFF.h"
#include "moses/FF/SkeletonStatefulFF.h"
@ -183,6 +184,7 @@ FeatureRegistry::FeatureRegistry()
MOSES_FNAME(SetSourcePhrase);
MOSES_FNAME(CountNonTerms);
MOSES_FNAME(ReferenceComparison);
MOSES_FNAME(RuleAmbiguity);
MOSES_FNAME(SkeletonStatelessFF);
MOSES_FNAME(SkeletonStatefulFF);

View File

@ -0,0 +1,61 @@
#include "RuleScope.h"
#include "moses/StaticData.h"
#include "moses/Word.h"
namespace Moses
{
RuleAmbiguity::RuleAmbiguity(const std::string &line)
: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);
}
void RuleAmbiguity::Evaluate(const Phrase &source
, const TargetPhrase &targetPhrase
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection &estimatedFutureScore) const
{
// 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);
}
void RuleAmbiguity::SetParameter(const std::string& key, const std::string& value)
{
if (key == "source-syntax") {
m_sourceSyntax = Scan<bool>(value);
} else {
StatelessFeatureFunction::SetParameter(key, value);
}
}
}

44
moses/FF/RuleAmbiguity.h Normal file
View File

@ -0,0 +1,44 @@
#pragma once
#include <string>
#include "StatelessFeatureFunction.h"
namespace Moses
{
// the only thing this FF does is set TargetPhrase::m_ruleSource so that other FF can use it in Evaluate(Search).
class RuleAmbiguity : public StatelessFeatureFunction
{
public:
RuleAmbiguity(const std::string &line);
virtual bool IsUseable(const FactorMask &mask) const
{ return true; }
virtual void Evaluate(const Phrase &source
, const TargetPhrase &targetPhrase
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection &estimatedFutureScore) const;
virtual void Evaluate(const InputType &input
, const InputPath &inputPath
, const TargetPhrase &targetPhrase
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection *estimatedFutureScore = NULL) const
{}
virtual void Evaluate(const Hypothesis& hypo,
ScoreComponentCollection* accumulator) const
{}
virtual void EvaluateChart(const ChartHypothesis &hypo,
ScoreComponentCollection* accumulator) const
{}
void SetParameter(const std::string& key, const std::string& value);
protected:
bool m_sourceSyntax;
};
}

View File

@ -121,6 +121,14 @@ public:
return m_words[GetSize() - 1];
}
inline const Word &Front() const {
return m_words[0];
}
inline const Word &Back() const {
return m_words[GetSize() - 1];
}
//! particular factor at a particular position
inline const Factor *GetFactor(size_t pos, FactorType factorType) const {
const Word &ptr = m_words[pos];