00001 #include <fstream>
00002 #include "DsgModel.h"
00003 #include "dsgHyp.h"
00004 #include "moses/Util.h"
00005 #include "util/exception.hh"
00006
00007 using namespace std;
00008 using namespace lm::ngram;
00009
00010 namespace Moses
00011 {
00012
00013 DesegModel::DesegModel(const std::string &line)
00014 :StatefulFeatureFunction(5, line )
00015 {
00016 tFactor = 0;
00017 order=5;
00018 numFeatures = 5;
00019 optimistic = 1;
00020 ReadParameters();
00021 }
00022
00023 DesegModel::~DesegModel()
00024 {
00025 delete DSGM;
00026 }
00027
00028 void DesegModel :: readLanguageModel(const char *lmFile)
00029 {
00030 DSGM = ConstructDsgLM(m_lmPath.c_str());
00031 State startState = DSGM->NullContextState();
00032 desegT=new Desegmenter(m_desegPath,m_simple);
00033 }
00034
00035
00036 void DesegModel::Load(AllOptions::ptr const& opts)
00037 {
00038 m_options = opts;
00039 readLanguageModel(m_lmPath.c_str());
00040 }
00041
00042
00043
00044 void DesegModel:: EvaluateInIsolation(const Phrase &source
00045 , const TargetPhrase &targetPhrase
00046 , ScoreComponentCollection &scoreBreakdown
00047 , ScoreComponentCollection &estimatedScores) const
00048 {
00049
00050 dsgHypothesis obj;
00051 vector <string> myTargetPhrase;
00052 vector<float> scores;
00053 vector<string> targ_phrase;
00054 const AlignmentInfo &align = targetPhrase.GetAlignTerm();
00055
00056 for (int i = 0; i < targetPhrase.GetSize(); i++) {
00057 targ_phrase.push_back(targetPhrase.GetWord(i).GetFactor(tFactor)->GetString().as_string());
00058 }
00059
00060 obj.setState(DSGM->NullContextState());
00061 obj.setPhrases(targ_phrase);
00062 obj.calculateDsgProbinIsol(*DSGM,*desegT,align);
00063 obj.populateScores(scores,numFeatures);
00064 estimatedScores.PlusEquals(this, scores);
00065 }
00066
00067
00068 FFState* DesegModel::EvaluateWhenApplied(
00069 const Hypothesis& cur_hypo,
00070 const FFState* prev_state,
00071 ScoreComponentCollection* accumulator) const
00072 {
00073 const TargetPhrase &target = cur_hypo.GetCurrTargetPhrase();
00074 const Range &src_rng =cur_hypo.GetCurrSourceWordsRange();
00075 const AlignmentInfo &align = cur_hypo.GetCurrTargetPhrase().GetAlignTerm();
00076 size_t sourceOffset = src_rng.GetStartPos();
00077
00078 dsgHypothesis obj;
00079 vector<float> scores;
00080 vector<string> targ_phrase;
00081 bool isCompleted;
00082
00083 isCompleted=cur_hypo.IsSourceCompleted();
00084 for (int i = 0; i < cur_hypo.GetCurrTargetLength(); i++) {
00085 targ_phrase.push_back(target.GetWord(i).GetFactor(tFactor)->GetString().as_string());
00086 }
00087
00088 obj.setState(prev_state);
00089 obj.setPhrases( targ_phrase );
00090 obj.calculateDsgProb(*DSGM,*desegT,isCompleted,align, sourceOffset, optimistic);
00091 obj.populateScores(scores,numFeatures);
00092 accumulator->PlusEquals(this, scores);
00093 return obj.saveState();
00094
00095 }
00096
00097 FFState* DesegModel::EvaluateWhenApplied(
00098 const ChartHypothesis& ,
00099 int ,
00100 ScoreComponentCollection* accumulator) const
00101 {
00102 UTIL_THROW2("Chart decoding not support by UTIL_THROW2");
00103 }
00104
00105 const FFState* DesegModel::EmptyHypothesisState(const InputType &input) const
00106 {
00107 VERBOSE(3,"DesegModel::EmptyHypothesisState()" << endl);
00108 State startState = DSGM->BeginSentenceState();
00109 dsgState ss= dsgState(startState);
00110 return new dsgState(ss);
00111 }
00112
00113 std::string DesegModel::GetScoreProducerWeightShortName(unsigned idx) const
00114 {
00115 return "dsg";
00116 }
00117
00118
00119 void DesegModel::SetParameter(const std::string& key, const std::string& value)
00120 {
00121
00122 if (key == "path") {
00123 m_lmPath = value;
00124 } else if (key == "contiguity-features") {
00125 if(value == "no")
00126 numFeatures = 1;
00127 else
00128 numFeatures = 5;
00129 } else if (key == "output-factor") {
00130 tFactor = Scan<int>(value);
00131 } else if (key == "optimistic") {
00132 if (value == "n")
00133 optimistic = 0;
00134 else
00135 optimistic = 1;
00136 } else if (key == "deseg-path") {
00137 m_desegPath = Scan<int>(value);
00138 } else if (key == "deseg-scheme") {
00139 if(value == "s")
00140 m_simple = 1;
00141 else
00142 m_simple = 0;
00143 } else if (key == "order") {
00144 order = Scan<int>(value);
00145 } else {
00146 StatefulFeatureFunction::SetParameter(key, value);
00147 }
00148 }
00149
00150 bool DesegModel::IsUseable(const FactorMask &mask) const
00151 {
00152 bool ret = mask[0];
00153 return ret;
00154 }
00155
00156 }