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