File size: 8,730 Bytes
158b61b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
// $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 "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;
////////////////////////////////////////////////////////////////////////////////
// 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;
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;
}
};
//! Allows comparison of two HypothesisQueueItem objects by the corresponding scores.
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 {
// Equal scores: break ties by comparing target phrases (if they exist)
// *Important*: these are pointers to copies of the target phrases from the
// hypotheses. This class is used to keep priority queues ordered in the
// background, so comparisons made as those data structures are cleaned up
// may occur *after* the target phrases in hypotheses have been cleaned up,
// leading to segfaults if relying on hypotheses to provide target phrases.
boost::shared_ptr<TargetPhrase> phrA = itemA->GetTargetPhrase();
boost::shared_ptr<TargetPhrase> phrB = itemB->GetTargetPhrase();
if (!phrA || !phrB) {
// Fallback: scoreA < scoreB == false, non-deterministic sort
return false;
}
return (phrA->Compare(*phrB) > 0);
}
}
};
////////////////////////////////////////////////////////////////////////////////
// Hypothesis Orderer Code
////////////////////////////////////////////////////////////////////////////////
// Allows to compare two Hypothesis objects by the corresponding scores.
////////////////////////////////////////////////////////////////////////////////
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) {
// Equal scores: break ties by comparing target phrases
return (hypoA->GetCurrTargetPhrase().Compare(hypoB->GetCurrTargetPhrase()) < 0);
}
// Fallback: scoreA > scoreB == false, non-deterministic sort
return false;
}
}
};
////////////////////////////////////////////////////////////////////////////////
// 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_estimatedScores;
float m_estimatedScore;
bool m_deterministic;
std::vector< const Hypothesis* > m_hypotheses;
boost::unordered_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 &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);
};
////////////////////////////////////////////////////////////////////////////////
// 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:
const Bitmap &m_bitmap;
HypothesisStackCubePruning &m_stack;
HypothesisSet m_hypotheses;
BackwardsEdgeSet m_edges;
HypothesisQueue m_queue;
size_t m_numStackInsertions;
bool m_deterministic;
// We always require a corresponding bitmap to be supplied.
BitmapContainer();
BitmapContainer(const BitmapContainer &);
public:
BitmapContainer(const Bitmap &bitmap
, HypothesisStackCubePruning &stack
, bool deterministic = false);
// 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 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
|