summaryrefslogtreecommitdiff
path: root/extractor/rule_factory.h
blob: cf344667be091f00607f50600ec144dd8e984ebe (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
#ifndef _RULE_FACTORY_H_
#define _RULE_FACTORY_H_

#include <memory>
#include <vector>

#include "matchings_trie.h"
#include "phrase_builder.h"

using namespace std;

class Alignment;
class DataArray;
class Grammar;
class MatchingsFinder;
class Intersector;
class Precomputation;
class Rule;
class RuleExtractor;
class Sampler;
class Scorer;
class State;
class SuffixArray;
class Vocabulary;

class HieroCachingRuleFactory {
 public:
  HieroCachingRuleFactory(
      shared_ptr<SuffixArray> source_suffix_array,
      shared_ptr<DataArray> target_data_array,
      shared_ptr<Alignment> alignment,
      const shared_ptr<Vocabulary>& vocabulary,
      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_beaza_yates,
      bool require_tight_phrases);

  // For testing only.
  HieroCachingRuleFactory(
      shared_ptr<MatchingsFinder> finder,
      shared_ptr<Intersector> intersector,
      shared_ptr<PhraseBuilder> phrase_builder,
      shared_ptr<RuleExtractor> rule_extractor,
      shared_ptr<Vocabulary> vocabulary,
      shared_ptr<Sampler> sampler,
      shared_ptr<Scorer> scorer,
      int min_gap_size,
      int max_rule_span,
      int max_nonterminals,
      int max_chunks,
      int max_rule_symbols);

  virtual ~HieroCachingRuleFactory();

  virtual Grammar GetGrammar(const vector<int>& word_ids);

 protected:
  HieroCachingRuleFactory();

 private:
  bool CannotHaveMatchings(shared_ptr<TrieNode> node, int word_id);

  bool RequiresLookup(shared_ptr<TrieNode> node, int word_id);

  void AddTrailingNonterminal(vector<int> symbols,
                              const Phrase& prefix,
                              const shared_ptr<TrieNode>& prefix_node,
                              bool starts_with_x);

  vector<State> ExtendState(const vector<int>& word_ids,
                            const State& state,
                            vector<int> symbols,
                            const Phrase& phrase,
                            const shared_ptr<TrieNode>& node);

  shared_ptr<MatchingsFinder> matchings_finder;
  shared_ptr<Intersector> intersector;
  MatchingsTrie trie;
  shared_ptr<PhraseBuilder> phrase_builder;
  shared_ptr<RuleExtractor> rule_extractor;
  shared_ptr<Vocabulary> vocabulary;
  shared_ptr<Sampler> sampler;
  shared_ptr<Scorer> scorer;
  int min_gap_size;
  int max_rule_span;
  int max_nonterminals;
  int max_chunks;
  int max_rule_symbols;
};

#endif