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 "WordsBitmap.h"
00035
00036 namespace Moses
00037 {
00038
00039 class BitmapContainer;
00040 class BackwardsEdge;
00041 class Hypothesis;
00042 class HypothesisStackCubePruning;
00043 class HypothesisQueueItem;
00044 class QueueItemOrderer;
00045
00046 typedef std::vector< Hypothesis* > HypothesisSet;
00047 typedef std::set< BackwardsEdge* > BackwardsEdgeSet;
00048 typedef std::priority_queue< HypothesisQueueItem*, std::vector< HypothesisQueueItem* >, QueueItemOrderer> HypothesisQueue;
00049
00051
00053
00055 class HypothesisQueueItem
00056 {
00057 private:
00058 size_t m_hypothesis_pos, m_translation_pos;
00059 Hypothesis *m_hypothesis;
00060 BackwardsEdge *m_edge;
00061
00062 HypothesisQueueItem();
00063
00064 public:
00065 HypothesisQueueItem(const size_t hypothesis_pos
00066 , const size_t translation_pos
00067 , Hypothesis *hypothesis
00068 , BackwardsEdge *edge)
00069 : m_hypothesis_pos(hypothesis_pos)
00070 , m_translation_pos(translation_pos)
00071 , m_hypothesis(hypothesis)
00072 , m_edge(edge) {
00073 }
00074
00075 ~HypothesisQueueItem() {
00076 }
00077
00078 int GetHypothesisPos() {
00079 return m_hypothesis_pos;
00080 }
00081
00082 int GetTranslationPos() {
00083 return m_translation_pos;
00084 }
00085
00086 Hypothesis *GetHypothesis() {
00087 return m_hypothesis;
00088 }
00089
00090 BackwardsEdge *GetBackwardsEdge() {
00091 return m_edge;
00092 }
00093 };
00094
00096 class QueueItemOrderer
00097 {
00098 public:
00099 bool operator()(HypothesisQueueItem* itemA, HypothesisQueueItem* itemB) const {
00100 float scoreA = itemA->GetHypothesis()->GetTotalScore();
00101 float scoreB = itemB->GetHypothesis()->GetTotalScore();
00102
00103 return (scoreA < scoreB);
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 }
00118 };
00119
00121
00123
00125
00126 class HypothesisScoreOrderer
00127 {
00128 public:
00129 bool operator()(const Hypothesis* hypoA, const Hypothesis* hypoB) const {
00130 float scoreA = hypoA->GetTotalScore();
00131 float scoreB = hypoB->GetTotalScore();
00132
00133 return (scoreA > scoreB);
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146 }
00147 };
00148
00150
00152
00154
00155 class BackwardsEdge
00156 {
00157 private:
00158 friend class BitmapContainer;
00159 bool m_initialized;
00160
00161 const BitmapContainer &m_prevBitmapContainer;
00162 BitmapContainer &m_parent;
00163 const TranslationOptionList &m_translations;
00164 const SquareMatrix &m_futurescore;
00165
00166 std::vector< const Hypothesis* > m_hypotheses;
00167 std::set< int > m_seenPosition;
00168
00169
00170 BackwardsEdge();
00171
00172 Hypothesis *CreateHypothesis(const Hypothesis &hypothesis, const TranslationOption &transOpt);
00173 bool SeenPosition(const size_t x, const size_t y);
00174 void SetSeenPosition(const size_t x, const size_t y);
00175
00176 protected:
00177 void Initialize();
00178
00179 public:
00180 BackwardsEdge(const BitmapContainer &prevBitmapContainer
00181 , BitmapContainer &parent
00182 , const TranslationOptionList &translations
00183 , const SquareMatrix &futureScore,
00184 const InputType& source);
00185 ~BackwardsEdge();
00186
00187 bool GetInitialized();
00188 const BitmapContainer &GetBitmapContainer() const;
00189 int GetDistortionPenalty();
00190 void PushSuccessors(const size_t x, const size_t y);
00191 };
00192
00194
00196
00197
00198
00200
00201 class BitmapContainer
00202 {
00203 private:
00204 WordsBitmap m_bitmap;
00205 HypothesisStackCubePruning &m_stack;
00206 HypothesisSet m_hypotheses;
00207 BackwardsEdgeSet m_edges;
00208 HypothesisQueue m_queue;
00209 size_t m_numStackInsertions;
00210
00211
00212 BitmapContainer();
00213 BitmapContainer(const BitmapContainer &);
00214 public:
00215 BitmapContainer(const WordsBitmap &bitmap
00216 , HypothesisStackCubePruning &stack);
00217
00218
00219
00220 ~BitmapContainer();
00221
00222 void Enqueue(int hypothesis_pos, int translation_pos, Hypothesis *hypothesis, BackwardsEdge *edge);
00223 HypothesisQueueItem *Dequeue(bool keepValue=false);
00224 HypothesisQueueItem *Top() const;
00225 size_t Size();
00226 bool Empty() const;
00227
00228 const WordsBitmap &GetWordsBitmap();
00229 const HypothesisSet &GetHypotheses() const;
00230 size_t GetHypothesesSize() const;
00231 const BackwardsEdgeSet &GetBackwardsEdges();
00232
00233 void InitializeEdges();
00234 void ProcessBestHypothesis();
00235 void EnsureMinStackHyps(const size_t minNumHyps);
00236 void AddHypothesis(Hypothesis *hypothesis);
00237 void AddBackwardsEdge(BackwardsEdge *edge);
00238 void SortHypotheses();
00239 };
00240
00241 }
00242
00243 #endif