FST  openfst-1.8.2
OpenFst Library
matcher-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 // Class to add a matcher to an FST.
19 
20 #ifndef FST_MATCHER_FST_H_
21 #define FST_MATCHER_FST_H_
22 
23 #include <cstdint>
24 #include <memory>
25 #include <string>
26 
27 
28 #include <fst/add-on.h>
29 #include <fst/const-fst.h>
30 #include <fst/lookahead-matcher.h>
31 
32 
33 namespace fst {
34 
35 // Writeable matchers have the same interface as Matchers (as defined in
36 // matcher.h) along with the following additional methods:
37 //
38 // template <class F>
39 // class Matcher {
40 // public:
41 // using FST = F;
42 // ...
43 // using MatcherData = ...; // Initialization data.
44 //
45 // // Constructor with additional argument for external initialization data;
46 // // matcher increments its reference count on construction and decrements
47 // // the reference count, and deletes once the reference count has reached
48 // // zero.
49 // Matcher(const FST &fst, MatchType type, MatcherData *data);
50 //
51 // // Returns pointer to initialization data that can be passed to a Matcher
52 // // constructor.
53 // MatcherData *GetData() const;
54 // };
55 
56 // The matcher initialization data class must also provide the following
57 // interface:
58 //
59 // class MatcherData {
60 // public:
61 // // Required copy constructor.
62 // MatcherData(const MatcherData &);
63 //
64 // // Required I/O methods.
65 // static MatcherData *Read(std::istream &istrm, const FstReadOptions &opts);
66 // bool Write(std::ostream &ostrm, const FstWriteOptions &opts) const;
67 // };
68 
69 // Trivial (no-op) MatcherFst initializer functor.
70 template <class M>
72  public:
73  using MatcherData = typename M::MatcherData;
76 
77  explicit NullMatcherFstInit(std::shared_ptr<Impl> *) {}
78 };
79 
80 // Class adding a matcher to an FST type. Creates a new FST whose name is given
81 // by N. An optional functor Init can be used to initialize the FST. The Data
82 // template parameter allows the user to select the type of the add-on.
83 template <
84  class F, class M, const char *Name, class Init = NullMatcherFstInit<M>,
86 class MatcherFst : public ImplToExpandedFst<internal::AddOnImpl<F, Data>> {
87  public:
88  using FST = F;
89  using Arc = typename FST::Arc;
90  using StateId = typename Arc::StateId;
91 
92  using FstMatcher = M;
93  using MatcherData = typename FstMatcher::MatcherData;
94 
96  using D = Data;
97 
98  friend class StateIterator<MatcherFst<FST, FstMatcher, Name, Init, Data>>;
99  friend class ArcIterator<MatcherFst<FST, FstMatcher, Name, Init, Data>>;
100 
101  MatcherFst() : ImplToExpandedFst<Impl>(std::make_shared<Impl>(FST(), Name)) {}
102 
103  // Constructs a MatcherFst from an FST, which is the underlying FST type used
104  // by this class. Uses the existing Data if present, and runs Init on it.
105  // Stores fst internally, making a thread-safe copy of it.
106  explicit MatcherFst(const FST &fst, std::shared_ptr<Data> data = nullptr)
107  : ImplToExpandedFst<Impl>(data ? CreateImpl(fst, Name, data)
108  : CreateDataAndImpl(fst, Name)) {}
109 
110  // Constructs a MatcherFst from an Fst<Arc>, which is *not* the underlying
111  // FST type used by this class. Uses the existing Data if present, and
112  // runs Init on it. Stores fst internally, converting Fst<Arc> to FST and
113  // therefore making a deep copy.
114  explicit MatcherFst(const Fst<Arc> &fst, std::shared_ptr<Data> data = nullptr)
115  : ImplToExpandedFst<Impl>(data ? CreateImpl(fst, Name, data)
116  : CreateDataAndImpl(fst, Name)) {}
117 
118  // See Fst<>::Copy() for doc.
119  MatcherFst(const MatcherFst &fst, bool safe = false)
120  : ImplToExpandedFst<Impl>(fst, safe) {}
121 
122  // Get a copy of this MatcherFst. See Fst<>::Copy() for further doc.
123  MatcherFst *Copy(bool safe = false) const override {
124  return new MatcherFst(*this, safe);
125  }
126 
127  // Read a MatcherFst from an input stream; return nullptr on error
128  static MatcherFst *Read(std::istream &strm, const FstReadOptions &opts) {
129  auto *impl = Impl::Read(strm, opts);
130  return impl ? new MatcherFst(std::shared_ptr<Impl>(impl)) : nullptr;
131  }
132 
133  // Read a MatcherFst from a file; return nullptr on error
134  // Empty source reads from standard input
135  static MatcherFst *Read(const std::string &source) {
136  auto *impl = ImplToExpandedFst<Impl>::Read(source);
137  return impl ? new MatcherFst(std::shared_ptr<Impl>(impl)) : nullptr;
138  }
139 
140  bool Write(std::ostream &strm, const FstWriteOptions &opts) const override {
141  return GetImpl()->Write(strm, opts);
142  }
143 
144  bool Write(const std::string &source) const override {
145  return Fst<Arc>::WriteFile(source);
146  }
147 
148  void InitStateIterator(StateIteratorData<Arc> *data) const override {
149  return GetImpl()->InitStateIterator(data);
150  }
151 
152  void InitArcIterator(StateId s, ArcIteratorData<Arc> *data) const override {
153  return GetImpl()->InitArcIterator(s, data);
154  }
155 
156  FstMatcher *InitMatcher(MatchType match_type) const override {
157  return new FstMatcher(&GetFst(), match_type, GetSharedData(match_type));
158  }
159 
160  const FST &GetFst() const { return GetImpl()->GetFst(); }
161 
162  const Data *GetAddOn() const { return GetImpl()->GetAddOn(); }
163 
164  std::shared_ptr<Data> GetSharedAddOn() const {
165  return GetImpl()->GetSharedAddOn();
166  }
167 
168  const MatcherData *GetData(MatchType match_type) const {
169  const auto *data = GetAddOn();
170  return match_type == MATCH_INPUT ? data->First() : data->Second();
171  }
172 
173  std::shared_ptr<MatcherData> GetSharedData(MatchType match_type) const {
174  const auto *data = GetAddOn();
175  return match_type == MATCH_INPUT ? data->SharedFirst()
176  : data->SharedSecond();
177  }
178 
179  protected:
180  using ImplToFst<Impl, ExpandedFst<Arc>>::GetImpl;
181 
182  // Makes a thread-safe copy of fst.
183  static std::shared_ptr<Impl> CreateDataAndImpl(const FST &fst,
184  const std::string &name) {
185  FstMatcher imatcher(fst, MATCH_INPUT);
186  FstMatcher omatcher(fst, MATCH_OUTPUT);
187  return CreateImpl(fst, name,
188  std::make_shared<Data>(imatcher.GetSharedData(),
189  omatcher.GetSharedData()));
190  }
191 
192  // Makes a deep copy of fst.
193  static std::shared_ptr<Impl> CreateDataAndImpl(const Fst<Arc> &fst,
194  const std::string &name) {
195  FST result(fst);
196  return CreateDataAndImpl(result, name);
197  }
198 
199  // Makes a thread-safe copy of fst.
200  static std::shared_ptr<Impl> CreateImpl(const FST &fst,
201  const std::string &name,
202  std::shared_ptr<Data> data) {
203  auto impl = std::make_shared<Impl>(fst, name);
204  impl->SetAddOn(data);
205  Init init(&impl);
206  return impl;
207  }
208 
209  // Makes a deep copy of fst.
210  static std::shared_ptr<Impl> CreateImpl(const Fst<Arc> &fst,
211  const std::string &name,
212  std::shared_ptr<Data> data) {
213  auto impl = std::make_shared<Impl>(fst, name);
214  impl->SetAddOn(data);
215  Init init(&impl);
216  return impl;
217  }
218 
219  explicit MatcherFst(std::shared_ptr<Impl> impl)
220  : ImplToExpandedFst<Impl>(impl) {}
221 
222  private:
223  MatcherFst &operator=(const MatcherFst &) = delete;
224 };
225 
226 // Specialization for MatcherFst.
227 template <class FST, class M, const char *Name, class Init>
228 class StateIterator<MatcherFst<FST, M, Name, Init>>
229  : public StateIterator<FST> {
230  public:
232  : StateIterator<FST>(fst.GetImpl()->GetFst()) {}
233 };
234 
235 // Specialization for MatcherFst.
236 template <class FST, class M, const char *Name, class Init>
237 class ArcIterator<MatcherFst<FST, M, Name, Init>> : public ArcIterator<FST> {
238  public:
239  using StateId = typename FST::Arc::StateId;
240 
242  typename FST::Arc::StateId s)
243  : ArcIterator<FST>(fst.GetImpl()->GetFst(), s) {}
244 };
245 
246 // Specialization for MatcherFst.
247 template <class F, class M, const char *Name, class Init>
248 class Matcher<MatcherFst<F, M, Name, Init>> {
249  public:
251  using Arc = typename F::Arc;
252  using Label = typename Arc::Label;
253  using StateId = typename Arc::StateId;
254 
255  Matcher(const FST &fst, MatchType match_type)
256  : matcher_(fst.InitMatcher(match_type)) {}
257 
258  Matcher(const Matcher &matcher) : matcher_(matcher.matcher_->Copy()) {}
259 
260  Matcher *Copy() const { return new Matcher(*this); }
261 
262  MatchType Type(bool test) const { return matcher_->Type(test); }
263 
264  void SetState(StateId s) { matcher_->SetState(s); }
265 
266  bool Find(Label label) { return matcher_->Find(label); }
267 
268  bool Done() const { return matcher_->Done(); }
269 
270  const Arc &Value() const { return matcher_->Value(); }
271 
272  void Next() { matcher_->Next(); }
273 
274  uint64_t Properties(uint64_t props) const {
275  return matcher_->Properties(props);
276  }
277 
278  uint32_t Flags() const { return matcher_->Flags(); }
279 
280  private:
281  std::unique_ptr<M> matcher_;
282 };
283 
284 // Specialization for MatcherFst.
285 template <class F, class M, const char *Name, class Init>
286 class LookAheadMatcher<MatcherFst<F, M, Name, Init>> {
287  public:
289  using Arc = typename F::Arc;
290  using Label = typename Arc::Label;
291  using StateId = typename Arc::StateId;
292  using Weight = typename Arc::Weight;
293 
294  LookAheadMatcher(const FST &fst, MatchType match_type)
295  : matcher_(fst.InitMatcher(match_type)) {}
296 
297  LookAheadMatcher(const LookAheadMatcher &matcher, bool safe = false)
298  : matcher_(matcher.matcher_->Copy(safe)) {}
299 
300  // General matcher methods.
301  LookAheadMatcher *Copy(bool safe = false) const {
302  return new LookAheadMatcher(*this, safe);
303  }
304 
305  MatchType Type(bool test) const { return matcher_->Type(test); }
306 
307  void SetState(StateId s) { matcher_->SetState(s); }
308 
309  bool Find(Label label) { return matcher_->Find(label); }
310 
311  bool Done() const { return matcher_->Done(); }
312 
313  const Arc &Value() const { return matcher_->Value(); }
314 
315  void Next() { matcher_->Next(); }
316 
317  const FST &GetFst() const { return matcher_->GetFst(); }
318 
319  uint64_t Properties(uint64_t props) const {
320  return matcher_->Properties(props);
321  }
322 
323  uint32_t Flags() const { return matcher_->Flags(); }
324 
325  bool LookAheadLabel(Label label) const {
326  return matcher_->LookAheadLabel(label);
327  }
328 
329  bool LookAheadFst(const Fst<Arc> &fst, StateId s) {
330  return matcher_->LookAheadFst(fst, s);
331  }
332 
333  Weight LookAheadWeight() const { return matcher_->LookAheadWeight(); }
334 
335  bool LookAheadPrefix(Arc *arc) const {
336  return matcher_->LookAheadPrefix(arc);
337  }
338 
339  void InitLookAheadFst(const Fst<Arc> &fst, bool copy = false) {
340  matcher_->InitLookAheadFst(fst, copy);
341  }
342 
343  private:
344  std::unique_ptr<M> matcher_;
345 };
346 
347 // Useful aliases when using StdArc.
348 
349 inline constexpr char arc_lookahead_fst_type[] = "arc_lookahead";
350 
351 using StdArcLookAheadFst =
354  arc_lookahead_fst_type>;
355 
356 inline constexpr char ilabel_lookahead_fst_type[] = "ilabel_lookahead";
357 inline constexpr char olabel_lookahead_fst_type[] = "olabel_lookahead";
358 
359 constexpr auto ilabel_lookahead_flags =
362 
363 constexpr auto olabel_lookahead_flags =
366 
372 
374  ConstFst<StdArc>,
375  LabelLookAheadMatcher<SortedMatcher<ConstFst<StdArc>>,
376  olabel_lookahead_flags, FastLogAccumulator<StdArc>>,
377  olabel_lookahead_fst_type, LabelLookAheadRelabeler<StdArc>>;
378 
379 } // namespace fst
380 
381 #endif // FST_MATCHER_FST_H_
static Impl * Read(std::istream &strm, const FstReadOptions &opts)
Definition: expanded-fst.h:145
typename FstMatcher::MatcherData MatcherData
Definition: matcher-fst.h:93
void InitLookAheadFst(const Fst< Arc > &fst, bool copy=false)
Definition: matcher-fst.h:339
typename M::MatcherData MatcherData
Definition: matcher-fst.h:73
LookAheadMatcher * Copy(bool safe=false) const
Definition: matcher-fst.h:301
MatcherFst * Copy(bool safe=false) const override
Definition: matcher-fst.h:123
const FST & GetFst() const
Definition: matcher-fst.h:160
constexpr char olabel_lookahead_fst_type[]
Definition: matcher-fst.h:357
const MatcherData * GetData(MatchType match_type) const
Definition: matcher-fst.h:168
MatchType
Definition: fst.h:193
constexpr char ilabel_lookahead_fst_type[]
Definition: matcher-fst.h:356
static std::shared_ptr< Impl > CreateImpl(const FST &fst, const std::string &name, std::shared_ptr< Data > data)
Definition: matcher-fst.h:200
static MatcherFst * Read(std::istream &strm, const FstReadOptions &opts)
Definition: matcher-fst.h:128
std::shared_ptr< Data > GetSharedAddOn() const
Definition: matcher-fst.h:164
static std::shared_ptr< Impl > CreateDataAndImpl(const FST &fst, const std::string &name)
Definition: matcher-fst.h:183
uint64_t Properties(uint64_t props) const
Definition: matcher-fst.h:274
LookAheadMatcher(const FST &fst, MatchType match_type)
Definition: matcher-fst.h:294
static MatcherFst * Read(const std::string &source)
Definition: matcher-fst.h:135
constexpr auto olabel_lookahead_flags
Definition: matcher-fst.h:363
void InitArcIterator(StateId s, ArcIteratorData< Arc > *data) const override
Definition: matcher-fst.h:152
constexpr uint32_t kOutputLookAheadMatcher
static std::shared_ptr< Impl > CreateImpl(const Fst< Arc > &fst, const std::string &name, std::shared_ptr< Data > data)
Definition: matcher-fst.h:210
constexpr uint32_t kLookAheadEpsilons
LookAheadMatcher(const LookAheadMatcher &matcher, bool safe=false)
Definition: matcher-fst.h:297
constexpr uint32_t kLookAheadNonEpsilonPrefix
MatcherFst(const Fst< Arc > &fst, std::shared_ptr< Data > data=nullptr)
Definition: matcher-fst.h:114
void InitStateIterator(StateIteratorData< Arc > *data) const override
Definition: matcher-fst.h:148
constexpr auto ilabel_lookahead_flags
Definition: matcher-fst.h:359
MatcherFst(const FST &fst, std::shared_ptr< Data > data=nullptr)
Definition: matcher-fst.h:106
constexpr uint32_t kInputLookAheadMatcher
AddOnPair< MatcherData, MatcherData > Data
Definition: matcher-fst.h:74
typename Arc::StateId StateId
Definition: fst.h:519
constexpr uint32_t kLookAheadPrefix
MatcherFst(std::shared_ptr< Impl > impl)
Definition: matcher-fst.h:219
static std::shared_ptr< Impl > CreateDataAndImpl(const Fst< Arc > &fst, const std::string &name)
Definition: matcher-fst.h:193
bool Write(std::ostream &strm, const FstWriteOptions &opts) const override
Definition: matcher-fst.h:140
Matcher(const FST &fst, MatchType match_type)
Definition: matcher-fst.h:255
bool WriteFile(const std::string &source) const
Definition: fst.h:332
ArcIterator(const MatcherFst< FST, M, Name, Init > &fst, typename FST::Arc::StateId s)
Definition: matcher-fst.h:241
bool LookAheadFst(const Fst< Arc > &fst, StateId s)
Definition: matcher-fst.h:329
A Arc
Definition: fst.h:211
bool Write(const std::string &source) const override
Definition: matcher-fst.h:144
constexpr uint32_t kLookAheadWeight
StateIterator(const MatcherFst< FST, M, Name, Init > &fst)
Definition: matcher-fst.h:231
std::shared_ptr< MatcherData > GetSharedData(MatchType match_type) const
Definition: matcher-fst.h:173
NullMatcherFstInit(std::shared_ptr< Impl > *)
Definition: matcher-fst.h:77
const Data * GetAddOn() const
Definition: matcher-fst.h:162
MatcherFst(const MatcherFst &fst, bool safe=false)
Definition: matcher-fst.h:119
FstMatcher * InitMatcher(MatchType match_type) const override
Definition: matcher-fst.h:156
typename Arc::StateId StateId
Definition: fst.h:212
constexpr char arc_lookahead_fst_type[]
Definition: matcher-fst.h:349
static AddOnImpl * Read(std::istream &strm, const FstReadOptions &opts)
Definition: add-on.h:168