summaryrefslogtreecommitdiff
path: root/decoder/cfg_options.h
blob: 956586f0107f2ff5af1869512ef9664148fd9abc (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
#ifndef CFG_OPTIONS_H
#define CFG_OPTIONS_H

#include "hg_cfg.h"
#include "cfg_format.h"
#include "cfg_binarize.h"
//#include "program_options.h"

struct CFGOptions {
  CFGFormat format;
  CFGBinarize binarize;
  std::string cfg_output,source_cfg_output;
  void set_defaults() {
    format.set_defaults();
    binarize.set_defaults();
    cfg_output="";
  }
  CFGOptions() { set_defaults(); }
  template <class Opts> // template to support both printable_opts and boost nonprintable
  void AddOptions(Opts *opts) {
    opts->add_options()
      ("cfg_output", defaulted_value(&cfg_output),"write final target CFG (before FSA rescoring) to this file")
      ("source_cfg_output", defaulted_value(&source_cfg_output),"write source CFG (after prelm-scoring, prelm-prune) to this file")
    ;
    binarize.AddOptions(opts);
    format.AddOptions(opts);
  }
  void Validate() {
    format.Validate();
    binarize.Validate();
  }
  char const* description() const {
    return "CFG output options";
  }
  void maybe_output_source(Hypergraph const& hg) {
    if (source_cfg_output.empty()) return;
    std::cerr<<"Printing source CFG to "<<source_cfg_output<<": "<<format<<'\n';
    WriteFile o(source_cfg_output);
    CFG cfg(hg,false,format.features,format.goal_nt());
    cfg.Print(o.get(),format);
  }
  void maybe_output(HgCFG &hgcfg) {
    if (cfg_output.empty()) return;
    std::cerr<<"Printing target CFG to "<<cfg_output<<": "<<format<<'\n';
    WriteFile o(cfg_output);
    maybe_binarize(hgcfg);
    hgcfg.GetCFG().Print(o.get(),format);
  }
  void maybe_binarize(HgCFG &hgcfg) {
    if (hgcfg.binarized) return;
    hgcfg.GetCFG().Binarize(binarize);
    hgcfg.binarized=true;
  }
};


#endif