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

Main.cpp « extract-mixed-syntax « phrase-extract - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 10656b5776ae47f8add35bbdb44fce3661b3b6e0 (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
#include <iostream>
#include <cstdlib>
#include <boost/program_options.hpp>

#include "Main.h"
#include "InputFileStream.h"
#include "OutputFileStream.h"
#include "AlignedSentence.h"
#include "AlignedSentenceSyntax.h"
#include "Parameter.h"
#include "Rules.h"

using namespace std;

bool g_debug = false;

int main(int argc, char** argv)
{
  cerr << "Starting" << endl;

  Parameter params;

  namespace po = boost::program_options;
  po::options_description desc("Options");
  desc.add_options()
    ("help", "Print help messages")
    ("MaxSpan", po::value<int>()->default_value(params.maxSpan), "Max (source) span of a rule. ie. number of words in the source")
    ("MinSpan", po::value<int>()->default_value(params.minSpan), "Min (source) span of a rule.")
    ("GlueGrammar", po::value<string>()->default_value(params.gluePath), "Output glue grammar to here")
    ("SentenceOffset", po::value<long>()->default_value(params.sentenceOffset), "Starting sentence id. Not used")
    ("GZOutput", "Compress extract files")
    ("MaxNonTerm", po::value<int>()->default_value(params.maxNonTerm), "Maximum number of non-terms allowed per rule")
    ("MaxHieroNonTerm", po::value<int>()->default_value(params.maxHieroNonTerm), "Maximum number of Hiero non-term. Usually, --MaxNonTerm is the normal constraint")
    ("MinHoleSource", po::value<int>()->default_value(params.minHoleSource), "Minimum source span for a non-term.")
    ("MinHoleSourceSyntax", po::value<int>()->default_value(params.minHoleSourceSyntax), "Minimum source span for a syntactic non-term (source or target).")

    ("SourceSyntax", "Source sentence is a parse tree")
    ("TargetSyntax", "Target sentence is a parse tree")
    ("MixedSyntaxType", po::value<int>()->default_value(params.mixedSyntaxType), "Hieu's Mixed syntax type. 0(default)=no mixed syntax, 1=add [X] only if no syntactic label. 2=add [X] everywhere")
    ("MultiLabel", po::value<int>()->default_value(params.multiLabel), "What to do with multiple labels on the same span. 0(default)=keep them all, 1=keep only top-most, 2=keep only bottom-most")
    ("HieroSourceLHS", "Always use Hiero source LHS? Default = 0")
    ("MaxSpanFreeNonTermSource", po::value<int>()->default_value(params.maxSpanFreeNonTermSource), "Max number of words covered by beginning/end NT. Default = 0 (no limit)")
    ("NoNieceTerminal", "Don't extract rule if 1 of the non-term covers the same word as 1 of the terminals")
    ("MaxScope", po::value<int>()->default_value(params.maxScope), "maximum scope (see Hopkins and Langmead (2010)). Default is HIGH")
    ("MinScope", po::value<int>()->default_value(params.minScope), "min scope.")

    ("SpanLength", "Property - span length of RHS each non-term")

    ("NonTermContext", "Property - (source) left and right, inside and outside words of each non-term ")
    ("NonTermContextTarget", "Property - (target) left and right, inside and outside words of each non-term")
    ("NonTermContextFactor", po::value<int>()->default_value(params.nonTermContextFactor), "Factor to use for non-term context property.")

    ("NumSourceFactors", po::value<int>()->default_value(params.numSourceFactors), "Number of source factors.")
    ("NumTargetFactors", po::value<int>()->default_value(params.numTargetFactors), "Number of target factors.")

    ("HieroNonTerm", po::value<string>()->default_value(params.hieroNonTerm), "Hiero non-terminal label, including bracket")
    ("ScopeSpan", po::value<string>()->default_value(params.scopeSpanStr), "Min and max span for rules of each scope. Format is min,max:min,max...")

    ("NonTermConsecSource", "Allow consecutive non-terms on the source side")
    ("NonTermConsecSourceMixedSyntax", po::value<int>()->default_value(params.nonTermConsecSourceMixedSyntax), "In mixed syntax mode, what nt can be consecutive. 0=don't allow consec nt. 1(default)=hiero+syntax. 2=syntax+syntax. 3=always allow");


  po::variables_map vm;
  try
  {
    po::store(po::parse_command_line(argc, argv, desc),
              vm); // can throw

    /** --help option
     */
    if ( vm.count("help") || argc < 5 )
    {
      std::cout << argv[0] << " target source alignment [options...]" << std::endl
                << desc << std::endl;
      return EXIT_SUCCESS;
    }

    po::notify(vm); // throws on error, so do after help in case
                    // there are any problems
  }
  catch(po::error& e)
  {
    std::cerr << "ERROR: " << e.what() << std::endl << std::endl;
    std::cerr << desc << std::endl;
    return EXIT_FAILURE;
  }

  if (vm.count("MaxSpan")) params.maxSpan = vm["MaxSpan"].as<int>();
  if (vm.count("MinSpan")) params.minSpan = vm["MinSpan"].as<int>();
  if (vm.count("GZOutput")) params.gzOutput = true;
  if (vm.count("GlueGrammar")) params.gluePath = vm["GlueGrammar"].as<string>();
  if (vm.count("SentenceOffset")) params.sentenceOffset = vm["SentenceOffset"].as<long>();
  if (vm.count("MaxNonTerm")) params.maxNonTerm = vm["MaxNonTerm"].as<int>();
  if (vm.count("MaxHieroNonTerm")) params.maxHieroNonTerm = vm["MaxHieroNonTerm"].as<int>();
  if (vm.count("MinHoleSource")) params.minHoleSource = vm["MinHoleSource"].as<int>();
  if (vm.count("MinHoleSourceSyntax")) params.minHoleSourceSyntax = vm["MinHoleSourceSyntax"].as<int>();

  if (vm.count("SourceSyntax")) params.sourceSyntax = true;
  if (vm.count("TargetSyntax")) params.targetSyntax = true;
  if (vm.count("MixedSyntaxType")) params.mixedSyntaxType = vm["MixedSyntaxType"].as<int>();
  if (vm.count("MultiLabel")) params.multiLabel = vm["MultiLabel"].as<int>();
  if (vm.count("HieroSourceLHS")) params.hieroSourceLHS = true;
  if (vm.count("MaxSpanFreeNonTermSource")) params.maxSpanFreeNonTermSource = vm["MaxSpanFreeNonTermSource"].as<int>();
  if (vm.count("NoNieceTerminal")) params.nieceTerminal = false;
  if (vm.count("MaxScope")) params.maxScope = vm["MaxScope"].as<int>();
  if (vm.count("MinScope")) params.minScope = vm["MinScope"].as<int>();

  // properties
  if (vm.count("SpanLength")) params.spanLength = true;
  if (vm.count("NonTermContext")) params.nonTermContext = true;
  if (vm.count("NonTermContextTarget")) params.nonTermContextTarget = true;
  if (vm.count("NonTermContextFactor")) params.nonTermContextFactor = vm["NonTermContextFactor"].as<int>();

  if (vm.count("NumSourceFactors")) params.numSourceFactors = vm["NumSourceFactors"].as<int>();
  if (vm.count("NumTargetFactors")) params.numTargetFactors = vm["NumTargetFactors"].as<int>();

  if (vm.count("HieroNonTerm")) params.hieroNonTerm = vm["HieroNonTerm"].as<string>();
  if (vm.count("ScopeSpan")) {
	  params.SetScopeSpan(vm["ScopeSpan"].as<string>());
  }

  if (vm.count("NonTermConsecSource")) params.nonTermConsecSource = true;
  if (vm.count("NonTermConsecSourceMixedSyntax")) params.nonTermConsecSourceMixedSyntax = vm["NonTermConsecSourceMixedSyntax"].as<int>();


  // input files;
  string pathTarget = argv[1];
  string pathSource = argv[2];
  string pathAlignment = argv[3];

  string pathExtract = argv[4];
  string pathExtractInv = pathExtract + ".inv";
  if (params.gzOutput) {
	  pathExtract += ".gz";
	  pathExtractInv += ".gz";
  }

  Moses::InputFileStream strmTarget(pathTarget);
  Moses::InputFileStream strmSource(pathSource);
  Moses::InputFileStream strmAlignment(pathAlignment);
  Moses::OutputFileStream extractFile(pathExtract);
  Moses::OutputFileStream extractInvFile(pathExtractInv);


  // MAIN LOOP
  int lineNum = 1;
  string lineTarget, lineSource, lineAlignment;
  while (getline(strmTarget, lineTarget)) {
	  if (lineNum % 10000 == 0) {
		  cerr << lineNum << " ";
	  }

	  bool success;
	  success = getline(strmSource, lineSource);
	  if (!success) {
		  throw "Couldn't read source";
	  }
	  success = getline(strmAlignment, lineAlignment);
	  if (!success) {
		  throw "Couldn't read alignment";
	  }

	  /*
	  cerr << "lineTarget=" << lineTarget << endl;
	  cerr << "lineSource=" << lineSource << endl;
	  cerr << "lineAlignment=" << lineAlignment << endl;
	  */

	  AlignedSentence *alignedSentence;

	  if (params.sourceSyntax || params.targetSyntax) {
		  alignedSentence = new AlignedSentenceSyntax(lineNum, lineSource, lineTarget, lineAlignment);
	  }
	  else {
		  alignedSentence = new AlignedSentence(lineNum, lineSource, lineTarget, lineAlignment);
	  }

	  alignedSentence->Create(params);
	  //cerr << alignedSentence->Debug();

	  Rules rules(*alignedSentence);
	  rules.Extend(params);
	  rules.Consolidate(params);
	  //cerr << rules.Debug();

	  rules.Output(extractFile, true, params);
	  rules.Output(extractInvFile, false, params);

	  delete alignedSentence;

	  ++lineNum;
  }

  if (!params.gluePath.empty()) {
	  Moses::OutputFileStream glueFile(params.gluePath);
	  CreateGlueGrammar(glueFile);
  }

  cerr << "Finished" << endl;
}

void CreateGlueGrammar(Moses::OutputFileStream &glueFile)
{
	glueFile << "<s> [X] ||| <s> [S] ||| 1 ||| ||| 0" << endl
			<< "[X][S] </s> [X] ||| [X][S] </s> [S] ||| 1 ||| 0-0 ||| 0" << endl
			<< "[X][S] [X][X] [X] ||| [X][S] [X][X] [S] ||| 2.718 ||| 0-0 1-1 ||| 0" << endl;

}