Welcome to mirror list, hosted at ThFree Co, Russian Federation.

TargetPhrase.cpp « moses - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fc7af968755c68bf9bb4997ebe06748e7d710528 (plain)
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
// $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 <algorithm>
#include <cstdlib>
#include "util/exception.hh"
#include "util/tokenize_piece.hh"

#include "TargetPhrase.h"
#include "GenerationDictionary.h"
#include "LM/Base.h"
#include "StaticData.h"
#include "ScoreComponentCollection.h"
#include "Util.h"
#include "AlignmentInfoCollection.h"
#include "InputPath.h"
#include "TranslationTask.h"
#include "moses/TranslationModel/PhraseDictionary.h"
#include <boost/foreach.hpp>

using namespace std;

namespace Moses
{
TargetPhrase::TargetPhrase( std::string out_string, const PhraseDictionary *pt)
  :Phrase(0)
  , m_fullScore(0.0)
  , m_futureScore(0.0)
  , m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_lhsTarget(NULL)
  , m_ruleSource(NULL)
  , m_ttask_flag(false)
  , m_container(pt)
{
  //ACAT
  const StaticData &staticData = StaticData::Instance();
  // XXX should this really be InputFactorOrder???
  CreateFromString(Output, staticData.GetInputFactorOrder(), out_string,
                   // staticData.GetFactorDelimiter(), // eliminated [UG]
                   NULL);
}

TargetPhrase::TargetPhrase(ttasksptr& ttask, std::string out_string, const PhraseDictionary *pt)
  :Phrase(0)
  , m_fullScore(0.0)
  , m_futureScore(0.0)
  , m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_lhsTarget(NULL)
  , m_ruleSource(NULL)
  , m_ttask(ttask)
  , m_ttask_flag(true)
  , m_container(pt)
{

  //ACAT
  const StaticData &staticData = StaticData::Instance();
  // XXX should this really be InputFactorOrder???
  CreateFromString(Output, staticData.GetInputFactorOrder(), out_string,
                   // staticData.GetFactorDelimiter(), // eliminated [UG]
                   NULL);
}

TargetPhrase::TargetPhrase(ttasksptr& ttask, const PhraseDictionary *pt)
  :Phrase()
  , m_fullScore(0.0)
  , m_futureScore(0.0)
  , m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_lhsTarget(NULL)
  , m_ruleSource(NULL)
  , m_ttask(ttask)
  , m_ttask_flag(true)
  , m_container(pt)
{
}

TargetPhrase::TargetPhrase(ttasksptr& ttask, const Phrase &phrase, const PhraseDictionary *pt)
  : Phrase(phrase)
  , m_fullScore(0.0)
  , m_futureScore(0.0)
  , m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_lhsTarget(NULL)
  , m_ruleSource(NULL)
  , m_ttask(ttask)
  , m_ttask_flag(true)
  , m_container(pt)
{
}

TargetPhrase::TargetPhrase(const PhraseDictionary *pt)
  :Phrase()
  , m_fullScore(0.0)
  , m_futureScore(0.0)
  , m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_lhsTarget(NULL)
  , m_ruleSource(NULL)
  , m_ttask_flag(false)
  , m_container(pt)
{
}

TargetPhrase::TargetPhrase(const Phrase &phrase, const PhraseDictionary *pt)
  : Phrase(phrase)
  , m_fullScore(0.0)
  , m_futureScore(0.0)
  , m_alignTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_alignNonTerm(&AlignmentInfoCollection::Instance().GetEmptyAlignmentInfo())
  , m_lhsTarget(NULL)
  , m_ruleSource(NULL)
  , m_ttask_flag(false)
  , m_container(pt)
{
}

TargetPhrase::TargetPhrase(const TargetPhrase &copy)
  : Phrase(copy)
  , m_cached_scores(copy.m_cached_scores)
  , m_fullScore(copy.m_fullScore)
  , m_futureScore(copy.m_futureScore)
  , m_scoreBreakdown(copy.m_scoreBreakdown)
  , m_alignTerm(copy.m_alignTerm)
  , m_alignNonTerm(copy.m_alignNonTerm)
  , m_properties(copy.m_properties)
  , m_ttask(copy.m_ttask)
  , m_ttask_flag(copy.m_ttask_flag)
  , m_container(copy.m_container)
{
  if (copy.m_lhsTarget) {
    m_lhsTarget = new Word(*copy.m_lhsTarget);
  } else {
    m_lhsTarget = NULL;
  }

  if (copy.m_ruleSource) {
    m_ruleSource = new Phrase(*copy.m_ruleSource);
  } else {
    m_ruleSource = NULL;
  }
}

TargetPhrase::~TargetPhrase()
{
  //cerr << "m_lhsTarget=" << m_lhsTarget << endl;

  delete m_lhsTarget;
  delete m_ruleSource;
}

#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

bool TargetPhrase::HasTtaskSPtr() const {
  return m_ttask_flag;
}

const ttasksptr& TargetPhrase::GetTtask() const {
  return m_ttask.lock();
}

void TargetPhrase::EvaluateInIsolation(const Phrase &source)
{
  const std::vector<FeatureFunction*> &ffs = FeatureFunction::GetFeatureFunctions();
  EvaluateInIsolation(source, ffs);
}

void TargetPhrase::EvaluateInIsolation(const Phrase &source, const std::vector<FeatureFunction*> &ffs)
{
  if (ffs.size()) {
    const StaticData &staticData = StaticData::Instance();
    ScoreComponentCollection futureScoreBreakdown;
    for (size_t i = 0; i < ffs.size(); ++i) {
      const FeatureFunction &ff = *ffs[i];
      if (! staticData.IsFeatureFunctionIgnored( ff )) {
        ff.EvaluateInIsolation(source, *this, m_scoreBreakdown, futureScoreBreakdown);
      }
    }

    float weightedScore = m_scoreBreakdown.GetWeightedScore();
    m_futureScore += futureScoreBreakdown.GetWeightedScore();
    m_fullScore = weightedScore + m_futureScore;
  }
}

void TargetPhrase::EvaluateWithSourceContext(const InputType &input, const InputPath &inputPath)
{
  const std::vector<FeatureFunction*> &ffs = FeatureFunction::GetFeatureFunctions();
  const StaticData &staticData = StaticData::Instance();
  ScoreComponentCollection futureScoreBreakdown;
  for (size_t i = 0; i < ffs.size(); ++i) {
    const FeatureFunction &ff = *ffs[i];
    if (! staticData.IsFeatureFunctionIgnored( ff )) {
      ff.EvaluateWithSourceContext(input, inputPath, *this, NULL, m_scoreBreakdown, &futureScoreBreakdown);
    }
  }
  float weightedScore = m_scoreBreakdown.GetWeightedScore();
  m_futureScore += futureScoreBreakdown.GetWeightedScore();
  m_fullScore = weightedScore + m_futureScore;
}

void TargetPhrase::UpdateScore(ScoreComponentCollection* futureScoreBreakdown)
{
  float weightedScore = m_scoreBreakdown.GetWeightedScore();
  if(futureScoreBreakdown)
    m_futureScore += futureScoreBreakdown->GetWeightedScore();
  m_fullScore = weightedScore + m_futureScore;
}

void TargetPhrase::SetXMLScore(float score)
{
  const FeatureFunction* prod = PhraseDictionary::GetColl()[0];
  size_t numScores = prod->GetNumScoreComponents();
  vector <float> scoreVector(numScores,score/numScores);

  m_scoreBreakdown.Assign(prod, scoreVector);
}

void TargetPhrase::SetAlignmentInfo(const StringPiece &alignString)
{
  AlignmentInfo::CollType alignTerm, alignNonTerm;
  for (util::TokenIter<util::AnyCharacter, true> token(alignString, util::AnyCharacter(" \t")); token; ++token) {
    util::TokenIter<util::SingleCharacter, false> dash(*token, util::SingleCharacter('-'));

    char *endptr;
    size_t sourcePos = strtoul(dash->data(), &endptr, 10);
    UTIL_THROW_IF(endptr != dash->data() + dash->size(), util::ErrnoException, "Error parsing alignment" << *dash);
    ++dash;
    size_t targetPos = strtoul(dash->data(), &endptr, 10);
    UTIL_THROW_IF(endptr != dash->data() + dash->size(), util::ErrnoException, "Error parsing alignment" << *dash);
    UTIL_THROW_IF2(++dash, "Extra gunk in alignment " << *token);

    if (GetWord(targetPos).IsNonTerminal()) {
      alignNonTerm.insert(std::pair<size_t,size_t>(sourcePos, targetPos));
    } else {
      alignTerm.insert(std::pair<size_t,size_t>(sourcePos, targetPos));
    }
  }
  SetAlignTerm(alignTerm);
  SetAlignNonTerm(alignNonTerm);
  //		cerr << "TargetPhrase::SetAlignmentInfo(const StringPiece &alignString) this:|" << *this << "|\n";
}

// void TargetPhrase::SetAlignTerm(const AlignmentInfo::CollType &coll)
// {
//   const AlignmentInfo *alignmentInfo = AlignmentInfoCollection::Instance().Add(coll);
//   m_alignTerm = alignmentInfo;

// }

// void TargetPhrase::SetAlignNonTerm(const AlignmentInfo::CollType &coll)
// {
//   const AlignmentInfo *alignmentInfo = AlignmentInfoCollection::Instance().Add(coll);
//   m_alignNonTerm = alignmentInfo;
// }

void TargetPhrase::SetSparseScore(const FeatureFunction* translationScoreProducer, const StringPiece &sparseString)
{
  m_scoreBreakdown.Assign(translationScoreProducer, sparseString.as_string());
}

boost::shared_ptr<Scores>
mergescores(boost::shared_ptr<Scores> const& a,
            boost::shared_ptr<Scores> const& b)
{
  boost::shared_ptr<Scores> ret;
  if (!a) return b ? b : ret;
  if (!b) return a;
  if (a->size() != b->size()) return ret;
  ret.reset(new Scores(*a));
  for (size_t i = 0; i < a->size(); ++i) {
    if ((*a)[i] == 0) (*a)[i] = (*b)[i];
    else if ((*b)[i]) {
      UTIL_THROW_IF2((*a)[i] != (*b)[i], "can't merge feature vectors");
    }
  }
  return ret;
}

void
TargetPhrase::
Merge(const TargetPhrase &copy, const std::vector<FactorType>& factorVec)
{
  Phrase::MergeFactors(copy, factorVec);
  m_scoreBreakdown.Merge(copy.GetScoreBreakdown());
  m_futureScore += copy.m_futureScore;
  m_fullScore += copy.m_fullScore;
  typedef ScoreCache_t::iterator iter;
  typedef ScoreCache_t::value_type item;
  BOOST_FOREACH(item const& s, copy.m_cached_scores) {
    pair<iter,bool> foo = m_cached_scores.insert(s);
    if (foo.second == false)
      foo.first->second = mergescores(foo.first->second, s.second);
  }
}

TargetPhrase::ScoreCache_t const&
TargetPhrase::
GetExtraScores() const
{
  return m_cached_scores;
}

Scores const*
TargetPhrase::
GetExtraScores(FeatureFunction const* ff) const
{
  ScoreCache_t::const_iterator m = m_cached_scores.find(ff);
  return m != m_cached_scores.end() ? m->second.get() : NULL;
}

void
TargetPhrase::
SetExtraScores(FeatureFunction const* ff,
               boost::shared_ptr<Scores> const& s)
{
  m_cached_scores[ff] = s;
}


void TargetPhrase::SetProperties(const StringPiece &str)
{
  if (str.size() == 0) {
    return;
  }

  vector<string> toks;
  TokenizeMultiCharSeparator(toks, str.as_string(), "{{");
  for (size_t i = 0; i < toks.size(); ++i) {
    string &tok = toks[i];
    if (tok.empty()) {
      continue;
    }
    size_t endPos = tok.rfind("}");

    tok = tok.substr(0, endPos - 1);

    vector<string> keyValue = TokenizeFirstOnly(tok, " ");
    UTIL_THROW_IF2(keyValue.size() != 2,
                   "Incorrect format of property: " << str);
    SetProperty(keyValue[0], keyValue[1]);
  }
}

void TargetPhrase::SetProperty(const std::string &key, const std::string &value)
{
  const StaticData &staticData = StaticData::Instance();
  const PhrasePropertyFactory& phrasePropertyFactory = staticData.GetPhrasePropertyFactory();
  m_properties[key] = phrasePropertyFactory.ProduceProperty(key,value);
}

const PhraseProperty *TargetPhrase::GetProperty(const std::string &key) const
{
  std::map<std::string, boost::shared_ptr<PhraseProperty> >::const_iterator iter;
  iter = m_properties.find(key);
  if (iter != m_properties.end()) {
    const boost::shared_ptr<PhraseProperty> &pp = iter->second;
    return pp.get();
  }
  return NULL;
}

void TargetPhrase::SetRuleSource(const Phrase &ruleSource) const
{
  if (m_ruleSource == NULL) {
    m_ruleSource = new Phrase(ruleSource);
  }
}

void swap(TargetPhrase &first, TargetPhrase &second)
{
  first.SwapWords(second);
  std::swap(first.m_fullScore, second.m_fullScore);
  std::swap(first.m_futureScore, second.m_futureScore);
  swap(first.m_scoreBreakdown, second.m_scoreBreakdown);
  std::swap(first.m_alignTerm, second.m_alignTerm);
  std::swap(first.m_alignNonTerm, second.m_alignNonTerm);
  std::swap(first.m_lhsTarget, second.m_lhsTarget);
  std::swap(first.m_cached_scores, second.m_cached_scores);
}

TO_STRING_BODY(TargetPhrase);

std::ostream& operator<<(std::ostream& os, const TargetPhrase& tp)
{
  if (tp.m_lhsTarget) {
    os << *tp.m_lhsTarget<< " -> ";
  }

  os << static_cast<const Phrase&>(tp) << ":" << flush;
  os << tp.GetAlignNonTerm() << flush;
  os << ": term=" << tp.GetAlignTerm() << flush;
  os << ": nonterm=" << tp.GetAlignNonTerm() << flush;
  os << ": c=" << tp.m_fullScore << flush;
  os << " " << tp.m_scoreBreakdown << flush;

  const Phrase *sourcePhrase = tp.GetRuleSource();
  if (sourcePhrase) {
    os << " sourcePhrase=" << *sourcePhrase << flush;
  }

  if (tp.m_properties.size()) {
    os << " properties: " << flush;

    TargetPhrase::Properties::const_iterator iter;
    for (iter = tp.m_properties.begin(); iter != tp.m_properties.end(); ++iter) {
      const string &key = iter->first;
      const PhraseProperty *prop = iter->second.get();
      assert(prop);

      os << key << "=" << *prop << " ";
    }
  }

  return os;
}



}