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

TargetPhrase.cpp « OnDiskPt - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f0501a9376cbbcfe2b9c5491ac0103f8e2dd5fc3 (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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// $Id$
/***********************************************************************
 Moses - factored phrase-based, hierarchical and syntactic language decoder
 Copyright (C) 2009 Hieu Hoang

 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 <iostream>
#include "moses/Util.h"
#include "moses/TargetPhrase.h"
#include "moses/TranslationModel/PhraseDictionary.h"
#include "TargetPhrase.h"
#include "OnDiskWrapper.h"
#include "util/exception.hh"

#include <boost/algorithm/string.hpp>

using namespace std;

namespace OnDiskPt
{

TargetPhrase::TargetPhrase(size_t numScores)
  :m_scores(numScores)
{
}

TargetPhrase::TargetPhrase(const 	TargetPhrase &copy)
  :Phrase(copy)
  ,m_scores(copy.m_scores)
{

}

TargetPhrase::~TargetPhrase()
{
}

void TargetPhrase::SetLHS(WordPtr lhs)
{
  AddWord(lhs);
}

void TargetPhrase::Create1AlignFromString(const std::string &align1Str)
{
  vector<size_t> alignPoints;
  Moses::Tokenize<size_t>(alignPoints, align1Str, "-");
  UTIL_THROW_IF2(alignPoints.size() != 2, "Incorrectly formatted word alignment: " << align1Str);
  m_align.push_back(pair<size_t, size_t>(alignPoints[0], alignPoints[1]) );
}

void TargetPhrase::CreateAlignFromString(const std::string &alignStr)
{
  vector<std::string> alignPairs;
  boost::split(alignPairs, alignStr, boost::is_any_of("\t "));
  for (size_t i = 0; i < alignPairs.size(); ++i) {
    vector<size_t> alignPoints;
    Moses::Tokenize<size_t>(alignPoints, alignPairs[i], "-");
    m_align.push_back(pair<size_t, size_t>(alignPoints[0], alignPoints[1]) );
  }
}


void TargetPhrase::SetScore(float score, size_t ind)
{
  assert(ind < m_scores.size());
  m_scores[ind] = score;
}

class AlignOrderer
{
public:
  bool operator()(const AlignPair &a, const AlignPair &b) const {
    return a.first < b.first;
  }
};

void TargetPhrase::SortAlign()
{
  std::sort(m_align.begin(), m_align.end(), AlignOrderer());
}

char *TargetPhrase::WriteToMemory(OnDiskWrapper &onDiskWrapper, size_t &memUsed) const
{
  size_t phraseSize = GetSize();
  size_t targetWordSize = onDiskWrapper.GetTargetWordSize();

  const PhrasePtr sp = GetSourcePhrase();
  size_t spSize = sp->GetSize();
  size_t sourceWordSize = onDiskWrapper.GetSourceWordSize();

  size_t memNeeded = sizeof(uint64_t)						// num of words
                     + targetWordSize * phraseSize	// actual words. lhs as last words
                     + sizeof(uint64_t)					// num source words
                     + sourceWordSize * spSize;   // actual source words

  memUsed = 0;
  uint64_t *mem = (uint64_t*) malloc(memNeeded);

  // write size
  mem[0] = phraseSize;
  memUsed += sizeof(uint64_t);

  // write each word
  for (size_t pos = 0; pos < phraseSize; ++pos) {
    const Word &word = GetWord(pos);
    char *currPtr = (char*)mem + memUsed;
    memUsed += word.WriteToMemory((char*) currPtr);
  }

  // write size of source phrase and all source words
  char *currPtr = (char*)mem + memUsed;
  uint64_t *memTmp = (uint64_t*) currPtr;
  memTmp[0] = spSize;
  memUsed += sizeof(uint64_t);
  for (size_t pos = 0; pos < spSize; ++pos) {
    const Word &word = sp->GetWord(pos);
    char *currPtr = (char*)mem + memUsed;
    memUsed += word.WriteToMemory((char*) currPtr);
  }

  assert(memUsed == memNeeded);
  return (char *) mem;
}

void TargetPhrase::Save(OnDiskWrapper &onDiskWrapper)
{
  // save in target ind
  size_t memUsed;
  char *mem = WriteToMemory(onDiskWrapper, memUsed);

  std::fstream &file = onDiskWrapper.GetFileTargetInd();

  uint64_t startPos = file.tellp();

  file.seekp(0, ios::end);
  file.write(mem, memUsed);

#ifndef NDEBUG
  uint64_t endPos = file.tellp();
  assert(startPos + memUsed == endPos);
#endif

  m_filePos = startPos;
  free(mem);
}

char *TargetPhrase::WriteOtherInfoToMemory(OnDiskWrapper &onDiskWrapper, size_t &memUsed) const
{
  // allocate mem
  size_t numScores = onDiskWrapper.GetNumScores()
                     ,numAlign = GetAlign().size();
  size_t sparseFeatureSize = m_sparseFeatures.size();
  size_t propSize = m_property.size();

  size_t memNeeded = sizeof(uint64_t) // file pos (phrase id)
                     + sizeof(uint64_t) + 2 * sizeof(uint64_t) * numAlign // align
                     + sizeof(float) * numScores // scores
                     + sizeof(uint64_t) + sparseFeatureSize // sparse features string
                     + sizeof(uint64_t) + propSize; // property string

  char *mem = (char*) malloc(memNeeded);
  //memset(mem, 0, memNeeded);

  memUsed = 0;

  // phrase id
  memcpy(mem, &m_filePos, sizeof(uint64_t));
  memUsed += sizeof(uint64_t);

  // align
  size_t tmp = WriteAlignToMemory(mem + memUsed);
  memUsed += tmp;

  // scores
  memUsed += WriteScoresToMemory(mem + memUsed);

  // sparse features
  memUsed += WriteStringToMemory(mem + memUsed, m_sparseFeatures);

  // property string
  memUsed += WriteStringToMemory(mem + memUsed, m_property);

  //DebugMem(mem, memNeeded);
  assert(memNeeded == memUsed);
  return mem;
}

size_t TargetPhrase::WriteStringToMemory(char *mem, const std::string &str) const
{
  size_t memUsed = 0;
  uint64_t *memTmp = (uint64_t*) mem;

  size_t strSize = str.size();
  memTmp[0] = strSize;
  memUsed += sizeof(uint64_t);

  const char *charStr = str.c_str();
  memcpy(mem + memUsed, charStr, strSize);
  memUsed += strSize;

  return memUsed;
}

size_t TargetPhrase::WriteAlignToMemory(char *mem) const
{
  size_t memUsed = 0;

  // num of alignments
  uint64_t numAlign = m_align.size();
  memcpy(mem, &numAlign, sizeof(numAlign));
  memUsed += sizeof(numAlign);

  // actual alignments
  AlignType::const_iterator iter;
  for (iter = m_align.begin(); iter != m_align.end(); ++iter) {
    const AlignPair &alignPair = *iter;

    memcpy(mem + memUsed, &alignPair.first, sizeof(alignPair.first));
    memUsed += sizeof(alignPair.first);

    memcpy(mem + memUsed, &alignPair.second, sizeof(alignPair.second));
    memUsed += sizeof(alignPair.second);
  }

  return memUsed;
}

size_t TargetPhrase::WriteScoresToMemory(char *mem) const
{
  float *scoreMem = (float*) mem;

  for (size_t ind = 0; ind < m_scores.size(); ++ind)
    scoreMem[ind] = m_scores[ind];

  size_t memUsed = sizeof(float) * m_scores.size();
  return memUsed;
}


Moses::TargetPhrase *TargetPhrase::ConvertToMoses(const std::vector<Moses::FactorType> & inputFactors
    , const std::vector<Moses::FactorType> &outputFactors
    , const Vocab &vocab
    , const Moses::PhraseDictionary &phraseDict
    , const std::vector<float> &weightT
    , bool isSyntax) const
{
  Moses::TargetPhrase *ret = new Moses::TargetPhrase(&phraseDict);

  // words
  size_t phraseSize = GetSize();
  UTIL_THROW_IF2(phraseSize == 0, "Target phrase cannot be empty"); // last word is lhs
  if (isSyntax) {
    --phraseSize;
  }

  for (size_t pos = 0; pos < phraseSize; ++pos) {
    GetWord(pos).ConvertToMoses(outputFactors, vocab, ret->AddWord());
  }

  // alignments
  // int index = 0;
  Moses::AlignmentInfo::CollType alignTerm, alignNonTerm;
  std::set<std::pair<size_t, size_t> > alignmentInfo;
  const PhrasePtr sp = GetSourcePhrase();
  for (size_t ind = 0; ind < m_align.size(); ++ind) {
    const std::pair<size_t, size_t> &entry = m_align[ind];
    alignmentInfo.insert(entry);
    size_t sourcePos = entry.first;
    size_t targetPos = entry.second;

    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));
    }

  }
  ret->SetAlignTerm(alignTerm);
  ret->SetAlignNonTerm(alignNonTerm);

  if (isSyntax) {
    Moses::Word *lhsTarget = new Moses::Word(true);
    GetWord(GetSize() - 1).ConvertToMoses(outputFactors, vocab, *lhsTarget);
    ret->SetTargetLHS(lhsTarget);
  }

  // set source phrase
  Moses::Phrase mosesSP(Moses::Input);
  for (size_t pos = 0; pos < sp->GetSize(); ++pos) {
    sp->GetWord(pos).ConvertToMoses(inputFactors, vocab, mosesSP.AddWord());
  }

  // scores
  ret->GetScoreBreakdown().Assign(&phraseDict, m_scores);

  // sparse features
  ret->GetScoreBreakdown().Assign(&phraseDict, m_sparseFeatures);

  // property
  ret->SetProperties(m_property);

  ret->EvaluateInIsolation(mosesSP, phraseDict.GetFeaturesToApply());

  return ret;
}

uint64_t TargetPhrase::ReadOtherInfoFromFile(uint64_t filePos, std::fstream &fileTPColl)
{
  assert(filePos == (uint64_t)fileTPColl.tellg());

  uint64_t memUsed = 0;
  fileTPColl.read((char*) &m_filePos, sizeof(uint64_t));
  memUsed += sizeof(uint64_t);
  assert(m_filePos != 0);

  memUsed += ReadAlignFromFile(fileTPColl);
  assert((memUsed + filePos) == (uint64_t)fileTPColl.tellg());

  memUsed += ReadScoresFromFile(fileTPColl);
  assert((memUsed + filePos) == (uint64_t)fileTPColl.tellg());

  // sparse features
  memUsed += ReadStringFromFile(fileTPColl, m_sparseFeatures);

  // properties
  memUsed += ReadStringFromFile(fileTPColl, m_property);

  return memUsed;
}

uint64_t TargetPhrase::ReadStringFromFile(std::fstream &fileTPColl, std::string &outStr)
{
  uint64_t bytesRead = 0;

  uint64_t strSize;
  fileTPColl.read((char*) &strSize, sizeof(uint64_t));
  bytesRead += sizeof(uint64_t);

  if (strSize) {
    char *mem = (char*) malloc(strSize + 1);
    mem[strSize] = '\0';
    fileTPColl.read(mem, strSize);
    outStr = string(mem);
    free(mem);

    bytesRead += strSize;
  }

  return bytesRead;
}

uint64_t TargetPhrase::ReadFromFile(std::fstream &fileTP)
{
  uint64_t bytesRead = 0;

  fileTP.seekg(m_filePos);

  uint64_t numWords;
  fileTP.read((char*) &numWords, sizeof(uint64_t));
  bytesRead += sizeof(uint64_t);

  for (size_t ind = 0; ind < numWords; ++ind) {
    WordPtr word(new Word());
    bytesRead += word->ReadFromFile(fileTP);
    AddWord(word);
  }

  // read source words
  uint64_t numSourceWords;
  fileTP.read((char*) &numSourceWords, sizeof(uint64_t));
  bytesRead += sizeof(uint64_t);

  PhrasePtr sp(new SourcePhrase());
  for (size_t ind = 0; ind < numSourceWords; ++ind) {
    WordPtr word( new Word());
    bytesRead += word->ReadFromFile(fileTP);
    sp->AddWord(word);
  }
  SetSourcePhrase(sp);

  return bytesRead;
}

uint64_t TargetPhrase::ReadAlignFromFile(std::fstream &fileTPColl)
{
  uint64_t bytesRead = 0;

  uint64_t numAlign;
  fileTPColl.read((char*) &numAlign, sizeof(uint64_t));
  bytesRead += sizeof(uint64_t);

  for (size_t ind = 0; ind < numAlign; ++ind) {
    AlignPair alignPair;
    fileTPColl.read((char*) &alignPair.first, sizeof(uint64_t));
    fileTPColl.read((char*) &alignPair.second, sizeof(uint64_t));
    m_align.push_back(alignPair);

    bytesRead += sizeof(uint64_t) * 2;
  }

  return bytesRead;
}

uint64_t TargetPhrase::ReadScoresFromFile(std::fstream &fileTPColl)
{
  UTIL_THROW_IF2(m_scores.size() == 0, "Translation rules must must have some scores");

  uint64_t bytesRead = 0;

  for (size_t ind = 0; ind < m_scores.size(); ++ind) {
    fileTPColl.read((char*) &m_scores[ind], sizeof(float));

    bytesRead += sizeof(float);
  }

  std::transform(m_scores.begin(),m_scores.end(),m_scores.begin(), Moses::TransformScore);
  std::transform(m_scores.begin(),m_scores.end(),m_scores.begin(), Moses::FloorScore);

  return bytesRead;
}

void TargetPhrase::DebugPrint(ostream &out, const Vocab &vocab) const
{
  Phrase::DebugPrint(out, vocab);

  for (size_t ind = 0; ind < m_align.size(); ++ind) {
    const AlignPair &alignPair = m_align[ind];
    out << alignPair.first << "-" << alignPair.second << " ";
  }
  out << ", ";

  for (size_t ind = 0; ind < m_scores.size(); ++ind) {
    out << m_scores[ind] << " ";
  }

  return;
}

std::ostream& operator<<(std::ostream &out, const TargetPhrase &phrase)
{
  out << (const Phrase&) phrase << ", " ;

  for (size_t ind = 0; ind < phrase.m_align.size(); ++ind) {
    const AlignPair &alignPair = phrase.m_align[ind];
    out << alignPair.first << "-" << alignPair.second << " ";
  }
  out << ", ";

  for (size_t ind = 0; ind < phrase.m_scores.size(); ++ind) {
    out << phrase.m_scores[ind] << " ";
  }

  return out;
}

} // namespace