00001 #include "RuleScope.h"
00002 #include "moses/StaticData.h"
00003 #include "moses/Word.h"
00004
00005 using namespace std;
00006
00007 namespace Moses
00008 {
00009 RuleScope::RuleScope(const std::string &line)
00010 :StatelessFeatureFunction(1, line)
00011 ,m_sourceSyntax(true)
00012 ,m_perScope(false)
00013 ,m_futureCostOnly(false)
00014 {
00015 }
00016
00017
00018
00019
00020
00021
00022
00023 void RuleScope::EvaluateInIsolation(const Phrase &source
00024 , const TargetPhrase &targetPhrase
00025 , ScoreComponentCollection &scoreBreakdown
00026 , ScoreComponentCollection &estimatedScores) const
00027 {
00028 if (IsGlueRule(source)) {
00029 return;
00030 }
00031
00032 float score = 0;
00033
00034 if (source.GetSize() > 0 && source.Front().IsNonTerminal()) {
00035 ++score;
00036 }
00037 if (source.GetSize() > 1 && source.Back().IsNonTerminal()) {
00038 ++score;
00039 }
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063 if (m_perScope) {
00064 UTIL_THROW_IF2(m_numScoreComponents <= score,
00065 "Insufficient number of score components. Scope=" << score << ". NUmber of score components=" << score);
00066 vector<float> scores(m_numScoreComponents, 0);
00067 scores[score] = 1;
00068
00069 if (m_futureCostOnly) {
00070 estimatedScores.PlusEquals(this, scores);
00071 } else {
00072 scoreBreakdown.PlusEquals(this, scores);
00073 }
00074 } else if (m_futureCostOnly) {
00075 estimatedScores.PlusEquals(this, score);
00076 } else {
00077 scoreBreakdown.PlusEquals(this, score);
00078 }
00079 }
00080
00081 void RuleScope::SetParameter(const std::string& key, const std::string& value)
00082 {
00083 if (key == "source-syntax") {
00084 m_sourceSyntax = Scan<bool>(value);
00085 } else if (key == "per-scope") {
00086 m_perScope = Scan<bool>(value);
00087 } else if ("future-cost-only") {
00088 m_futureCostOnly = Scan<bool>(value);
00089 } else {
00090 StatelessFeatureFunction::SetParameter(key, value);
00091 }
00092 }
00093
00094 bool RuleScope::IsGlueRule(const Phrase &source) const
00095 {
00096 string sourceStr = source.ToString();
00097 if (sourceStr == "<s> " || sourceStr == "X </s> " || sourceStr == "X X ") {
00098
00099
00100 return true;
00101 } else {
00102 return false;
00103 }
00104
00105 }
00106
00107 }
00108