00001 #include "RulePairUnlexicalizedSource.h"
00002 #include "moses/StaticData.h"
00003 #include "moses/InputFileStream.h"
00004 #include "moses/ScoreComponentCollection.h"
00005 #include "moses/FactorCollection.h"
00006 #include <sstream>
00007 #include "util/string_stream.hh"
00008
00009 using namespace std;
00010
00011 namespace Moses
00012 {
00013
00014 RulePairUnlexicalizedSource::RulePairUnlexicalizedSource(const std::string &line)
00015 : StatelessFeatureFunction(1, line)
00016 , m_glueRules(false)
00017 , m_nonGlueRules(true)
00018 , m_glueTargetLHSStr("Q")
00019 {
00020 VERBOSE(1, "Initializing feature " << GetScoreProducerDescription() << " ...");
00021 ReadParameters();
00022 FactorCollection &factorCollection = FactorCollection::Instance();
00023 m_glueTargetLHS = factorCollection.AddFactor(m_glueTargetLHSStr, true);
00024 VERBOSE(1, " Done.");
00025 }
00026
00027 void RulePairUnlexicalizedSource::SetParameter(const std::string& key, const std::string& value)
00028 {
00029 if (key == "glueRules") {
00030 m_glueRules = Scan<bool>(value);
00031 } else if (key == "nonGlueRules") {
00032 m_nonGlueRules = Scan<bool>(value);
00033 } else if (key == "glueTargetLHS") {
00034 m_glueTargetLHSStr = value;
00035 } else {
00036 StatelessFeatureFunction::SetParameter(key, value);
00037 }
00038 }
00039
00040
00041 void RulePairUnlexicalizedSource::EvaluateInIsolation(const Phrase &source
00042 , const TargetPhrase &targetPhrase
00043 , ScoreComponentCollection &scoreBreakdown
00044 , ScoreComponentCollection &estimatedScores) const
00045 {
00046 const Factor* targetPhraseLHS = targetPhrase.GetTargetLHS()[0];
00047 if ( !m_glueRules && (targetPhraseLHS == m_glueTargetLHS) ) {
00048 return;
00049 }
00050 if ( !m_nonGlueRules && (targetPhraseLHS != m_glueTargetLHS) ) {
00051 return;
00052 }
00053
00054 for (size_t posS=0; posS<source.GetSize(); ++posS) {
00055 const Word &wordS = source.GetWord(posS);
00056 if ( !wordS.IsNonTerminal() ) {
00057 return;
00058 }
00059 }
00060
00061 util::StringStream namestr;
00062
00063 for (size_t posT=0; posT<targetPhrase.GetSize(); ++posT) {
00064 const Word &wordT = targetPhrase.GetWord(posT);
00065 const Factor* factorT = wordT[0];
00066 if ( wordT.IsNonTerminal() ) {
00067 namestr << "[";
00068 }
00069 namestr << factorT->GetString();
00070 if ( wordT.IsNonTerminal() ) {
00071 namestr << "]";
00072 }
00073 namestr << "|";
00074 }
00075
00076 namestr << targetPhraseLHS->GetString() << "|";
00077
00078 for (AlignmentInfo::const_iterator it=targetPhrase.GetAlignNonTerm().begin();
00079 it!=targetPhrase.GetAlignNonTerm().end(); ++it) {
00080 namestr << "|" << it->first << "-" << it->second;
00081 }
00082
00083 scoreBreakdown.PlusEquals(this, namestr.str(), 1);
00084 if ( targetPhraseLHS != m_glueTargetLHS ) {
00085 scoreBreakdown.PlusEquals(this, 1);
00086 }
00087 }
00088
00089 }
00090