00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef moses_Hypothesis_h
00022 #define moses_Hypothesis_h
00023
00024 #include <iostream>
00025 #include <memory>
00026
00027 #include <boost/scoped_ptr.hpp>
00028
00029 #include <vector>
00030 #include "Phrase.h"
00031 #include "TypeDef.h"
00032 #include "Bitmap.h"
00033 #include "Sentence.h"
00034 #include "Phrase.h"
00035 #include "GenerationDictionary.h"
00036 #include "ScoreComponentCollection.h"
00037 #include "InputType.h"
00038 #include "ObjectPool.h"
00039 #include "xmlrpc-c.h"
00040
00041 namespace Moses
00042 {
00043
00044 class SquareMatrix;
00045 class StaticData;
00046 class TranslationOption;
00047 class Range;
00048 class Hypothesis;
00049 class FFState;
00050 class StatelessFeatureFunction;
00051 class StatefulFeatureFunction;
00052 class Manager;
00053 struct ReportingOptions;
00054
00055 typedef std::vector<Hypothesis*> ArcList;
00056
00065 class Hypothesis
00066 {
00067 friend std::ostream& operator<<(std::ostream&, const Hypothesis&);
00068 protected:
00069 const Hypothesis* m_prevHypo;
00070 const Bitmap &m_sourceCompleted;
00071 InputType const& m_sourceInput;
00072 Range m_currSourceWordsRange;
00073 Range m_currTargetWordsRange;
00074 bool m_wordDeleted;
00075 float m_futureScore;
00076 float m_estimatedScore;
00078 mutable boost::scoped_ptr<ScoreComponentCollection> m_scoreBreakdown;
00079 ScoreComponentCollection m_currScoreBreakdown;
00080 std::vector<const FFState*> m_ffStates;
00081 const Hypothesis *m_winningHypo;
00082 ArcList *m_arcList;
00083 const TranslationOption &m_transOpt;
00084 Manager& m_manager;
00085
00086 int m_id;
00088 public:
00090 Hypothesis(Manager& manager, InputType const& source, const TranslationOption &initialTransOpt, const Bitmap &bitmap, int id);
00092 Hypothesis(const Hypothesis &prevHypo, const TranslationOption &transOpt, const Bitmap &bitmap, int id);
00093 ~Hypothesis();
00094
00095 void PrintHypothesis() const;
00096
00097 const InputType& GetInput() const {
00098 return m_sourceInput;
00099 }
00100
00102
00103 const TargetPhrase &GetCurrTargetPhrase() const;
00104
00106 inline const Range &GetCurrSourceWordsRange() const {
00107 return m_currSourceWordsRange;
00108 }
00109
00110 inline const Range &GetCurrTargetWordsRange() const {
00111 return m_currTargetWordsRange;
00112 }
00113
00114 Manager& GetManager() const {
00115 return m_manager;
00116 }
00117
00119 inline size_t GetCurrTargetLength() const {
00120 return m_currTargetWordsRange.GetNumWordsCovered();
00121 }
00122
00123 void EvaluateWhenApplied(float estimatedScore);
00124
00125 int GetId()const {
00126 return m_id;
00127 }
00128
00129 const Hypothesis* GetPrevHypo() const;
00130
00132 inline size_t GetSize() const {
00133 return m_currTargetWordsRange.GetEndPos() + 1;
00134 }
00135
00136 std::string GetSourcePhraseStringRep(const std::vector<FactorType> factorsToPrint) const;
00137 std::string GetTargetPhraseStringRep(const std::vector<FactorType> factorsToPrint) const;
00138 std::string GetSourcePhraseStringRep() const;
00139 std::string GetTargetPhraseStringRep() const;
00140
00144 inline const Word &GetCurrWord(size_t pos) const {
00145 return GetCurrTargetPhrase().GetWord(pos);
00146 }
00147 inline const Factor *GetCurrFactor(size_t pos, FactorType factorType) const {
00148 return GetCurrTargetPhrase().GetFactor(pos, factorType);
00149 }
00151 inline const Word &GetWord(size_t pos) const {
00152 const Hypothesis *hypo = this;
00153 while (pos < hypo->GetCurrTargetWordsRange().GetStartPos()) {
00154 hypo = hypo->GetPrevHypo();
00155 UTIL_THROW_IF2(hypo == NULL, "Previous hypothesis should not be NULL");
00156 }
00157 return hypo->GetCurrWord(pos - hypo->GetCurrTargetWordsRange().GetStartPos());
00158 }
00159 inline const Factor* GetFactor(size_t pos, FactorType factorType) const {
00160 return GetWord(pos)[factorType];
00161 }
00162
00163
00164
00165
00166 inline const Bitmap &GetWordsBitmap() const {
00167 return m_sourceCompleted;
00168 }
00169
00170 inline bool IsSourceCompleted() const {
00171 return m_sourceCompleted.IsComplete();
00172 }
00173
00174 void GetOutputPhrase(Phrase &out) const;
00175
00176 void ToStream(std::ostream& out) const {
00177 Phrase ret;
00178 GetOutputPhrase(ret);
00179 out << ret;
00180 }
00181
00182 void ToStringStream(std::stringstream& out) const {
00183 if (m_prevHypo != NULL) {
00184 m_prevHypo->ToStream(out);
00185 }
00186 out << (const Phrase&) GetCurrTargetPhrase();
00187 }
00188
00189 std::string GetOutputString() const {
00190 std::stringstream out;
00191 ToStringStream(out);
00192 return out.str();
00193 }
00194
00195 TO_STRING();
00196
00197 inline void SetWinningHypo(const Hypothesis *hypo) {
00198 m_winningHypo = hypo;
00199 }
00200 inline const Hypothesis *GetWinningHypo() const {
00201 return m_winningHypo;
00202 }
00203
00204 void AddArc(Hypothesis *loserHypo);
00205 void CleanupArcList(size_t nBestSize, bool distinctNBest);
00206
00208 inline const ArcList* GetArcList() const {
00209 return m_arcList;
00210 }
00211 const ScoreComponentCollection& GetScoreBreakdown() const {
00212 if (!m_scoreBreakdown) {
00213 m_scoreBreakdown.reset(new ScoreComponentCollection);
00214 m_scoreBreakdown->PlusEquals(m_currScoreBreakdown);
00215 if (m_prevHypo) {
00216 m_scoreBreakdown->PlusEquals(m_prevHypo->GetScoreBreakdown());
00217 }
00218 }
00219 return *(m_scoreBreakdown.get());
00220 }
00221 float GetFutureScore() const {
00222 return m_futureScore;
00223 }
00224 float GetScore() const {
00225 return m_futureScore-m_estimatedScore;
00226 }
00227 const FFState* GetFFState(int idx) const {
00228 return m_ffStates[idx];
00229 }
00230 void SetFFState(int idx, FFState* state) {
00231 m_ffStates[idx] = state;
00232 }
00233
00234 std::vector<std::vector<unsigned int> > *GetLMStats() const {
00235 return NULL;
00236 }
00237
00238 const TranslationOption &GetTranslationOption() const {
00239 return m_transOpt;
00240 }
00241
00242 size_t OutputAlignment(std::ostream &out, bool recursive) const;
00243
00244 void OutputInput(std::ostream& os) const;
00245 static void OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo);
00246
00247
00248 std::map<size_t, const Moses::Factor*> GetPlaceholders(const Moses::Hypothesis &hypo, Moses::FactorType placeholderFactor) const;
00249
00250
00251 size_t hash() const;
00252 bool operator==(const Hypothesis& other) const;
00253
00254 #ifdef HAVE_XMLRPC_C
00255
00256 void OutputWordAlignment(std::vector<xmlrpc_c::value>& out) const;
00257 void OutputLocalWordAlignment(std::vector<xmlrpc_c::value>& dest) const;
00258 #endif
00259
00260 bool beats(Hypothesis const& b) const;
00261
00262
00263 };
00264
00265 std::ostream& operator<<(std::ostream& out, const Hypothesis& hypothesis);
00266
00267
00268 struct CompareHypothesisTotalScore {
00269 bool operator()(const Hypothesis* a, const Hypothesis* b) const {
00270 return a->beats(*b);
00271 }
00272 };
00273
00274 }
00275 #endif