far-class.h
7.77 KB
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
// See www.openfst.org for extensive documentation on this weighted
// finite-state transducer library.
//
// Scripting API support for FarReader and FarWriter.
#ifndef FST_EXTENSIONS_FAR_FAR_CLASS_H_
#define FST_EXTENSIONS_FAR_FAR_CLASS_H_
#include <memory>
#include <string>
#include <vector>
#include <fst/script/arg-packs.h>
#include <fst/script/fstscript.h>
#include <fst/extensions/far/far.h>
namespace fst {
namespace script {
// FarReader API.
// Virtual interface implemented by each concrete FarReaderImpl<A>.
// See the FarReader interface in far.h for the exact semantics.
class FarReaderImplBase {
public:
virtual const string &ArcType() const = 0;
virtual bool Done() const = 0;
virtual bool Error() const = 0;
virtual const string &GetKey() const = 0;
virtual const FstClass *GetFstClass() const = 0;
virtual bool Find(const string &key) = 0;
virtual void Next() = 0;
virtual void Reset() = 0;
virtual FarType Type() const = 0;
virtual ~FarReaderImplBase() {}
};
// Templated implementation.
template <class Arc>
class FarReaderClassImpl : public FarReaderImplBase {
public:
explicit FarReaderClassImpl(const string &filename)
: impl_(FarReader<Arc>::Open(filename)) {}
explicit FarReaderClassImpl(const std::vector<string> &filenames)
: impl_(FarReader<Arc>::Open(filenames)) {}
const string &ArcType() const final { return Arc::Type(); }
bool Done() const final { return impl_->Done(); }
bool Error() const final { return impl_->Error(); }
bool Find(const string &key) final { return impl_->Find(key); }
const FstClass *GetFstClass() const final {
fstc_.reset(new FstClass(*impl_->GetFst()));
return fstc_.get();
}
const string &GetKey() const final { return impl_->GetKey(); }
void Next() final { return impl_->Next(); }
void Reset() final { impl_->Reset(); }
FarType Type() const final { return impl_->Type(); }
const FarReader<Arc> *GetImpl() const { return impl_.get(); }
FarReader<Arc> *GetImpl() { return impl_.get(); }
private:
std::unique_ptr<FarReader<Arc>> impl_;
mutable std::unique_ptr<FstClass> fstc_;
};
class FarReaderClass;
using OpenFarReaderClassArgs1 =
WithReturnValue<FarReaderClass *, const string &>;
using OpenFarReaderClassArgs2 =
WithReturnValue<FarReaderClass *, const std::vector<string> &>;
// Untemplated user-facing class holding a templated pimpl.
class FarReaderClass {
public:
const string &ArcType() const { return impl_->ArcType(); }
bool Done() const { return impl_->Done(); }
// Returns True if the impl is null (i.e., due to read failure).
// Attempting to call any other function will result in null dereference.
bool Error() const { return (impl_) ? impl_->Error() : true; }
bool Find(const string &key) { return impl_->Find(key); }
const FstClass *GetFstClass() const { return impl_->GetFstClass(); }
const string &GetKey() const { return impl_->GetKey(); }
void Next() { impl_->Next(); }
void Reset() { impl_->Reset(); }
FarType Type() const { return impl_->Type(); }
template <class Arc>
const FarReader<Arc> *GetFarReader() const {
if (Arc::Type() != ArcType()) return nullptr;
const FarReaderClassImpl<Arc> *typed_impl =
static_cast<FarReaderClassImpl<Arc> *>(impl_.get());
return typed_impl->GetImpl();
}
template <class Arc>
FarReader<Arc> *GetFarReader() {
if (Arc::Type() != ArcType()) return nullptr;
FarReaderClassImpl<Arc> *typed_impl =
static_cast<FarReaderClassImpl<Arc> *>(impl_.get());
return typed_impl->GetImpl();
}
template <class Arc>
friend void OpenFarReaderClass(OpenFarReaderClassArgs1 *args);
template <class Arc>
friend void OpenFarReaderClass(OpenFarReaderClassArgs2 *args);
// Defined in the CC.
static FarReaderClass *Open(const string &filename);
static FarReaderClass *Open(const std::vector<string> &filenames);
private:
template <class Arc>
explicit FarReaderClass(FarReaderClassImpl<Arc> *impl) : impl_(impl) {}
std::unique_ptr<FarReaderImplBase> impl_;
};
// These exist solely for registration purposes; users should call the
// static method FarReaderClass::Open instead.
template <class Arc>
void OpenFarReaderClass(OpenFarReaderClassArgs1 *args) {
args->retval = new FarReaderClass(new FarReaderClassImpl<Arc>(args->args));
}
template <class Arc>
void OpenFarReaderClass(OpenFarReaderClassArgs2 *args) {
args->retval = new FarReaderClass(new FarReaderClassImpl<Arc>(args->args));
}
// FarWriter API.
// Virtual interface implemented by each concrete FarWriterImpl<A>.
class FarWriterImplBase {
public:
// Unlike the lower-level library, this returns a boolean to signal failure
// due to non-conformant arc types.
virtual bool Add(const string &key, const FstClass &fst) = 0;
virtual const string &ArcType() const = 0;
virtual bool Error() const = 0;
virtual FarType Type() const = 0;
virtual ~FarWriterImplBase() {}
};
// Templated implementation.
template <class Arc>
class FarWriterClassImpl : public FarWriterImplBase {
public:
explicit FarWriterClassImpl(const string &filename,
FarType type = FAR_DEFAULT)
: impl_(FarWriter<Arc>::Create(filename, type)) {}
bool Add(const string &key, const FstClass &fst) final {
if (ArcType() != fst.ArcType()) {
FSTERROR() << "Cannot write FST with " << fst.ArcType() << " arcs to "
<< "FAR with " << ArcType() << " arcs";
return false;
}
impl_->Add(key, *(fst.GetFst<Arc>()));
return true;
}
const string &ArcType() const final { return Arc::Type(); }
bool Error() const final { return impl_->Error(); }
FarType Type() const final { return impl_->Type(); }
const FarWriter<Arc> *GetImpl() const { return impl_.get(); }
FarWriter<Arc> *GetImpl() { return impl_.get(); }
private:
std::unique_ptr<FarWriter<Arc>> impl_;
};
class FarWriterClass;
using CreateFarWriterClassInnerArgs = std::pair<const string &, FarType>;
using CreateFarWriterClassArgs =
WithReturnValue<FarWriterClass *, CreateFarWriterClassInnerArgs>;
// Untemplated user-facing class holding a templated pimpl.
class FarWriterClass {
public:
static FarWriterClass *Create(const string &filename, const string &arc_type,
FarType type = FAR_DEFAULT);
bool Add(const string &key, const FstClass &fst) {
return impl_->Add(key, fst);
}
// Returns True if the impl is null (i.e., due to construction failure).
// Attempting to call any other function will result in null dereference.
bool Error() const { return (impl_) ? impl_->Error() : true; }
const string &ArcType() const { return impl_->ArcType(); }
FarType Type() const { return impl_->Type(); }
template <class Arc>
const FarWriter<Arc> *GetFarWriter() const {
if (Arc::Type() != ArcType()) return nullptr;
const FarWriterClassImpl<Arc> *typed_impl =
static_cast<FarWriterClassImpl<Arc> *>(impl_.get());
return typed_impl->GetImpl();
}
template <class Arc>
FarWriter<Arc> *GetFarWriter() {
if (Arc::Type() != ArcType()) return nullptr;
FarWriterClassImpl<Arc> *typed_impl =
static_cast<FarWriterClassImpl<Arc> *>(impl_.get());
return typed_impl->GetImpl();
}
template <class Arc>
friend void CreateFarWriterClass(CreateFarWriterClassArgs *args);
private:
template <class Arc>
explicit FarWriterClass(FarWriterClassImpl<Arc> *impl) : impl_(impl) {}
std::unique_ptr<FarWriterImplBase> impl_;
};
// This exists solely for registration purposes; users should call the
// static method FarWriterClass::Create instead.
template <class Arc>
void CreateFarWriterClass(CreateFarWriterClassArgs *args) {
args->retval = new FarWriterClass(new FarWriterClassImpl<Arc>(
std::get<0>(args->args), std::get<1>(args->args)));
}
} // namespace script
} // namespace fst
#endif // FST_EXTENSIONS_FAR_FAR_CLASS_H_