FST  openfst-1.8.3
OpenFst Library
sigma-fst.h
Go to the documentation of this file.
1 // Copyright 2005-2024 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/log.h>
28 #include <fst/arc.h>
29 #include <fst/const-fst.h>
30 #include <fst/fst.h>
31 #include <fst/matcher-fst.h>
32 #include <fst/matcher.h>
33 #include <fst/util.h>
34 
35 DECLARE_int64(sigma_fst_sigma_label);
36 DECLARE_string(sigma_fst_rewrite_mode);
37 
38 namespace fst {
39 namespace internal {
40 
41 template <class Label>
43  public:
45  Label sigma_label = FST_FLAGS_sigma_fst_sigma_label,
46  MatcherRewriteMode rewrite_mode =
47  RewriteMode(FST_FLAGS_sigma_fst_rewrite_mode))
48  : sigma_label_(sigma_label), rewrite_mode_(rewrite_mode) {}
49 
51  : sigma_label_(data.sigma_label_), rewrite_mode_(data.rewrite_mode_) {}
52 
53  static SigmaFstMatcherData<Label> *Read(std::istream &istrm,
54  const FstReadOptions &read) {
55  auto data = std::make_unique<SigmaFstMatcherData<Label>>();
56  ReadType(istrm, &data->sigma_label_);
57  int32_t rewrite_mode;
58  ReadType(istrm, &rewrite_mode);
59  data->rewrite_mode_ = static_cast<MatcherRewriteMode>(rewrite_mode);
60  return data.release();
61  }
62 
63  bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const {
64  WriteType(ostrm, sigma_label_);
65  WriteType(ostrm, static_cast<int32_t>(rewrite_mode_));
66  return !ostrm ? false : true;
67  }
68 
69  Label SigmaLabel() const { return sigma_label_; }
70 
71  MatcherRewriteMode RewriteMode() const { return rewrite_mode_; }
72 
73  private:
74  static MatcherRewriteMode RewriteMode(const std::string &mode) {
75  if (mode == "auto") return MATCHER_REWRITE_AUTO;
76  if (mode == "always") return MATCHER_REWRITE_ALWAYS;
77  if (mode == "never") return MATCHER_REWRITE_NEVER;
78  LOG(WARNING) << "SigmaFst: Unknown rewrite mode: " << mode << ". "
79  << "Defaulting to auto.";
80  return MATCHER_REWRITE_AUTO;
81  }
82 
83  Label sigma_label_;
84  MatcherRewriteMode rewrite_mode_;
85 };
86 
87 } // namespace internal
88 
89 inline constexpr uint8_t kSigmaFstMatchInput =
90  0x01; // Input matcher is SigmaMatcher.
91 inline constexpr uint8_t kSigmaFstMatchOutput =
92  0x02; // Output matcher is SigmaMatcher.
93 
94 template <class M, uint8_t flags = kSigmaFstMatchInput | kSigmaFstMatchOutput>
95 class SigmaFstMatcher : public SigmaMatcher<M> {
96  public:
97  using FST = typename M::FST;
98  using Arc = typename M::Arc;
99  using StateId = typename Arc::StateId;
100  using Label = typename Arc::Label;
101  using Weight = typename Arc::Weight;
103 
104  static constexpr uint8_t kFlags = flags;
105 
106  // This makes a copy of the FST.
108  const FST &fst, MatchType match_type,
109  std::shared_ptr<MatcherData> data = std::make_shared<MatcherData>())
110  : SigmaMatcher<M>(
111  fst, match_type,
112  SigmaLabel(match_type,
113  data ? data->SigmaLabel() : MatcherData().SigmaLabel()),
114  data ? data->RewriteMode() : MatcherData().RewriteMode()),
115  data_(data) {}
116 
117  // This doesn't copy the FST.
119  const FST *fst, MatchType match_type,
120  std::shared_ptr<MatcherData> data = std::make_shared<MatcherData>())
121  : SigmaMatcher<M>(
122  fst, match_type,
123  SigmaLabel(match_type,
124  data ? data->SigmaLabel() : MatcherData().SigmaLabel()),
125  data ? data->RewriteMode() : MatcherData().RewriteMode()),
126  data_(data) {}
127 
128  // This makes a copy of the FST.
129  SigmaFstMatcher(const SigmaFstMatcher<M, flags> &matcher, bool safe = false)
130  : SigmaMatcher<M>(matcher, safe), data_(matcher.data_) {}
131 
132  SigmaFstMatcher<M, flags> *Copy(bool safe = false) const override {
133  return new SigmaFstMatcher<M, flags>(*this, safe);
134  }
135 
136  const MatcherData *GetData() const { return data_.get(); }
137 
138  std::shared_ptr<MatcherData> GetSharedData() const { return data_; }
139 
140  private:
141  static Label SigmaLabel(MatchType match_type, Label label) {
142  if (match_type == MATCH_INPUT && flags & kSigmaFstMatchInput) return label;
143  if (match_type == MATCH_OUTPUT && flags & kSigmaFstMatchOutput)
144  return label;
145  return kNoLabel;
146  }
147 
148  std::shared_ptr<MatcherData> data_;
149 };
150 
151 inline constexpr char sigma_fst_type[] = "sigma";
152 inline constexpr char input_sigma_fst_type[] = "input_sigma";
153 inline constexpr char output_sigma_fst_type[] = "output_sigma";
154 
155 template <class Arc>
156 using SigmaFst =
158  sigma_fst_type>;
159 
161 
162 template <class Arc>
163 using InputSigmaFst = MatcherFst<
165  SigmaFstMatcher<SortedMatcher<ConstFst<Arc>>, kSigmaFstMatchInput>,
166  input_sigma_fst_type>;
167 
169 
170 template <class Arc>
171 using OutputSigmaFst = MatcherFst<
172  ConstFst<Arc>,
173  SigmaFstMatcher<SortedMatcher<ConstFst<Arc>>, kSigmaFstMatchOutput>,
174  output_sigma_fst_type>;
175 
177 
178 } // namespace fst
179 
180 #endif // FST_EXTENSIONS_SPECIAL_SIGMA_FST_H_
static SigmaFstMatcherData< Label > * Read(std::istream &istrm, const FstReadOptions &read)
Definition: sigma-fst.h:53
std::shared_ptr< MatcherData > GetSharedData() const
Definition: sigma-fst.h:138
typename M::FST FST
Definition: sigma-fst.h:97
constexpr int kNoLabel
Definition: fst.h:195
SigmaFstMatcher(const FST &fst, MatchType match_type, std::shared_ptr< MatcherData > data=std::make_shared< MatcherData >())
Definition: sigma-fst.h:107
constexpr char output_sigma_fst_type[]
Definition: sigma-fst.h:153
typename Arc::StateId StateId
Definition: sigma-fst.h:99
MatchType
Definition: fst.h:187
#define LOG(type)
Definition: log.h:53
constexpr uint8_t kSigmaFstMatchOutput
Definition: sigma-fst.h:91
typename Arc::Label Label
Definition: sigma-fst.h:100
typename M::Arc Arc
Definition: sigma-fst.h:98
std::ostream & WriteType(std::ostream &strm, const T t)
Definition: util.h:228
SigmaFstMatcherData(const SigmaFstMatcherData &data)
Definition: sigma-fst.h:50
DECLARE_int64(sigma_fst_sigma_label)
constexpr char input_sigma_fst_type[]
Definition: sigma-fst.h:152
SigmaFstMatcherData(Label sigma_label=FST_FLAGS_sigma_fst_sigma_label, MatcherRewriteMode rewrite_mode=RewriteMode(FST_FLAGS_sigma_fst_rewrite_mode))
Definition: sigma-fst.h:44
MatcherRewriteMode
Definition: matcher.h:584
SigmaFstMatcher(const FST *fst, MatchType match_type, std::shared_ptr< MatcherData > data=std::make_shared< MatcherData >())
Definition: sigma-fst.h:118
constexpr char sigma_fst_type[]
Definition: sigma-fst.h:151
bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const
Definition: sigma-fst.h:63
constexpr uint8_t kSigmaFstMatchInput
Definition: sigma-fst.h:89
std::istream & ReadType(std::istream &strm, T *t)
Definition: util.h:80
DECLARE_string(sigma_fst_rewrite_mode)
MatcherRewriteMode RewriteMode() const
Definition: sigma-fst.h:71
SigmaFstMatcher< M, flags > * Copy(bool safe=false) const override
Definition: sigma-fst.h:132
SigmaFstMatcher(const SigmaFstMatcher< M, flags > &matcher, bool safe=false)
Definition: sigma-fst.h:129
const MatcherData * GetData() const
Definition: sigma-fst.h:136
typename Arc::Weight Weight
Definition: sigma-fst.h:101