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

Compactify.cpp « compact-rule-table « training « scripts - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ceb7eb090e7b8c6faf33b24756596fa9fbcca88f (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
#include "Compactify.h"

#include "NumberedSet.h"
#include "Options.h"
#include "RuleTableParser.h"

#include <boost/algorithm/string.hpp>
#include <boost/program_options.hpp>
#include <boost/unordered_map.hpp>

#include <cstdlib>
#include <fstream>
#include <iostream>
#include <set>
#include <sstream>

namespace moses {

int Compactify::main(int argc, char *argv[]) {
  // Process the command-line arguments.
  Options options;
  processOptions(argc, argv, options);

  // Open the input stream.
  std::istream *inputPtr;
  std::ifstream inputFileStream;
  if (options.inputFile.empty() || options.inputFile == "-") {
    inputPtr = &(std::cin);
  } else {
    inputFileStream.open(options.inputFile.c_str());
    if (!inputFileStream) {
      std::ostringstream msg;
      msg << "failed to open input file: " << options.inputFile;
      error(msg.str());
    }
    inputPtr = &inputFileStream;
  }
  std::istream &input = *inputPtr;

  // Open the output stream.
  std::ostream *outputPtr;
  std::ofstream outputFileStream;
  if (options.outputFile.empty()) {
    outputPtr = &(std::cout);
  } else {
    outputFileStream.open(options.outputFile.c_str());
    if (!outputFileStream) {
      std::ostringstream msg;
      msg << "failed to open output file: " << options.outputFile;
      error(msg.str());
    }
    outputPtr = &outputFileStream;
  }
  std::ostream &output = *outputPtr;

  // Open a temporary file: the rule section must appear last in the output
  // file, but we don't want to store the full set of rules in memory during
  // processing, so instead they're written to a temporary file then copied to
  // the output file as a final step.
  std::fstream tempFileStream;
  {
    char fileNameTemplate[] = "/tmp/compact_XXXXXX";
    int fd = mkstemp(fileNameTemplate);
    if (fd == -1) {
      std::ostringstream msg;
      msg << "failed to open temporary file with pattern " << fileNameTemplate;
      error(msg.str());
    }
    tempFileStream.open(fileNameTemplate);
    if (!tempFileStream) {
      std::ostringstream msg;
      msg << "failed to open existing temporary file: " << fileNameTemplate;
      error(msg.str());
    }
    // Close the original file descriptor.
    close(fd);
    // Unlink the file.  Its contents will be safe until tempFileStream is
    // closed.
    unlink(fileNameTemplate);
  }

  // Write the version number
  output << "1" << '\n';

  SymbolSet symbolSet;
  PhraseSet sourcePhraseSet;
  PhraseSet targetPhraseSet;
  AlignmentSetSet alignmentSetSet;

  SymbolPhrase symbolPhrase;

  size_t ruleCount = 0;
  RuleTableParser end;
  try {
    for (RuleTableParser parser(input); parser != end; ++parser) {
      const RuleTableParser::Entry &entry = *parser;
      ++ruleCount;

      // Report progress in the same format as extract-rules.
      if (ruleCount % 100000 == 0) {
        std::cerr << "." << std::flush;
      }
      if (ruleCount % 1000000 == 0) {
        std::cerr << " " << ruleCount << std::endl;
      }

      // Encode the source LHS + RHS as a vector of symbol IDs and insert into
      // sourcePhraseSet.
      encodePhrase(entry.sourceLhs, entry.sourceRhs, symbolSet, symbolPhrase);
      SymbolIDType sourceId = sourcePhraseSet.insert(symbolPhrase);

      // Encode the target LHS + RHS as a vector of symbol IDs and insert into
      // targetPhraseSet.
      encodePhrase(entry.targetLhs, entry.targetRhs, symbolSet, symbolPhrase);
      SymbolIDType targetId = targetPhraseSet.insert(symbolPhrase);

      // Insert the alignments into alignmentSetSet.
      AlignmentSetIDType alignmentSetId = alignmentSetSet.insert(
          entry.alignments);

      // Write this rule to the temporary file.
      tempFileStream << sourceId << " " << targetId << " " << alignmentSetId;
      for (std::vector<std::string>::const_iterator p = entry.scores.begin();
           p != entry.scores.end(); ++p) {
        tempFileStream << " " << *p;
      }
      tempFileStream << " :";
      for (std::vector<std::string>::const_iterator p = entry.counts.begin();
           p != entry.counts.end(); ++p) {
        tempFileStream << " " << *p;
      }
      tempFileStream << '\n';
    }
  } catch (Exception &e) {
    std::ostringstream msg;
    msg << "error processing line " << ruleCount+1 << ": " << e.getMsg();
    error(msg.str());
  }

  // Report the counts.

  if (ruleCount % 1000000 != 0) {
    std::cerr << std::endl;
  }
  std::cerr << "Rule count:          " << ruleCount << std::endl;
  std::cerr << "Symbol count:        " << symbolSet.size() << std::endl;
  std::cerr << "Source phrase count: " << sourcePhraseSet.size() << std::endl;
  std::cerr << "Target phrase count: " << targetPhraseSet.size() << std::endl;
  std::cerr << "Alignment set count: " << alignmentSetSet.size() << std::endl;

  // Write the symbol vocabulary.

  output << symbolSet.size() << '\n';
  for (SymbolSet::const_iterator p = symbolSet.begin();
       p != symbolSet.end(); ++p) {
    const std::string &str = **p;
    output << str << '\n';
  }

  // Write the source phrases.

  output << sourcePhraseSet.size() << '\n';
  for (PhraseSet::const_iterator p = sourcePhraseSet.begin();
       p != sourcePhraseSet.end(); ++p) {
    const SymbolPhrase &sourcePhrase = **p;
    for (SymbolPhrase::const_iterator q = sourcePhrase.begin();
         q != sourcePhrase.end(); ++q) {
      if (q != sourcePhrase.begin()) {
        output << " ";
      }
      output << *q;
    }
    output << '\n';
  }

  // Write the target phrases.

  output << targetPhraseSet.size() << '\n';
  for (PhraseSet::const_iterator p = targetPhraseSet.begin();
       p != targetPhraseSet.end(); ++p) {
    const SymbolPhrase &targetPhrase = **p;
    for (SymbolPhrase::const_iterator q = targetPhrase.begin();
         q != targetPhrase.end(); ++q) {
      if (q != targetPhrase.begin()) {
        output << " ";
      }
      output << *q;
    }
    output << '\n';
  }

  // Write the alignment sets.

  output << alignmentSetSet.size() << '\n';
  for (AlignmentSetSet::const_iterator p = alignmentSetSet.begin();
       p != alignmentSetSet.end(); ++p) {
    const AlignmentSet &alignmentSet = **p;
    for (AlignmentSet::const_iterator q = alignmentSet.begin();
          q != alignmentSet.end(); ++q) {
      if (q != alignmentSet.begin()) {
        output << " ";
      }
      output << q->first << "-" << q->second;
    }
    output << '\n';
  }

  // Write the rule count.
  output << ruleCount << '\n';

  // Copy the rules from the temporary file.
  tempFileStream.seekg(0);
  std::string line;
  while (std::getline(tempFileStream, line)) {
    output << line << '\n';
  }

  return 0;
}

void Compactify::processOptions(int argc, char *argv[],
                                Options &options) const {
  namespace po = boost::program_options;

  std::ostringstream usageMsg;
  usageMsg << "usage: " << getName() << " [OPTION]... [FILE]";

  // Declare the command line options that are visible to the user.
  std::string caption = usageMsg.str() + std::string("\n\nAllowed options");
  po::options_description visible(caption);
  visible.add_options()
    ("help", "print help message and exit")
    ("output,o", po::value<std::string>(),
                 "write rule table to arg instead of standard output")
  ;

  // 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()
    ("input", po::value<std::string>(), "input 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("input", 1);

  // Process the command-line.
  po::variables_map vm;
  try {
    po::store(po::command_line_parser(argc, argv).
              options(cmdLineOptions).positional(p).run(), vm);
    po::notify(vm);
  } catch (const std::exception &e) {
    std::ostringstream msg;
    msg << e.what() << "\n\n" << visible;
    error(msg.str());
    std::exit(1);
  }

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

  // Process positional options.

  if (vm.count("input")) {
    options.inputFile = vm["input"].as<std::string>();
  }

  // Process remaining options.

  if (vm.count("output")) {
    options.outputFile = vm["output"].as<std::string>();
  }
}

void Compactify::encodePhrase(const std::string &lhs, const StringPhrase &rhs,
                              SymbolSet &symbolSet, SymbolPhrase &vec) const {
  vec.clear();
  vec.reserve(rhs.size()+1);
  SymbolIDType id = symbolSet.insert(lhs);
  vec.push_back(id);
  for (std::vector<std::string>::const_iterator p = rhs.begin();
       p != rhs.end(); ++p) {
    SymbolIDType id = symbolSet.insert(*p);
    vec.push_back(id);
  }
}

}  // namespace moses