summaryrefslogtreecommitdiff
path: root/extractor/grammar_extractor.cc
blob: a03e805f20c88fdae06782d2bb64a3388c05249a (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
#include "grammar_extractor.h"

#include <iterator>
#include <sstream>
#include <vector>

#include "grammar.h"
#include "rule.h"
#include "vocabulary.h"

using namespace std;

GrammarExtractor::GrammarExtractor(
    shared_ptr<SuffixArray> source_suffix_array,
    shared_ptr<DataArray> target_data_array,
    shared_ptr<Alignment> alignment, shared_ptr<Precomputation> precomputation,
    shared_ptr<Scorer> scorer, int min_gap_size, int max_rule_span,
    int max_nonterminals, int max_rule_symbols, int max_samples,
    bool use_fast_intersect, bool use_baeza_yates, bool require_tight_phrases) :
    vocabulary(make_shared<Vocabulary>()),
    rule_factory(make_shared<HieroCachingRuleFactory>(
        source_suffix_array, target_data_array, alignment, vocabulary,
        precomputation, scorer, min_gap_size, max_rule_span, max_nonterminals,
        max_rule_symbols, max_samples, use_fast_intersect, use_baeza_yates,
        require_tight_phrases)) {}

GrammarExtractor::GrammarExtractor(
    shared_ptr<Vocabulary> vocabulary,
    shared_ptr<HieroCachingRuleFactory> rule_factory) :
    vocabulary(vocabulary),
    rule_factory(rule_factory) {}

Grammar GrammarExtractor::GetGrammar(const string& sentence) {
  vector<string> words = TokenizeSentence(sentence);
  vector<int> word_ids = AnnotateWords(words);
  return rule_factory->GetGrammar(word_ids);
}

vector<string> GrammarExtractor::TokenizeSentence(const string& sentence) {
  vector<string> result;
  result.push_back("<s>");

  istringstream buffer(sentence);
  copy(istream_iterator<string>(buffer),
       istream_iterator<string>(),
       back_inserter(result));

  result.push_back("</s>");
  return result;
}

vector<int> GrammarExtractor::AnnotateWords(const vector<string>& words) {
  vector<int> result;
  for (string word: words) {
    result.push_back(vocabulary->GetTerminalIndex(word));
  }
  return result;
}