FST  openfst-1.7.2
OpenFst Library
sigma-fst.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 
4 #ifndef FST_EXTENSIONS_SPECIAL_SIGMA_FST_H_
5 #define FST_EXTENSIONS_SPECIAL_SIGMA_FST_H_
6 
7 #include <memory>
8 #include <string>
9 
10 #include <fst/const-fst.h>
11 #include <fst/matcher-fst.h>
12 #include <fst/matcher.h>
13 
14 DECLARE_int64(sigma_fst_sigma_label);
15 DECLARE_string(sigma_fst_rewrite_mode);
16 
17 namespace fst {
18 namespace internal {
19 
20 template <class Label>
22  public:
23  explicit SigmaFstMatcherData(Label sigma_label = FLAGS_sigma_fst_sigma_label,
24  MatcherRewriteMode rewrite_mode =
25  RewriteMode(FLAGS_sigma_fst_rewrite_mode))
26  : sigma_label_(sigma_label), rewrite_mode_(rewrite_mode) {}
27 
29  : sigma_label_(data.sigma_label_), rewrite_mode_(data.rewrite_mode_) {}
30 
31  static SigmaFstMatcherData<Label> *Read(std::istream &istrm,
32  const FstReadOptions &read) {
33  auto *data = new SigmaFstMatcherData<Label>();
34  ReadType(istrm, &data->sigma_label_);
35  int32 rewrite_mode;
36  ReadType(istrm, &rewrite_mode);
37  data->rewrite_mode_ = static_cast<MatcherRewriteMode>(rewrite_mode);
38  return data;
39  }
40 
41  bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const {
42  WriteType(ostrm, sigma_label_);
43  WriteType(ostrm, static_cast<int32>(rewrite_mode_));
44  return !ostrm ? false : true;
45  }
46 
47  Label SigmaLabel() const { return sigma_label_; }
48 
49  MatcherRewriteMode RewriteMode() const { return rewrite_mode_; }
50 
51  private:
52  static MatcherRewriteMode RewriteMode(const string &mode) {
53  if (mode == "auto") return MATCHER_REWRITE_AUTO;
54  if (mode == "always") return MATCHER_REWRITE_ALWAYS;
55  if (mode == "never") return MATCHER_REWRITE_NEVER;
56  LOG(WARNING) << "SigmaFst: Unknown rewrite mode: " << mode << ". "
57  << "Defaulting to auto.";
58  return MATCHER_REWRITE_AUTO;
59  }
60 
61  Label sigma_label_;
62  MatcherRewriteMode rewrite_mode_;
63 };
64 
65 } // namespace internal
66 
67 constexpr uint8 kSigmaFstMatchInput = 0x01; // Input matcher is SigmaMatcher.
68 constexpr uint8 kSigmaFstMatchOutput = 0x02; // Output matcher is SigmaMatcher.
69 
70 template <class M, uint8 flags = kSigmaFstMatchInput | kSigmaFstMatchOutput>
71 class SigmaFstMatcher : public SigmaMatcher<M> {
72  public:
73  using FST = typename M::FST;
74  using Arc = typename M::Arc;
75  using StateId = typename Arc::StateId;
76  using Label = typename Arc::Label;
77  using Weight = typename Arc::Weight;
79 
80  enum : uint8 { kFlags = flags };
81 
82  // This makes a copy of the FST.
84  const FST &fst, MatchType match_type,
85  std::shared_ptr<MatcherData> data = std::make_shared<MatcherData>())
86  : SigmaMatcher<M>(
87  fst, match_type,
88  SigmaLabel(match_type,
89  data ? data->SigmaLabel() : MatcherData().SigmaLabel()),
90  data ? data->RewriteMode() : MatcherData().RewriteMode()),
91  data_(data) {}
92 
93  // This doesn't copy the FST.
95  const FST *fst, MatchType match_type,
96  std::shared_ptr<MatcherData> data = std::make_shared<MatcherData>())
97  : SigmaMatcher<M>(
98  fst, match_type,
99  SigmaLabel(match_type,
100  data ? data->SigmaLabel() : MatcherData().SigmaLabel()),
101  data ? data->RewriteMode() : MatcherData().RewriteMode()),
102  data_(data) {}
103 
104  // This makes a copy of the FST.
105  SigmaFstMatcher(const SigmaFstMatcher<M, flags> &matcher, bool safe = false)
106  : SigmaMatcher<M>(matcher, safe), data_(matcher.data_) {}
107 
108  SigmaFstMatcher<M, flags> *Copy(bool safe = false) const override {
109  return new SigmaFstMatcher<M, flags>(*this, safe);
110  }
111 
112  const MatcherData *GetData() const { return data_.get(); }
113 
114  std::shared_ptr<MatcherData> GetSharedData() const { return data_; }
115 
116  private:
117  static Label SigmaLabel(MatchType match_type, Label label) {
118  if (match_type == MATCH_INPUT && flags & kSigmaFstMatchInput) return label;
119  if (match_type == MATCH_OUTPUT && flags & kSigmaFstMatchOutput)
120  return label;
121  return kNoLabel;
122  }
123 
124  std::shared_ptr<MatcherData> data_;
125 };
126 
127 extern const char sigma_fst_type[];
128 extern const char input_sigma_fst_type[];
129 extern const char output_sigma_fst_type[];
130 
133  sigma_fst_type>;
134 
137  sigma_fst_type>;
138 
139 using Log64SigmaFst =
142  input_sigma_fst_type>;
143 
146  SigmaFstMatcher<SortedMatcher<ConstFst<StdArc>>, kSigmaFstMatchInput>,
147  input_sigma_fst_type>;
148 
151  SigmaFstMatcher<SortedMatcher<ConstFst<LogArc>>, kSigmaFstMatchInput>,
152  input_sigma_fst_type>;
153 
156  SigmaFstMatcher<SortedMatcher<ConstFst<Log64Arc>>, kSigmaFstMatchInput>,
157  input_sigma_fst_type>;
158 
160  ConstFst<StdArc>,
161  SigmaFstMatcher<SortedMatcher<ConstFst<StdArc>>, kSigmaFstMatchOutput>,
162  output_sigma_fst_type>;
163 
165  ConstFst<LogArc>,
166  SigmaFstMatcher<SortedMatcher<ConstFst<LogArc>>, kSigmaFstMatchOutput>,
167  output_sigma_fst_type>;
168 
170  ConstFst<Log64Arc>,
171  SigmaFstMatcher<SortedMatcher<ConstFst<Log64Arc>>, kSigmaFstMatchOutput>,
172  output_sigma_fst_type>;
173 
174 } // namespace fst
175 
176 #endif // FST_EXTENSIONS_SPECIAL_SIGMA_FST_H_
static SigmaFstMatcherData< Label > * Read(std::istream &istrm, const FstReadOptions &read)
Definition: sigma-fst.h:31
std::shared_ptr< MatcherData > GetSharedData() const
Definition: sigma-fst.h:114
SigmaFstMatcherData(Label sigma_label=FLAGS_sigma_fst_sigma_label, MatcherRewriteMode rewrite_mode=RewriteMode(FLAGS_sigma_fst_rewrite_mode))
Definition: sigma-fst.h:23
typename M::FST FST
Definition: sigma-fst.h:73
constexpr int kNoLabel
Definition: fst.h:179
SigmaFstMatcher(const FST &fst, MatchType match_type, std::shared_ptr< MatcherData > data=std::make_shared< MatcherData >())
Definition: sigma-fst.h:83
typename Arc::StateId StateId
Definition: sigma-fst.h:75
MatchType
Definition: fst.h:171
#define LOG(type)
Definition: log.h:48
const char sigma_fst_type[]
Definition: sigma-fst.cc:17
typename Arc::Label Label
Definition: sigma-fst.h:76
typename M::Arc Arc
Definition: sigma-fst.h:74
std::ostream & WriteType(std::ostream &strm, const T t)
Definition: util.h:155
uint8_t uint8
Definition: types.h:29
SigmaFstMatcherData(const SigmaFstMatcherData &data)
Definition: sigma-fst.h:28
const char output_sigma_fst_type[]
Definition: sigma-fst.cc:19
DECLARE_int64(sigma_fst_sigma_label)
MatcherRewriteMode
Definition: matcher.h:568
SigmaFstMatcher(const FST *fst, MatchType match_type, std::shared_ptr< MatcherData > data=std::make_shared< MatcherData >())
Definition: sigma-fst.h:94
bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const
Definition: sigma-fst.h:41
int32_t int32
Definition: types.h:26
const char input_sigma_fst_type[]
Definition: sigma-fst.cc:18
std::istream & ReadType(std::istream &strm, T *t)
Definition: util.h:47
DECLARE_string(sigma_fst_rewrite_mode)
MatcherRewriteMode RewriteMode() const
Definition: sigma-fst.h:49
SigmaFstMatcher< M, flags > * Copy(bool safe=false) const override
Definition: sigma-fst.h:108
constexpr uint8 kSigmaFstMatchOutput
Definition: sigma-fst.h:68
SigmaFstMatcher(const SigmaFstMatcher< M, flags > &matcher, bool safe=false)
Definition: sigma-fst.h:105
constexpr uint8 kSigmaFstMatchInput
Definition: sigma-fst.h:67
const MatcherData * GetData() const
Definition: sigma-fst.h:112
typename Arc::Weight Weight
Definition: sigma-fst.h:77