mosesdecoder/moses/FF/SpanLength.cpp

90 lines
2.4 KiB
C++
Raw Normal View History

2014-06-04 19:52:57 +04:00
#include <boost/shared_ptr.hpp>
#include "SpanLength.h"
#include "moses/StaticData.h"
#include "moses/Word.h"
#include "moses/ChartCellLabel.h"
#include "moses/WordsRange.h"
#include "moses/StackVec.h"
#include "moses/TargetPhrase.h"
#include "moses/PP/PhraseProperty.h"
2014-06-06 13:40:40 +04:00
#include "moses/PP/SpanLengthPhraseProperty.h"
2014-06-04 19:52:57 +04:00
using namespace std;
namespace Moses
{
SpanLength::SpanLength(const std::string &line)
2015-01-14 14:07:42 +03:00
:StatelessFeatureFunction(1, line)
,m_smoothingMethod(None)
,m_const(0)
2014-06-04 19:52:57 +04:00
{
2014-06-06 13:40:40 +04:00
ReadParameters();
2014-06-04 19:52:57 +04:00
}
void SpanLength::EvaluateInIsolation(const Phrase &source
2015-01-14 14:07:42 +03:00
, const TargetPhrase &targetPhrase
, ScoreComponentCollection &scoreBreakdown
, ScoreComponentCollection &estimatedFutureScore) const
2014-06-04 19:52:57 +04:00
{
targetPhrase.SetRuleSource(source);
}
void SpanLength::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-06-04 19:52:57 +04:00
{
assert(stackVec);
2014-06-13 18:45:34 +04:00
const PhraseProperty *property = targetPhrase.GetProperty("SpanLength");
if (property == NULL) {
2015-01-14 14:07:42 +03:00
return;
2014-06-05 02:02:11 +04:00
}
2014-06-04 19:52:57 +04:00
2014-06-13 18:45:34 +04:00
const SpanLengthPhraseProperty *slProp = static_cast<const SpanLengthPhraseProperty*>(property);
2014-06-05 23:57:00 +04:00
2014-06-04 19:52:57 +04:00
const Phrase *ruleSource = targetPhrase.GetRuleSource();
assert(ruleSource);
2014-06-06 13:40:40 +04:00
float score = 0;
2014-06-04 19:52:57 +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();
size_t sourceWidth = ntRange.GetNumWordsCovered();
float prob = slProp->GetProb(i, sourceWidth, m_const);
score += TransformScore(prob);
2014-06-04 19:52:57 +04:00
}
2014-06-10 13:29:49 +04:00
2014-06-14 07:22:03 +04:00
if (score < -100.0f) {
float weight = StaticData::Instance().GetWeight(this);
if (weight < 0) {
2015-01-14 14:07:42 +03:00
score = -100;
2014-06-14 07:22:03 +04:00
}
}
2014-06-06 13:40:40 +04:00
scoreBreakdown.PlusEquals(this, score);
2014-06-04 19:52:57 +04:00
}
void SpanLength::SetParameter(const std::string& key, const std::string& value)
{
if (key == "smoothing") {
2015-01-14 14:07:42 +03:00
if (value == "plus-constant") {
m_smoothingMethod = PlusConst;
} else if (value == "none") {
m_smoothingMethod = None;
} else {
UTIL_THROW(util::Exception, "Unknown smoothing type " << value);
}
} else if (key == "constant") {
m_const = Scan<float>(value);
} else {
2014-06-04 19:52:57 +04:00
StatelessFeatureFunction::SetParameter(key, value);
}
}
}