FST  openfst-1.8.2.post1
OpenFst Library
sigma-fst.h
Go to the documentation of this file.
1 // Copyright 2005-2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 
18 #ifndef FST_EXTENSIONS_SPECIAL_SIGMA_FST_H_
19 #define FST_EXTENSIONS_SPECIAL_SIGMA_FST_H_
20 
21 #include <cstdint>
22 #include <istream>
23 #include <memory>
24 #include <ostream>
25 #include <string>
26 
27 #include <fst/const-fst.h>
28 #include <fst/matcher-fst.h>
29 #include <fst/matcher.h>
30 
31 DECLARE_int64(sigma_fst_sigma_label);
32 DECLARE_string(sigma_fst_rewrite_mode);
33 
34 namespace fst {
35 namespace internal {
36 
37 template <class Label>
39  public:
41  Label sigma_label = FST_FLAGS_sigma_fst_sigma_label,
42  MatcherRewriteMode rewrite_mode =
43  RewriteMode(FST_FLAGS_sigma_fst_rewrite_mode))
44  : sigma_label_(sigma_label), rewrite_mode_(rewrite_mode) {}
45 
47  : sigma_label_(data.sigma_label_), rewrite_mode_(data.rewrite_mode_) {}
48 
49  static SigmaFstMatcherData<Label> *Read(std::istream &istrm,
50  const FstReadOptions &read) {
51  auto *data = new SigmaFstMatcherData<Label>();
52  ReadType(istrm, &data->sigma_label_);
53  int32_t rewrite_mode;
54  ReadType(istrm, &rewrite_mode);
55  data->rewrite_mode_ = static_cast<MatcherRewriteMode>(rewrite_mode);
56  return data;
57  }
58 
59  bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const {
60  WriteType(ostrm, sigma_label_);
61  WriteType(ostrm, static_cast<int32_t>(rewrite_mode_));
62  return !ostrm ? false : true;
63  }
64 
65  Label SigmaLabel() const { return sigma_label_; }
66 
67  MatcherRewriteMode RewriteMode() const { return rewrite_mode_; }
68 
69  private:
70  static MatcherRewriteMode RewriteMode(const std::string &mode) {
71  if (mode == "auto") return MATCHER_REWRITE_AUTO;
72  if (mode == "always") return MATCHER_REWRITE_ALWAYS;
73  if (mode == "never") return MATCHER_REWRITE_NEVER;
74  LOG(WARNING) << "SigmaFst: Unknown rewrite mode: " << mode << ". "
75  << "Defaulting to auto.";
76  return MATCHER_REWRITE_AUTO;
77  }
78 
79  Label sigma_label_;
80  MatcherRewriteMode rewrite_mode_;
81 };
82 
83 } // namespace internal
84 
85 inline constexpr uint8_t kSigmaFstMatchInput =
86  0x01; // Input matcher is SigmaMatcher.
87 inline constexpr uint8_t kSigmaFstMatchOutput =
88  0x02; // Output matcher is SigmaMatcher.
89 
90 template <class M, uint8_t flags = kSigmaFstMatchInput | kSigmaFstMatchOutput>
91 class SigmaFstMatcher : public SigmaMatcher<M> {
92  public:
93  using FST = typename M::FST;
94  using Arc = typename M::Arc;
95  using StateId = typename Arc::StateId;
96  using Label = typename Arc::Label;
97  using Weight = typename Arc::Weight;
99 
100  static constexpr uint8_t kFlags = flags;
101 
102  // This makes a copy of the FST.
104  const FST &fst, MatchType match_type,
105  std::shared_ptr<MatcherData> data = std::make_shared<MatcherData>())
106  : SigmaMatcher<M>(
107  fst, match_type,
108  SigmaLabel(match_type,
109  data ? data->SigmaLabel() : MatcherData().SigmaLabel()),
110  data ? data->RewriteMode() : MatcherData().RewriteMode()),
111  data_(data) {}
112 
113  // This doesn't copy the FST.
115  const FST *fst, MatchType match_type,
116  std::shared_ptr<MatcherData> data = std::make_shared<MatcherData>())
117  : SigmaMatcher<M>(
118  fst, match_type,
119  SigmaLabel(match_type,
120  data ? data->SigmaLabel() : MatcherData().SigmaLabel()),
121  data ? data->RewriteMode() : MatcherData().RewriteMode()),
122  data_(data) {}
123 
124  // This makes a copy of the FST.
125  SigmaFstMatcher(const SigmaFstMatcher<M, flags> &matcher, bool safe = false)
126  : SigmaMatcher<M>(matcher, safe), data_(matcher.data_) {}
127 
128  SigmaFstMatcher<M, flags> *Copy(bool safe = false) const override {
129  return new SigmaFstMatcher<M, flags>(*this, safe);
130  }
131 
132  const MatcherData *GetData() const { return data_.get(); }
133 
134  std::shared_ptr<MatcherData> GetSharedData() const { return data_; }
135 
136  private:
137  static Label SigmaLabel(MatchType match_type, Label label) {
138  if (match_type == MATCH_INPUT && flags & kSigmaFstMatchInput) return label;
139  if (match_type == MATCH_OUTPUT && flags & kSigmaFstMatchOutput)
140  return label;
141  return kNoLabel;
142  }
143 
144  std::shared_ptr<MatcherData> data_;
145 };
146 
147 inline constexpr char sigma_fst_type[] = "sigma";
148 inline constexpr char input_sigma_fst_type[] = "input_sigma";
149 inline constexpr char output_sigma_fst_type[] = "output_sigma";
150 
151 template <class Arc>
152 using SigmaFst =
154  sigma_fst_type>;
155 
157 
158 template <class Arc>
159 using InputSigmaFst = MatcherFst<
161  SigmaFstMatcher<SortedMatcher<ConstFst<Arc>>, kSigmaFstMatchInput>,
162  input_sigma_fst_type>;
163 
165 
166 template <class Arc>
167 using OutputSigmaFst = MatcherFst<
168  ConstFst<Arc>,
169  SigmaFstMatcher<SortedMatcher<ConstFst<Arc>>, kSigmaFstMatchOutput>,
170  output_sigma_fst_type>;
171 
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:49
std::shared_ptr< MatcherData > GetSharedData() const
Definition: sigma-fst.h:134
typename M::FST FST
Definition: sigma-fst.h:93
constexpr int kNoLabel
Definition: fst.h:201
SigmaFstMatcher(const FST &fst, MatchType match_type, std::shared_ptr< MatcherData > data=std::make_shared< MatcherData >())
Definition: sigma-fst.h:103
constexpr char output_sigma_fst_type[]
Definition: sigma-fst.h:149
typename Arc::StateId StateId
Definition: sigma-fst.h:95
MatchType
Definition: fst.h:193
#define LOG(type)
Definition: log.h:49
constexpr uint8_t kSigmaFstMatchOutput
Definition: sigma-fst.h:87
typename Arc::Label Label
Definition: sigma-fst.h:96
typename M::Arc Arc
Definition: sigma-fst.h:94
std::ostream & WriteType(std::ostream &strm, const T t)
Definition: util.h:214
SigmaFstMatcherData(const SigmaFstMatcherData &data)
Definition: sigma-fst.h:46
DECLARE_int64(sigma_fst_sigma_label)
constexpr char input_sigma_fst_type[]
Definition: sigma-fst.h:148
SigmaFstMatcherData(Label sigma_label=FST_FLAGS_sigma_fst_sigma_label, MatcherRewriteMode rewrite_mode=RewriteMode(FST_FLAGS_sigma_fst_rewrite_mode))
Definition: sigma-fst.h:40
MatcherRewriteMode
Definition: matcher.h:577
SigmaFstMatcher(const FST *fst, MatchType match_type, std::shared_ptr< MatcherData > data=std::make_shared< MatcherData >())
Definition: sigma-fst.h:114
constexpr char sigma_fst_type[]
Definition: sigma-fst.h:147
bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const
Definition: sigma-fst.h:59
constexpr uint8_t kSigmaFstMatchInput
Definition: sigma-fst.h:85
std::istream & ReadType(std::istream &strm, T *t)
Definition: util.h:68
DECLARE_string(sigma_fst_rewrite_mode)
MatcherRewriteMode RewriteMode() const
Definition: sigma-fst.h:67
SigmaFstMatcher< M, flags > * Copy(bool safe=false) const override
Definition: sigma-fst.h:128
SigmaFstMatcher(const SigmaFstMatcher< M, flags > &matcher, bool safe=false)
Definition: sigma-fst.h:125
const MatcherData * GetData() const
Definition: sigma-fst.h:132
typename Arc::Weight Weight
Definition: sigma-fst.h:97