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
00054 class HypothesisQueueItem
00055 {
00056 private:
00057 size_t m_hypothesis_pos, m_translation_pos;
00058 Hypothesis *m_hypothesis;
00059 BackwardsEdge *m_edge;
00060
00061 HypothesisQueueItem();
00062
00063 public:
00064 HypothesisQueueItem(const size_t hypothesis_pos
00065 , const size_t translation_pos
00066 , Hypothesis *hypothesis
00067 , BackwardsEdge *edge)
00068 : m_hypothesis_pos(hypothesis_pos)
00069 , m_translation_pos(translation_pos)
00070 , m_hypothesis(hypothesis)
00071 , m_edge(edge) {
00072 }
00073
00074 ~HypothesisQueueItem() {
00075 }
00076
00077 int GetHypothesisPos() {
00078 return m_hypothesis_pos;
00079 }
00080
00081 int GetTranslationPos() {
00082 return m_translation_pos;
00083 }
00084
00085 Hypothesis *GetHypothesis() {
00086 return m_hypothesis;
00087 }
00088
00089 BackwardsEdge *GetBackwardsEdge() {
00090 return m_edge;
00091 }
00092 };
00093
00094
00095 class QueueItemOrderer
00096 {
00097 public:
00098 bool operator()(HypothesisQueueItem* itemA, HypothesisQueueItem* itemB) const {
00099 float scoreA = itemA->GetHypothesis()->GetTotalScore();
00100 float scoreB = itemB->GetHypothesis()->GetTotalScore();
00101
00102 return (scoreA < scoreB);
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 }
00117 };
00118
00120
00122
00124
00125 class HypothesisScoreOrderer
00126 {
00127 public:
00128 bool operator()(const Hypothesis* hypoA, const Hypothesis* hypoB) const {
00129 float scoreA = hypoA->GetTotalScore();
00130 float scoreB = hypoB->GetTotalScore();
00131
00132 return (scoreA > scoreB);
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 }
00146 };
00147
00149
00151
00153
00154 class BackwardsEdge
00155 {
00156 private:
00157 friend class BitmapContainer;
00158 bool m_initialized;
00159
00160 const BitmapContainer &m_prevBitmapContainer;
00161 BitmapContainer &m_parent;
00162 const TranslationOptionList &m_translations;
00163 const SquareMatrix &m_futurescore;
00164
00165 std::vector< const Hypothesis* > m_hypotheses;
00166 std::set< int > m_seenPosition;
00167
00168
00169 BackwardsEdge();
00170
00171 Hypothesis *CreateHypothesis(const Hypothesis &hypothesis, const TranslationOption &transOpt);
00172 bool SeenPosition(const size_t x, const size_t y);
00173 void SetSeenPosition(const size_t x, const size_t y);
00174
00175 protected:
00176 void Initialize();
00177
00178 public:
00179 BackwardsEdge(const BitmapContainer &prevBitmapContainer
00180 , BitmapContainer &parent
00181 , const TranslationOptionList &translations
00182 , const SquareMatrix &futureScore,
00183 const InputType& source,
00184 const TranslationSystem* system);
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