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

ExtractGHKM.cpp « extract-ghkm « phrase-extract - github.com/moses-smt/mosesdecoder.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7c210541d5a36c61eae38315c5dcf3ea4e8234c6 (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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/***********************************************************************
 Moses - statistical machine translation system
 Copyright (C) 2006-2011 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 "ExtractGHKM.h"

#include "Alignment.h"
#include "AlignmentGraph.h"
#include "Exception.h"
#include "InputFileStream.h"
#include "Node.h"
#include "OutputFileStream.h"
#include "Options.h"
#include "ParseTree.h"
#include "PhraseOrientation.h"
#include "ScfgRule.h"
#include "ScfgRuleWriter.h"
#include "Span.h"
#include "StsgRule.h"
#include "StsgRuleWriter.h"
#include "SyntaxTree.h"
#include "tables-core.h"
#include "XmlException.h"
#include "XmlTree.h"
#include "XmlTreeParser.h"

#include <boost/program_options.hpp>

#include <cassert>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <vector>

namespace Moses
{
namespace GHKM
{

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

  // Open input files.
  //
  // The GHKM algorithm is neutral about whether the model is string-to-tree or
  // tree-to-string.  This implementation assumes the model to be
  // string-to-tree, but if the -t2s option is given then the source and target
  // input files are switched prior to extraction and then the source and
  // target of the extracted rules are switched on output.
  std::string effectiveTargetFile = options.t2s ? options.sourceFile
                                                : options.targetFile;
  std::string effectiveSourceFile = options.t2s ? options.targetFile
                                                : options.sourceFile;
  InputFileStream targetStream(effectiveTargetFile);
  InputFileStream sourceStream(effectiveSourceFile);
  InputFileStream alignmentStream(options.alignmentFile);

  // Open output files.
  OutputFileStream fwdExtractStream;
  OutputFileStream invExtractStream;
  OutputFileStream glueGrammarStream;
  OutputFileStream targetUnknownWordStream;
  OutputFileStream sourceUnknownWordStream;
  OutputFileStream sourceLabelSetStream;
  OutputFileStream unknownWordSoftMatchesStream;

  std::string fwdFileName = options.extractFile;
  std::string invFileName = options.extractFile + std::string(".inv");
  if (options.gzOutput) {
    fwdFileName += ".gz";
    invFileName += ".gz";
  }
  OpenOutputFileOrDie(fwdFileName, fwdExtractStream);
  OpenOutputFileOrDie(invFileName, invExtractStream);

  if (!options.glueGrammarFile.empty()) {
    OpenOutputFileOrDie(options.glueGrammarFile, glueGrammarStream);
  }
  if (!options.targetUnknownWordFile.empty()) {
    OpenOutputFileOrDie(options.targetUnknownWordFile, targetUnknownWordStream);
  }
  if (!options.sourceUnknownWordFile.empty()) {
    OpenOutputFileOrDie(options.sourceUnknownWordFile, sourceUnknownWordStream);
  }
  if (!options.sourceLabelSetFile.empty()) {
    if (!options.sourceLabels) {
      Error("SourceLabels should be active if SourceLabelSet is supposed to be written to a file");
    }
    OpenOutputFileOrDie(options.sourceLabelSetFile, sourceLabelSetStream); // note that this is not a global source label set if extraction is parallelized
  }
  if (!options.unknownWordSoftMatchesFile.empty()) {
    OpenOutputFileOrDie(options.unknownWordSoftMatchesFile, unknownWordSoftMatchesStream);
  }

  // Target label sets for producing glue grammar.
  std::set<std::string> targetLabelSet;
  std::map<std::string, int> targetTopLabelSet;

  // Source label sets for producing glue grammar.
  std::set<std::string> sourceLabelSet;
  std::map<std::string, int> sourceTopLabelSet;

  // Word count statistics for producing unknown word labels.
  std::map<std::string, int> targetWordCount;
  std::map<std::string, std::string> targetWordLabel;

  // Word count statistics for producing unknown word labels: source side.
  std::map<std::string, int> sourceWordCount;
  std::map<std::string, std::string> sourceWordLabel;

  std::string targetLine;
  std::string sourceLine;
  std::string alignmentLine;
  Alignment alignment;
  XmlTreeParser targetXmlTreeParser(targetLabelSet, targetTopLabelSet);
//  XmlTreeParser sourceXmlTreeParser(sourceLabelSet, sourceTopLabelSet);
  ScfgRuleWriter scfgWriter(fwdExtractStream, invExtractStream, options);
  StsgRuleWriter stsgWriter(fwdExtractStream, invExtractStream, options);
  size_t lineNum = options.sentenceOffset;
  while (true) {
    std::getline(targetStream, targetLine);
    std::getline(sourceStream, sourceLine);
    std::getline(alignmentStream, alignmentLine);

    if (targetStream.eof() && sourceStream.eof() && alignmentStream.eof()) {
      break;
    }

    if (targetStream.eof() || sourceStream.eof() || alignmentStream.eof()) {
      Error("Files must contain same number of lines");
    }

    ++lineNum;

    // Parse target tree.
    if (targetLine.size() == 0) {
      std::cerr << "skipping line " << lineNum << " with empty target tree\n";
      continue;
    }
    std::auto_ptr<ParseTree> targetParseTree;
    try {
      targetParseTree = targetXmlTreeParser.Parse(targetLine);
      assert(targetParseTree.get());
    } catch (const Exception &e) {
      std::ostringstream oss;
      oss << "Failed to parse target XML tree at line " << lineNum;
      if (!e.GetMsg().empty()) {
        oss << ": " << e.GetMsg();
      }
      Error(oss.str());
    }


    // Parse source tree and construct a SyntaxTree object.
    MosesTraining::SyntaxTree sourceSyntaxTree;
    MosesTraining::SyntaxNode *sourceSyntaxTreeRoot=NULL;

    if (options.sourceLabels) {
      try {
        if (!ProcessAndStripXMLTags(sourceLine, sourceSyntaxTree, sourceLabelSet, sourceTopLabelSet, false)) {
          throw Exception("");
        }
        sourceSyntaxTree.ConnectNodes();
        sourceSyntaxTreeRoot = sourceSyntaxTree.GetTop();
        assert(sourceSyntaxTreeRoot);
      } catch (const Exception &e) {
        std::ostringstream oss;
        oss << "Failed to parse source XML tree at line " << lineNum;
        if (!e.GetMsg().empty()) {
          oss << ": " << e.GetMsg();
        }
        Error(oss.str());
      }
    }

    // Read source tokens.
    std::vector<std::string> sourceTokens(ReadTokens(sourceLine));

    // Construct a source ParseTree object from the SyntaxTree object.
    std::auto_ptr<ParseTree> sourceParseTree;

    if (options.sourceLabels) {
      try {
        sourceParseTree = XmlTreeParser::ConvertTree(*sourceSyntaxTreeRoot, sourceTokens);
        assert(sourceParseTree.get());
      } catch (const Exception &e) {
        std::ostringstream oss;
        oss << "Failed to parse source XML tree at line " << lineNum;
        if (!e.GetMsg().empty()) {
          oss << ": " << e.GetMsg();
        }
        Error(oss.str());
      }
    }


    // Read word alignments.
    try {
      ReadAlignment(alignmentLine, alignment);
    } catch (const Exception &e) {
      std::ostringstream oss;
      oss << "Failed to read alignment at line " << lineNum << ": ";
      oss << e.GetMsg();
      Error(oss.str());
    }
    if (alignment.size() == 0) {
      std::cerr << "skipping line " << lineNum << " without alignment points\n";
      continue;
    }
    if (options.t2s) {
      FlipAlignment(alignment);
    }

    // Record word counts.
    if (!options.targetUnknownWordFile.empty()) {
      CollectWordLabelCounts(*targetParseTree, options, targetWordCount, targetWordLabel);
    }

    // Record word counts: source side.
    if (options.sourceLabels && !options.sourceUnknownWordFile.empty()) {
      CollectWordLabelCounts(*sourceParseTree, options, sourceWordCount, sourceWordLabel);
    }

    // Form an alignment graph from the target tree, source words, and
    // alignment.
    AlignmentGraph graph(targetParseTree.get(), sourceTokens, alignment);

    // Extract minimal rules, adding each rule to its root node's rule set.
    graph.ExtractMinimalRules(options);

    // Extract composed rules.
    if (!options.minimal) {
      graph.ExtractComposedRules(options);
    }

    // Initialize phrase orientation scoring object
    PhraseOrientation phraseOrientation( sourceTokens.size(), targetXmlTreeParser.GetWords().size(), alignment);

    // Write the rules, subject to scope pruning.
    const std::vector<Node *> &targetNodes = graph.GetTargetNodes();
    for (std::vector<Node *>::const_iterator p = targetNodes.begin();
         p != targetNodes.end(); ++p) {

      const std::vector<const Subgraph *> &rules = (*p)->GetRules();

      Moses::GHKM::PhraseOrientation::REO_CLASS l2rOrientation=Moses::GHKM::PhraseOrientation::REO_CLASS_UNKNOWN, r2lOrientation=Moses::GHKM::PhraseOrientation::REO_CLASS_UNKNOWN;
      if (options.phraseOrientation && !rules.empty()) {
        int sourceSpanBegin = *((*p)->GetSpan().begin());
        int sourceSpanEnd   = *((*p)->GetSpan().rbegin());
        l2rOrientation = phraseOrientation.GetOrientationInfo(sourceSpanBegin,sourceSpanEnd,Moses::GHKM::PhraseOrientation::REO_DIR_L2R);
        r2lOrientation = phraseOrientation.GetOrientationInfo(sourceSpanBegin,sourceSpanEnd,Moses::GHKM::PhraseOrientation::REO_DIR_R2L);
        // std::cerr << "span " << sourceSpanBegin << " " << sourceSpanEnd << std::endl;
        // std::cerr << "phraseOrientation " << phraseOrientation.GetOrientationInfo(sourceSpanBegin,sourceSpanEnd) << std::endl;
      }

      for (std::vector<const Subgraph *>::const_iterator q = rules.begin();
           q != rules.end(); ++q) {
        // STSG output.
        if (options.stsg) {
          StsgRule rule(**q);
          if (rule.Scope() <= options.maxScope) {
            stsgWriter.Write(rule);
          }
          continue;
        }
        // SCFG output.
        ScfgRule *r = 0;
        if (options.sourceLabels) {
          r = new ScfgRule(**q, &sourceSyntaxTree);
        } else {
          r = new ScfgRule(**q);
        }
        // TODO Can scope pruning be done earlier?
        if (r->Scope() <= options.maxScope) {
          if (!options.treeFragments) {
            scfgWriter.Write(*r,false);
          } else {
            scfgWriter.Write(*r,**q,false);
          }
          if (options.phraseOrientation) {
            fwdExtractStream << " {{Orientation ";
            phraseOrientation.WriteOrientation(fwdExtractStream,l2rOrientation);
            fwdExtractStream << " ";
            phraseOrientation.WriteOrientation(fwdExtractStream,r2lOrientation);
            fwdExtractStream << "}}";
            phraseOrientation.IncrementPriorCount(Moses::GHKM::PhraseOrientation::REO_DIR_L2R,l2rOrientation,1);
            phraseOrientation.IncrementPriorCount(Moses::GHKM::PhraseOrientation::REO_DIR_R2L,r2lOrientation,1);
          }
          fwdExtractStream << std::endl;
          invExtractStream << std::endl;
        }
        delete r;
      }
    }
  }

  if (options.phraseOrientation) {
    std::string phraseOrientationPriorsFileName = options.extractFile + std::string(".phraseOrientationPriors");
    OutputFileStream phraseOrientationPriorsStream;
    OpenOutputFileOrDie(phraseOrientationPriorsFileName, phraseOrientationPriorsStream);
    PhraseOrientation::WritePriorCounts(phraseOrientationPriorsStream);
  }

  std::map<std::string,size_t> sourceLabels;
  if (options.sourceLabels && !options.sourceLabelSetFile.empty()) {

    sourceLabelSet.insert("XLHS"); // non-matching label (left-hand side)
    sourceLabelSet.insert("XRHS"); // non-matching label (right-hand side)
    sourceLabelSet.insert("TOPLABEL");  // as used in the glue grammar
    sourceLabelSet.insert("SOMELABEL"); // as used in the glue grammar
    size_t index = 0;
    for (std::set<std::string>::const_iterator iter=sourceLabelSet.begin();
         iter!=sourceLabelSet.end(); ++iter, ++index) {
      sourceLabels.insert(std::pair<std::string,size_t>(*iter,index));
    }
    WriteSourceLabelSet(sourceLabels, sourceLabelSetStream);
  }

  if (!options.glueGrammarFile.empty()) {
    WriteGlueGrammar(targetLabelSet, targetTopLabelSet, sourceLabels, options, glueGrammarStream);
  }

  if (!options.targetUnknownWordFile.empty()) {
    WriteUnknownWordLabel(targetWordCount, targetWordLabel, options, targetUnknownWordStream);
  }

  if (options.sourceLabels && !options.sourceUnknownWordFile.empty()) {
    WriteUnknownWordLabel(sourceWordCount, sourceWordLabel, options, sourceUnknownWordStream, true);
  }

  if (!options.unknownWordSoftMatchesFile.empty()) {
    WriteUnknownWordSoftMatches(targetLabelSet, unknownWordSoftMatchesStream);
  }

  return 0;
}

void ExtractGHKM::OpenInputFileOrDie(const std::string &filename,
                                     std::ifstream &stream)
{
  stream.open(filename.c_str());
  if (!stream) {
    std::ostringstream msg;
    msg << "failed to open input file: " << filename;
    Error(msg.str());
  }
}

void ExtractGHKM::OpenOutputFileOrDie(const std::string &filename,
                                      std::ofstream &stream)
{
  stream.open(filename.c_str());
  if (!stream) {
    std::ostringstream msg;
    msg << "failed to open output file: " << filename;
    Error(msg.str());
  }
}

void ExtractGHKM::OpenOutputFileOrDie(const std::string &filename,
                                      OutputFileStream &stream)
{
  bool ret = stream.Open(filename);
  if (!ret) {
    std::ostringstream msg;
    msg << "failed to open output file: " << filename;
    Error(msg.str());
  }
}

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

  // Construct the 'top' of the usage message: the bit that comes before the
  // options list.
  std::ostringstream usageTop;
  usageTop << "Usage: " << GetName()
           << " [OPTION]... TARGET SOURCE ALIGNMENT EXTRACT\n\n"
           << "SCFG rule extractor based on the GHKM algorithm described in\n"
           << "Galley et al. (2004).\n\n"
           << "Options";

  // Construct the 'bottom' of the usage message.
  std::ostringstream usageBottom;
  usageBottom << "\nImplementation Notes:\n"
              << "\nThe parse tree is assumed to contain part-of-speech preterminal nodes.\n"
              << "\n"
              << "For the composed rule constraints: rule depth is the maximum distance from the\nrule's root node to a sink node, not counting preterminal expansions or word\nalignments.  Rule size is the measure defined in DeNeefe et al (2007): the\nnumber of non-part-of-speech, non-leaf constituent labels in the target tree.\nNode count is the number of target tree nodes (excluding target words).\n"
              << "\n"
              << "Scope pruning (Hopkins and Langmead, 2010) is applied to both minimal and\ncomposed rules.\n"
              << "\n"
              << "Unaligned source words are attached to the tree using the following heuristic:\nif there are aligned source words to both the left and the right of an unaligned\nsource word then it is attached to the lowest common ancestor of its nearest\nsuch left and right neighbours.  Otherwise, it is attached to the root of the\nparse tree.\n"
              << "\n"
              << "Unless the --AllowUnary option is given, unary rules containing no lexical\nsource items are eliminated using the method described in Chung et al. (2011).\nThe parsing algorithm used in Moses is unable to handle such rules.\n"
              << "\n"
              << "References:\n"
              << "Galley, M., Hopkins, M., Knight, K., and Marcu, D. (2004)\n"
              << "\"What's in a Translation Rule?\", In Proceedings of HLT/NAACL 2004.\n"
              << "\n"
              << "DeNeefe, S., Knight, K., Wang, W., and Marcu, D. (2007)\n"
              << "\"What Can Syntax-Based MT Learn from Phrase-Based MT?\", In Proceedings of\nEMNLP-CoNLL 2007.\n"
              << "\n"
              << "Hopkins, M. and Langmead, G. (2010)\n"
              << "\"SCFG Decoding Without Binarization\", In Proceedings of EMNLP 2010.\n"
              << "\n"
              << "Chung, T. and Fang, L. and Gildea, D. (2011)\n"
              << "\"Issues Concerning Decoding with Synchronous Context-free Grammar\", In\nProceedings of ACL/HLT 2011.";

  // Declare the command line options that are visible to the user.
  po::options_description visible(usageTop.str());
  visible.add_options()
  //("help", "print this help message and exit")
  ("AllowUnary",
   "allow fully non-lexical unary rules")
  ("ConditionOnTargetLHS",
   "write target LHS instead of \"X\" as source LHS")
  ("GlueGrammar",
   po::value(&options.glueGrammarFile),
   "write glue grammar to named file")
  ("GZOutput",
   "write gzipped extract files")
  ("MaxNodes",
   po::value(&options.maxNodes)->default_value(options.maxNodes),
   "set maximum number of tree nodes for composed rules")
  ("MaxRuleDepth",
   po::value(&options.maxRuleDepth)->default_value(options.maxRuleDepth),
   "set maximum depth for composed rules")
  ("MaxRuleSize",
   po::value(&options.maxRuleSize)->default_value(options.maxRuleSize),
   "set maximum size for composed rules")
  ("MaxScope",
   po::value(&options.maxScope)->default_value(options.maxScope),
   "set maximum allowed scope")
  ("Minimal",
   "extract minimal rules only")
  ("PCFG",
   "include score based on PCFG scores in target corpus")
  ("PhraseOrientation",
   "output phrase orientation information")
  ("STSG",
   "output STSG rules (default is SCFG)")
  ("T2S",
   "enable tree-to-string rule extraction (string-to-tree is assumed by default)")
  ("TreeFragments",
   "output parse tree information")
  ("SourceLabels",
   "output source syntax label information")
  ("SourceLabelSet",
   po::value(&options.sourceLabelSetFile),
   "write source syntax label set to named file")
  ("SentenceOffset",
   po::value(&options.sentenceOffset)->default_value(options.sentenceOffset),
   "set sentence number offset if processing split corpus")
  ("UnknownWordLabel",
   po::value(&options.targetUnknownWordFile),
   "write unknown word labels to named file")
  ("SourceUnknownWordLabel",
   po::value(&options.sourceUnknownWordFile),
   "write source syntax unknown word labels to named file")
  ("UnknownWordMinRelFreq",
   po::value(&options.unknownWordMinRelFreq)->default_value(
     options.unknownWordMinRelFreq),
   "set minimum relative frequency for unknown word labels")
  ("UnknownWordSoftMatches",
   po::value(&options.unknownWordSoftMatchesFile),
   "write dummy value to unknown word label file, and mappings from dummy value to other labels to named file")
  ("UnknownWordUniform",
   "write uniform weights to unknown word label file")
  ("UnpairedExtractFormat",
   "do not pair non-terminals in extract files")
  ;

  // 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()
  ("TargetFile",
   po::value(&options.targetFile),
   "target file")
  ("SourceFile",
   po::value(&options.sourceFile),
   "source file")
  ("AlignmentFile",
   po::value(&options.alignmentFile),
   "alignment file")
  ("ExtractFile",
   po::value(&options.extractFile),
   "extract 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("TargetFile", 1);
  p.add("SourceFile", 1);
  p.add("AlignmentFile", 1);
  p.add("ExtractFile", 1);

  // Process the command-line.
  po::variables_map vm;
  const int optionStyle = cls::allow_long
                          | cls::long_allow_adjacent
                          | cls::long_allow_next;
  try {
    po::store(po::command_line_parser(argc, argv).style(optionStyle).
              options(cmdLineOptions).positional(p).run(), vm);
    po::notify(vm);
  } catch (const std::exception &e) {
    std::ostringstream msg;
    msg << e.what() << "\n\n" << visible << usageBottom.str();
    Error(msg.str());
  }

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

  // Check all positional options were given.
  if (!vm.count("TargetFile") ||
      !vm.count("SourceFile") ||
      !vm.count("AlignmentFile") ||
      !vm.count("ExtractFile")) {
    std::ostringstream msg;
    std::cerr << visible << usageBottom.str() << std::endl;
    std::exit(1);
  }

  // Process Boolean options.
  if (vm.count("AllowUnary")) {
    options.allowUnary = true;
  }
  if (vm.count("ConditionOnTargetLHS")) {
    options.conditionOnTargetLhs = true;
  }
  if (vm.count("GZOutput")) {
    options.gzOutput = true;
  }
  if (vm.count("Minimal")) {
    options.minimal = true;
  }
  if (vm.count("PCFG")) {
    options.pcfg = true;
  }
  if (vm.count("PhraseOrientation")) {
    options.phraseOrientation = true;
  }
  if (vm.count("STSG")) {
    options.stsg = true;
  }
  if (vm.count("T2S")) {
    options.t2s = true;
  }
  if (vm.count("TreeFragments")) {
    options.treeFragments = true;
  }
  if (vm.count("SourceLabels")) {
    options.sourceLabels = true;
  }
  if (vm.count("UnknownWordUniform")) {
    options.unknownWordUniform = true;
  }
  if (vm.count("UnpairedExtractFormat")) {
    options.unpairedExtractFormat = true;
  }

  // Workaround for extract-parallel issue.
  if (options.sentenceOffset > 0) {
    options.targetUnknownWordFile.clear();
  }
  if (options.sentenceOffset > 0) {
    options.sourceUnknownWordFile.clear();
    options.unknownWordSoftMatchesFile.clear();
  }
}

void ExtractGHKM::Error(const std::string &msg) const
{
  std::cerr << GetName() << ": " << msg << std::endl;
  std::exit(1);
}

std::vector<std::string> ExtractGHKM::ReadTokens(const std::string &s) const
{
  std::vector<std::string> tokens;

  std::string whitespace = " \t";

  std::string::size_type begin = s.find_first_not_of(whitespace);
  assert(begin != std::string::npos);
  while (true) {
    std::string::size_type end = s.find_first_of(whitespace, begin);
    std::string token;
    if (end == std::string::npos) {
      token = s.substr(begin);
    } else {
      token = s.substr(begin, end-begin);
    }
    tokens.push_back(token);
    if (end == std::string::npos) {
      break;
    }
    begin = s.find_first_not_of(whitespace, end);
    if (begin == std::string::npos) {
      break;
    }
  }

  return tokens;
}

void ExtractGHKM::WriteGlueGrammar(
  const std::set<std::string> &labelSet,
  const std::map<std::string, int> &topLabelSet,
  const std::map<std::string,size_t> &sourceLabels,
  const Options &options,
  std::ostream &out)
{
  // choose a top label that is not already a label
  std::string topLabel = "QQQQQQ";
  for(size_t i = 1; i <= topLabel.length(); i++) {
    if (labelSet.find(topLabel.substr(0,i)) == labelSet.end() ) {
      topLabel = topLabel.substr(0,i);
      break;
    }
  }

  size_t sourceLabelGlueTop = 0;
  size_t sourceLabelGlueX = 1;

  // basic rules
  out << "<s> [X] ||| <s> [" << topLabel << "] ||| 1 ||| 0-0 ||| ||| |||";
  if (options.treeFragments) {
    out << " {{Tree [" << topLabel << " [SSTART <s>]]}}";
  }
  if (options.sourceLabels) {
    out << " {{SourceLabels 1 1 " << sourceLabelGlueTop << " 1}}";
  }
  if (options.phraseOrientation) {
    out << " {{Orientation 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25}}";
  }
  out << std::endl;

  out << "[X][" << topLabel << "] </s> [X] ||| [X][" << topLabel << "] </s> [" << topLabel << "] ||| 1 ||| 0-0 1-1 ||| ||| |||";
  if (options.treeFragments) {
    out << " {{Tree [" << topLabel << " [" << topLabel << "] [SEND </s>]]}}";
  }
  if (options.sourceLabels) {
    out << " {{SourceLabels 2 1 " << sourceLabelGlueTop << " 1 1 " << sourceLabelGlueTop << " 1}}";
  }
    if (options.phraseOrientation) {
      out << " {{Orientation 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25}}";
    }
  out << std::endl;

  // top rules
  for (std::map<std::string, int>::const_iterator i = topLabelSet.begin();
       i != topLabelSet.end(); ++i) {
    out << "<s> [X][" << i->first << "] </s> [X] ||| <s> [X][" << i->first << "] </s> [" << topLabel << "] ||| 1 ||| 0-0 1-1 2-2 ||| ||| |||";
    if (options.treeFragments) { 
      out << " {{Tree [" << topLabel << " [SSTART <s>] [" << i->first << "] [SEND </s>]]}}";
    }
    if (options.sourceLabels) {
      out << " {{SourceLabels 2 1 " << sourceLabelGlueX << " 1 1 " << sourceLabelGlueTop << " 1}}";
    }
    if (options.phraseOrientation) {
      out << " {{Orientation 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25}}";
    }
    out << std::endl;
  }

  // glue rules
  for(std::set<std::string>::const_iterator i = labelSet.begin();
      i != labelSet.end(); i++ ) {
    out << "[X][" << topLabel << "] [X][" << *i << "] [X] ||| [X][" << topLabel << "] [X][" << *i << "] [" << topLabel << "] ||| 2.718 ||| 0-0 1-1 ||| ||| |||";
    if (options.treeFragments) { 
      out << " {{Tree [" << topLabel << " ["<< topLabel << "] [" << *i << "]]}}";
    }
    if (options.sourceLabels) {
      out << " {{SourceLabels 3 2.718 " << sourceLabelGlueTop << " " << sourceLabelGlueX << " 2.718 1 " << sourceLabelGlueTop << " 2.718}}"; 
    }
    if (options.phraseOrientation) {
      out << " {{Orientation 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25}}";
    }
    out << std::endl;
  }

  // glue rule for unknown word...
  out << "[X][" << topLabel << "] [X][X] [X] ||| [X][" << topLabel << "] [X][X] [" << topLabel << "] ||| 2.718 ||| 0-0 1-1 ||| ||| |||";
  if (options.treeFragments) {
    out << " {{Tree [" << topLabel << " [" << topLabel << "] [X]]}}";
  }
  if (options.sourceLabels) {
    out << " {{SourceLabels 3 1 " << sourceLabelGlueTop << " " << sourceLabelGlueX << " 1 1 " << sourceLabelGlueTop << " 1}}";
  }
  if (options.phraseOrientation) {
    out << " {{Orientation 0.25 0.25 0.25 0.25 0.25 0.25 0.25 0.25}}";
  }
  out << std::endl;
}

void ExtractGHKM::WriteSourceLabelSet(
  const std::map<std::string,size_t> &sourceLabels,
  std::ostream &out)
{
  out << sourceLabels.size() << std::endl;
  for (std::map<std::string,size_t>::const_iterator iter=sourceLabels.begin();
       iter!=sourceLabels.end(); ++iter) {
    out << iter->first << " " << iter->second << std::endl;
  }
}

void ExtractGHKM::CollectWordLabelCounts(
  ParseTree &root,
  const Options &options,
  std::map<std::string, int> &wordCount,
  std::map<std::string, std::string> &wordLabel)
{
  std::vector<const ParseTree*> leaves;
  root.GetLeaves(std::back_inserter(leaves));
  for (std::vector<const ParseTree *>::const_iterator p = leaves.begin();
       p != leaves.end(); ++p) {
    const ParseTree &leaf = **p;
    const std::string &word = leaf.GetLabel();
    const ParseTree *ancestor = leaf.GetParent();
    // If unary rule elimination is enabled and this word is at the end of a
    // chain of unary rewrites, e.g.
    //    PN-SB -> NE -> word
    // then record the constituent label at the top of the chain instead of
    // the part-of-speech label.
    while (!options.allowUnary &&
           ancestor->GetParent() &&
           ancestor->GetParent()->GetChildren().size() == 1) {
      ancestor = ancestor->GetParent();
    }
    const std::string &label = ancestor->GetLabel();
    ++wordCount[word];
    wordLabel[word] = label;
  }
}

std::vector<std::string> ExtractGHKM::ReadTokens(const ParseTree &root) const
{
  std::vector<std::string> tokens;
  std::vector<const ParseTree*> leaves;
  root.GetLeaves(std::back_inserter(leaves));
  for (std::vector<const ParseTree *>::const_iterator p = leaves.begin();
       p != leaves.end(); ++p) {
    const ParseTree &leaf = **p;
    const std::string &word = leaf.GetLabel();
    tokens.push_back(word);
  }
  return tokens;
}

void ExtractGHKM::WriteUnknownWordLabel(
  const std::map<std::string, int> &wordCount,
  const std::map<std::string, std::string> &wordLabel,
  const Options &options,
  std::ostream &out,
  bool writeCounts)
{
  if (!options.unknownWordSoftMatchesFile.empty()) {
    out << "UNK 1" << std::endl;
    return;
  }

  std::map<std::string, int> labelCount;
  int total = 0;
  for (std::map<std::string, int>::const_iterator p = wordCount.begin();
       p != wordCount.end(); ++p) {
    // Only consider singletons.
    if (p->second == 1) {
      std::map<std::string, std::string>::const_iterator q =
        wordLabel.find(p->first);
      assert(q != wordLabel.end());
      ++labelCount[q->second];
      ++total;
    }
  }
  if ( writeCounts ) {
    for (std::map<std::string, int>::const_iterator p = labelCount.begin();
         p != labelCount.end(); ++p) {
      out << p->first << " " << p->second << std::endl;
    }
  } else {
    for (std::map<std::string, int>::const_iterator p = labelCount.begin();
         p != labelCount.end(); ++p) {
      double ratio = static_cast<double>(p->second) / static_cast<double>(total);
      if (ratio >= options.unknownWordMinRelFreq) {
        float weight = options.unknownWordUniform ? 1.0f : ratio;
        out << p->first << " " << weight << std::endl;
      }
    }
  }
}

void ExtractGHKM::WriteUnknownWordSoftMatches(
  const std::set<std::string> &labelSet,
  std::ostream &out)
{
  for (std::set<std::string>::const_iterator p = labelSet.begin(); p != labelSet.end(); ++p) {
      std::string label = *p;
      out << "UNK " << label << std::endl;
  }
}

}  // namespace GHKM
}  // namespace Moses