diff options
author | Hieu Hoang <hieuhoang@gmail.com> | 2012-11-12 23:56:18 +0400 |
---|---|---|
committer | Hieu Hoang <hieuhoang@gmail.com> | 2012-11-12 23:56:18 +0400 |
commit | 5e3ef23cef6101d2c098eb3445f562e8f595655b (patch) | |
tree | b8c332b6fa82bae84ea4910967a10ba1b08a7107 /moses/BitmapContainer.h | |
parent | 8c785cff2b1be3cccd76ea9026f71b649762dfc3 (diff) |
move moses/src/* to moses/
Diffstat (limited to 'moses/BitmapContainer.h')
-rw-r--r-- | moses/BitmapContainer.h | 244 |
1 files changed, 244 insertions, 0 deletions
diff --git a/moses/BitmapContainer.h b/moses/BitmapContainer.h new file mode 100644 index 000000000..3b9654468 --- /dev/null +++ b/moses/BitmapContainer.h @@ -0,0 +1,244 @@ +// $Id$ +// vim:tabstop=2 +/*********************************************************************** +Moses - factored phrase-based language decoder +Copyright (C) 2006 University of Edinburgh + +This library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. + +This library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public +License along with this library; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +***********************************************************************/ + +#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 "WordsBitmap.h" + +namespace Moses +{ + +class BitmapContainer; +class BackwardsEdge; +class Hypothesis; +class HypothesisStackCubePruning; +class HypothesisQueueItem; +class QueueItemOrderer; + +typedef std::vector< Hypothesis* > HypothesisSet; +typedef std::set< BackwardsEdge* > BackwardsEdgeSet; +typedef std::priority_queue< HypothesisQueueItem*, std::vector< HypothesisQueueItem* >, QueueItemOrderer> HypothesisQueue; + +//////////////////////////////////////////////////////////////////////////////// +// Hypothesis Priority Queue Code +//////////////////////////////////////////////////////////////////////////////// + +//! 1 item in the priority queue for stack decoding (phrase-based) +class HypothesisQueueItem +{ +private: + size_t m_hypothesis_pos, m_translation_pos; + Hypothesis *m_hypothesis; + BackwardsEdge *m_edge; + + HypothesisQueueItem(); + +public: + HypothesisQueueItem(const size_t hypothesis_pos + , const size_t translation_pos + , Hypothesis *hypothesis + , BackwardsEdge *edge) + : m_hypothesis_pos(hypothesis_pos) + , m_translation_pos(translation_pos) + , m_hypothesis(hypothesis) + , m_edge(edge) { + } + + ~HypothesisQueueItem() { + } + + int GetHypothesisPos() { + return m_hypothesis_pos; + } + + int GetTranslationPos() { + return m_translation_pos; + } + + Hypothesis *GetHypothesis() { + return m_hypothesis; + } + + BackwardsEdge *GetBackwardsEdge() { + return m_edge; + } +}; + +//! Allows comparison of two HypothesisQueueItem objects by the corresponding scores. +class QueueItemOrderer +{ +public: + bool operator()(HypothesisQueueItem* itemA, HypothesisQueueItem* itemB) const { + float scoreA = itemA->GetHypothesis()->GetTotalScore(); + float scoreB = itemB->GetHypothesis()->GetTotalScore(); + + return (scoreA < scoreB); + + /* + { + return true; + } + else if (scoreA < scoreB) + { + return false; + } + else + { + return itemA < itemB; + }*/ + } +}; + +//////////////////////////////////////////////////////////////////////////////// +// Hypothesis Orderer Code +//////////////////////////////////////////////////////////////////////////////// +// Allows to compare two Hypothesis objects by the corresponding scores. +//////////////////////////////////////////////////////////////////////////////// + +class HypothesisScoreOrderer +{ +public: + bool operator()(const Hypothesis* hypoA, const Hypothesis* hypoB) const { + float scoreA = hypoA->GetTotalScore(); + float scoreB = hypoB->GetTotalScore(); + + return (scoreA > scoreB); + /* + { + return true; + } + else if (scoreA < scoreB) + { + return false; + } + else + { + return hypoA < hypoB; + }*/ + } +}; + +//////////////////////////////////////////////////////////////////////////////// +// Backwards Edge Code +//////////////////////////////////////////////////////////////////////////////// +// Encodes an edge pointing to a BitmapContainer. +//////////////////////////////////////////////////////////////////////////////// + +class BackwardsEdge +{ +private: + friend class BitmapContainer; + bool m_initialized; + + const BitmapContainer &m_prevBitmapContainer; + BitmapContainer &m_parent; + const TranslationOptionList &m_translations; + const SquareMatrix &m_futurescore; + + std::vector< const Hypothesis* > m_hypotheses; + std::set< int > m_seenPosition; + + // We don't want to instantiate "empty" objects. + 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 &futureScore, + const InputType& source, + const TranslationSystem* system); + ~BackwardsEdge(); + + bool GetInitialized(); + const BitmapContainer &GetBitmapContainer() const; + int GetDistortionPenalty(); + void PushSuccessors(const size_t x, const size_t y); +}; + +//////////////////////////////////////////////////////////////////////////////// +// Bitmap Container Code +//////////////////////////////////////////////////////////////////////////////// +// A BitmapContainer encodes an ordered set of hypotheses and a set of edges +// pointing to the "generating" BitmapContainers. It also stores a priority +// queue that contains expanded hypotheses from the connected edges. +//////////////////////////////////////////////////////////////////////////////// + +class BitmapContainer +{ +private: + WordsBitmap m_bitmap; + HypothesisStackCubePruning &m_stack; + HypothesisSet m_hypotheses; + BackwardsEdgeSet m_edges; + HypothesisQueue m_queue; + size_t m_numStackInsertions; + + // We always require a corresponding bitmap to be supplied. + BitmapContainer(); + BitmapContainer(const BitmapContainer &); +public: + BitmapContainer(const WordsBitmap &bitmap + , HypothesisStackCubePruning &stack); + + // The destructor will also delete all the edges that are + // connected to this BitmapContainer. + ~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 WordsBitmap &GetWordsBitmap(); + 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 |