Blame view

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