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