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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
|
// $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_Hypothesis_h
#define moses_Hypothesis_h
#include <iostream>
#include <memory>
#include <boost/scoped_ptr.hpp>
#include <vector>
#include "Phrase.h"
#include "TypeDef.h"
#include "WordsBitmap.h"
#include "Sentence.h"
#include "Phrase.h"
#include "GenerationDictionary.h"
#include "ScoreComponentCollection.h"
#include "InputType.h"
#include "ObjectPool.h"
#ifdef HAVE_XMLRPC_C
#include <xmlrpc-c/base.hpp>
#endif
namespace Moses
{
class SquareMatrix;
class StaticData;
class TranslationOption;
class WordsRange;
class Hypothesis;
class FFState;
class StatelessFeatureFunction;
class StatefulFeatureFunction;
class Manager;
typedef std::vector<Hypothesis*> ArcList;
/** Used to store a state in the beam search
for the best translation. With its link back to the previous hypothesis
m_prevHypo, we can trace back to the sentence start to read of the
(partial) translation to this point.
The expansion of hypotheses is handled in the class Manager, which
stores active hypothesis in the search in hypothesis stacks.
***/
class Hypothesis
{
friend std::ostream& operator<<(std::ostream&, const Hypothesis&);
protected:
static ObjectPool<Hypothesis> s_objectPool;
const Hypothesis* m_prevHypo; /*! backpointer to previous hypothesis (from which this one was created) */
// const Phrase &m_targetPhrase; /*! target phrase being created at the current decoding step */
WordsBitmap m_sourceCompleted; /*! keeps track of which words have been translated so far */
//TODO: how to integrate this into confusion network framework; what if
//it's a confusion network in the end???
InputType const& m_sourceInput;
WordsRange m_currSourceWordsRange; /*! source word positions of the last phrase that was used to create this hypothesis */
WordsRange m_currTargetWordsRange; /*! target word positions of the last phrase that was used to create this hypothesis */
bool m_wordDeleted;
float m_totalScore; /*! score so far */
float m_futureScore; /*! estimated future cost to translate rest of sentence */
/*! sum of scores of this hypothesis, and previous hypotheses. Lazily initialised. */
mutable boost::scoped_ptr<ScoreComponentCollection> m_scoreBreakdown;
ScoreComponentCollection m_currScoreBreakdown; /*! scores for this hypothesis only */
std::vector<const FFState*> m_ffStates;
const Hypothesis *m_winningHypo;
ArcList *m_arcList; /*! all arcs that end at the same trellis point as this hypothesis */
const TranslationOption &m_transOpt;
Manager& m_manager;
int m_id; /*! numeric ID of this hypothesis, used for logging */
/*! used by initial seeding of the translation process */
Hypothesis(Manager& manager, InputType const& source, const TranslationOption &initialTransOpt);
/*! used when creating a new hypothesis using a translation option (phrase translation) */
Hypothesis(const Hypothesis &prevHypo, const TranslationOption &transOpt);
public:
static ObjectPool<Hypothesis> &GetObjectPool() {
return s_objectPool;
}
~Hypothesis();
/** return the subclass of Hypothesis most appropriate to the given translation option */
static Hypothesis* Create(const Hypothesis &prevHypo, const TranslationOption &transOpt);
static Hypothesis* Create(Manager& manager, const WordsBitmap &initialCoverage);
/** return the subclass of Hypothesis most appropriate to the given target phrase */
static Hypothesis* Create(Manager& manager, InputType const& source, const TranslationOption &initialTransOpt);
/** return the subclass of Hypothesis most appropriate to the given translation option */
Hypothesis* CreateNext(const TranslationOption &transOpt) const;
void PrintHypothesis() const;
const InputType& GetInput() const {
return m_sourceInput;
}
/** return target phrase used to create this hypothesis */
// const Phrase &GetCurrTargetPhrase() const
const TargetPhrase &GetCurrTargetPhrase() const;
/** return input positions covered by the translation option (phrasal translation) used to create this hypothesis */
inline const WordsRange &GetCurrSourceWordsRange() const {
return m_currSourceWordsRange;
}
inline const WordsRange &GetCurrTargetWordsRange() const {
return m_currTargetWordsRange;
}
Manager& GetManager() const {
return m_manager;
}
/** output length of the translation option used to create this hypothesis */
inline size_t GetCurrTargetLength() const {
return m_currTargetWordsRange.GetNumWordsCovered();
}
void EvaluateWhenApplied(float futureScore);
int GetId()const {
return m_id;
}
const Hypothesis* GetPrevHypo() const;
/** length of the partial translation (from the start of the sentence) */
inline size_t GetSize() const {
return m_currTargetWordsRange.GetEndPos() + 1;
}
std::string GetSourcePhraseStringRep(const std::vector<FactorType> factorsToPrint) const;
std::string GetTargetPhraseStringRep(const std::vector<FactorType> factorsToPrint) const;
std::string GetSourcePhraseStringRep() const;
std::string GetTargetPhraseStringRep() const;
/** curr - pos is relative from CURRENT hypothesis's starting index
* (ie, start of sentence would be some negative number, which is
* not allowed- USE WITH CAUTION) */
inline const Word &GetCurrWord(size_t pos) const {
return GetCurrTargetPhrase().GetWord(pos);
}
inline const Factor *GetCurrFactor(size_t pos, FactorType factorType) const {
return GetCurrTargetPhrase().GetFactor(pos, factorType);
}
/** recursive - pos is relative from start of sentence */
inline const Word &GetWord(size_t pos) const {
const Hypothesis *hypo = this;
while (pos < hypo->GetCurrTargetWordsRange().GetStartPos()) {
hypo = hypo->GetPrevHypo();
UTIL_THROW_IF2(hypo == NULL, "Previous hypothesis should not be NULL");
}
return hypo->GetCurrWord(pos - hypo->GetCurrTargetWordsRange().GetStartPos());
}
inline const Factor* GetFactor(size_t pos, FactorType factorType) const {
return GetWord(pos)[factorType];
}
/***
* \return The bitmap of source words we cover
*/
inline const WordsBitmap &GetWordsBitmap() const {
return m_sourceCompleted;
}
inline bool IsSourceCompleted() const {
return m_sourceCompleted.IsComplete();
}
void GetOutputPhrase(Phrase &out) const;
void ToStream(std::ostream& out) const {
Phrase ret;
GetOutputPhrase(ret);
out << ret;
}
void ToStringStream(std::stringstream& out) const {
if (m_prevHypo != NULL) {
m_prevHypo->ToStream(out);
}
out << (const Phrase&) GetCurrTargetPhrase();
}
std::string GetOutputString() const {
std::stringstream out;
ToStringStream(out);
return out.str();
}
TO_STRING();
inline void SetWinningHypo(const Hypothesis *hypo) {
m_winningHypo = hypo;
}
inline const Hypothesis *GetWinningHypo() const {
return m_winningHypo;
}
void AddArc(Hypothesis *loserHypo);
void CleanupArcList();
//! returns a list alternative previous hypotheses (or NULL if n-best support is disabled)
inline const ArcList* GetArcList() const {
return m_arcList;
}
const ScoreComponentCollection& GetScoreBreakdown() const {
if (!m_scoreBreakdown.get()) {
m_scoreBreakdown.reset(new ScoreComponentCollection());
m_scoreBreakdown->PlusEquals(m_currScoreBreakdown);
if (m_prevHypo) {
m_scoreBreakdown->PlusEquals(m_prevHypo->GetScoreBreakdown());
}
}
return *(m_scoreBreakdown.get());
}
float GetTotalScore() const {
return m_totalScore;
}
float GetScore() const {
return m_totalScore-m_futureScore;
}
const FFState* GetFFState(int idx) const {
return m_ffStates[idx];
}
void SetFFState(int idx, FFState* state) {
m_ffStates[idx] = state;
}
// Added by oliver.wilson@ed.ac.uk for async lm stuff.
void EvaluateWhenApplied(const StatefulFeatureFunction &sfff, int state_idx);
void EvaluateWhenApplied(const StatelessFeatureFunction &slff);
//! target span that trans opt would populate if applied to this hypo. Used for alignment check
size_t GetNextStartPos(const TranslationOption &transOpt) const;
std::vector<std::vector<unsigned int> > *GetLMStats() const {
return NULL;
}
const TranslationOption &GetTranslationOption() const {
return m_transOpt;
}
void OutputAlignment(std::ostream &out) const;
static void OutputAlignment(std::ostream &out, const std::vector<const Hypothesis *> &edges);
static void OutputAlignment(std::ostream &out, const Moses::AlignmentInfo &ai, size_t sourceOffset, size_t targetOffset);
void OutputInput(std::ostream& os) const;
static void OutputInput(std::vector<const Phrase*>& map, const Hypothesis* hypo);
void OutputBestSurface(std::ostream &out, const std::vector<Moses::FactorType> &outputFactorOrder, char reportSegmentation, bool reportAllFactors) const;
void OutputSurface(std::ostream &out, const Hypothesis &edge, const std::vector<FactorType> &outputFactorOrder,
char reportSegmentation, bool reportAllFactors) const;
// creates a map of TARGET positions which should be replaced by word using placeholder
std::map<size_t, const Moses::Factor*> GetPlaceholders(const Moses::Hypothesis &hypo, Moses::FactorType placeholderFactor) const;
// for unordered_set in stack
size_t hash() const;
bool operator==(const Hypothesis& other) const;
#ifdef HAVE_XMLRPC_C
void OutputWordAlignment(std::vector<xmlrpc_c::value>& out) const;
void OutputLocalWordAlignment(std::vector<xmlrpc_c::value>& dest) const;
#endif
};
std::ostream& operator<<(std::ostream& out, const Hypothesis& hypothesis);
// sorting helper
struct CompareHypothesisTotalScore {
bool operator()(const Hypothesis* hypo1, const Hypothesis* hypo2) const {
return hypo1->GetTotalScore() > hypo2->GetTotalScore();
}
};
#ifdef USE_HYPO_POOL
#define FREEHYPO(hypo) \
{ \
ObjectPool<Hypothesis> &pool = Hypothesis::GetObjectPool(); \
pool.freeObject(hypo); \
} \
#else
#define FREEHYPO(hypo) delete hypo
#endif
}
#endif
|