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
348
349
350
351
352
353
354
355
356
357
358
359
|
// matcher-fst.h
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Copyright 2005-2010 Google, Inc.
// Author: riley@google.com (Michael Riley)
//
// \file
// Class to add a matcher to an FST.
#ifndef FST_LIB_MATCHER_FST_FST_H__
#define FST_LIB_MATCHER_FST_FST_H__
#include <fst/add-on.h>
#include <fst/const-fst.h>
#include <fst/lookahead-matcher.h>
namespace fst {
// WRITABLE MATCHERS - these have the interface of Matchers (see
// matcher.h) and these additional methods:
//
// template <class F>
// class Matcher {
// public:
// typedef ... MatcherData; // Initialization data
// ...
// // Constructor with additional argument for external initialization
// // data; matcher increments its reference count on construction and
// // decrements the reference count, and if 0 deletes, on destruction.
// Matcher(const F &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 have the form:
// class MatcherData {
// public:
// // Required copy constructor.
// MatcherData(const MatcherData &);
// //
// // Required I/O methods.
// static MatcherData *Read(istream &istrm);
// bool Write(ostream &ostrm);
//
// // Required reference counting.
// int RefCount() const;
// int IncrRefCount();
// int DecrRefCount();
// };
// Default MatcherFst initializer - does nothing.
template <class M>
class NullMatcherFstInit {
public:
typedef AddOnPair<typename M::MatcherData, typename M::MatcherData> D;
typedef AddOnImpl<typename M::FST, D> Impl;
NullMatcherFstInit(Impl **) {}
};
// Class to add a matcher M to an Fst F. Creates a new Fst of type name N.
// Optional function object I can be used to initialize the Fst.
template <class F, class M, const char* N,
class I = NullMatcherFstInit<M> >
class MatcherFst
: public ImplToExpandedFst<
AddOnImpl<F,
AddOnPair<typename M::MatcherData,
typename M::MatcherData> > > {
public:
friend class StateIterator< MatcherFst<F, M, N, I> >;
friend class ArcIterator< MatcherFst<F, M, N, I> >;
typedef F FST;
typedef M FstMatcher;
typedef typename F::Arc Arc;
typedef typename Arc::StateId StateId;
typedef AddOnPair<typename M::MatcherData, typename M::MatcherData> D;
typedef AddOnImpl<F, D> Impl;
MatcherFst() : ImplToExpandedFst<Impl>(new Impl(F(), N)) {}
explicit MatcherFst(const F &fst)
: ImplToExpandedFst<Impl>(CreateImpl(fst, N)) {}
explicit MatcherFst(const Fst<Arc> &fst)
: ImplToExpandedFst<Impl>(CreateImpl(fst, N)) {}
// See Fst<>::Copy() for doc.
MatcherFst(const MatcherFst<F, M, N, I> &fst, bool safe = false)
: ImplToExpandedFst<Impl>(fst, safe) {}
// Get a copy of this MatcherFst. See Fst<>::Copy() for further doc.
virtual MatcherFst<F, M, N, I> *Copy(bool safe = false) const {
return new MatcherFst<F, M, N, I>(*this, safe);
}
// Read a MatcherFst from an input stream; return NULL on error
static MatcherFst<F, M, N, I> *Read(istream &strm,
const FstReadOptions &opts) {
Impl *impl = Impl::Read(strm, opts);
return impl ? new MatcherFst<F, M, N, I>(impl) : 0;
}
// Read a MatcherFst from a file; return NULL on error
// Empty filename reads from standard input
static MatcherFst<F, M, N, I> *Read(const string &filename) {
Impl *impl = ImplToExpandedFst<Impl>::Read(filename);
return impl ? new MatcherFst<F, M, N, I>(impl) : 0;
}
virtual bool Write(ostream &strm, const FstWriteOptions &opts) const {
return GetImpl()->Write(strm, opts);
}
virtual bool Write(const string &filename) const {
return Fst<Arc>::WriteFile(filename);
}
virtual void InitStateIterator(StateIteratorData<Arc> *data) const {
return GetImpl()->InitStateIterator(data);
}
virtual void InitArcIterator(StateId s, ArcIteratorData<Arc> *data) const {
return GetImpl()->InitArcIterator(s, data);
}
virtual M *InitMatcher(MatchType match_type) const {
return new M(GetFst(), match_type, GetData(match_type));
}
// Allows access to MatcherFst components.
Impl *GetImpl() const {
return ImplToFst<Impl, ExpandedFst<Arc> >::GetImpl();
}
F& GetFst() const { return GetImpl()->GetFst(); }
typename M::MatcherData *GetData(MatchType match_type) const {
D *data = GetImpl()->GetAddOn();
return match_type == MATCH_INPUT ? data->First() : data->Second();
}
private:
static Impl *CreateImpl(const F &fst, const string &name) {
M imatcher(fst, MATCH_INPUT);
M omatcher(fst, MATCH_OUTPUT);
D *data = new D(imatcher.GetData(), omatcher.GetData());
Impl *impl = new Impl(fst, name);
impl->SetAddOn(data);
I init(&impl);
data->DecrRefCount();
return impl;
}
static Impl *CreateImpl(const Fst<Arc> &fst, const string &name) {
F ffst(fst);
return CreateImpl(ffst, name);
}
explicit MatcherFst(Impl *impl) : ImplToExpandedFst<Impl>(impl) {}
// Makes visible to friends.
void SetImpl(Impl *impl, bool own_impl = true) {
ImplToFst< Impl, ExpandedFst<Arc> >::SetImpl(impl, own_impl);
}
void operator=(const MatcherFst<F, M, N, I> &fst); // disallow
};
// Specialization fo MatcherFst.
template <class F, class M, const char* N, class I>
class StateIterator< MatcherFst<F, M, N, I> > : public StateIterator<F> {
public:
explicit StateIterator(const MatcherFst<F, M, N, I> &fst) :
StateIterator<F>(fst.GetImpl()->GetFst()) {}
};
// Specialization for MatcherFst.
template <class F, class M, const char* N, class I>
class ArcIterator< MatcherFst<F, M, N, I> > : public ArcIterator<F> {
public:
ArcIterator(const MatcherFst<F, M, N, I> &fst, typename F::Arc::StateId s)
: ArcIterator<F>(fst.GetImpl()->GetFst(), s) {}
};
// Specialization for MatcherFst
template <class F, class M, const char* N, class I>
class Matcher< MatcherFst<F, M, N, I> > {
public:
typedef MatcherFst<F, M, N, I> FST;
typedef typename F::Arc Arc;
typedef typename Arc::StateId StateId;
typedef typename Arc::Label Label;
Matcher(const FST &fst, MatchType match_type) {
matcher_ = fst.InitMatcher(match_type);
}
Matcher(const Matcher<FST> &matcher) {
matcher_ = matcher.matcher_->Copy();
}
~Matcher() { delete matcher_; }
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); }
|