00001 #include <iostream>
00002 #include "MaxSpanFreeNonTermSource.h"
00003 #include "moses/StaticData.h"
00004 #include "moses/Word.h"
00005 #include "moses/InputPath.h"
00006 #include "moses/TargetPhrase.h"
00007 #include "moses/StackVec.h"
00008 #include "moses/Range.h"
00009 #include "moses/ChartCellLabel.h"
00010 #include "moses/FactorCollection.h"
00011
00012 using namespace std;
00013
00014 namespace Moses
00015 {
00016 MaxSpanFreeNonTermSource::MaxSpanFreeNonTermSource(const std::string &line)
00017 :StatelessFeatureFunction(1, line)
00018 ,m_maxSpan(2)
00019 ,m_glueTargetLHSStr("S")
00020 ,m_glueTargetLHS(true)
00021 {
00022 m_tuneable = false;
00023 ReadParameters();
00024
00025 FactorCollection &fc = FactorCollection::Instance();
00026 const Factor *factor = fc.AddFactor(m_glueTargetLHSStr, true);
00027 m_glueTargetLHS.SetFactor(0, factor);
00028 }
00029
00030 void MaxSpanFreeNonTermSource::EvaluateInIsolation(const Phrase &source
00031 , const TargetPhrase &targetPhrase
00032 , ScoreComponentCollection &scoreBreakdown
00033 , ScoreComponentCollection &estimatedScores) const
00034 {
00035 targetPhrase.SetRuleSource(source);
00036 }
00037
00038 void MaxSpanFreeNonTermSource::EvaluateWithSourceContext(const InputType &input
00039 , const InputPath &inputPath
00040 , const TargetPhrase &targetPhrase
00041 , const StackVec *stackVec
00042 , ScoreComponentCollection &scoreBreakdown
00043 , ScoreComponentCollection *estimatedScores) const
00044 {
00045 const Word &targetLHS = targetPhrase.GetTargetLHS();
00046
00047 if (targetLHS == m_glueTargetLHS) {
00048
00049 return;
00050 }
00051
00052 const Phrase *source = targetPhrase.GetRuleSource();
00053 assert(source);
00054 float score = 0;
00055
00056 if (source->Front().IsNonTerminal()) {
00057 const ChartCellLabel &cell = *stackVec->front();
00058 if (cell.GetCoverage().GetNumWordsCovered() > m_maxSpan) {
00059 score = - std::numeric_limits<float>::infinity();
00060 }
00061 }
00062
00063 if (source->Back().IsNonTerminal()) {
00064 const ChartCellLabel &cell = *stackVec->back();
00065 if (cell.GetCoverage().GetNumWordsCovered() > m_maxSpan) {
00066 score = - std::numeric_limits<float>::infinity();
00067 }
00068 }
00069
00070
00071 scoreBreakdown.PlusEquals(this, score);
00072
00073 }
00074
00075
00076 void MaxSpanFreeNonTermSource::SetParameter(const std::string& key, const std::string& value)
00077 {
00078 if (key == "max-span") {
00079 m_maxSpan = Scan<int>(value);
00080 } else {
00081 StatelessFeatureFunction::SetParameter(key, value);
00082 }
00083 }
00084
00085 std::vector<float> MaxSpanFreeNonTermSource::DefaultWeights() const
00086 {
00087 std::vector<float> ret(1, 1);
00088 return ret;
00089 }
00090
00091 }
00092