summaryrefslogtreecommitdiff
path: root/decoder/translator.h
blob: ba218a0b58947787122362d743b4945597c6b768 (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#ifndef _TRANSLATOR_H_
#define _TRANSLATOR_H_

#include <string>
#include <vector>
#include <map>
#include <boost/shared_ptr.hpp>
#include <boost/program_options/variables_map.hpp>

#include "grammar.h"

class Hypergraph;
class SentenceMetadata;

// Workflow: for each sentence to be translated
//   1) call ProcessMarkupHints(markup)
//   2) call Translate(...)
//   3) call SentenceComplete()
class Translator {
 public:
  Translator() : state_(kUninitialized) {}
  virtual ~Translator();
  // returns true if goal reached, false otherwise
  // minus_lm_forest will contain the unpruned forest. the
  // feature values from the phrase table / grammar / etc
  // should be in the forest already - the "late" features
  // should not just copy values that are available without
  // any context or computation.
  // SentenceMetadata contains information about the sentence,
  // but it is an input/output parameter since the Translator
  // is also responsible for setting the value of src_len.
  bool Translate(const std::string& src,
                 SentenceMetadata* smeta,
                 const std::vector<double>& weights,
                 Hypergraph* minus_lm_forest);

  // This is called before Translate(...) with the sentence-
  // level markup passed in. This can be used to set sentence-
  // specific behavior of the translator.
  void ProcessMarkupHints(const std::map<std::string, std::string>& kv);

  // Free any sentence-specific resources
  void SentenceComplete();
  virtual std::string GetDecoderType() const;
 protected:
  virtual bool TranslateImpl(const std::string& src,
                             SentenceMetadata* smeta,
                             const std::vector<double>& weights,
                             Hypergraph* minus_lm_forest) = 0;
  virtual void ProcessMarkupHintsImpl(const std::map<std::string, std::string>& kv);
  virtual void SentenceCompleteImpl();
 private:
  enum State { kUninitialized, kReadyToTranslate, kTranslated };
  State state_;
};

class SCFGTranslatorImpl;
class SCFGTranslator : public Translator {
 public:
  SCFGTranslator(const boost::program_options::variables_map& conf);
  void AddSupplementalGrammar(GrammarPtr gp);
  void AddSupplementalGrammarFromString(const std::string& grammar);
  virtual std::string GetDecoderType() const;
 protected:
  bool TranslateImpl(const std::string& src,
                 SentenceMetadata* smeta,
                 const std::vector<double>& weights,
                 Hypergraph* minus_lm_forest);
  void ProcessMarkupHintsImpl(const std::map<std::string, std::string>& kv);
  void SentenceCompleteImpl();
 private:
  boost::shared_ptr<SCFGTranslatorImpl> pimpl_;
};

class FSTTranslatorImpl;
class FSTTranslator : public Translator {
 public:
  FSTTranslator(const boost::program_options::variables_map& conf);
 private:
  bool TranslateImpl(const std::string& src,
                 SentenceMetadata* smeta,
                 const std::vector<double>& weights,
                 Hypergraph* minus_lm_forest);
 private:
  boost::shared_ptr<FSTTranslatorImpl> pimpl_;
};

class RescoreTranslatorImpl;
class RescoreTranslator : public Translator {
 public:
  RescoreTranslator(const boost::program_options::variables_map& conf);
 private:
  bool TranslateImpl(const std::string& src,
                 SentenceMetadata* smeta,
                 const std::vector<double>& weights,
                 Hypergraph* minus_lm_forest);
 private:
  boost::shared_ptr<RescoreTranslatorImpl> pimpl_;
};

class Tree2StringTranslatorImpl;
class Tree2StringTranslator : public Translator {
 public:
  Tree2StringTranslator(const boost::program_options::variables_map& conf,
                        bool has_multiple_states);
  virtual std::string GetDecoderType() const;
 protected:
  bool TranslateImpl(const std::string& src,
                 SentenceMetadata* smeta,
                 const std::vector<double>& weights,
                 Hypergraph* minus_lm_forest);
  void ProcessMarkupHintsImpl(const std::map<std::string, std::string>& kv);
  void SentenceCompleteImpl();
 private:
  boost::shared_ptr<Tree2StringTranslatorImpl> pimpl_;
};

#endif