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
|
/***********************************************************************
Moses - statistical machine translation system
Copyright (C) 2006-2012 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 "pcfg_extract.h"
#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include <boost/program_options.hpp>
#include "syntax-common/exception.h"
#include "syntax-common/xml_tree_parser.h"
#include "SyntaxTree.h"
#include "pcfg-common/pcfg.h"
#include "pcfg-common/typedef.h"
#include "options.h"
#include "rule_collection.h"
#include "rule_extractor.h"
namespace MosesTraining
{
namespace Syntax
{
namespace PCFG
{
int PcfgExtract::Main(int argc, char *argv[])
{
// Process command-line options.
Options options;
ProcessOptions(argc, argv, options);
// Extract PCFG rules from corpus.
Vocabulary non_term_vocab;
RuleExtractor rule_extractor(non_term_vocab);
RuleCollection rule_collection;
XmlTreeParser parser;
std::string line;
std::size_t line_num = 0;
std::auto_ptr<MosesTraining::SyntaxTree> tree;
while (std::getline(std::cin, line)) {
++line_num;
try {
tree = parser.Parse(line);
} catch (Exception &e) {
std::ostringstream msg;
msg << "line " << line_num << ": " << e.msg();
Error(msg.str());
}
if (!tree.get()) {
std::ostringstream msg;
msg << "no tree at line " << line_num;
Warn(msg.str());
continue;
}
rule_extractor.Extract(*tree, rule_collection);
}
// Score rules and write PCFG to output.
Pcfg pcfg;
rule_collection.CreatePcfg(pcfg);
pcfg.Write(non_term_vocab, std::cout);
return 0;
}
void PcfgExtract::ProcessOptions(int argc, char *argv[],
Options &options) const
{
namespace po = boost::program_options;
std::ostringstream usage_top;
usage_top << "Usage: " << name() << "\n\n" << "Options";
// Declare the command line options that are visible to the user.
po::options_description visible(usage_top.str());
visible.add_options()
("help", "print help message and exit")
;
// 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();
// Compose the full set of command-line options.
po::options_description cmd_line_options;
cmd_line_options.add(visible).add(hidden);
// Register the positional options.
po::positional_options_description p;
// Process the command-line.
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv).style(CommonOptionStyle()).
options(cmd_line_options).positional(p).run(), vm);
po::notify(vm);
} catch (const std::exception &e) {
std::ostringstream msg;
msg << e.what() << "\n\n" << visible;
Error(msg.str());
}
if (vm.count("help")) {
std::cout << visible << std::endl;
std::exit(0);
}
}
} // namespace PCFG
} // namespace Syntax
} // namespace MosesTraining
|