FST  openfst-1.8.3
OpenFst Library
rho-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_RHO_FST_H_
19 #define FST_EXTENSIONS_SPECIAL_RHO_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(rho_fst_rho_label);
36 DECLARE_string(rho_fst_rewrite_mode);
37 
38 namespace fst {
39 namespace internal {
40 
41 template <class Label>
43  public:
45  Label rho_label = FST_FLAGS_rho_fst_rho_label,
46  MatcherRewriteMode rewrite_mode =
47  RewriteMode(FST_FLAGS_rho_fst_rewrite_mode))
48  : rho_label_(rho_label), rewrite_mode_(rewrite_mode) {}
49 
51  : rho_label_(data.rho_label_), rewrite_mode_(data.rewrite_mode_) {}
52 
53  static RhoFstMatcherData<Label> *Read(std::istream &istrm,
54  const FstReadOptions &read) {
55  auto data = std::make_unique<RhoFstMatcherData<Label>>();
56  ReadType(istrm, &data->rho_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, rho_label_);
65  WriteType(ostrm, static_cast<int32_t>(rewrite_mode_));
66  return !ostrm ? false : true;
67  }
68 
69  Label RhoLabel() const { return rho_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) << "RhoFst: Unknown rewrite mode: " << mode << ". "
79  << "Defaulting to auto.";
80  return MATCHER_REWRITE_AUTO;
81  }
82 
83  Label rho_label_;
84  MatcherRewriteMode rewrite_mode_;
85 };
86 
87 } // namespace internal
88 
89 inline constexpr uint8_t kRhoFstMatchInput =
90  0x01; // Input matcher is RhoMatcher.
91 inline constexpr uint8_t kRhoFstMatchOutput =
92  0x02; // Output matcher is RhoMatcher.
93 
94 template <class M, uint8_t flags = kRhoFstMatchInput | kRhoFstMatchOutput>
95 class RhoFstMatcher : public RhoMatcher<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  : RhoMatcher<M>(fst, match_type,
111  RhoLabel(match_type, data ? data->RhoLabel()
112  : MatcherData().RhoLabel()),
113  data ? data->RewriteMode() : MatcherData().RewriteMode()),
114  data_(data) {}
115 
116  // This doesn't copy the FST.
118  const FST *fst, MatchType match_type,
119  std::shared_ptr<MatcherData> data = std::make_shared<MatcherData>())
120  : RhoMatcher<M>(fst, match_type,
121  RhoLabel(match_type, data ? data->RhoLabel()
122  : MatcherData().RhoLabel()),
123  data ? data->RewriteMode() : MatcherData().RewriteMode()),
124  data_(data) {}
125 
126  // This makes a copy of the FST.
127  RhoFstMatcher(const RhoFstMatcher<M, flags> &matcher, bool safe = false)
128  : RhoMatcher<M>(matcher, safe), data_(matcher.data_) {}
129 
130  RhoFstMatcher<M, flags> *Copy(bool safe = false) const override {
131  return new RhoFstMatcher<M, flags>(*this, safe);
132  }
133 
134  const MatcherData *GetData() const { return data_.get(); }
135 
136  std::shared_ptr<MatcherData> GetSharedData() const { return data_; }
137 
138  private:
139  static Label RhoLabel(MatchType match_type, Label label) {
140  if (match_type == MATCH_INPUT && flags & kRhoFstMatchInput) return label;
141  if (match_type == MATCH_OUTPUT && flags & kRhoFstMatchOutput) return label;
142  return kNoLabel;
143  }
144 
145  std::shared_ptr<MatcherData> data_;
146 };
147 
148 inline constexpr char rho_fst_type[] = "rho";
149 inline constexpr char input_rho_fst_type[] = "input_rho";
150 inline constexpr char output_rho_fst_type[] = "output_rho";
151 
152 template <class Arc>
153 using RhoFst =
155  rho_fst_type>;
156 
158 
159 template <class Arc>
160 using InputRhoFst =
162  RhoFstMatcher<SortedMatcher<ConstFst<Arc>>, kRhoFstMatchInput>,
163  input_rho_fst_type>;
164 
166 
167 template <class Arc>
168 using OutputRhoFst =
170  RhoFstMatcher<SortedMatcher<ConstFst<Arc>>, kRhoFstMatchOutput>,
171  output_rho_fst_type>;
172 
174 
175 } // namespace fst
176 
177 #endif // FST_EXTENSIONS_SPECIAL_RHO_FST_H_
constexpr char input_rho_fst_type[]
Definition: rho-fst.h:149
typename M::FST FST
Definition: rho-fst.h:97
constexpr int kNoLabel
Definition: fst.h:195
RhoFstMatcher(const FST *fst, MatchType match_type, std::shared_ptr< MatcherData > data=std::make_shared< MatcherData >())
Definition: rho-fst.h:117
MatchType
Definition: fst.h:187
RhoFstMatcherData(Label rho_label=FST_FLAGS_rho_fst_rho_label, MatcherRewriteMode rewrite_mode=RewriteMode(FST_FLAGS_rho_fst_rewrite_mode))
Definition: rho-fst.h:44
#define LOG(type)
Definition: log.h:53
RhoFstMatcherData(const RhoFstMatcherData &data)
Definition: rho-fst.h:50
RhoFstMatcher(const RhoFstMatcher< M, flags > &matcher, bool safe=false)
Definition: rho-fst.h:127
constexpr char output_rho_fst_type[]
Definition: rho-fst.h:150
RhoFstMatcher< M, flags > * Copy(bool safe=false) const override
Definition: rho-fst.h:130
std::ostream & WriteType(std::ostream &strm, const T t)
Definition: util.h:228
constexpr uint8_t kRhoFstMatchOutput
Definition: rho-fst.h:91
const MatcherData * GetData() const
Definition: rho-fst.h:134
typename Arc::Label Label
Definition: rho-fst.h:100
RhoFstMatcher(const FST &fst, MatchType match_type, std::shared_ptr< MatcherData > data=std::make_shared< MatcherData >())
Definition: rho-fst.h:107
DECLARE_string(rho_fst_rewrite_mode)
MatcherRewriteMode RewriteMode() const
Definition: rho-fst.h:71
constexpr char rho_fst_type[]
Definition: rho-fst.h:148
constexpr uint8_t kRhoFstMatchInput
Definition: rho-fst.h:89
MatcherRewriteMode
Definition: matcher.h:584
bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const
Definition: rho-fst.h:63
typename M::Arc Arc
Definition: rho-fst.h:98
typename Arc::StateId StateId
Definition: rho-fst.h:99
std::istream & ReadType(std::istream &strm, T *t)
Definition: util.h:80
std::shared_ptr< MatcherData > GetSharedData() const
Definition: rho-fst.h:136
typename Arc::Weight Weight
Definition: rho-fst.h:101
DECLARE_int64(rho_fst_rho_label)
static RhoFstMatcherData< Label > * Read(std::istream &istrm, const FstReadOptions &read)
Definition: rho-fst.h:53