FST  openfst-1.8.3
OpenFst Library
phi-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_PHI_FST_H_
19 #define FST_EXTENSIONS_SPECIAL_PHI_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(phi_fst_phi_label);
36 DECLARE_bool(phi_fst_phi_loop);
37 DECLARE_string(phi_fst_rewrite_mode);
38 
39 namespace fst {
40 namespace internal {
41 
42 template <class Label>
44  public:
46  Label phi_label = FST_FLAGS_phi_fst_phi_label,
47  bool phi_loop = FST_FLAGS_phi_fst_phi_loop,
48  MatcherRewriteMode rewrite_mode =
49  RewriteMode(FST_FLAGS_phi_fst_rewrite_mode))
50  : phi_label_(phi_label),
51  phi_loop_(phi_loop),
52  rewrite_mode_(rewrite_mode) {}
53 
55  : phi_label_(data.phi_label_),
56  phi_loop_(data.phi_loop_),
57  rewrite_mode_(data.rewrite_mode_) {}
58 
59  static PhiFstMatcherData<Label> *Read(std::istream &istrm,
60  const FstReadOptions &read) {
61  auto data = std::make_unique<PhiFstMatcherData<Label>>();
62  ReadType(istrm, &data->phi_label_);
63  ReadType(istrm, &data->phi_loop_);
64  int32_t rewrite_mode;
65  ReadType(istrm, &rewrite_mode);
66  data->rewrite_mode_ = static_cast<MatcherRewriteMode>(rewrite_mode);
67  return data.release();
68  }
69 
70  bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const {
71  WriteType(ostrm, phi_label_);
72  WriteType(ostrm, phi_loop_);
73  WriteType(ostrm, static_cast<int32_t>(rewrite_mode_));
74  return !ostrm ? false : true;
75  }
76 
77  Label PhiLabel() const { return phi_label_; }
78 
79  bool PhiLoop() const { return phi_loop_; }
80 
81  MatcherRewriteMode RewriteMode() const { return rewrite_mode_; }
82 
83  private:
84  static MatcherRewriteMode RewriteMode(const std::string &mode) {
85  if (mode == "auto") return MATCHER_REWRITE_AUTO;
86  if (mode == "always") return MATCHER_REWRITE_ALWAYS;
87  if (mode == "never") return MATCHER_REWRITE_NEVER;
88  LOG(WARNING) << "PhiFst: Unknown rewrite mode: " << mode << ". "
89  << "Defaulting to auto.";
90  return MATCHER_REWRITE_AUTO;
91  }
92 
93  Label phi_label_;
94  bool phi_loop_;
95  MatcherRewriteMode rewrite_mode_;
96 };
97 
98 } // namespace internal
99 
100 inline constexpr uint8_t kPhiFstMatchInput =
101  0x01; // Input matcher is PhiMatcher.
102 inline constexpr uint8_t kPhiFstMatchOutput =
103  0x02; // Output matcher is PhiMatcher.
104 
105 template <class M, uint8_t flags = kPhiFstMatchInput | kPhiFstMatchOutput>
106 class PhiFstMatcher : public PhiMatcher<M> {
107  public:
108  using FST = typename M::FST;
109  using Arc = typename M::Arc;
110  using StateId = typename Arc::StateId;
111  using Label = typename Arc::Label;
112  using Weight = typename Arc::Weight;
114 
115  static constexpr uint8_t kFlags = flags;
116 
117  // This makes a copy of the FST.
119  const FST &fst, MatchType match_type,
120  std::shared_ptr<MatcherData> data = std::make_shared<MatcherData>())
121  : PhiMatcher<M>(fst, match_type,
122  PhiLabel(match_type, data ? data->PhiLabel()
123  : MatcherData().PhiLabel()),
124  data ? data->PhiLoop() : MatcherData().PhiLoop(),
125  data ? data->RewriteMode() : MatcherData().RewriteMode()),
126  data_(data) {}
127 
128  // This doesn't copy the FST.
130  const FST *fst, MatchType match_type,
131  std::shared_ptr<MatcherData> data = std::make_shared<MatcherData>())
132  : PhiMatcher<M>(fst, match_type,
133  PhiLabel(match_type, data ? data->PhiLabel()
134  : MatcherData().PhiLabel()),
135  data ? data->PhiLoop() : MatcherData().PhiLoop(),
136  data ? data->RewriteMode() : MatcherData().RewriteMode()),
137  data_(data) {}
138 
139  // This makes a copy of the FST.
140  PhiFstMatcher(const PhiFstMatcher<M, flags> &matcher, bool safe = false)
141  : PhiMatcher<M>(matcher, safe), data_(matcher.data_) {}
142 
143  PhiFstMatcher<M, flags> *Copy(bool safe = false) const override {
144  return new PhiFstMatcher<M, flags>(*this, safe);
145  }
146 
147  const MatcherData *GetData() const { return data_.get(); }
148 
149  std::shared_ptr<MatcherData> GetSharedData() const { return data_; }
150 
151  private:
152  static Label PhiLabel(MatchType match_type, Label label) {
153  if (match_type == MATCH_INPUT && flags & kPhiFstMatchInput) return label;
154  if (match_type == MATCH_OUTPUT && flags & kPhiFstMatchOutput) return label;
155  return kNoLabel;
156  }
157 
158  std::shared_ptr<MatcherData> data_;
159 };
160 
161 inline constexpr char phi_fst_type[] = "phi";
162 inline constexpr char input_phi_fst_type[] = "input_phi";
163 inline constexpr char output_phi_fst_type[] = "output_phi";
164 
165 template <class Arc>
166 using PhiFst =
168  phi_fst_type>;
169 
171 
172 template <class Arc>
173 using InputPhiFst =
175  PhiFstMatcher<SortedMatcher<ConstFst<Arc>>, kPhiFstMatchInput>,
176  input_phi_fst_type>;
177 
179 
180 template <class Arc>
181 using OutputPhiFst =
183  PhiFstMatcher<SortedMatcher<ConstFst<Arc>>, kPhiFstMatchOutput>,
184  output_phi_fst_type>;
185 
187 
188 } // namespace fst
189 
190 #endif // FST_EXTENSIONS_SPECIAL_PHI_FST_H_
DECLARE_int64(phi_fst_phi_label)
PhiFstMatcher< M, flags > * Copy(bool safe=false) const override
Definition: phi-fst.h:143
PhiFstMatcherData(Label phi_label=FST_FLAGS_phi_fst_phi_label, bool phi_loop=FST_FLAGS_phi_fst_phi_loop, MatcherRewriteMode rewrite_mode=RewriteMode(FST_FLAGS_phi_fst_rewrite_mode))
Definition: phi-fst.h:45
constexpr int kNoLabel
Definition: fst.h:195
MatcherRewriteMode RewriteMode() const
Definition: phi-fst.h:81
typename Arc::StateId StateId
Definition: phi-fst.h:110
MatchType
Definition: fst.h:187
typename M::FST FST
Definition: phi-fst.h:108
#define LOG(type)
Definition: log.h:53
bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const
Definition: phi-fst.h:70
constexpr uint8_t kPhiFstMatchInput
Definition: phi-fst.h:100
std::ostream & WriteType(std::ostream &strm, const T t)
Definition: util.h:228
std::shared_ptr< MatcherData > GetSharedData() const
Definition: phi-fst.h:149
PhiFstMatcherData(const PhiFstMatcherData &data)
Definition: phi-fst.h:54
constexpr char output_phi_fst_type[]
Definition: phi-fst.h:163
PhiFstMatcher(const PhiFstMatcher< M, flags > &matcher, bool safe=false)
Definition: phi-fst.h:140
const MatcherData * GetData() const
Definition: phi-fst.h:147
DECLARE_bool(phi_fst_phi_loop)
static PhiFstMatcherData< Label > * Read(std::istream &istrm, const FstReadOptions &read)
Definition: phi-fst.h:59
typename M::Arc Arc
Definition: phi-fst.h:109
PhiFstMatcher(const FST &fst, MatchType match_type, std::shared_ptr< MatcherData > data=std::make_shared< MatcherData >())
Definition: phi-fst.h:118
MatcherRewriteMode
Definition: matcher.h:584
DECLARE_string(phi_fst_rewrite_mode)
std::istream & ReadType(std::istream &strm, T *t)
Definition: util.h:80
constexpr char phi_fst_type[]
Definition: phi-fst.h:161
typename Arc::Weight Weight
Definition: phi-fst.h:112
typename Arc::Label Label
Definition: phi-fst.h:111
constexpr char input_phi_fst_type[]
Definition: phi-fst.h:162
constexpr uint8_t kPhiFstMatchOutput
Definition: phi-fst.h:102
PhiFstMatcher(const FST *fst, MatchType match_type, std::shared_ptr< MatcherData > data=std::make_shared< MatcherData >())
Definition: phi-fst.h:129