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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
// $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
***********************************************************************/
#include <algorithm>
#include <limits>
#include <utility>
#include "BitmapContainer.h"
#include "HypothesisStackCubePruning.h"
#include "moses/FF/DistortionScoreProducer.h"
#include "TranslationOptionList.h"
namespace Moses
{
class HypothesisScoreOrdererNoDistortion
{
public:
bool operator()(const Hypothesis* hypoA, const Hypothesis* hypoB) const {
const float scoreA = hypoA->GetScore();
const float scoreB = hypoB->GetScore();
if (scoreA > scoreB) {
return true;
} else if (scoreA < scoreB) {
return false;
} else {
return hypoA < hypoB;
}
}
};
class HypothesisScoreOrdererWithDistortion
{
public:
HypothesisScoreOrdererWithDistortion(const WordsRange* transOptRange) :
m_transOptRange(transOptRange) {}
const WordsRange* m_transOptRange;
bool operator()(const Hypothesis* hypoA, const Hypothesis* hypoB) const {
UTIL_THROW_IF2(m_transOptRange == NULL, "Words range not set");
const StaticData &staticData = StaticData::Instance();
const float distortionScoreA = DistortionScoreProducer::CalculateDistortionScore(
*hypoA,
hypoA->GetCurrSourceWordsRange(),
*m_transOptRange,
hypoA->GetWordsBitmap().GetFirstGapPos()
);
const float distortionScoreB = DistortionScoreProducer::CalculateDistortionScore(
*hypoB,
hypoB->GetCurrSourceWordsRange(),
*m_transOptRange,
hypoB->GetWordsBitmap().GetFirstGapPos()
);
float totalWeightDistortion = 0;
const std::vector<FeatureFunction*> &ffs = FeatureFunction::GetFeatureFunctions();
std::vector<FeatureFunction*>::const_iterator iter;
for (iter = ffs.begin(); iter != ffs.end(); ++iter) {
const FeatureFunction *ff = *iter;
const DistortionScoreProducer *model = dynamic_cast<const DistortionScoreProducer*>(ff);
if (model) {
float weight =staticData.GetAllWeights().GetScoreForProducer(model);
totalWeightDistortion += weight;
}
}
const float scoreA = hypoA->GetScore() + distortionScoreA * totalWeightDistortion;
const float scoreB = hypoB->GetScore() + distortionScoreB * totalWeightDistortion;
if (scoreA > scoreB) {
return true;
} else if (scoreA < scoreB) {
return false;
} else {
return hypoA < hypoB;
}
}
};
////////////////////////////////////////////////////////////////////////////////
// BackwardsEdge Code
////////////////////////////////////////////////////////////////////////////////
BackwardsEdge::BackwardsEdge(const BitmapContainer &prevBitmapContainer
, BitmapContainer &parent
, const TranslationOptionList &translations
, const SquareMatrix &futureScore,
const InputType& itype)
: m_initialized(false)
, m_prevBitmapContainer(prevBitmapContainer)
, m_parent(parent)
, m_translations(translations)
, m_futurescore(futureScore)
, m_seenPosition()
{
// If either dimension is empty, we haven't got anything to do.
if(m_prevBitmapContainer.GetHypotheses().size() == 0 || m_translations.size() == 0) {
VERBOSE(3, "Empty cube on BackwardsEdge" << std::endl);
return;
}
// Fetch the things we need for distortion cost computation.
int maxDistortion = StaticData::Instance().GetMaxDistortion();
if (maxDistortion == -1) {
for (HypothesisSet::const_iterator iter = m_prevBitmapContainer.GetHypotheses().begin(); iter != m_prevBitmapContainer.GetHypotheses().end(); ++iter) {
m_hypotheses.push_back(*iter);
}
return;
}
const WordsRange &transOptRange = translations.Get(0)->GetSourceWordsRange();
HypothesisSet::const_iterator iterHypo = m_prevBitmapContainer.GetHypotheses().begin();
HypothesisSet::const_iterator iterEnd = m_prevBitmapContainer.GetHypotheses().end();
while (iterHypo != iterEnd) {
const Hypothesis &hypo = **iterHypo;
// Special case: If this is the first hypothesis used to seed the search,
// it doesn't have a valid range, and we create the hypothesis, if the
// initial position is not further into the sentence than the distortion limit.
if (hypo.GetWordsBitmap().GetNumWordsCovered() == 0) {
if ((int)transOptRange.GetStartPos() <= maxDistortion)
m_hypotheses.push_back(&hypo);
} else {
int distortionDistance = itype.ComputeDistortionDistance(hypo.GetCurrSourceWordsRange()
, transOptRange);
if (distortionDistance <= maxDistortion)
m_hypotheses.push_back(&hypo);
}
++iterHypo;
}
if (m_translations.size() > 1) {
UTIL_THROW_IF2(m_translations.Get(0)->GetFutureScore() < m_translations.Get(1)->GetFutureScore(),
"Non-monotonic future score");
}
if (m_hypotheses.size() > 1) {
UTIL_THROW_IF2(m_hypotheses[0]->GetTotalScore() < m_hypotheses[1]->GetTotalScore(),
"Non-monotonic total score");
}
HypothesisScoreOrdererWithDistortion orderer (&transOptRange);
std::sort(m_hypotheses.begin(), m_hypotheses.end(), orderer);
// std::sort(m_hypotheses.begin(), m_hypotheses.end(), HypothesisScoreOrdererNoDistortion());
}
BackwardsEdge::~BackwardsEdge()
{
m_seenPosition.clear();
m_hypotheses.clear();
}
void
BackwardsEdge::Initialize()
{
if(m_hypotheses.size() == 0 || m_translations.size() == 0) {
m_initialized = true;
return;
}
Hypothesis *expanded = CreateHypothesis(*m_hypotheses[0], *m_translations.Get(0));
m_parent.Enqueue(0, 0, expanded, this);
SetSeenPosition(0, 0);
m_initialized = true;
}
Hypothesis *BackwardsEdge::CreateHypothesis(const Hypothesis &hypothesis, const TranslationOption &transOpt)
{
// create hypothesis and calculate all its scores
Hypothesis *newHypo = hypothesis.CreateNext(transOpt); // TODO FIXME This is absolutely broken - don't pass null here
newHypo->Evaluate(m_futurescore);
return newHypo;
}
bool
BackwardsEdge::SeenPosition(const size_t x, const size_t y)
{
std::set< int >::iterator iter = m_seenPosition.find((x<<16) + y);
return (iter != m_seenPosition.end());
}
void
BackwardsEdge::SetSeenPosition(const size_t x, const size_t y)
{
UTIL_THROW_IF2(x >= (1<<17), "Error");
UTIL_THROW_IF2(y >= (1<<17), "Error");
m_seenPosition.insert((x<<16) + y);
}
bool
BackwardsEdge::GetInitialized()
{
return m_initialized;
}
const BitmapContainer&
BackwardsEdge::GetBitmapContainer() const
{
return m_prevBitmapContainer;
}
void
BackwardsEdge::PushSuccessors(const size_t x, const size_t y)
{
Hypothesis *newHypo;
if(y + 1 < m_translations.size() && !SeenPosition(x, y + 1)) {
SetSeenPosition(x, y + 1);
newHypo = CreateHypothesis(*m_hypotheses[x], *m_translations.Get(y + 1));
if(newHypo != NULL) {
m_parent.Enqueue(x, y + 1, newHypo, (BackwardsEdge*)this);
}
}
if(x + 1 < m_hypotheses.size() && !SeenPosition(x + 1, y)) {
SetSeenPosition(x + 1, y);
newHypo = CreateHypothesis(*m_hypotheses[x + 1], *m_translations.Get(y));
if(newHypo != NULL) {
m_parent.Enqueue(x + 1, y, newHypo, (BackwardsEdge*)this);
}
}
}
////////////////////////////////////////////////////////////////////////////////
// BitmapContainer Code
////////////////////////////////////////////////////////////////////////////////
BitmapContainer::BitmapContainer(const WordsBitmap &bitmap
, HypothesisStackCubePruning &stack)
: m_bitmap(bitmap)
, m_stack(stack)
, m_numStackInsertions(0)
{
m_hypotheses = HypothesisSet();
m_edges = BackwardsEdgeSet();
m_queue = HypothesisQueue();
}
BitmapContainer::~BitmapContainer()
{
// As we have created the square position objects we clean up now.
while (!m_queue.empty()) {
HypothesisQueueItem *item = m_queue.top();
m_queue.pop();
FREEHYPO( item->GetHypothesis() );
delete item;
}
// Delete all edges.
RemoveAllInColl(m_edges);
m_hypotheses.clear();
m_edges.clear();
}
void
BitmapContainer::Enqueue(int hypothesis_pos
, int translation_pos
, Hypothesis *hypothesis
, BackwardsEdge *edge)
{
HypothesisQueueItem *item = new HypothesisQueueItem(hypothesis_pos
, translation_pos
, hypothesis
, edge);
m_queue.push(item);
}
HypothesisQueueItem*
BitmapContainer::Dequeue(bool keepValue)
{
if (!m_queue.empty()) {
HypothesisQueueItem *item = m_queue.top();
if (!keepValue) {
m_queue.pop();
}
return item;
}
return NULL;
}
HypothesisQueueItem*
BitmapContainer::Top() const
{
return m_queue.top();
}
size_t
BitmapContainer::Size()
{
return m_queue.size();
}
bool
BitmapContainer::Empty() const
{
return m_queue.empty();
}
const WordsBitmap&
BitmapContainer::GetWordsBitmap()
{
return m_bitmap;
}
const HypothesisSet&
BitmapContainer::GetHypotheses() const
{
return m_hypotheses;
}
size_t
BitmapContainer::GetHypothesesSize() const
{
return m_hypotheses.size();
}
const BackwardsEdgeSet&
BitmapContainer::GetBackwardsEdges()
{
return m_edges;
}
void
BitmapContainer::AddHypothesis(Hypothesis *hypothesis)
{
bool itemExists = false;
HypothesisSet::const_iterator iter = m_hypotheses.begin();
HypothesisSet::const_iterator iterEnd = m_hypotheses.end();
// cfedermann: do we actually need this check?
while (iter != iterEnd) {
if (*iter == hypothesis) {
itemExists = true;
break;
}
++iter;
}
UTIL_THROW_IF2(itemExists, "Duplicate hypotheses");
m_hypotheses.push_back(hypothesis);
}
void
BitmapContainer::AddBackwardsEdge(BackwardsEdge *edge)
{
m_edges.insert(edge);
}
void
BitmapContainer::InitializeEdges()
{
BackwardsEdgeSet::iterator iter = m_edges.begin();
BackwardsEdgeSet::iterator iterEnd = m_edges.end();
while (iter != iterEnd) {
BackwardsEdge *edge = *iter;
edge->Initialize();
++iter;
}
}
void
BitmapContainer::EnsureMinStackHyps(const size_t minNumHyps)
{
while ((!Empty()) && m_numStackInsertions < minNumHyps) {
ProcessBestHypothesis();
}
}
void
BitmapContainer::ProcessBestHypothesis()
{
if (m_queue.empty()) {
return;
}
// Get the currently best hypothesis from the queue.
HypothesisQueueItem *item = Dequeue();
// If the priority queue is exhausted, we are done and should have exited
UTIL_THROW_IF2(item == NULL, "Null object");
// check we are pulling things off of priority queue in right order
if (!Empty()) {
HypothesisQueueItem *check = Dequeue(true);
UTIL_THROW_IF2(item->GetHypothesis()->GetTotalScore() < check->GetHypothesis()->GetTotalScore(),
"Non-monotonic total score");
}
// Logging for the criminally insane
IFVERBOSE(3) {
// const StaticData &staticData = StaticData::Instance();
item->GetHypothesis()->PrintHypothesis();
}
// Add best hypothesis to hypothesis stack.
const bool newstackentry = m_stack.AddPrune(item->GetHypothesis());
if (newstackentry)
m_numStackInsertions++;
IFVERBOSE(3) {
TRACE_ERR("new stack entry flag is " << newstackentry << std::endl);
}
// Create new hypotheses for the two successors of the hypothesis just added.
item->GetBackwardsEdge()->PushSuccessors(item->GetHypothesisPos(), item->GetTranslationPos());
// We are done with the queue item, we delete it.
delete item;
}
void
BitmapContainer::SortHypotheses()
{
std::sort(m_hypotheses.begin(), m_hypotheses.end(), HypothesisScoreOrderer());
}
}
|