FST  openfst-1.8.2
OpenFst Library
far-class.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 // Scripting API support for FarReader and FarWriter.
19 
20 #ifndef FST_EXTENSIONS_FAR_FAR_CLASS_H_
21 #define FST_EXTENSIONS_FAR_FAR_CLASS_H_
22 
23 #include <memory>
24 #include <string>
25 #include <vector>
26 
27 #include <fst/extensions/far/far.h>
28 #include <fst/script/arg-packs.h>
29 #include <fst/script/fstscript.h>
30 
31 namespace fst {
32 namespace script {
33 
34 // FarReader API.
35 
36 // Virtual interface implemented by each concrete FarReaderImpl<A>.
37 // See the FarReader interface in far.h for the exact semantics.
39  public:
40  virtual const std::string &ArcType() const = 0;
41  virtual bool Done() const = 0;
42  virtual bool Error() const = 0;
43  virtual const std::string &GetKey() const = 0;
44  virtual const FstClass *GetFstClass() const = 0;
45  virtual bool Find(const std::string &key) = 0;
46  virtual void Next() = 0;
47  virtual void Reset() = 0;
48  virtual FarType Type() const = 0;
49  virtual ~FarReaderImplBase() {}
50 };
51 
52 // Templated implementation.
53 template <class Arc>
55  public:
56  explicit FarReaderClassImpl(const std::string &source)
57  : reader_(FarReader<Arc>::Open(source)) {}
58 
59  explicit FarReaderClassImpl(const std::vector<std::string> &sources)
60  : reader_(FarReader<Arc>::Open(sources)) {}
61 
62  const std::string &ArcType() const final { return Arc::Type(); }
63 
64  bool Done() const final { return reader_->Done(); }
65 
66  bool Error() const final { return reader_->Error(); }
67 
68  bool Find(const std::string &key) final { return reader_->Find(key); }
69 
70  const FstClass *GetFstClass() const final {
71  fstc_ = std::make_unique<FstClass>(*reader_->GetFst());
72  return fstc_.get();
73  }
74 
75  const std::string &GetKey() const final { return reader_->GetKey(); }
76 
77  void Next() final { return reader_->Next(); }
78 
79  void Reset() final { reader_->Reset(); }
80 
81  FarType Type() const final { return reader_->Type(); }
82 
83  const FarReader<Arc> *GetFarReader() const { return reader_.get(); }
84 
85  FarReader<Arc> *GetFarReader() { return reader_.get(); }
86 
87  private:
88  std::unique_ptr<FarReader<Arc>> reader_;
89  mutable std::unique_ptr<FstClass> fstc_;
90 };
91 
92 class FarReaderClass;
93 
96  const std::vector<std::string> &>;
97 
98 // Untemplated user-facing class holding a templated pimpl.
100  public:
101  const std::string &ArcType() const { return impl_->ArcType(); }
102 
103  bool Done() const { return impl_->Done(); }
104 
105  // Returns True if the impl is null (i.e., due to read failure).
106  // Attempting to call any other function will result in null dereference.
107  bool Error() const { return (impl_) ? impl_->Error() : true; }
108 
109  bool Find(const std::string &key) { return impl_->Find(key); }
110 
111  const FstClass *GetFstClass() const { return impl_->GetFstClass(); }
112 
113  const std::string &GetKey() const { return impl_->GetKey(); }
114 
115  void Next() { impl_->Next(); }
116 
117  void Reset() { impl_->Reset(); }
118 
119  FarType Type() const { return impl_->Type(); }
120 
121  template <class Arc>
122  const FarReader<Arc> *GetFarReader() const {
123  if (Arc::Type() != ArcType()) return nullptr;
124  const FarReaderClassImpl<Arc> *typed_impl =
125  down_cast<FarReaderClassImpl<Arc> *>(impl_.get());
126  return typed_impl->GetFarReader();
127  }
128 
129  template <class Arc>
131  if (Arc::Type() != ArcType()) return nullptr;
132  FarReaderClassImpl<Arc> *typed_impl =
133  down_cast<FarReaderClassImpl<Arc> *>(impl_.get());
134  return typed_impl->GetFarReader();
135  }
136 
137  template <class Arc>
138  friend void OpenFarReaderClass(OpenFarReaderClassArgs *args);
139 
140  // Defined in the CC.
141 
142  static std::unique_ptr<FarReaderClass> Open(const std::string &source);
143 
144  static std::unique_ptr<FarReaderClass> Open(
145  const std::vector<std::string> &sources);
146 
147  private:
148  template <class Arc>
149  explicit FarReaderClass(std::unique_ptr<FarReaderClassImpl<Arc>> impl)
150  : impl_(std::move(impl)) {}
151 
152  std::unique_ptr<FarReaderImplBase> impl_;
153 };
154 
155 // These exist solely for registration purposes; users should call the
156 // static method FarReaderClass::Open instead.
157 
158 template <class Arc>
160  auto impl = std::make_unique<FarReaderClassImpl<Arc>>(args->args);
161  if (impl->GetFarReader() == nullptr) {
162  // Underlying reader failed to open, so return failure here, too.
163  args->retval = nullptr;
164  } else {
165  args->retval = fst::WrapUnique(new FarReaderClass(std::move(impl)));
166  }
167 }
168 
169 // FarWriter API.
170 
171 // Virtual interface implemented by each concrete FarWriterImpl<A>.
173  public:
174  // Unlike the lower-level library, this returns a boolean to signal failure
175  // due to non-conformant arc types.
176  virtual bool Add(const std::string &key, const FstClass &fst) = 0;
177  virtual const std::string &ArcType() const = 0;
178  virtual bool Error() const = 0;
179  virtual FarType Type() const = 0;
180  virtual ~FarWriterImplBase() {}
181 };
182 
183 // Templated implementation.
184 template <class Arc>
186  public:
187  explicit FarWriterClassImpl(const std::string &source,
188  FarType type = FarType::DEFAULT)
189  : writer_(FarWriter<Arc>::Create(source, type)) {}
190 
191  bool Add(const std::string &key, const FstClass &fst) final {
192  if (ArcType() != fst.ArcType()) {
193  FSTERROR() << "Cannot write FST with " << fst.ArcType() << " arcs to "
194  << "FAR with " << ArcType() << " arcs";
195  return false;
196  }
197  writer_->Add(key, *(fst.GetFst<Arc>()));
198  return true;
199  }
200 
201  const std::string &ArcType() const final { return Arc::Type(); }
202 
203  bool Error() const final { return writer_->Error(); }
204 
205  FarType Type() const final { return writer_->Type(); }
206 
207  const FarWriter<Arc> *GetFarWriter() const { return writer_.get(); }
208 
209  FarWriter<Arc> *GetFarWriter() { return writer_.get(); }
210 
211  private:
212  std::unique_ptr<FarWriter<Arc>> writer_;
213 };
214 
215 class FarWriterClass;
216 
217 using CreateFarWriterClassInnerArgs = std::pair<const std::string &, FarType>;
218 
222 
223 // Untemplated user-facing class holding a templated pimpl.
225  public:
226  static std::unique_ptr<FarWriterClass> Create(
227  const std::string &source, const std::string &arc_type,
228  FarType type = FarType::DEFAULT);
229 
230  bool Add(const std::string &key, const FstClass &fst) {
231  return impl_->Add(key, fst);
232  }
233 
234  // Returns True if the impl is null (i.e., due to construction failure).
235  // Attempting to call any other function will result in null dereference.
236  bool Error() const { return (impl_) ? impl_->Error() : true; }
237 
238  const std::string &ArcType() const { return impl_->ArcType(); }
239 
240  FarType Type() const { return impl_->Type(); }
241 
242  template <class Arc>
243  const FarWriter<Arc> *GetFarWriter() const {
244  if (Arc::Type() != ArcType()) return nullptr;
245  const FarWriterClassImpl<Arc> *typed_impl =
246  down_cast<FarWriterClassImpl<Arc> *>(impl_.get());
247  return typed_impl->GetFarWriter();
248  }
249 
250  template <class Arc>
252  if (Arc::Type() != ArcType()) return nullptr;
253  FarWriterClassImpl<Arc> *typed_impl =
254  down_cast<FarWriterClassImpl<Arc> *>(impl_.get());
255  return typed_impl->GetFarWriter();
256  }
257 
258  template <class Arc>
260 
261  private:
262  template <class Arc>
263  explicit FarWriterClass(std::unique_ptr<FarWriterClassImpl<Arc>> impl)
264  : impl_(std::move(impl)) {}
265 
266  std::unique_ptr<FarWriterImplBase> impl_;
267 };
268 
269 // This exists solely for registration purposes; users should call the
270 // static method FarWriterClass::Create instead.
271 template <class Arc>
273  args->retval = fst::WrapUnique(
274  new FarWriterClass(std::make_unique<FarWriterClassImpl<Arc>>(
275  std::get<0>(args->args), std::get<1>(args->args))));
276 }
277 
278 } // namespace script
279 } // namespace fst
280 
281 #endif // FST_EXTENSIONS_FAR_FAR_CLASS_H_
FarType Type() const final
Definition: far-class.h:81
const FarReader< Arc > * GetFarReader() const
Definition: far-class.h:122
bool Add(const std::string &key, const FstClass &fst)
Definition: far-class.h:230
FarReader< Arc > * GetFarReader()
Definition: far-class.h:85
bool Add(const std::string &key, const FstClass &fst) final
Definition: far-class.h:191
bool Done() const final
Definition: far-class.h:64
const std::string & ArcType() const final
Definition: far-class.h:201
virtual bool Error() const =0
const FstClass * GetFstClass() const
Definition: far-class.h:111
To down_cast(From *f)
Definition: compat.h:50
std::unique_ptr< T > WrapUnique(T *ptr)
Definition: compat.h:125
FarReader< Arc > * GetFarReader()
Definition: far-class.h:130
FarReaderClassImpl(const std::vector< std::string > &sources)
Definition: far-class.h:59
#define FSTERROR()
Definition: util.h:53
FarType Type() const
Definition: far-class.h:240
const std::string & ArcType() const final
Definition: far-class.h:62
bool Error() const final
Definition: far-class.h:203
FarReaderClassImpl(const std::string &source)
Definition: far-class.h:56
std::pair< const std::string &, FarType > CreateFarWriterClassInnerArgs
Definition: far-class.h:217
FarType
Definition: far.h:43
void OpenFarReaderClass(OpenFarReaderClassArgs *args)
Definition: far-class.h:159
bool Find(const std::string &key) final
Definition: far-class.h:68
const std::string & ArcType() const
Definition: far-class.h:238
FarWriter< Arc > * GetFarWriter()
Definition: far-class.h:209
const std::string & GetKey() const
Definition: far-class.h:113
virtual const FstClass * GetFstClass() const =0
virtual bool Find(const std::string &key)=0
virtual FarType Type() const =0
FarType Type() const
Definition: far-class.h:119
FarType Type() const final
Definition: far-class.h:205
bool Error() const final
Definition: far-class.h:66
FarWriterClassImpl(const std::string &source, FarType type=FarType::DEFAULT)
Definition: far-class.h:187
const FarReader< Arc > * GetFarReader() const
Definition: far-class.h:83
void CreateFarWriterClass(CreateFarWriterClassArgs *args)
Definition: far-class.h:272
const FarWriter< Arc > * GetFarWriter() const
Definition: far-class.h:243
bool Find(const std::string &key)
Definition: far-class.h:109
FarWriter< Arc > * GetFarWriter()
Definition: far-class.h:251
void Create(const std::vector< std::string > &sources, FarWriterClass &writer, const int32_t generate_keys, const std::string &key_prefix, const std::string &key_suffix)
Definition: farscript.cc:72
virtual const std::string & ArcType() const =0
virtual const std::string & GetKey() const =0
const FarWriter< Arc > * GetFarWriter() const
Definition: far-class.h:207
const std::string & ArcType() const
Definition: far-class.h:101
virtual bool Done() const =0
const std::string & GetKey() const final
Definition: far-class.h:75
const FstClass * GetFstClass() const final
Definition: far-class.h:70