1f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// matcher-fst.h
2f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
3f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Licensed under the Apache License, Version 2.0 (the "License");
4f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// you may not use this file except in compliance with the License.
5f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// You may obtain a copy of the License at
6f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
7f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//     http://www.apache.org/licenses/LICENSE-2.0
8f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
9f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Unless required by applicable law or agreed to in writing, software
10f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// distributed under the License is distributed on an "AS IS" BASIS,
11f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// See the License for the specific language governing permissions and
13f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// limitations under the License.
14f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
15f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Copyright 2005-2010 Google, Inc.
16f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Author: riley@google.com (Michael Riley)
17f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
18f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// \file
19f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Class to add a matcher to an FST.
20f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
21f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#ifndef FST_LIB_MATCHER_FST_FST_H__
22f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#define FST_LIB_MATCHER_FST_FST_H__
23f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
24f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/add-on.h>
25f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/const-fst.h>
26f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#include <fst/lookahead-matcher.h>
27f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
28f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
29f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonnamespace fst {
30f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
31f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// WRITABLE MATCHERS - these have the interface of Matchers (see
32f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// matcher.h) and these additional methods:
33f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
34f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// template <class F>
35f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// class Matcher {
36f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//  public:
37f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   typedef ... MatcherData;  // Initialization data
38f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//  ...
39f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   // Constructor with additional argument for external initialization
40f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   // data; matcher increments its reference count on construction and
41f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   // decrements the reference count, and if 0 deletes, on destruction.
42f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   Matcher(const F &fst, MatchType type, MatcherData *data);
43f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
44f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   // Returns pointer to initialization data that can be
45f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   // passed to a Matcher constructor.
46f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   MatcherData *GetData() const;
47f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// };
48f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
49f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// The matcher initialization data class must have the form:
50f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// class MatcherData {
51f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// public:
52f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   // Required copy constructor.
53f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   MatcherData(const MatcherData &);
54f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   //
55f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   // Required I/O methods.
56f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   static MatcherData *Read(istream &istrm);
57f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   bool Write(ostream &ostrm);
58f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
59f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   // Required reference counting.
60f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   int RefCount() const;
61f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   int IncrRefCount();
62f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//   int DecrRefCount();
63f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// };
64f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
65f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Default MatcherFst initializer - does nothing.
66f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class M>
67f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass NullMatcherFstInit {
68f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public:
69f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef AddOnPair<typename M::MatcherData, typename M::MatcherData> D;
70f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef AddOnImpl<typename M::FST, D> Impl;
71f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  NullMatcherFstInit(Impl **) {}
72f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson};
73f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
74f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Class to add a matcher M to an Fst F. Creates a new Fst of type name N.
75f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Optional function object I can be used to initialize the Fst.
76f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class F, class M, const char* N,
77f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson          class I = NullMatcherFstInit<M> >
78f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass MatcherFst
79f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    : public ImplToExpandedFst<
80f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson               AddOnImpl<F,
81f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                         AddOnPair<typename M::MatcherData,
82f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                   typename M::MatcherData> > > {
83f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public:
84f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  friend class StateIterator< MatcherFst<F, M, N, I> >;
85f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  friend class ArcIterator< MatcherFst<F, M, N, I> >;
86f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
87f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef F FST;
88f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef M FstMatcher;
89f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef typename F::Arc Arc;
90f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef typename Arc::StateId StateId;
91f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef AddOnPair<typename M::MatcherData, typename M::MatcherData> D;
92f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef AddOnImpl<F, D> Impl;
93f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
94f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  MatcherFst() : ImplToExpandedFst<Impl>(new Impl(F(), N)) {}
95f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
96f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  explicit MatcherFst(const F &fst)
97f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      : ImplToExpandedFst<Impl>(CreateImpl(fst, N)) {}
98f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
99f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  explicit MatcherFst(const Fst<Arc> &fst)
100f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      : ImplToExpandedFst<Impl>(CreateImpl(fst, N)) {}
101f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
102f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  // See Fst<>::Copy() for doc.
103f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  MatcherFst(const MatcherFst<F, M, N, I> &fst, bool safe = false)
104f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      : ImplToExpandedFst<Impl>(fst, safe) {}
105f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
106f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  // Get a copy of this MatcherFst. See Fst<>::Copy() for further doc.
107f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  virtual MatcherFst<F, M, N, I> *Copy(bool safe = false) const {
108f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return new MatcherFst<F, M, N, I>(*this, safe);
109f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
110f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
111f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  // Read a MatcherFst from an input stream; return NULL on error
112f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  static MatcherFst<F, M, N, I> *Read(istream &strm,
113f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                      const FstReadOptions &opts) {
114f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    Impl *impl = Impl::Read(strm, opts);
115f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return impl ? new MatcherFst<F, M, N, I>(impl) : 0;
116f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
117f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
118f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  // Read a MatcherFst from a file; return NULL on error
119f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  // Empty filename reads from standard input
120f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  static MatcherFst<F, M, N, I> *Read(const string &filename) {
121f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    Impl *impl = ImplToExpandedFst<Impl>::Read(filename);
122f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return impl ? new MatcherFst<F, M, N, I>(impl) : 0;
123f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
124f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
125f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  virtual bool Write(ostream &strm, const FstWriteOptions &opts) const {
126f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return GetImpl()->Write(strm, opts);
127f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
128f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
129f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  virtual bool Write(const string &filename) const {
130f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return Fst<Arc>::WriteFile(filename);
131f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
132f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
133f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  virtual void InitStateIterator(StateIteratorData<Arc> *data) const {
134f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return GetImpl()->InitStateIterator(data);
135f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
136f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
137f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  virtual void InitArcIterator(StateId s, ArcIteratorData<Arc> *data) const {
138f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return GetImpl()->InitArcIterator(s, data);
139f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
140f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
141f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  virtual M *InitMatcher(MatchType match_type) const {
142f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return new M(GetFst(), match_type, GetData(match_type));
143f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
144f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
145f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  // Allows access to MatcherFst components.
146f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  Impl *GetImpl() const {
147f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return ImplToFst<Impl, ExpandedFst<Arc> >::GetImpl();
148f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
149f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
150f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  F& GetFst() const { return GetImpl()->GetFst(); }
151f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
152f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typename M::MatcherData *GetData(MatchType match_type) const {
153f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    D *data = GetImpl()->GetAddOn();
154f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return match_type == MATCH_INPUT ? data->First() : data->Second();
155f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
156f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
157f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson private:
158f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  static Impl *CreateImpl(const F &fst, const string &name) {
159f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    M imatcher(fst, MATCH_INPUT);
160f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    M omatcher(fst, MATCH_OUTPUT);
161f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    D *data = new D(imatcher.GetData(), omatcher.GetData());
162f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    Impl *impl = new Impl(fst, name);
163f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    impl->SetAddOn(data);
164f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    I init(&impl);
165f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    data->DecrRefCount();
166f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return impl;
167f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
168f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
169f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  static Impl *CreateImpl(const Fst<Arc> &fst, const string &name) {
170f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    F ffst(fst);
171f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return CreateImpl(ffst, name);
172f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
173f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
174f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  explicit MatcherFst(Impl *impl) : ImplToExpandedFst<Impl>(impl) {}
175f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
176f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  // Makes visible to friends.
177f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  void SetImpl(Impl *impl, bool own_impl = true) {
178f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    ImplToFst< Impl, ExpandedFst<Arc> >::SetImpl(impl, own_impl);
179f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
180f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
181f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  void operator=(const MatcherFst<F, M, N, I> &fst);  // disallow
182f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson};
183f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
184f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
185f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Specialization fo MatcherFst.
186f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class F, class M, const char* N, class I>
187f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass StateIterator< MatcherFst<F, M, N, I> > : public StateIterator<F> {
188f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public:
189f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  explicit StateIterator(const MatcherFst<F, M, N, I> &fst) :
190f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      StateIterator<F>(fst.GetImpl()->GetFst()) {}
191f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson};
192f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
193f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
194f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Specialization for MatcherFst.
195f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class F, class M, const char* N, class I>
196f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass ArcIterator< MatcherFst<F, M, N, I> > : public ArcIterator<F> {
197f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public:
198f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  ArcIterator(const MatcherFst<F, M, N, I> &fst, typename F::Arc::StateId s)
199f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson      : ArcIterator<F>(fst.GetImpl()->GetFst(), s) {}
200f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson};
201f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
202f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
203f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Specialization for MatcherFst
204f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class F, class M, const char* N, class I>
205f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass Matcher< MatcherFst<F, M, N, I> > {
206f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public:
207f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef MatcherFst<F, M, N, I> FST;
208f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef typename F::Arc Arc;
209f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef typename Arc::StateId StateId;
210f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef typename Arc::Label Label;
211f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
212f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  Matcher(const FST &fst, MatchType match_type) {
213f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    matcher_ = fst.InitMatcher(match_type);
214f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
215f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
216f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  Matcher(const Matcher<FST> &matcher) {
217f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    matcher_ = matcher.matcher_->Copy();
218f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
219f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
220f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  ~Matcher() { delete matcher_; }
221f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
222f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  Matcher<FST> *Copy() const {
223f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return new Matcher<FST>(*this);
224f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
225f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
226f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  MatchType Type(bool test) const { return matcher_->Type(test); }
227f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  void SetState(StateId s) { matcher_->SetState(s); }
228f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool Find(Label label) { return matcher_->Find(label); }
229f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool Done() const { return matcher_->Done(); }
230f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  const Arc& Value() const { return matcher_->Value(); }
231f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  void Next() { matcher_->Next(); }
232f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  uint64 Properties(uint64 props) const { return matcher_->Properties(props); }
233f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  uint32 Flags() const { return matcher_->Flags(); }
234f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
235f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson private:
236f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  M *matcher_;
237f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
238f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  void operator=(const Matcher<Arc> &);  // disallow
239f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson};
240f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
241f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
242f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Specialization for MatcherFst
243f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontemplate <class F, class M, const char* N, class I>
244f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonclass LookAheadMatcher< MatcherFst<F, M, N, I> > {
245f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson public:
246f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef MatcherFst<F, M, N, I> FST;
247f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef typename F::Arc Arc;
248f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef typename Arc::StateId StateId;
249f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef typename Arc::Label Label;
250f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  typedef typename Arc::Weight Weight;
251f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
252f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  LookAheadMatcher(const FST &fst, MatchType match_type) {
253f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    matcher_ = fst.InitMatcher(match_type);
254f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
255f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
256f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  LookAheadMatcher(const LookAheadMatcher<FST> &matcher, bool safe = false) {
257f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    matcher_ = matcher.matcher_->Copy(safe);
258f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
259f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
260f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  ~LookAheadMatcher() { delete matcher_; }
261f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
262f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  // General matcher methods
263f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  LookAheadMatcher<FST> *Copy(bool safe = false) const {
264f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return new LookAheadMatcher<FST>(*this, safe);
265f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
266f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
267f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  MatchType Type(bool test) const { return matcher_->Type(test); }
268f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  void SetState(StateId s) { matcher_->SetState(s); }
269f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool Find(Label label) { return matcher_->Find(label); }
270f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool Done() const { return matcher_->Done(); }
271f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  const Arc& Value() const { return matcher_->Value(); }
272f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  void Next() { matcher_->Next(); }
273f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  const FST &GetFst() const { return matcher_->GetFst(); }
274f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  uint64 Properties(uint64 props) const { return matcher_->Properties(props); }
275f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  uint32 Flags() const { return matcher_->Flags(); }
276f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
277f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  // Look-ahead methods
278f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool LookAheadLabel(Label label) const {
279f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return matcher_->LookAheadLabel(label);
280f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
281f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
282f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool LookAheadFst(const Fst<Arc> &fst, StateId s) {
283f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return matcher_->LookAheadFst(fst, s);
284f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
285f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
286f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  Weight LookAheadWeight() const { return matcher_->LookAheadWeight(); }
287f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
288f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  bool LookAheadPrefix(Arc *arc) const {
289f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    return matcher_->LookAheadPrefix(arc);
290f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
291f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
292f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  void InitLookAheadFst(const Fst<Arc>& fst, bool copy = false) {
293f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    matcher_->InitLookAheadFst(fst, copy);
294f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  }
295f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
296f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson private:
297f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  M *matcher_;
298f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
299f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson  void operator=(const LookAheadMatcher<FST> &);  // disallow
300f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson};
301f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
302f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
303f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Useful aliases when using StdArc and LogArc.
304f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson//
305f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
306f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Arc look-ahead matchers
307f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonextern const char arc_lookahead_fst_type[];
308f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
309f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef MatcherFst<ConstFst<StdArc>,
310f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   ArcLookAheadMatcher<SortedMatcher<ConstFst<StdArc> > >,
311f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   arc_lookahead_fst_type> StdArcLookAheadFst;
312f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
313f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef MatcherFst<ConstFst<LogArc>,
314f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   ArcLookAheadMatcher<SortedMatcher<ConstFst<LogArc> > >,
315f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   arc_lookahead_fst_type> LogArcLookAheadFst;
316f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
317f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
318f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson// Label look-ahead matchers
319f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonextern const char ilabel_lookahead_fst_type[];
320f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonextern const char olabel_lookahead_fst_type[];
321f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
322f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstatic const uint32 ilabel_lookahead_flags = kInputLookAheadMatcher |
323f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    kLookAheadWeight | kLookAheadPrefix |
324f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    kLookAheadEpsilons | kLookAheadNonEpsilonPrefix;
325f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsonstatic const uint32 olabel_lookahead_flags = kOutputLookAheadMatcher |
326f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    kLookAheadWeight | kLookAheadPrefix |
327f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson    kLookAheadEpsilons | kLookAheadNonEpsilonPrefix;
328f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
329f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef MatcherFst<ConstFst<StdArc>,
330f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   LabelLookAheadMatcher<SortedMatcher<ConstFst<StdArc> >,
331f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                         ilabel_lookahead_flags,
332f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                         FastLogAccumulator<StdArc> >,
333f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   ilabel_lookahead_fst_type,
334f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   LabelLookAheadRelabeler<StdArc> > StdILabelLookAheadFst;
335f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
336f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef MatcherFst<ConstFst<LogArc>,
337f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   LabelLookAheadMatcher<SortedMatcher<ConstFst<LogArc> >,
338f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                         ilabel_lookahead_flags,
339f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                         FastLogAccumulator<LogArc> >,
340f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   ilabel_lookahead_fst_type,
341f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   LabelLookAheadRelabeler<LogArc> > LogILabelLookAheadFst;
342f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
343f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef MatcherFst<ConstFst<StdArc>,
344f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   LabelLookAheadMatcher<SortedMatcher<ConstFst<StdArc> >,
345f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                         olabel_lookahead_flags,
346f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                         FastLogAccumulator<StdArc> >,
347f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   olabel_lookahead_fst_type,
348f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   LabelLookAheadRelabeler<StdArc> > StdOLabelLookAheadFst;
349f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
350f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodsontypedef MatcherFst<ConstFst<LogArc>,
351f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   LabelLookAheadMatcher<SortedMatcher<ConstFst<LogArc> >,
352f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                         olabel_lookahead_flags,
353f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                                         FastLogAccumulator<LogArc> >,
354f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   olabel_lookahead_fst_type,
355f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson                   LabelLookAheadRelabeler<LogArc> > LogOLabelLookAheadFst;
356f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
357f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson}  // namespace fst
358f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson
359f4c12fce1ee58e670f9c3fce46c40296ba9ee8a2Ian Hodson#endif  // FST_LIB_MATCHER_FST_FST_H__
360