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