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
|
// $Id$
#include "util/check.hh"
#include "FFState.h"
#include "StaticData.h"
#include "DummyScoreProducers.h"
#include "WordsRange.h"
#include "TranslationOption.h"
namespace Moses
{
struct DistortionState_traditional : public FFState {
WordsRange range;
int first_gap;
DistortionState_traditional(const WordsRange& wr, int fg) : range(wr), first_gap(fg) {}
int Compare(const FFState& other) const {
const DistortionState_traditional& o =
static_cast<const DistortionState_traditional&>(other);
if (range.GetEndPos() < o.range.GetEndPos()) return -1;
if (range.GetEndPos() > o.range.GetEndPos()) return 1;
return 0;
}
};
const FFState* DistortionScoreProducer::EmptyHypothesisState(const InputType &input) const
{
// fake previous translated phrase start and end
size_t start = NOT_FOUND;
size_t end = NOT_FOUND;
if (input.m_frontSpanCoveredLength > 0) {
// can happen with --continue-partial-translation
start = 0;
end = input.m_frontSpanCoveredLength -1;
}
return new DistortionState_traditional(
WordsRange(start, end),
NOT_FOUND);
}
std::string DistortionScoreProducer::GetScoreProducerWeightShortName(unsigned) const
{
return "d";
}
float DistortionScoreProducer::CalculateDistortionScore(const Hypothesis& hypo,
const WordsRange &prev, const WordsRange &curr, const int FirstGap) const
{
const int USE_OLD = 1;
if (USE_OLD) {
return - (float) hypo.GetInput().ComputeDistortionDistance(prev, curr);
}
// Pay distortion score as soon as possible, from Moore and Quirk MT Summit 2007
int prefixEndPos = FirstGap-1;
if ((int) curr.GetStartPos() == prefixEndPos+1) {
return 0;
}
if ((int) curr.GetEndPos() < (int) prev.GetEndPos()) {
return (float) -2*curr.GetNumWordsCovered();
}
if ((int) prev.GetEndPos() <= prefixEndPos) {
int z = curr.GetStartPos()-prefixEndPos;
return (float) -2*(z + curr.GetNumWordsCovered());
}
return (float) -2*(curr.GetNumWordsBetween(prev) + curr.GetNumWordsCovered());
}
FFState* DistortionScoreProducer::Evaluate(
const Hypothesis& hypo,
const FFState* prev_state,
ScoreComponentCollection* out) const
{
const DistortionState_traditional* prev = static_cast<const DistortionState_traditional*>(prev_state);
const float distortionScore = CalculateDistortionScore(
hypo,
prev->range,
hypo.GetCurrSourceWordsRange(),
prev->first_gap);
out->PlusEquals(this, distortionScore);
DistortionState_traditional* res = new DistortionState_traditional(
hypo.GetCurrSourceWordsRange(),
hypo.GetPrevHypo()->GetWordsBitmap().GetFirstGapPos());
return res;
}
std::string WordPenaltyProducer::GetScoreProducerWeightShortName(unsigned) const
{
return "w";
}
void WordPenaltyProducer::Evaluate(const Hypothesis& cur_hypo, ScoreComponentCollection* out) const
{
const TargetPhrase& tp = cur_hypo.GetCurrTargetPhrase();
out->PlusEquals(this, -static_cast<float>(tp.GetSize()));
}
std::string UnknownWordPenaltyProducer::GetScoreProducerWeightShortName(unsigned) const
{
return "u";
}
bool UnknownWordPenaltyProducer::ComputeValueInTranslationOption() const
{
return true;
}
}
|