FST  openfst-1.7.1
OpenFst Library
encodemapper-class.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 
4 #ifndef FST_SCRIPT_ENCODEMAPPER_CLASS_H_
5 #define FST_SCRIPT_ENCODEMAPPER_CLASS_H_
6 
7 #include <memory>
8 #include <string>
9 #include <iostream>
10 
11 #include <fst/fstlib.h>
12 #include <fst/script/arc-class.h>
13 #include <fst/script/fst-class.h>
14 
15 // Scripting API support for EncodeMapper.
16 
17 namespace fst {
18 namespace script {
19 
20 // Virtual interface implemented by each concrete EncodeMapperClassImpl<A>.
22  public:
23  // Returns an encoded ArcClass.
24  virtual ArcClass operator()(const ArcClass &a) = 0;
25  virtual const string &ArcType() const = 0;
26  virtual uint32 Flags() const = 0;
27  virtual uint64 Properties(uint64 inprops) = 0;
28  virtual EncodeType Type() const = 0;
29  virtual const SymbolTable *InputSymbols() const = 0;
30  virtual const SymbolTable *OutputSymbols() const = 0;
31  virtual void SetInputSymbols(const SymbolTable *syms) = 0;
32  virtual void SetOutputSymbols(const SymbolTable *syms) = 0;
33  virtual const string &WeightType() const = 0;
34  virtual ~EncodeMapperImplBase() {}
35 };
36 
37 // Templated implementation.
38 template <class Arc>
40  public:
42  : encoder_(flags, type) {}
43 
44  ArcClass operator()(const ArcClass &a) final;
45 
46  const string &ArcType() const final { return Arc::Type(); }
47 
48  uint32 Flags() const final { return encoder_.Flags(); }
49 
50  uint64 Properties(uint64 inprops) final {
51  return encoder_.Properties(inprops);
52  }
53 
54  EncodeType Type() const final { return encoder_.Type(); }
55 
56  const SymbolTable *InputSymbols() const final {
57  return encoder_.InputSymbols();
58  }
59 
60  const SymbolTable *OutputSymbols() const final {
61  return encoder_.OutputSymbols();
62  }
63 
64  void SetInputSymbols(const SymbolTable *syms) final {
65  encoder_.SetInputSymbols(syms);
66  }
67 
68  void SetOutputSymbols(const SymbolTable *syms) final {
69  encoder_.SetOutputSymbols(syms);
70  }
71 
72  const string &WeightType() const final { return Arc::Weight::Type(); }
73 
74  ~EncodeMapperClassImpl() override {}
75 
76  EncodeMapper<Arc> *GetImpl() const { return &encoder_; }
77 
78  EncodeMapper<Arc> *GetImpl() { return &encoder_; }
79 
80  private:
81  EncodeMapper<Arc> encoder_;
82 };
83 
84 // This is returned by value because it is very likely to undergo return-value
85 // optimization.
86 template <class Arc>
88  Arc arc(a.ilabel, a.olabel, *(a.weight.GetWeight<typename Arc::Weight>()),
89  a.nextstate);
90  return ArcClass(encoder_(arc));
91 }
92 
93 class EncodeMapperClass;
94 
96  std::tuple<uint32, EncodeType, EncodeMapperClass *>;
97 
99  public:
100  EncodeMapperClass(const string &arc_type, uint32 flags, EncodeType type);
101 
102  template <class Arc>
104  : impl_(new EncodeMapperClassImpl<Arc>(flags, type)) {}
105 
106  ArcClass operator()(const ArcClass &arc) { return (*impl_)(arc); }
107 
108  const string &ArcType() const { return impl_->ArcType(); }
109 
110  uint32 Flags() const { return impl_->Flags(); }
111 
112  uint64 Properties(uint64 inprops) { return impl_->Properties(inprops); }
113 
114  EncodeType Type() const { return impl_->Type(); }
115 
116  const SymbolTable *InputSymbols() const { return impl_->InputSymbols(); }
117 
118  const SymbolTable *OutputSymbols() const { return impl_->OutputSymbols(); }
119 
120  void SetInputSymbols(const SymbolTable *syms) {
121  impl_->SetInputSymbols(syms);
122  }
123 
124  void SetOutputSymbols(const SymbolTable *syms) {
125  impl_->SetOutputSymbols(syms);
126  }
127 
128  const string &WeightType() const { return impl_->WeightType(); }
129 
130  template <class Arc>
132 
133  // Naturally, this exists in non-const and const forms. Encoding arcs or FSTs
134  // mutates the underlying encoder; decoding them does not.
135 
136  template <class Arc>
138  if (Arc::Type() != ArcType()) {
139  return nullptr;
140  } else {
141  auto *typed_impl = static_cast<EncodeMapperClassImpl<Arc> *>(impl_.get());
142  return typed_impl->GetImpl();
143  }
144  }
145 
146  template <class Arc>
148  if (Arc::Type() != ArcType()) {
149  return nullptr;
150  } else {
151  auto *typed_impl = static_cast<EncodeMapperClassImpl<Arc> *>(impl_.get());
152  return typed_impl->GetImpl();
153  }
154  }
155 
156  private:
157  std::unique_ptr<EncodeMapperImplBase> impl_;
158 };
159 
160 template <class Arc>
162  std::get<2>(*args)->impl_.reset(
163  new EncodeMapperClassImpl<Arc>(std::get<0>(*args), std::get<1>(*args)));
164 }
165 
166 } // namespace script
167 } // namespace fst
168 
169 #endif // FST_SCRIPT_ENCODEMAPPER_CLASS_H_
ArcClass operator()(const ArcClass &a) final
std::tuple< uint32, EncodeType, EncodeMapperClass * > InitEncodeMapperClassArgs
void SetOutputSymbols(const SymbolTable *syms)
EncodeMapper< Arc > * GetImpl() const
virtual const SymbolTable * OutputSymbols() const =0
uint64_t uint64
Definition: types.h:32
const string & ArcType() const final
virtual const string & ArcType() const =0
virtual const SymbolTable * InputSymbols() const =0
EncodeMapperClass(uint32 flags, EncodeType type)
ArcClass operator()(const ArcClass &arc)
const string & ArcType() const
virtual void SetOutputSymbols(const SymbolTable *syms)=0
void SetOutputSymbols(const SymbolTable *syms) final
const SymbolTable * OutputSymbols() const final
const SymbolTable * InputSymbols() const
const EncodeMapper< Arc > * GetEncodeMapper() const
EncodeType
Definition: encode.h:25
virtual const string & WeightType() const =0
virtual void SetInputSymbols(const SymbolTable *syms)=0
void InitEncodeMapperClass(InitEncodeMapperClassArgs *args)
const string & WeightType() const
void SetInputSymbols(const SymbolTable *syms) final
virtual EncodeType Type() const =0
const SymbolTable * InputSymbols() const final
const W * GetWeight() const
Definition: weight-class.h:133
uint32_t uint32
Definition: types.h:31
virtual ArcClass operator()(const ArcClass &a)=0
EncodeMapperClassImpl(uint32 flags, EncodeType type)
virtual uint32 Flags() const =0
virtual uint64 Properties(uint64 inprops)=0
const SymbolTable * OutputSymbols() const
WeightClass weight
Definition: arc-class.h:33
const string & WeightType() const final
void SetInputSymbols(const SymbolTable *syms)
uint64 Properties(uint64 inprops) final
uint64 Properties(uint64 inprops)
EncodeMapper< Arc > * GetEncodeMapper()