00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #ifndef moses_Hypothesis_h
00024 #define moses_Hypothesis_h
00025
00026 #include <iostream>
00027 #include <vector>
00028 #include "Phrase.h"
00029 #include "TypeDef.h"
00030 #include "WordsBitmap.h"
00031 #include "Sentence.h"
00032 #include "Phrase.h"
00033 #include "PhraseDictionaryMemory.h"
00034 #include "GenerationDictionary.h"
00035 #include "ScoreComponentCollection.h"
00036 #include "InputType.h"
00037 #include "ObjectPool.h"
00038
00039 namespace Moses
00040 {
00041
00042 class SquareMatrix;
00043 class StaticData;
00044 class TranslationOption;
00045 class WordsRange;
00046 class Hypothesis;
00047 class FFState;
00048 class Manager;
00049 class LexicalReordering;
00050
00051 typedef std::vector<Hypothesis*> ArcList;
00052
00061 class Hypothesis
00062 {
00063 friend std::ostream& operator<<(std::ostream&, const Hypothesis&);
00064
00065 protected:
00066 static ObjectPool<Hypothesis> s_objectPool;
00067
00068 const Hypothesis* m_prevHypo;
00069
00070 const TargetPhrase &m_targetPhrase;
00071 Phrase const* m_sourcePhrase;
00072 WordsBitmap m_sourceCompleted;
00073
00074
00075 InputType const& m_sourceInput;
00076 WordsRange m_currSourceWordsRange;
00077 WordsRange m_currTargetWordsRange;
00078 bool m_wordDeleted;
00079 float m_totalScore;
00080 float m_futureScore;
00081 ScoreComponentCollection m_scoreBreakdown;
00082 std::vector<const FFState*> m_ffStates;
00083 const Hypothesis *m_winningHypo;
00084 ArcList *m_arcList;
00085 const TranslationOption *m_transOpt;
00086 Manager& m_manager;
00087
00088 int m_id;
00091 Hypothesis(Manager& manager, InputType const& source, const TargetPhrase &emptyTarget);
00093 Hypothesis(const Hypothesis &prevHypo, const TranslationOption &transOpt);
00094
00095 public:
00096 static ObjectPool<Hypothesis> &GetObjectPool() {
00097 return s_objectPool;
00098 }
00099
00100 ~Hypothesis();
00101
00103 static Hypothesis* Create(const Hypothesis &prevHypo, const TranslationOption &transOpt, const Phrase* constraint);
00104
00105 static Hypothesis* Create(Manager& manager, const WordsBitmap &initialCoverage);
00106
00108 static Hypothesis* Create(Manager& manager, InputType const& source, const TargetPhrase &emptyTarget);
00109
00111 Hypothesis* CreateNext(const TranslationOption &transOpt, const Phrase* constraint) const;
00112
00113 void PrintHypothesis() const;
00114
00115 const InputType& GetInput() const {
00116 return m_sourceInput;
00117 }
00118
00120
00121 const TargetPhrase &GetCurrTargetPhrase() const {
00122 return m_targetPhrase;
00123 }
00124
00125
00126
00128 inline const WordsRange &GetCurrSourceWordsRange() const {
00129 return m_currSourceWordsRange;
00130 }
00131
00132 inline const WordsRange &GetCurrTargetWordsRange() const {
00133 return m_currTargetWordsRange;
00134 }
00135
00136 Manager& GetManager() const {
00137 return m_manager;
00138 }
00139
00141 inline size_t GetCurrTargetLength() const {
00142 return m_currTargetWordsRange.GetNumWordsCovered();
00143 }
00144
00145 void ResetScore();
00146
00147 void CalcScore(const SquareMatrix &futureScore);
00148
00149 float CalcExpectedScore( const SquareMatrix &futureScore );
00150 void CalcRemainingScore();
00151
00152 int GetId()const {
00153 return m_id;
00154 }
00155
00156 const Hypothesis* GetPrevHypo() const;
00157
00159 inline size_t GetSize() const {
00160 return m_currTargetWordsRange.GetEndPos() + 1;
00161 }
00162
00163 inline const Phrase* GetSourcePhrase() const {
00164 return m_sourcePhrase;
00165 }
00166
00167 std::string GetSourcePhraseStringRep(const std::vector<FactorType> factorsToPrint) const;
00168 std::string GetTargetPhraseStringRep(const std::vector<FactorType> factorsToPrint) const;
00169 std::string GetSourcePhraseStringRep() const;
00170 std::string GetTargetPhraseStringRep() const;
00171
00175 inline const Word &GetCurrWord(size_t pos) const {
00176 return m_targetPhrase.GetWord(pos);
00177 }
00178 inline const Factor *GetCurrFactor(size_t pos, FactorType factorType) const {
00179 return m_targetPhrase.GetFactor(pos, factorType);
00180 }
00182 inline const Word &GetWord(size_t pos) const {
00183 const Hypothesis *hypo = this;
00184 while (pos < hypo->GetCurrTargetWordsRange().GetStartPos()) {
00185 hypo = hypo->GetPrevHypo();
00186 CHECK(hypo != NULL);
00187 }
00188 return hypo->GetCurrWord(pos - hypo->GetCurrTargetWordsRange().GetStartPos());
00189 }
00190 inline const Factor* GetFactor(size_t pos, FactorType factorType) const {
00191 return GetWord(pos)[factorType];
00192 }
00193
00194
00195
00196
00197 inline const WordsBitmap &GetWordsBitmap() const {
00198 return m_sourceCompleted;
00199 }
00200
00201 inline bool IsSourceCompleted() const {
00202 return m_sourceCompleted.IsComplete();
00203 }
00204
00205 int RecombineCompare(const Hypothesis &compare) const;
00206
00207 void ToStream(std::ostream& out) const {
00208 if (m_prevHypo != NULL) {
00209 m_prevHypo->ToStream(out);
00210 }
00211 out << (Phrase) GetCurrTargetPhrase();
00212 }
00213
00214 TO_STRING();
00215
00216 inline void SetWinningHypo(const Hypothesis *hypo) {
00217 m_winningHypo = hypo;
00218 }
00219 inline const Hypothesis *GetWinningHypo() const {
00220 return m_winningHypo;
00221 }
00222
00223 void AddArc(Hypothesis *loserHypo);
00224 void CleanupArcList();
00225
00227 inline const ArcList* GetArcList() const {
00228 return m_arcList;
00229 }
00230 const ScoreComponentCollection& GetScoreBreakdown() const {
00231 return m_scoreBreakdown;
00232 }
00233 float GetTotalScore() const {
00234 return m_totalScore;
00235 }
00236 float GetScore() const {
00237 return m_totalScore-m_futureScore;
00238 }
00239
00240
00241
00242
00244 size_t GetNextStartPos(const TranslationOption &transOpt) const;
00245
00246 std::vector<std::vector<unsigned int> > *GetLMStats() const {
00247 return NULL;
00248 }
00249
00250 const TranslationOption &GetTranslationOption() const {
00251 return *m_transOpt;
00252 }
00253 };
00254
00255 std::ostream& operator<<(std::ostream& out, const Hypothesis& hypothesis);
00256
00257
00258 struct CompareHypothesisTotalScore {
00259 bool operator()(const Hypothesis* hypo1, const Hypothesis* hypo2) const {
00260 return hypo1->GetTotalScore() > hypo2->GetTotalScore();
00261 }
00262 };
00263
00264 #ifdef USE_HYPO_POOL
00265
00266 #define FREEHYPO(hypo) \
00267 { \
00268 ObjectPool<Hypothesis> &pool = Hypothesis::GetObjectPool(); \
00269 pool.freeObject(hypo); \
00270 } \
00271
00272 #else
00273 #define FREEHYPO(hypo) delete hypo
00274 #endif
00275
00285 class HypothesisRecombinationOrderer
00286 {
00287 public:
00288 bool operator()(const Hypothesis* hypoA, const Hypothesis* hypoB) const {
00289 return (hypoA->RecombineCompare(*hypoB) < 0);
00290 }
00291 };
00292
00293 }
00294 #endif