00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "pcfg_score.h"
00021
00022 #include <cassert>
00023 #include <cstdlib>
00024 #include <fstream>
00025 #include <iostream>
00026 #include <map>
00027 #include <memory>
00028 #include <set>
00029 #include <string>
00030 #include <vector>
00031 #include "options.h"
00032 #include "tree_scorer.h"
00033
00034 #include <boost/program_options.hpp>
00035
00036 #include "SyntaxTree.h"
00037
00038 #include "syntax-common/exception.h"
00039 #include "syntax-common/pcfg.h"
00040 #include "syntax-common/vocabulary.h"
00041 #include "syntax-common/xml_tree_parser.h"
00042 #include "syntax-common/xml_tree_writer.h"
00043
00044 namespace MosesTraining
00045 {
00046 namespace Syntax
00047 {
00048 namespace PCFG
00049 {
00050
00051 int PcfgScore::Main(int argc, char *argv[])
00052 {
00053
00054 Options options;
00055 ProcessOptions(argc, argv, options);
00056
00057
00058 std::ifstream pcfg_stream;
00059 OpenInputFileOrDie(options.pcfg_file, pcfg_stream);
00060
00061
00062 Pcfg pcfg;
00063 Vocabulary non_term_vocab;
00064 pcfg.Read(pcfg_stream, non_term_vocab);
00065
00066
00067 TreeScorer scorer(pcfg, non_term_vocab);
00068 XmlTreeParser parser;
00069 XmlTreeWriter writer(std::cout);
00070 std::string line;
00071 std::size_t line_num = 0;
00072 std::auto_ptr<SyntaxTree> tree;
00073 while (std::getline(std::cin, line)) {
00074 ++line_num;
00075 try {
00076 tree = parser.Parse(line, true);
00077 } catch (Exception &e) {
00078 std::ostringstream msg;
00079 msg << "line " << line_num << ": " << e.msg();
00080 Error(msg.str());
00081 }
00082 if (!tree.get()) {
00083 std::ostringstream msg;
00084 msg << "no tree at line " << line_num;
00085 Warn(msg.str());
00086 std::cout << line << std::endl;
00087 continue;
00088 }
00089 if (!scorer.Score(*tree)) {
00090 std::ostringstream msg;
00091 msg << "failed to score tree at line " << line_num;
00092 Warn(msg.str());
00093 std::cout << line << std::endl;
00094 continue;
00095 }
00096 writer.Write(*tree);
00097 }
00098
00099 return 0;
00100 }
00101
00102 void PcfgScore::ProcessOptions(int argc, char *argv[], Options &options) const
00103 {
00104 namespace po = boost::program_options;
00105
00106 std::ostringstream usage_top;
00107 usage_top << "Usage: " << name() << " PCFG\n\n"
00108 << "Options";
00109
00110
00111 po::options_description visible(usage_top.str());
00112 visible.add_options()
00113 ("help", "print help message and exit")
00114 ;
00115
00116
00117
00118 po::options_description hidden("Hidden options");
00119 hidden.add_options()
00120 ("pcfg-file", po::value(&options.pcfg_file), "pcfg file")
00121 ;
00122
00123
00124 po::options_description cmd_line_options;
00125 cmd_line_options.add(visible).add(hidden);
00126
00127
00128 po::positional_options_description p;
00129 p.add("pcfg-file", 1);
00130
00131
00132 po::variables_map vm;
00133 try {
00134 po::store(po::command_line_parser(argc, argv).style(MosesOptionStyle()).
00135 options(cmd_line_options).positional(p).run(), vm);
00136 po::notify(vm);
00137 } catch (const std::exception &e) {
00138 std::ostringstream msg;
00139 msg << e.what() << "\n\n" << visible;
00140 Error(msg.str());
00141 }
00142
00143 if (vm.count("help")) {
00144 std::cout << visible << std::endl;
00145 std::exit(0);
00146 }
00147
00148
00149
00150 if (!vm.count("pcfg-file")) {
00151 std::ostringstream msg;
00152 msg << "missing required argument\n\n" << visible << std::endl;
00153 Error(msg.str());
00154 }
00155 }
00156
00157 }
00158 }
00159 }