FST  openfst-1.8.2.post1
OpenFst Library
encodemapper-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 #ifndef FST_SCRIPT_ENCODEMAPPER_CLASS_H_
19 #define FST_SCRIPT_ENCODEMAPPER_CLASS_H_
20 
21 #include <cstdint>
22 #include <iostream>
23 #include <memory>
24 #include <string>
25 #include <utility>
26 
27 #include <fst/encode.h>
28 #include <fst/generic-register.h>
29 #include <fst/script/arc-class.h>
30 #include <fst/script/fst-class.h>
31 #include <string_view>
32 
33 // Scripting API support for EncodeMapper.
34 
35 namespace fst {
36 namespace script {
37 
38 // Virtual interface implemented by each concrete EncodeMapperClassImpl<Arc>.
40  public:
41  // Returns an encoded ArcClass.
42  virtual ArcClass operator()(const ArcClass &) = 0;
43  virtual const std::string &ArcType() const = 0;
44  virtual const std::string &WeightType() const = 0;
45  virtual EncodeMapperImplBase *Copy() const = 0;
46  virtual uint8_t Flags() const = 0;
47  virtual uint64_t Properties(uint64_t) = 0;
48  virtual EncodeType Type() const = 0;
49  virtual bool Write(const std::string &) const = 0;
50  virtual bool Write(std::ostream &, const std::string &) const = 0;
51  virtual const SymbolTable *InputSymbols() const = 0;
52  virtual const SymbolTable *OutputSymbols() const = 0;
53  virtual void SetInputSymbols(const SymbolTable *) = 0;
54  virtual void SetOutputSymbols(const SymbolTable *) = 0;
55  virtual ~EncodeMapperImplBase() {}
56 };
57 
58 // Templated implementation.
59 template <class Arc>
61  public:
62  explicit EncodeMapperClassImpl(const EncodeMapper<Arc> &mapper)
63  : mapper_(mapper) {}
64 
65  ArcClass operator()(const ArcClass &a) final;
66 
67  const std::string &ArcType() const final { return Arc::Type(); }
68 
69  const std::string &WeightType() const final { return Arc::Weight::Type(); }
70 
72  return new EncodeMapperClassImpl<Arc>(mapper_);
73  }
74 
75  uint8_t Flags() const final { return mapper_.Flags(); }
76 
77  uint64_t Properties(uint64_t inprops) final {
78  return mapper_.Properties(inprops);
79  }
80 
81  EncodeType Type() const final { return mapper_.Type(); }
82 
83  bool Write(const std::string &source) const final {
84  return mapper_.Write(source);
85  }
86 
87  bool Write(std::ostream &strm, const std::string &source) const final {
88  return mapper_.Write(strm, source);
89  }
90 
91  const SymbolTable *InputSymbols() const final {
92  return mapper_.InputSymbols();
93  }
94 
95  const SymbolTable *OutputSymbols() const final {
96  return mapper_.OutputSymbols();
97  }
98 
99  void SetInputSymbols(const SymbolTable *syms) final {
100  mapper_.SetInputSymbols(syms);
101  }
102 
103  void SetOutputSymbols(const SymbolTable *syms) final {
104  mapper_.SetOutputSymbols(syms);
105  }
106 
108 
109  const EncodeMapper<Arc> *GetImpl() const { return &mapper_; }
110 
111  EncodeMapper<Arc> *GetImpl() { return &mapper_; }
112 
113  private:
114  EncodeMapper<Arc> mapper_;
115 };
116 
117 template <class Arc>
119  const Arc arc(a.ilabel, a.olabel,
120  *(a.weight.GetWeight<typename Arc::Weight>()), a.nextstate);
121  return ArcClass(mapper_(arc));
122 }
123 
125  public:
126  EncodeMapperClass() : impl_(nullptr) {}
127 
128  EncodeMapperClass(std::string_view arc_type, uint8_t flags,
129  EncodeType type = ENCODE);
130 
131  template <class Arc>
132  explicit EncodeMapperClass(const EncodeMapper<Arc> &mapper)
133  : impl_(std::make_unique<EncodeMapperClassImpl<Arc>>(mapper)) {}
134 
136  : impl_(other.impl_ == nullptr ? nullptr : other.impl_->Copy()) {}
137 
139  impl_.reset(other.impl_ == nullptr ? nullptr : other.impl_->Copy());
140  return *this;
141  }
142 
143  ArcClass operator()(const ArcClass &arc) { return (*impl_)(arc); }
144 
145  const std::string &ArcType() const { return impl_->ArcType(); }
146 
147  const std::string &WeightType() const { return impl_->WeightType(); }
148 
149  uint8_t Flags() const { return impl_->Flags(); }
150 
151  uint64_t Properties(uint64_t inprops) { return impl_->Properties(inprops); }
152 
153  EncodeType Type() const { return impl_->Type(); }
154 
155  static std::unique_ptr<EncodeMapperClass> Read(const std::string &source);
156 
157  static std::unique_ptr<EncodeMapperClass> Read(std::istream &strm,
158  const std::string &source);
159 
160  bool Write(const std::string &source) const { return impl_->Write(source); }
161 
162  bool Write(std::ostream &strm, const std::string &source) const {
163  return impl_->Write(strm, source);
164  }
165 
166  const SymbolTable *InputSymbols() const { return impl_->InputSymbols(); }
167 
168  const SymbolTable *OutputSymbols() const { return impl_->OutputSymbols(); }
169 
170  void SetInputSymbols(const SymbolTable *syms) {
171  impl_->SetInputSymbols(syms);
172  }
173 
174  void SetOutputSymbols(const SymbolTable *syms) {
175  impl_->SetOutputSymbols(syms);
176  }
177 
178  // Implementation stuff.
179 
180  template <class Arc>
182  if (Arc::Type() != ArcType()) {
183  return nullptr;
184  } else {
185  auto *typed_impl = down_cast<EncodeMapperClassImpl<Arc> *>(impl_.get());
186  return typed_impl->GetImpl();
187  }
188  }
189 
190  template <class Arc>
192  if (Arc::Type() != ArcType()) {
193  return nullptr;
194  } else {
195  auto *typed_impl = down_cast<EncodeMapperClassImpl<Arc> *>(impl_.get());
196  return typed_impl->GetImpl();
197  }
198  }
199 
200  // Required for registration.
201 
202  template <class Arc>
203  static std::unique_ptr<EncodeMapperClass> Read(std::istream &strm,
204  const std::string &source) {
205  std::unique_ptr<EncodeMapper<Arc>> mapper(
206  EncodeMapper<Arc>::Read(strm, source));
207  return mapper ? std::make_unique<EncodeMapperClass>(*mapper) : nullptr;
208  }
209 
210  template <class Arc>
211  static std::unique_ptr<EncodeMapperImplBase> Create(
212  uint8_t flags, EncodeType type = ENCODE) {
213  return std::make_unique<EncodeMapperClassImpl<Arc>>(
214  EncodeMapper<Arc>(flags, type));
215  }
216 
217  private:
218  explicit EncodeMapperClass(std::unique_ptr<EncodeMapperImplBase> impl)
219  : impl_(std::move(impl)) {}
220 
221  const EncodeMapperImplBase *GetImpl() const { return impl_.get(); }
222 
223  EncodeMapperImplBase *GetImpl() { return impl_.get(); }
224 
225  std::unique_ptr<EncodeMapperImplBase> impl_;
226 };
227 
228 // Registration for EncodeMapper types.
229 
230 // This class definition is to avoid a nested class definition inside the
231 // EncodeMapperIORegistration struct.
232 
233 template <class Reader, class Creator>
235  Reader reader;
236  Creator creator;
237 
238  EncodeMapperClassRegEntry(Reader reader, Creator creator)
239  : reader(reader), creator(creator) {}
240 
241  EncodeMapperClassRegEntry() : reader(nullptr), creator(nullptr) {}
242 };
243 
244 template <class Reader, class Creator>
246  : public GenericRegister<std::string,
247  EncodeMapperClassRegEntry<Reader, Creator>,
248  EncodeMapperClassIORegister<Reader, Creator>> {
249  public:
250  Reader GetReader(std::string_view arc_type) const {
251  return this->GetEntry(arc_type).reader;
252  }
253 
254  Creator GetCreator(std::string_view arc_type) const {
255  return this->GetEntry(arc_type).creator;
256  }
257 
258  protected:
259  std::string ConvertKeyToSoFilename(std::string_view key) const final {
260  std::string legal_type(key);
261  ConvertToLegalCSymbol(&legal_type);
262  legal_type.append("-arc.so");
263  return legal_type;
264  }
265 };
266 
267 // Struct containing everything needed to register a particular type
269  using Reader = std::unique_ptr<EncodeMapperClass> (*)(
270  std::istream &stream, const std::string &source);
271 
272  using Creator = std::unique_ptr<EncodeMapperImplBase> (*)(uint8_t flags,
273  EncodeType type);
274 
276 
277  // EncodeMapper register.
279 
280  // EncodeMapper register-er.
281  using Registerer =
283 };
284 
285 #define REGISTER_ENCODEMAPPER_CLASS(Arc) \
286  static EncodeMapperClassIORegistration::Registerer \
287  EncodeMapperClass_##Arc##_registerer( \
288  Arc::Type(), \
289  EncodeMapperClassIORegistration::Entry( \
290  EncodeMapperClass::Read<Arc>, EncodeMapperClass::Create<Arc>));
291 
292 } // namespace script
293 } // namespace fst
294 
295 #endif // FST_SCRIPT_ENCODEMAPPER_CLASS_H_
ArcClass operator()(const ArcClass &a) final
std::unique_ptr< EncodeMapperImplBase >(*)(uint8_t flags, EncodeType type) Creator
std::unique_ptr< EncodeMapperClass >(*)(std::istream &stream, const std::string &source) Reader
void ConvertToLegalCSymbol(std::string *s)
Definition: util.cc:71
bool Write(const std::string &source) const final
void SetOutputSymbols(const SymbolTable *syms)
virtual const SymbolTable * OutputSymbols() const =0
bool Write(std::ostream &strm, const std::string &source) const
virtual const std::string & ArcType() const =0
EncodeMapperClass(const EncodeMapperClass &other)
const std::string & WeightType() const final
uint64_t Properties(uint64_t inprops) final
virtual const SymbolTable * InputSymbols() const =0
bool Write(std::ostream &strm, const std::string &source) const final
ArcClass operator()(const ArcClass &arc)
const std::string & ArcType() const
void SetOutputSymbols(const SymbolTable *syms) final
const SymbolTable * OutputSymbols() const final
To down_cast(From *f)
Definition: compat.h:50
Reader GetReader(std::string_view arc_type) const
Creator GetCreator(std::string_view arc_type) const
const SymbolTable * InputSymbols() const
const EncodeMapper< Arc > * GetEncodeMapper() const
virtual const std::string & WeightType() const =0
EncodeType
Definition: encode.h:41
EncodeMapperClass(const EncodeMapper< Arc > &mapper)
static std::unique_ptr< EncodeMapperClass > Read(std::istream &strm, const std::string &source)
std::string ConvertKeyToSoFilename(std::string_view key) const final
virtual void SetInputSymbols(const SymbolTable *)=0
EncodeMapperClass & operator=(const EncodeMapperClass &other)
virtual uint8_t Flags() const =0
void SetInputSymbols(const SymbolTable *syms) final
virtual EncodeType Type() const =0
virtual EncodeMapperImplBase * Copy() const =0
const SymbolTable * InputSymbols() const final
virtual ArcClass operator()(const ArcClass &)=0
EncodeMapperClassRegEntry(Reader reader, Creator creator)
const W * GetWeight() const
Definition: weight-class.h:143
static std::unique_ptr< EncodeMapperImplBase > Create(uint8_t flags, EncodeType type=ENCODE)
uint64_t Properties(uint64_t inprops)
EncodeMapperClassImpl< Arc > * Copy() const final
virtual bool Write(const std::string &) const =0
const SymbolTable * OutputSymbols() const
WeightClass weight
Definition: arc-class.h:51
virtual uint64_t Properties(uint64_t)=0
void SetInputSymbols(const SymbolTable *syms)
const std::string & ArcType() const final
bool Write(const std::string &source) const
EncodeMapper< Arc > * GetEncodeMapper()
EncodeMapperClassImpl(const EncodeMapper< Arc > &mapper)
virtual void SetOutputSymbols(const SymbolTable *)=0
const std::string & WeightType() const
const EncodeMapper< Arc > * GetImpl() const