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

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

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

using namespace std;

namespace extractor {

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 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, 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, const unordered_set<int>& blacklisted_sentence_ids, const shared_ptr<DataArray> source_data_array) {
  vector<string> words = TokenizeSentence(sentence);
  vector<int> word_ids = AnnotateWords(words);
  return rule_factory->GetGrammar(word_ids, blacklisted_sentence_ids, source_data_array);
}

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;
}

} // namespace extractor