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

pcfg_score.cc « pcfg-score « phrase-extract - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a561c18edc0589036413c46c1c2dcd6bdb8bc033 (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
/***********************************************************************
 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_score.h"

#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <map>
#include <memory>
#include <set>
#include <string>
#include <vector>
#include "options.h"
#include "tree_scorer.h"

#include <boost/program_options.hpp>

#include "syntax-common/exception.h"

#include "pcfg-common/pcfg.h"
#include "pcfg-common/pcfg_tree.h"
#include "pcfg-common/syntax_tree.h"
#include "pcfg-common/typedef.h"
#include "pcfg-common/xml_tree_parser.h"

namespace MosesTraining {
namespace Syntax {
namespace PCFG {

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

  // Open PCFG stream.
  std::ifstream pcfg_stream;
  OpenNamedInputOrDie(options.pcfg_file, pcfg_stream);

  // Read PCFG.
  Pcfg pcfg;
  Vocabulary non_term_vocab;
  pcfg.Read(pcfg_stream, non_term_vocab);

  // Score corpus according to PCFG.
  TreeScorer scorer(pcfg, non_term_vocab);
  XmlTreeParser parser;
  XmlTreeWriter<PcfgTree> writer;
  std::string line;
  std::size_t line_num = 0;
  std::auto_ptr<PcfgTree> 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());
      std::cout << line << std::endl;
      continue;
    }
    if (!scorer.Score(*tree)) {
      std::ostringstream msg;
      msg << "failed to score tree at line " << line_num;
      Warn(msg.str());
      std::cout << line << std::endl;
      continue;
    }
    writer.Write(*tree, std::cout);
  }

  return 0;
}

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

  std::ostringstream usage_top;
  usage_top << "Usage: " << name() << " PCFG\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()
    ("pcfg-file", po::value(&options.pcfg_file), "pcfg file")
  ;

  // 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;
  p.add("pcfg-file", 1);

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

  // Check positional options were given.

  if (!vm.count("pcfg-file")) {
    std::ostringstream msg;
    msg << "missing required argument\n\n" << visible << std::endl;
    Error(msg.str());
  }
}

}  // namespace PCFG
}  // namespace Syntax
}  // namespace MosesTraining