00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef moses_BitmapContainer_h
00023 #define moses_BitmapContainer_h
00024
00025 #include <queue>
00026 #include <set>
00027 #include <vector>
00028
00029 #include "Hypothesis.h"
00030 #include "HypothesisStackCubePruning.h"
00031 #include "SquareMatrix.h"
00032 #include "TranslationOption.h"
00033 #include "TypeDef.h"
00034 #include "Bitmap.h"
00035
00036 #include <boost/unordered_set.hpp>
00037
00038 namespace Moses
00039 {
00040
00041 class BitmapContainer;
00042 class BackwardsEdge;
00043 class Hypothesis;
00044 class HypothesisStackCubePruning;
00045 class HypothesisQueueItem;
00046 class QueueItemOrderer;
00047 class TranslationOptionList;
00048
00049 typedef std::vector< Hypothesis* > HypothesisSet;
00050 typedef std::set< BackwardsEdge* > BackwardsEdgeSet;
00051 typedef std::priority_queue< HypothesisQueueItem*, std::vector< HypothesisQueueItem* >, QueueItemOrderer> HypothesisQueue;
00052
00054
00056
00058 class HypothesisQueueItem
00059 {
00060 private:
00061 size_t m_hypothesis_pos, m_translation_pos;
00062 Hypothesis *m_hypothesis;
00063 BackwardsEdge *m_edge;
00064 boost::shared_ptr<TargetPhrase> m_target_phrase;
00065
00066 HypothesisQueueItem();
00067
00068 public:
00069 HypothesisQueueItem(const size_t hypothesis_pos
00070 , const size_t translation_pos
00071 , Hypothesis *hypothesis
00072 , BackwardsEdge *edge
00073 , const TargetPhrase *target_phrase = NULL)
00074 : m_hypothesis_pos(hypothesis_pos)
00075 , m_translation_pos(translation_pos)
00076 , m_hypothesis(hypothesis)
00077 , m_edge(edge) {
00078 if (target_phrase != NULL) {
00079 m_target_phrase.reset(new TargetPhrase(*target_phrase));
00080 }
00081 }
00082
00083 ~HypothesisQueueItem() {
00084 }
00085
00086 int GetHypothesisPos() {
00087 return m_hypothesis_pos;
00088 }
00089
00090 int GetTranslationPos() {
00091 return m_translation_pos;
00092 }
00093
00094 Hypothesis *GetHypothesis() {
00095 return m_hypothesis;
00096 }
00097
00098 BackwardsEdge *GetBackwardsEdge() {
00099 return m_edge;
00100 }
00101
00102 boost::shared_ptr<TargetPhrase> GetTargetPhrase() {
00103 return m_target_phrase;
00104 }
00105 };
00106
00108 class QueueItemOrderer
00109 {
00110 public:
00111 bool operator()(HypothesisQueueItem* itemA, HypothesisQueueItem* itemB) const {
00112 float scoreA = itemA->GetHypothesis()->GetFutureScore();
00113 float scoreB = itemB->GetHypothesis()->GetFutureScore();
00114
00115 if (scoreA < scoreB) {
00116 return true;
00117 } else if (scoreA > scoreB) {
00118 return false;
00119 } else {
00120
00121
00122
00123
00124
00125
00126 boost::shared_ptr<TargetPhrase> phrA = itemA->GetTargetPhrase();
00127 boost::shared_ptr<TargetPhrase> phrB = itemB->GetTargetPhrase();
00128 if (!phrA || !phrB) {
00129
00130 return false;
00131 }
00132 return (phrA->Compare(*phrB) > 0);
00133 }
00134 }
00135 };
00136
00138
00140
00142
00143 class HypothesisScoreOrderer
00144 {
00145 private:
00146 bool m_deterministic;
00147
00148 public:
00149 HypothesisScoreOrderer(const bool deterministic = false)
00150 : m_deterministic(deterministic) {}
00151
00152 bool operator()(const Hypothesis* hypoA, const Hypothesis* hypoB) const {
00153
00154 float scoreA = hypoA->GetFutureScore();
00155 float scoreB = hypoB->GetFutureScore();
00156
00157 if (scoreA > scoreB) {
00158 return true;
00159 } else if (scoreA < scoreB) {
00160 return false;
00161 } else {
00162 if (m_deterministic) {
00163
00164 return (hypoA->GetCurrTargetPhrase().Compare(hypoB->GetCurrTargetPhrase()) < 0);
00165 }
00166
00167 return false;
00168 }
00169 }
00170 };
00171
00173
00175
00177
00178 class BackwardsEdge
00179 {
00180 private:
00181 friend class BitmapContainer;
00182 bool m_initialized;
00183
00184 const BitmapContainer &m_prevBitmapContainer;
00185 BitmapContainer &m_parent;
00186 const TranslationOptionList &m_translations;
00187 const SquareMatrix &m_estimatedScores;
00188 float m_estimatedScore;
00189
00190 bool m_deterministic;
00191
00192 std::vector< const Hypothesis* > m_hypotheses;
00193 boost::unordered_set< int > m_seenPosition;
00194
00195
00196 BackwardsEdge();
00197
00198 Hypothesis *CreateHypothesis(const Hypothesis &hypothesis, const TranslationOption &transOpt);
00199 bool SeenPosition(const size_t x, const size_t y);
00200 void SetSeenPosition(const size_t x, const size_t y);
00201
00202 protected:
00203 void Initialize();
00204
00205 public:
00206 BackwardsEdge(const BitmapContainer &prevBitmapContainer
00207 , BitmapContainer &parent
00208 , const TranslationOptionList &translations
00209 , const SquareMatrix &estimatedScores
00210 , const InputType& source
00211 , const bool deterministic = false);
00212 ~BackwardsEdge();
00213
00214 bool GetInitialized();
00215 const BitmapContainer &GetBitmapContainer() const;
00216 int GetDistortionPenalty();
00217 void PushSuccessors(const size_t x, const size_t y);
00218 };
00219
00221
00223
00224
00225
00227
00228 class BitmapContainer
00229 {
00230 private:
00231 const Bitmap &m_bitmap;
00232 HypothesisStackCubePruning &m_stack;
00233 HypothesisSet m_hypotheses;
00234 BackwardsEdgeSet m_edges;
00235 HypothesisQueue m_queue;
00236 size_t m_numStackInsertions;
00237 bool m_deterministic;
00238
00239
00240 BitmapContainer();
00241 BitmapContainer(const BitmapContainer &);
00242 public:
00243 BitmapContainer(const Bitmap &bitmap
00244 , HypothesisStackCubePruning &stack
00245 , bool deterministic = false);
00246
00247
00248
00249 ~BitmapContainer();
00250
00251 void Enqueue(int hypothesis_pos, int translation_pos, Hypothesis *hypothesis, BackwardsEdge *edge);
00252 HypothesisQueueItem *Dequeue(bool keepValue=false);
00253 HypothesisQueueItem *Top() const;
00254 size_t Size();
00255 bool Empty() const;
00256
00257 const Bitmap &GetWordsBitmap() const {
00258 return m_bitmap;
00259 }
00260
00261 const HypothesisSet &GetHypotheses() const;
00262 size_t GetHypothesesSize() const;
00263 const BackwardsEdgeSet &GetBackwardsEdges();
00264
00265 void InitializeEdges();
00266 void ProcessBestHypothesis();
00267 void EnsureMinStackHyps(const size_t minNumHyps);
00268 void AddHypothesis(Hypothesis *hypothesis);
00269 void AddBackwardsEdge(BackwardsEdge *edge);
00270 void SortHypotheses();
00271 };
00272
00273 }
00274
00275 #endif