2014-05-08 19:34:04 +04:00
|
|
|
#include <iostream>
|
|
|
|
#include "MaxSpanFreeNonTermSource.h"
|
|
|
|
#include "moses/StaticData.h"
|
|
|
|
#include "moses/Word.h"
|
|
|
|
#include "moses/InputPath.h"
|
2014-05-08 20:51:45 +04:00
|
|
|
#include "moses/TargetPhrase.h"
|
|
|
|
#include "moses/StackVec.h"
|
|
|
|
#include "moses/WordsRange.h"
|
|
|
|
#include "moses/ChartCellLabel.h"
|
2014-05-12 22:44:31 +04:00
|
|
|
#include "moses/FactorCollection.h"
|
2014-05-08 19:34:04 +04:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
namespace Moses
|
|
|
|
{
|
|
|
|
MaxSpanFreeNonTermSource::MaxSpanFreeNonTermSource(const std::string &line)
|
2015-01-14 14:07:42 +03:00
|
|
|
:StatelessFeatureFunction(1, line)
|
|
|
|
,m_maxSpan(2)
|
|
|
|
,m_glueTargetLHSStr("S")
|
|
|
|
,m_glueTargetLHS(true)
|
2014-05-08 19:34:04 +04:00
|
|
|
{
|
|
|
|
m_tuneable = false;
|
|
|
|
ReadParameters();
|
2014-05-12 22:44:31 +04:00
|
|
|
|
|
|
|
FactorCollection &fc = FactorCollection::Instance();
|
2014-05-16 19:16:20 +04:00
|
|
|
const Factor *factor = fc.AddFactor(m_glueTargetLHSStr, true);
|
2014-05-12 22:44:31 +04:00
|
|
|
m_glueTargetLHS.SetFactor(0, factor);
|
2014-05-08 19:34:04 +04:00
|
|
|
}
|
|
|
|
|
2014-07-10 01:35:59 +04:00
|
|
|
void MaxSpanFreeNonTermSource::EvaluateInIsolation(const Phrase &source
|
2015-01-14 14:07:42 +03:00
|
|
|
, const TargetPhrase &targetPhrase
|
|
|
|
, ScoreComponentCollection &scoreBreakdown
|
|
|
|
, ScoreComponentCollection &estimatedFutureScore) const
|
2014-05-08 19:34:04 +04:00
|
|
|
{
|
2014-05-08 20:51:45 +04:00
|
|
|
targetPhrase.SetRuleSource(source);
|
2014-05-08 19:34:04 +04:00
|
|
|
}
|
|
|
|
|
2014-07-10 02:06:54 +04:00
|
|
|
void MaxSpanFreeNonTermSource::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-08 19:34:04 +04:00
|
|
|
{
|
2014-05-12 22:44:31 +04:00
|
|
|
const Word &targetLHS = targetPhrase.GetTargetLHS();
|
2014-05-16 19:16:20 +04:00
|
|
|
|
|
|
|
if (targetLHS == m_glueTargetLHS) {
|
2015-01-14 14:07:42 +03:00
|
|
|
// don't delete glue rules
|
|
|
|
return;
|
2014-05-12 22:44:31 +04:00
|
|
|
}
|
|
|
|
|
2014-05-08 20:51:45 +04:00
|
|
|
const Phrase *source = targetPhrase.GetRuleSource();
|
|
|
|
assert(source);
|
|
|
|
float score = 0;
|
|
|
|
|
|
|
|
if (source->Front().IsNonTerminal()) {
|
2015-01-14 14:07:42 +03:00
|
|
|
const ChartCellLabel &cell = *stackVec->front();
|
|
|
|
if (cell.GetCoverage().GetNumWordsCovered() > m_maxSpan) {
|
|
|
|
score = - std::numeric_limits<float>::infinity();
|
|
|
|
}
|
2014-05-08 20:51:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (source->Back().IsNonTerminal()) {
|
2015-01-14 14:07:42 +03:00
|
|
|
const ChartCellLabel &cell = *stackVec->back();
|
|
|
|
if (cell.GetCoverage().GetNumWordsCovered() > m_maxSpan) {
|
|
|
|
score = - std::numeric_limits<float>::infinity();
|
|
|
|
}
|
2014-05-08 20:51:45 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
scoreBreakdown.PlusEquals(this, score);
|
|
|
|
|
2014-05-08 19:34:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MaxSpanFreeNonTermSource::SetParameter(const std::string& key, const std::string& value)
|
|
|
|
{
|
|
|
|
if (key == "max-span") {
|
2015-01-14 14:07:42 +03:00
|
|
|
m_maxSpan = Scan<int>(value);
|
2014-05-08 19:34:04 +04:00
|
|
|
} else {
|
|
|
|
StatelessFeatureFunction::SetParameter(key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<float> MaxSpanFreeNonTermSource::DefaultWeights() const
|
|
|
|
{
|
2015-01-14 14:07:42 +03:00
|
|
|
std::vector<float> ret(1, 1);
|
|
|
|
return ret;
|
2014-05-08 19:34:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|