|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef moses_BitmapContainer_h |
|
#define moses_BitmapContainer_h |
|
|
|
#include <queue> |
|
#include <set> |
|
#include <vector> |
|
|
|
#include "Hypothesis.h" |
|
#include "HypothesisStackCubePruning.h" |
|
#include "SquareMatrix.h" |
|
#include "TranslationOption.h" |
|
#include "TypeDef.h" |
|
#include "Bitmap.h" |
|
|
|
#include <boost/unordered_set.hpp> |
|
|
|
namespace Moses |
|
{ |
|
|
|
class BitmapContainer; |
|
class BackwardsEdge; |
|
class Hypothesis; |
|
class HypothesisStackCubePruning; |
|
class HypothesisQueueItem; |
|
class QueueItemOrderer; |
|
class TranslationOptionList; |
|
|
|
typedef std::vector< Hypothesis* > HypothesisSet; |
|
typedef std::set< BackwardsEdge* > BackwardsEdgeSet; |
|
typedef std::priority_queue< HypothesisQueueItem*, std::vector< HypothesisQueueItem* >, QueueItemOrderer> HypothesisQueue; |
|
|
|
|
|
|
|
|
|
|
|
|
|
class HypothesisQueueItem |
|
{ |
|
private: |
|
size_t m_hypothesis_pos, m_translation_pos; |
|
Hypothesis *m_hypothesis; |
|
BackwardsEdge *m_edge; |
|
boost::shared_ptr<TargetPhrase> m_target_phrase; |
|
|
|
HypothesisQueueItem(); |
|
|
|
public: |
|
HypothesisQueueItem(const size_t hypothesis_pos |
|
, const size_t translation_pos |
|
, Hypothesis *hypothesis |
|
, BackwardsEdge *edge |
|
, const TargetPhrase *target_phrase = NULL) |
|
: m_hypothesis_pos(hypothesis_pos) |
|
, m_translation_pos(translation_pos) |
|
, m_hypothesis(hypothesis) |
|
, m_edge(edge) { |
|
if (target_phrase != NULL) { |
|
m_target_phrase.reset(new TargetPhrase(*target_phrase)); |
|
} |
|
} |
|
|
|
~HypothesisQueueItem() { |
|
} |
|
|
|
int GetHypothesisPos() { |
|
return m_hypothesis_pos; |
|
} |
|
|
|
int GetTranslationPos() { |
|
return m_translation_pos; |
|
} |
|
|
|
Hypothesis *GetHypothesis() { |
|
return m_hypothesis; |
|
} |
|
|
|
BackwardsEdge *GetBackwardsEdge() { |
|
return m_edge; |
|
} |
|
|
|
boost::shared_ptr<TargetPhrase> GetTargetPhrase() { |
|
return m_target_phrase; |
|
} |
|
}; |
|
|
|
|
|
class QueueItemOrderer |
|
{ |
|
public: |
|
bool operator()(HypothesisQueueItem* itemA, HypothesisQueueItem* itemB) const { |
|
float scoreA = itemA->GetHypothesis()->GetFutureScore(); |
|
float scoreB = itemB->GetHypothesis()->GetFutureScore(); |
|
|
|
if (scoreA < scoreB) { |
|
return true; |
|
} else if (scoreA > scoreB) { |
|
return false; |
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
|
|
boost::shared_ptr<TargetPhrase> phrA = itemA->GetTargetPhrase(); |
|
boost::shared_ptr<TargetPhrase> phrB = itemB->GetTargetPhrase(); |
|
if (!phrA || !phrB) { |
|
|
|
return false; |
|
} |
|
return (phrA->Compare(*phrB) > 0); |
|
} |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class HypothesisScoreOrderer |
|
{ |
|
private: |
|
bool m_deterministic; |
|
|
|
public: |
|
HypothesisScoreOrderer(const bool deterministic = false) |
|
: m_deterministic(deterministic) {} |
|
|
|
bool operator()(const Hypothesis* hypoA, const Hypothesis* hypoB) const { |
|
|
|
float scoreA = hypoA->GetFutureScore(); |
|
float scoreB = hypoB->GetFutureScore(); |
|
|
|
if (scoreA > scoreB) { |
|
return true; |
|
} else if (scoreA < scoreB) { |
|
return false; |
|
} else { |
|
if (m_deterministic) { |
|
|
|
return (hypoA->GetCurrTargetPhrase().Compare(hypoB->GetCurrTargetPhrase()) < 0); |
|
} |
|
|
|
return false; |
|
} |
|
} |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BackwardsEdge |
|
{ |
|
private: |
|
friend class BitmapContainer; |
|
bool m_initialized; |
|
|
|
const BitmapContainer &m_prevBitmapContainer; |
|
BitmapContainer &m_parent; |
|
const TranslationOptionList &m_translations; |
|
const SquareMatrix &m_estimatedScores; |
|
float m_estimatedScore; |
|
|
|
bool m_deterministic; |
|
|
|
std::vector< const Hypothesis* > m_hypotheses; |
|
boost::unordered_set< int > m_seenPosition; |
|
|
|
|
|
BackwardsEdge(); |
|
|
|
Hypothesis *CreateHypothesis(const Hypothesis &hypothesis, const TranslationOption &transOpt); |
|
bool SeenPosition(const size_t x, const size_t y); |
|
void SetSeenPosition(const size_t x, const size_t y); |
|
|
|
protected: |
|
void Initialize(); |
|
|
|
public: |
|
BackwardsEdge(const BitmapContainer &prevBitmapContainer |
|
, BitmapContainer &parent |
|
, const TranslationOptionList &translations |
|
, const SquareMatrix &estimatedScores |
|
, const InputType& source |
|
, const bool deterministic = false); |
|
~BackwardsEdge(); |
|
|
|
bool GetInitialized(); |
|
const BitmapContainer &GetBitmapContainer() const; |
|
int GetDistortionPenalty(); |
|
void PushSuccessors(const size_t x, const size_t y); |
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class BitmapContainer |
|
{ |
|
private: |
|
const Bitmap &m_bitmap; |
|
HypothesisStackCubePruning &m_stack; |
|
HypothesisSet m_hypotheses; |
|
BackwardsEdgeSet m_edges; |
|
HypothesisQueue m_queue; |
|
size_t m_numStackInsertions; |
|
bool m_deterministic; |
|
|
|
|
|
BitmapContainer(); |
|
BitmapContainer(const BitmapContainer &); |
|
public: |
|
BitmapContainer(const Bitmap &bitmap |
|
, HypothesisStackCubePruning &stack |
|
, bool deterministic = false); |
|
|
|
|
|
|
|
~BitmapContainer(); |
|
|
|
void Enqueue(int hypothesis_pos, int translation_pos, Hypothesis *hypothesis, BackwardsEdge *edge); |
|
HypothesisQueueItem *Dequeue(bool keepValue=false); |
|
HypothesisQueueItem *Top() const; |
|
size_t Size(); |
|
bool Empty() const; |
|
|
|
const Bitmap &GetWordsBitmap() const { |
|
return m_bitmap; |
|
} |
|
|
|
const HypothesisSet &GetHypotheses() const; |
|
size_t GetHypothesesSize() const; |
|
const BackwardsEdgeSet &GetBackwardsEdges(); |
|
|
|
void InitializeEdges(); |
|
void ProcessBestHypothesis(); |
|
void EnsureMinStackHyps(const size_t minNumHyps); |
|
void AddHypothesis(Hypothesis *hypothesis); |
|
void AddBackwardsEdge(BackwardsEdge *edge); |
|
void SortHypotheses(); |
|
}; |
|
|
|
} |
|
|
|
#endif |
|
|