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

ScoreStsg.cpp « score-stsg « phrase-extract - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 09395e21ecb0194b3402b20a45d5921ab7b0cc32 (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
#include "ScoreStsg.h"

#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <vector>

#include <boost/program_options.hpp>

#include "util/string_piece.hh"
#include "util/string_piece_hash.hh"
#include "util/tokenize_piece.hh"

#include "InputFileStream.h"
#include "OutputFileStream.h"

#include "syntax-common/exception.h"

#include "LexicalTable.h"
#include "Options.h"
#include "RuleGroup.h"
#include "RuleTableWriter.h"

namespace MosesTraining
{
namespace Syntax
{
namespace ScoreStsg
{

const int ScoreStsg::kCountOfCountsMax = 10;

ScoreStsg::ScoreStsg()
  : m_name("score-stsg")
  , m_lexTable(m_srcVocab, m_tgtVocab)
  , m_countOfCounts(kCountOfCountsMax, 0)
  , m_totalDistinct(0)
{
}

int ScoreStsg::Main(int argc, char *argv[])
{
  // Process command-line options.
  ProcessOptions(argc, argv, m_options);

  // Open input files.
  Moses::InputFileStream extractStream(m_options.extractFile);
  Moses::InputFileStream lexStream(m_options.lexFile);

  // Open output files.
  Moses::OutputFileStream outStream;
  Moses::OutputFileStream countOfCountsStream;
  OpenOutputFileOrDie(m_options.tableFile, outStream);
  if (m_options.goodTuring || m_options.kneserNey) {
    OpenOutputFileOrDie(m_options.tableFile+".coc", countOfCountsStream);
  }

  // Load lexical table.
  if (!m_options.noLex) {
    m_lexTable.Load(lexStream);
  }

  const util::MultiCharacter delimiter("|||");
  std::size_t lineNum = 0;
  std::size_t startLine= 0;
  std::string line;
  std::string tmp;
  RuleGroup ruleGroup;
  RuleTableWriter ruleTableWriter(m_options, outStream);

  while (std::getline(extractStream, line)) {
    ++lineNum;

    // Tokenize the input line.
    util::TokenIter<util::MultiCharacter> it(line, delimiter);
    StringPiece source = *it++;
    StringPiece target = *it++;
    StringPiece ntAlign = *it++;
    StringPiece fullAlign = *it++;
    it->CopyToString(&tmp);
    int count = std::atoi(tmp.c_str());
    double treeScore = 0.0f;
    if (m_options.treeScore && !m_options.inverse) {
      ++it;
      it->CopyToString(&tmp);
      treeScore = std::atof(tmp.c_str());
    }

    // If this is the first line or if source has changed since the last
    // line then process the current rule group and start a new one.
    if (source != ruleGroup.GetSource()) {
      if (lineNum > 1) {
        ProcessRuleGroupOrDie(ruleGroup, ruleTableWriter, startLine, lineNum-1);
      }
      startLine = lineNum;
      ruleGroup.SetNewSource(source);
    }

    // Add the rule to the current rule group.
    ruleGroup.AddRule(target, ntAlign, fullAlign, count, treeScore);
  }

  // Process the final rule group.
  ProcessRuleGroupOrDie(ruleGroup, ruleTableWriter, startLine, lineNum);

  // Write count of counts file.
  if (m_options.goodTuring || m_options.kneserNey) {
    // Kneser-Ney needs the total number of distinct rules.
    countOfCountsStream << m_totalDistinct << std::endl;
    // Write out counts of counts.
    for (int i = 1; i <= kCountOfCountsMax; ++i) {
      countOfCountsStream << m_countOfCounts[i] << std::endl;
    }
  }

  return 0;
}

void ScoreStsg::TokenizeRuleHalf(const std::string &s, TokenizedRuleHalf &half)
{
  // Copy s to half.string, but strip any leading or trailing whitespace.
  std::size_t start = s.find_first_not_of(" \t");
  if (start == std::string::npos) {
    throw Exception("rule half is empty");
  }
  std::size_t end = s.find_last_not_of(" \t");
  assert(end != std::string::npos);
  half.string = s.substr(start, end-start+1);

  // Tokenize half.string.
  half.tokens.clear();
  for (TreeFragmentTokenizer p(half.string);
       p != TreeFragmentTokenizer(); ++p) {
    half.tokens.push_back(*p);
  }

  // Extract the frontier symbols.
  half.frontierSymbols.clear();
  const std::size_t numTokens = half.tokens.size();
  for (int i = 0; i < numTokens; ++i) {
    if (half.tokens[i].type != TreeFragmentToken_WORD) {
      continue;
    }
    if (i == 0 || half.tokens[i-1].type != TreeFragmentToken_LSB) {
      // A word is a terminal iff it doesn't follow '['
      half.frontierSymbols.resize(half.frontierSymbols.size()+1);
      half.frontierSymbols.back().value = half.tokens[i].value;
      half.frontierSymbols.back().isNonTerminal = false;
    } else if (i+1 < numTokens &&
               half.tokens[i+1].type == TreeFragmentToken_RSB) {
      // A word is a non-terminal iff it it follows '[' and is succeeded by ']'
      half.frontierSymbols.resize(half.frontierSymbols.size()+1);
      half.frontierSymbols.back().value = half.tokens[i].value;
      half.frontierSymbols.back().isNonTerminal = true;
      ++i;  // Skip over the ']'
    }
  }
}

void ScoreStsg::ProcessRuleGroupOrDie(const RuleGroup &group,
                                      RuleTableWriter &writer,
                                      std::size_t start,
                                      std::size_t end)
{
  try {
    ProcessRuleGroup(group, writer);
  } catch (const Exception &e) {
    std::ostringstream msg;
    msg << "failed to process rule group at lines " << start << "-" << end
        << ": " << e.msg();
    Error(msg.str());
  } catch (const std::exception &e) {
    std::ostringstream msg;
    msg << "failed to process rule group at lines " << start << "-" << end
        << ": " << e.what();
    Error(msg.str());
  }
}

void ScoreStsg::ProcessRuleGroup(const RuleGroup &group,
                                 RuleTableWriter &writer)
{
  const std::size_t totalCount = group.GetTotalCount();
  const std::size_t distinctCount = group.GetSize();

  TokenizeRuleHalf(group.GetSource(), m_sourceHalf);

  const bool fullyLexical = m_sourceHalf.IsFullyLexical();

  // Process each distinct rule in turn.
  for (RuleGroup::ConstIterator p = group.Begin(); p != group.End(); ++p) {
    const RuleGroup::DistinctRule &rule = *p;

    // Update count of count statistics.
    if (m_options.goodTuring || m_options.kneserNey) {
      ++m_totalDistinct;
      int countInt = rule.count + 0.99999;
      if (countInt <= kCountOfCountsMax) {
        ++m_countOfCounts[countInt];
      }
    }

    // If the rule is not fully lexical then discard it if the count is below
    // the threshold value.
    if (!fullyLexical && rule.count < m_options.minCountHierarchical) {
      continue;
    }

    TokenizeRuleHalf(rule.target, m_targetHalf);

    // Find the most frequent alignment (if there's a tie, take the first one).
    std::vector<std::pair<std::string, int> >::const_iterator q =
      rule.alignments.begin();
    const std::pair<std::string, int> *bestAlignmentAndCount = &(*q++);
    for (; q != rule.alignments.end(); ++q) {
      if (q->second > bestAlignmentAndCount->second) {
        bestAlignmentAndCount = &(*q);
      }
    }
    const std::string &bestAlignment = bestAlignmentAndCount->first;
    ParseAlignmentString(bestAlignment, m_targetHalf.frontierSymbols.size(),
                         m_tgtToSrc);

    // Compute the lexical translation probability.
    double lexProb = ComputeLexProb(m_sourceHalf.frontierSymbols,
                                    m_targetHalf.frontierSymbols, m_tgtToSrc);

    // Write a line to the rule table.
    writer.WriteLine(m_sourceHalf, m_targetHalf, bestAlignment, lexProb,
                     rule.treeScore, p->count, totalCount, distinctCount);
  }
}

void ScoreStsg::ParseAlignmentString(const std::string &s, int numTgtWords,
                                     ALIGNMENT &tgtToSrc)
{
  tgtToSrc.clear();
  tgtToSrc.resize(numTgtWords);

  const std::string digits = "0123456789";

  std::string::size_type begin = 0;
  while (true) {
    std::string::size_type end = s.find("-", begin);
    if (end == std::string::npos) {
      return;
    }
    int src = std::atoi(s.substr(begin, end-begin).c_str());
    if (end+1 == s.size()) {
      throw Exception("Target index missing");
    }
    begin = end+1;
    end = s.find_first_not_of(digits, begin+1);
    int tgt;
    if (end == std::string::npos) {
      tgt = std::atoi(s.substr(begin).c_str());
      tgtToSrc[tgt].insert(src);
      return;
    } else {
      tgt = std::atoi(s.substr(begin, end-begin).c_str());
      tgtToSrc[tgt].insert(src);
    }
    begin = end+1;
  }
}

double ScoreStsg::ComputeLexProb(const std::vector<RuleSymbol> &sourceFrontier,
                                 const std::vector<RuleSymbol> &targetFrontier,
                                 const ALIGNMENT &tgtToSrc)
{
  double lexScore = 1.0;
  for (std::size_t i = 0; i < targetFrontier.size(); ++i) {
    if (targetFrontier[i].isNonTerminal) {
      continue;
    }
    Vocabulary::IdType tgtId = m_tgtVocab.Lookup(targetFrontier[i].value,
                               StringPieceCompatibleHash(),
                               StringPieceCompatibleEquals());
    const std::set<std::size_t> &srcIndices = tgtToSrc[i];
    if (srcIndices.empty()) {
      // Explain unaligned word by NULL.
      lexScore *= m_lexTable.PermissiveLookup(Vocabulary::NullId(), tgtId);
    } else {
      double thisWordScore = 0.0;
      for (std::set<std::size_t>::const_iterator p = srcIndices.begin();
           p != srcIndices.end(); ++p) {
        Vocabulary::IdType srcId =
          m_srcVocab.Lookup(sourceFrontier[*p].value,
                            StringPieceCompatibleHash(),
                            StringPieceCompatibleEquals());
        thisWordScore += m_lexTable.PermissiveLookup(srcId, tgtId);
      }
      lexScore *= thisWordScore / static_cast<double>(srcIndices.size());
    }
  }
  return lexScore;
}

void ScoreStsg::OpenOutputFileOrDie(const std::string &filename,
                                    Moses::OutputFileStream &stream)
{
  bool ret = stream.Open(filename);
  if (!ret) {
    std::ostringstream msg;
    msg << "failed to open output file: " << filename;
    Error(msg.str());
  }
}

void ScoreStsg::ProcessOptions(int argc, char *argv[], Options &options) const
{
  namespace po = boost::program_options;
  namespace cls = boost::program_options::command_line_style;

  // Construct the 'top' of the usage message: the bit that comes before the
  // options list.
  std::ostringstream usageTop;
  usageTop << "Usage: " << GetName()
           << " [OPTION]... EXTRACT LEX TABLE\n\n"
           << "STSG rule scorer\n\n"
           << "Options";

  // Construct the 'bottom' of the usage message.
  std::ostringstream usageBottom;
  usageBottom << "TODO";

  // Declare the command line options that are visible to the user.
  po::options_description visible(usageTop.str());
  visible.add_options()
  ("GoodTuring",
   "apply Good-Turing smoothing to relative frequency probability estimates")
  ("Hierarchical",
   "ignored (included for compatibility with score)")
  ("Inverse",
   "use inverse mode")
  ("KneserNey",
   "apply Kneser-Ney smoothing to relative frequency probability estimates")
  ("LogProb",
   "output log probabilities")
  ("MinCountHierarchical",
   po::value(&options.minCountHierarchical)->
   default_value(options.minCountHierarchical),
   "filter out rules with frequency < arg (except fully lexical rules)")
  ("NegLogProb",
   "output negative log probabilities")
  ("NoLex",
   "do not compute lexical translation score")
  ("NoWordAlignment",
   "do not output word alignments")
  ("PCFG",
   "synonym for TreeScore (included for compatibility with score)")
  ("TreeScore",
   "include pre-computed tree score from extract")
  ("UnpairedExtractFormat",
   "ignored (included for compatibility with score)")
  ;

  // Declare the command line options that are hidden from the user
  // (these are used as positional options).
  po::options_description hidden("Hidden options");
  hidden.add_options()
  ("ExtractFile",
   po::value(&options.extractFile),
   "extract file")
  ("LexFile",
   po::value(&options.lexFile),
   "lexical probability file")
  ("TableFile",
   po::value(&options.tableFile),
   "output file")
  ;

  // Compose the full set of command-line options.
  po::options_description cmdLineOptions;
  cmdLineOptions.add(visible).add(hidden);

  // Register the positional options.
  po::positional_options_description p;
  p.add("ExtractFile", 1);
  p.add("LexFile", 1);
  p.add("TableFile", 1);

  // Process the command-line.
  po::variables_map vm;
  const int optionStyle = cls::allow_long
                          | cls::long_allow_adjacent
                          | cls::long_allow_next;
  try {
    po::store(po::command_line_parser(argc, argv).style(optionStyle).
              options(cmdLineOptions).positional(p).run(), vm);
    po::notify(vm);
  } catch (const std::exception &e) {
    std::ostringstream msg;
    msg << e.what() << "\n\n" << visible << usageBottom.str();
    Error(msg.str());
  }

  if (vm.count("help")) {
    std::cout << visible << usageBottom.str() << std::endl;
    std::exit(0);
  }

  // Check all positional options were given.
  if (!vm.count("ExtractFile") ||
      !vm.count("LexFile") ||
      !vm.count("TableFile")) {
    std::ostringstream msg;
    std::cerr << visible << usageBottom.str() << std::endl;
    std::exit(1);
  }

  // Process Boolean options.
  if (vm.count("GoodTuring")) {
    options.goodTuring = true;
  }
  if (vm.count("Inverse")) {
    options.inverse = true;
  }
  if (vm.count("KneserNey")) {
    options.kneserNey = true;
  }
  if (vm.count("LogProb")) {
    options.logProb = true;
  }
  if (vm.count("NegLogProb")) {
    options.negLogProb = true;
  }
  if (vm.count("NoLex")) {
    options.noLex = true;
  }
  if (vm.count("NoWordAlignment")) {
    options.noWordAlignment = true;
  }
  if (vm.count("TreeScore") || vm.count("PCFG")) {
    options.treeScore = true;
  }
}

void ScoreStsg::Error(const std::string &msg) const
{
  std::cerr << GetName() << ": " << msg << std::endl;
  std::exit(1);
}

}  // namespace ScoreStsg
}  // namespace Syntax
}  // namespace MosesTraining