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
|
// $Id$
/***********************************************************************
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 "util/check.hh"
#include <algorithm>
#include <boost/lexical_cast.hpp>
#include "util/tokenize_piece.hh"
#include "TargetPhrase.h"
#include "PhraseDictionaryMemory.h"
#include "GenerationDictionary.h"
#include "LM/Base.h"
#include "StaticData.h"
#include "LMList.h"
#include "ScoreComponentCollection.h"
#include "Util.h"
#include "DummyScoreProducers.h"
#include "AlignmentInfoCollection.h"
#include <boost/algorithm/string.hpp>
using namespace std;
namespace Moses
{
TargetPhrase::TargetPhrase(std::string out_string)
:Phrase(0),m_transScore(0.0), m_fullScore(0.0), m_sourcePhrase(0)
, m_alignmentInfo(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
{
//ACAT
const StaticData &staticData = StaticData::Instance();
CreateFromString(staticData.GetInputFactorOrder(), out_string, staticData.GetFactorDelimiter());
}
TargetPhrase::TargetPhrase()
:Phrase(ARRAY_SIZE_INCR)
, m_transScore(0.0)
, m_fullScore(0.0)
,m_sourcePhrase(0)
, m_alignmentInfo(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
{
}
TargetPhrase::TargetPhrase(const Phrase &phrase)
: Phrase(phrase)
, m_transScore(0.0)
, m_fullScore(0.0)
, m_sourcePhrase(0)
, m_alignmentInfo(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
{
}
void TargetPhrase::SetScore(const TranslationSystem* system)
{
// used when creating translations of unknown words:
m_transScore = 0;
m_fullScore = - system->GetWeightWordPenalty();
}
#ifdef HAVE_PROTOBUF
void TargetPhrase::WriteToRulePB(hgmert::Rule* pb) const
{
pb->add_trg_words("[X,1]");
for (size_t pos = 0 ; pos < GetSize() ; pos++)
pb->add_trg_words(GetWord(pos)[0]->GetString());
}
#endif
void TargetPhrase::SetScore(float score)
{
//we use an existing score producer to figure out information for score setting (number of scores and weights)
//TODO: is this a good idea?
// Assume the default system.
const TranslationSystem& system = StaticData::Instance().GetTranslationSystem(TranslationSystem::DEFAULT);
const ScoreProducer* prod = system.GetPhraseDictionaries()[0];
vector<float> weights = StaticData::Instance().GetWeights(prod);
//find out how many items are in the score vector for this producer
size_t numScores = prod->GetNumScoreComponents();
//divide up the score among all of the score vectors
vector <float> scoreVector(numScores,score/numScores);
//Now we have what we need to call the full SetScore method
SetScore(prod, scoreVector, ScoreComponentCollection(), weights, system.GetWeightWordPenalty(), system.GetLanguageModels());
}
/**
* used for setting scores for unknown words with input link features (lattice/conf. nets)
* \param scoreVector input scores
*/
void TargetPhrase::SetScore(const TranslationSystem* system, const Scores &scoreVector)
{
//we use an existing score producer to figure out information for score setting (number of scores and weights)
const ScoreProducer* prod = system->GetPhraseDictionaries()[0];
vector<float> weights = StaticData::Instance().GetWeights(prod);
//expand the input weight vector
CHECK(scoreVector.size() <= prod->GetNumScoreComponents());
Scores sizedScoreVector = scoreVector;
sizedScoreVector.resize(prod->GetNumScoreComponents(),0.0f);
SetScore(prod,sizedScoreVector, ScoreComponentCollection(),weights,system->GetWeightWordPenalty(),system->GetLanguageModels());
}
void TargetPhrase::SetScore(const ScoreProducer* translationScoreProducer,
const Scores &scoreVector,
const ScoreComponentCollection &sparseScoreVector,
const vector<float> &weightT,
float weightWP, const LMList &languageModels)
{
CHECK(weightT.size() == scoreVector.size());
// calc average score if non-best
m_transScore = std::inner_product(scoreVector.begin(), scoreVector.end(), weightT.begin(), 0.0f);
m_scoreBreakdown.PlusEquals(translationScoreProducer, scoreVector);
m_scoreBreakdown.PlusEquals(sparseScoreVector);
// Replicated from TranslationOptions.cpp
float totalNgramScore = 0;
float totalFullScore = 0;
float totalOOVScore = 0;
LMList::const_iterator lmIter;
for (lmIter = languageModels.begin(); lmIter != languageModels.end(); ++lmIter) {
const LanguageModel &lm = **lmIter;
if (lm.Useable(*this)) {
// contains factors used by this LM
const float weightLM = lm.GetWeight();
const float oovWeightLM = lm.GetOOVWeight();
float fullScore, nGramScore;
size_t oovCount;
lm.CalcScore(*this, fullScore, nGramScore, oovCount);
if (StaticData::Instance().GetLMEnableOOVFeature()) {
vector<float> scores(2);
scores[0] = nGramScore;
scores[1] = oovCount;
m_scoreBreakdown.Assign(&lm, scores);
totalOOVScore += oovCount * oovWeightLM;
} else {
m_scoreBreakdown.Assign(&lm, nGramScore);
}
// total LM score so far
totalNgramScore += nGramScore * weightLM;
totalFullScore += fullScore * weightLM;
}
}
m_fullScore = m_transScore + totalFullScore + totalOOVScore
- (this->GetSize() * weightWP); // word penalty
}
void TargetPhrase::SetScoreChart(const ScoreProducer* translationScoreProducer,
const Scores &scoreVector
,const vector<float> &weightT
,const LMList &languageModels
,const WordPenaltyProducer* wpProducer)
{
CHECK(weightT.size() == scoreVector.size());
// calc average score if non-best
m_transScore = std::inner_product(scoreVector.begin(), scoreVector.end(), weightT.begin(), 0.0f);
m_scoreBreakdown.PlusEquals(translationScoreProducer, scoreVector);
// Replicated from TranslationOptions.cpp
float totalNgramScore = 0;
float totalFullScore = 0;
float totalOOVScore = 0;
LMList::const_iterator lmIter;
for (lmIter = languageModels.begin(); lmIter != languageModels.end(); ++lmIter) {
const LanguageModel &lm = **lmIter;
if (lm.Useable(*this)) {
// contains factors used by this LM
const float weightLM = lm.GetWeight();
const float oovWeightLM = lm.GetOOVWeight();
float fullScore, nGramScore;
size_t oovCount;
lm.CalcScore(*this, fullScore, nGramScore, oovCount);
fullScore = UntransformLMScore(fullScore);
nGramScore = UntransformLMScore(nGramScore);
if (StaticData::Instance().GetLMEnableOOVFeature()) {
vector<float> scores(2);
scores[0] = nGramScore;
scores[1] = oovCount;
m_scoreBreakdown.Assign(&lm, scores);
totalOOVScore += oovCount * oovWeightLM;
} else {
m_scoreBreakdown.Assign(&lm, nGramScore);
}
// total LM score so far
totalNgramScore += nGramScore * weightLM;
totalFullScore += fullScore * weightLM;
}
}
// word penalty
size_t wordCount = GetNumTerminals();
m_scoreBreakdown.Assign(wpProducer, - (float) wordCount * 0.434294482); // TODO log -> ln ??
m_fullScore = m_scoreBreakdown.GetWeightedScore() - totalNgramScore + totalFullScore + totalOOVScore;
}
void TargetPhrase::SetScore(const ScoreProducer* producer, const Scores &scoreVector)
{
// used when creating translations of unknown words (chart decoding)
m_scoreBreakdown.Assign(producer, scoreVector);
m_transScore = 0;
m_fullScore = m_scoreBreakdown.GetWeightedScore();
}
void TargetPhrase::SetWeights(const ScoreProducer* translationScoreProducer, const vector<float> &weightT)
{
// calling this function in case of confusion net input is undefined
CHECK(StaticData::Instance().GetInputType()==SentenceInput);
/* one way to fix this, you have to make sure the weightT contains (in
addition to the usual phrase translation scaling factors) the input
weight factor as last element
*/
m_transScore = m_scoreBreakdown.PartialInnerProduct(translationScoreProducer, weightT);
}
void TargetPhrase::ResetScore()
{
m_fullScore = 0;
m_scoreBreakdown.ZeroAll();
}
TargetPhrase *TargetPhrase::MergeNext(const TargetPhrase &inputPhrase) const
{
if (! IsCompatible(inputPhrase)) {
return NULL;
}
// ok, merge
TargetPhrase *clone = new TargetPhrase(*this);
clone->m_sourcePhrase = m_sourcePhrase;
int currWord = 0;
const size_t len = GetSize();
for (size_t currPos = 0 ; currPos < len ; currPos++) {
const Word &inputWord = inputPhrase.GetWord(currPos);
Word &cloneWord = clone->GetWord(currPos);
cloneWord.Merge(inputWord);
currWord++;
}
return clone;
}
namespace {
void MosesShouldUseExceptions(bool value) {
if (!value) {
std::cerr << "Could not parse alignment info" << std::endl;
abort();
}
}
} // namespace
void TargetPhrase::SetAlignmentInfo(const StringPiece &alignString)
{
set<pair<size_t,size_t> > alignmentInfo;
for (util::TokenIter<util::AnyCharacter, true> token(alignString, util::AnyCharacter(" \t")); token; ++token) {
util::TokenIter<util::AnyCharacter, false> dash(*token, util::AnyCharacter("-"));
MosesShouldUseExceptions(dash);
size_t sourcePos = boost::lexical_cast<size_t>(*dash++);
MosesShouldUseExceptions(dash);
size_t targetPos = boost::lexical_cast<size_t>(*dash++);
MosesShouldUseExceptions(!dash);
alignmentInfo.insert(pair<size_t,size_t>(sourcePos, targetPos));
}
SetAlignmentInfo(alignmentInfo);
}
void TargetPhrase::SetAlignmentInfo(const StringPiece &alignString, Phrase &sourcePhrase)
{
std::vector<std::string> alignPoints;
boost::split(alignPoints, alignString, boost::is_any_of("\t "));
int indicator[alignPoints.size()];
int index = 0;
set<pair<size_t,size_t> > alignmentInfo;
for (util::TokenIter<util::AnyCharacter, true> token(alignString, util::AnyCharacter(" \t")); token; ++token) {
util::TokenIter<util::AnyCharacter, false> dash(*token, util::AnyCharacter("-"));
MosesShouldUseExceptions(dash);
size_t sourcePos = boost::lexical_cast<size_t>(*dash++);
MosesShouldUseExceptions(dash);
size_t targetPos = boost::lexical_cast<size_t>(*dash++);
MosesShouldUseExceptions(!dash);
alignmentInfo.insert(pair<size_t,size_t>(sourcePos, targetPos));
indicator[index++] = sourcePhrase.GetWord(sourcePos).IsNonTerminal() ? 1: 0;
}
SetAlignmentInfo(alignmentInfo, indicator);
}
void TargetPhrase::SetAlignmentInfo(const std::set<std::pair<size_t,size_t> > &alignmentInfo)
{
m_alignmentInfo = AlignmentInfoCollection::Instance().Add(alignmentInfo);
}
void TargetPhrase::SetAlignmentInfo(const std::set<std::pair<size_t,size_t> > &alignmentInfo, int* indicator)
{
m_alignmentInfo = AlignmentInfoCollection::Instance().Add(alignmentInfo, indicator);
}
TO_STRING_BODY(TargetPhrase);
std::ostream& operator<<(std::ostream& os, const TargetPhrase& tp)
{
os << static_cast<const Phrase&>(tp) << ":" << tp.GetAlignmentInfo();
os << ": pC=" << tp.m_transScore << ", c=" << tp.m_fullScore;
return os;
}
void TargetPhrase::SetRuleCount(const StringPiece &ruleCountString, std::vector<float> &scoreVector) {
std::vector<std::string> tokens;
boost::split(tokens, ruleCountString, boost::is_any_of("\t "));
if (tokens.size() == 2) {
// TODO: if no third column is provided, do we have to take smoothing into account (consolidate.cpp)?
// infer rule counts from target counts
float targetCount = 0, sourceCount = 0;
float p_f_given_e = 0, p_e_given_f = 0;
p_f_given_e = scoreVector[0];
if (scoreVector.size() >= 5) {
p_f_given_e = scoreVector[0];
p_e_given_f = scoreVector[2];
}
else {
if (scoreVector.size() >= 1 ) p_f_given_e = scoreVector[0];
// std::cerr << "Warning: possibly wrong format of phrase translation scores, number of scores: " << scoreVector.size() << endl;
}
targetCount = Scan<float>(tokens[0]);
sourceCount = Scan<float>(tokens[1]);
float ruleCount = p_f_given_e * targetCount;
//float ruleCount2 = p_e_given_f * sourceCount; // could use this to double-check the counts
m_ruleCount = floor(ruleCount + 0.5);
}
else if (tokens.size() == 3) {
m_ruleCount = Scan<float>(tokens[2]);
}
}
}
|