summaryrefslogtreecommitdiff
path: root/decoder/tree2string_translator.cc
blob: 1c249836ceabb2ecb5a4926c04a6e4fb0364ccd4 (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
#include <algorithm>
#include <vector>
#include <boost/functional/hash.hpp>
#include <unordered_map>
#include "tree_fragment.h"
#include "translator.h"
#include "hg.h"
#include "sentence_metadata.h"
#include "filelib.h"
#include "stringlib.h"
#include "tdict.h"
#include "verbose.h"

using namespace std;

struct Tree2StringGrammarNode {
  map<unsigned, Tree2StringGrammarNode> next;
  string rules;
};

void ReadTree2StringGrammar(istream* in, unordered_map<unsigned, Tree2StringGrammarNode>* proots) {
  unordered_map<unsigned, Tree2StringGrammarNode>& roots = *proots;
  string line;
  while(getline(*in, line)) {
    size_t pos = line.find("|||");
    assert(pos != string::npos);
    assert(pos > 3);
    unsigned xc = 0;
    while (line[pos - 1] == ' ') { --pos; xc++; }
    cdec::TreeFragment rule_src(line.substr(0, pos), true);
    Tree2StringGrammarNode* cur = &roots[rule_src.root];
    for (auto sym : rule_src)
      cur = &cur->next[sym];
    pos += 3 + xc;
    while(line[pos] == ' ') { ++pos; }
    size_t pos2 = line.find("|||", pos);
    assert(pos2 != string::npos);
    while (line[pos2 - 1] == ' ') { --pos2; }
    cur->rules = line.substr(pos, pos2 - pos);
    cerr << "OUTPUT = '" << cur->rules << "'\n";
  }
}

struct Tree2StringTranslatorImpl {
  unordered_map<unsigned, Tree2StringGrammarNode> roots; // root['S'] gives rule network for S rules
  Tree2StringTranslatorImpl(const boost::program_options::variables_map& conf) {
    ReadFile rf(conf["grammar"].as<vector<string>>()[0]);
    ReadTree2StringGrammar(rf.stream(), &roots);
  }
  bool Translate(const string& input,
                 SentenceMetadata* smeta,
                 const vector<double>& weights,
                 Hypergraph* minus_lm_forest) {
    cdec::TreeFragment input_tree(input, false);
    cerr << "Tree2StringTranslatorImpl: please implement this!\n";
    return false;
  }
};

Tree2StringTranslator::Tree2StringTranslator(const boost::program_options::variables_map& conf) :
  pimpl_(new Tree2StringTranslatorImpl(conf)) {}

bool Tree2StringTranslator::TranslateImpl(const string& input,
                               SentenceMetadata* smeta,
                               const vector<double>& weights,
                               Hypergraph* minus_lm_forest) {
  return pimpl_->Translate(input, smeta, weights, minus_lm_forest);
}

void Tree2StringTranslator::ProcessMarkupHintsImpl(const map<string, string>& kv) {
}

void Tree2StringTranslator::SentenceCompleteImpl() {
}

std::string Tree2StringTranslator::GetDecoderType() const {
  return "tree2string";
}