00001 #include <boost/shared_ptr.hpp>
00002 #include "SpanLength.h"
00003 #include "moses/StaticData.h"
00004 #include "moses/Word.h"
00005 #include "moses/ChartCellLabel.h"
00006 #include "moses/Range.h"
00007 #include "moses/StackVec.h"
00008 #include "moses/TargetPhrase.h"
00009 #include "moses/PP/PhraseProperty.h"
00010 #include "moses/PP/SpanLengthPhraseProperty.h"
00011
00012 using namespace std;
00013
00014 namespace Moses
00015 {
00016 SpanLength::SpanLength(const std::string &line)
00017 :StatelessFeatureFunction(1, line)
00018 ,m_smoothingMethod(None)
00019 ,m_const(0)
00020 {
00021 ReadParameters();
00022 }
00023
00024 void SpanLength::EvaluateInIsolation(const Phrase &source
00025 , const TargetPhrase &targetPhrase
00026 , ScoreComponentCollection &scoreBreakdown
00027 , ScoreComponentCollection &estimatedScores) const
00028 {
00029 targetPhrase.SetRuleSource(source);
00030 }
00031
00032 void SpanLength::EvaluateWithSourceContext(const InputType &input
00033 , const InputPath &inputPath
00034 , const TargetPhrase &targetPhrase
00035 , const StackVec *stackVec
00036 , ScoreComponentCollection &scoreBreakdown
00037 , ScoreComponentCollection *estimatedScores) const
00038 {
00039 assert(stackVec);
00040
00041 const PhraseProperty *property = targetPhrase.GetProperty("SpanLength");
00042 if (property == NULL) {
00043 return;
00044 }
00045
00046 const SpanLengthPhraseProperty *slProp = static_cast<const SpanLengthPhraseProperty*>(property);
00047
00048 assert(targetPhrase.GetRuleSource());
00049
00050 float score = 0;
00051 for (size_t i = 0; i < stackVec->size(); ++i) {
00052 const ChartCellLabel &cell = *stackVec->at(i);
00053 const Range &ntRange = cell.GetCoverage();
00054 size_t sourceWidth = ntRange.GetNumWordsCovered();
00055 float prob = slProp->GetProb(i, sourceWidth, m_const);
00056 score += TransformScore(prob);
00057 }
00058
00059 if (score < -100.0f) {
00060 float weight = StaticData::Instance().GetWeight(this);
00061 if (weight < 0) {
00062 score = -100;
00063 }
00064 }
00065
00066 scoreBreakdown.PlusEquals(this, score);
00067
00068 }
00069
00070 void SpanLength::SetParameter(const std::string& key, const std::string& value)
00071 {
00072 if (key == "smoothing") {
00073 if (value == "plus-constant") {
00074 m_smoothingMethod = PlusConst;
00075 } else if (value == "none") {
00076 m_smoothingMethod = None;
00077 } else {
00078 UTIL_THROW(util::Exception, "Unknown smoothing type " << value);
00079 }
00080 } else if (key == "constant") {
00081 m_const = Scan<float>(value);
00082 } else {
00083 StatelessFeatureFunction::SetParameter(key, value);
00084 }
00085 }
00086
00087 }
00088