FST  openfst-1.8.3
OpenFst Library
encode.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 // Class to encode and decode an FST.
19 
20 #ifndef FST_ENCODE_H_
21 #define FST_ENCODE_H_
22 
23 #include <climits>
24 #include <cstddef>
25 #include <cstdint>
26 #include <ios>
27 #include <iostream>
28 #include <istream>
29 #include <memory>
30 #include <ostream>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 #include <fst/log.h>
36 #include <fst/arc-map.h>
37 #include <fst/arc.h>
38 #include <fst/cache.h>
39 #include <fstream>
40 #include <fst/float-weight.h>
41 #include <fst/fst.h>
42 #include <fst/impl-to-fst.h>
43 #include <fst/mutable-fst.h>
44 #include <fst/properties.h>
45 #include <fst/rmfinalepsilon.h>
46 #include <fst/symbol-table.h>
47 #include <fst/util.h>
48 #include <unordered_map>
49 #include <string_view>
50 
51 namespace fst {
52 
53 enum EncodeType { ENCODE = 1, DECODE = 2 };
54 
55 inline constexpr uint8_t kEncodeLabels = 0x01;
56 inline constexpr uint8_t kEncodeWeights = 0x02;
57 inline constexpr uint8_t kEncodeFlags = 0x03;
58 
59 namespace internal {
60 
61 // Bits storing whether or not an encode table has input and/or output symbol
62 // tables, for internal use only.
63 inline constexpr uint8_t kEncodeHasISymbols = 0x04;
64 inline constexpr uint8_t kEncodeHasOSymbols = 0x08;
65 
66 // Identifies stream data as an encode table (and its endianity).
67 inline constexpr int32_t kEncodeMagicNumber = 2128178506;
68 // TODO(b/141172858): deprecated, remove by 2020-01-01.
69 inline constexpr int32_t kEncodeDeprecatedMagicNumber = 2129983209;
70 
71 } // namespace internal
72 
73 // Header for the encoder table.
75  public:
76  EncodeTableHeader() = default;
77 
78  // Getters.
79 
80  const std::string &ArcType() const { return arctype_; }
81 
82  uint8_t Flags() const { return flags_; }
83 
84  size_t Size() const { return size_; }
85 
86  // Setters.
87 
88  void SetArcType(std::string_view arctype) {
89  arctype_ = std::string(arctype);
90  }
91 
92  void SetFlags(uint8_t flags) { flags_ = flags; }
93 
94  void SetSize(size_t size) { size_ = size; }
95 
96  // IO.
97 
98  bool Read(std::istream &strm, std::string_view source);
99 
100  bool Write(std::ostream &strm, std::string_view source) const;
101 
102  private:
103  std::string arctype_;
104  uint8_t flags_;
105  size_t size_;
106 };
107 
108 namespace internal {
109 
110 // The following class encapsulates implementation details for the encoding and
111 // decoding of label/weight triples used for encoding and decoding of FSTs. The
112 // EncodeTable is bidirectional, i.e, it stores both the Triple of encode labels
113 // and weights to a unique label, and the reverse.
114 template <class Arc>
115 class EncodeTable {
116  public:
117  using Label = typename Arc::Label;
118  using Weight = typename Arc::Weight;
119 
120  // Encoded data consists of arc input/output labels and arc weight.
121  struct Triple {
122  Triple() = default;
123 
124  Triple(Label ilabel, Label olabel, Weight weight)
125  : ilabel(ilabel), olabel(olabel), weight(std::move(weight)) {}
126 
127  // Constructs from arc and flags.
128  Triple(const Arc &arc, uint8_t flags)
129  : ilabel(arc.ilabel),
130  olabel(flags & kEncodeLabels ? arc.olabel : 0),
131  weight(flags & kEncodeWeights ? arc.weight : Weight::One()) {}
132 
133  static std::unique_ptr<Triple> Read(std::istream &strm) {
134  auto triple = std::make_unique<Triple>();
135  ReadType(strm, &triple->ilabel);
136  ReadType(strm, &triple->olabel);
137  ReadType(strm, &triple->weight);
138  return triple;
139  }
140 
141  void Write(std::ostream &strm) const {
142  WriteType(strm, ilabel);
143  WriteType(strm, olabel);
144  WriteType(strm, weight);
145  }
146 
147  // Exploited below for TripleEqual functor.
148  bool operator==(const Triple &other) const {
149  return (ilabel == other.ilabel && olabel == other.olabel &&
150  weight == other.weight);
151  }
152 
156  };
157 
158  // Equality functor for two Triple pointers.
159  struct TripleEqual {
160  bool operator()(const Triple *x, const Triple *y) const { return *x == *y; }
161  };
162 
163  // Hash functor for one Triple pointer.
164  class TripleHash {
165  public:
166  explicit TripleHash(uint8_t flags) : flags_(flags) {}
167 
168  size_t operator()(const Triple *triple) const {
169  size_t hash = triple->ilabel;
170  static constexpr int lshift = 5;
171  static constexpr int rshift = CHAR_BIT * sizeof(size_t) - 5;
172  if (flags_ & kEncodeLabels) {
173  hash = hash << lshift ^ hash >> rshift ^ triple->olabel;
174  }
175  if (flags_ & kEncodeWeights) {
176  hash = hash << lshift ^ hash >> rshift ^ triple->weight.Hash();
177  }
178  return hash;
179  }
180 
181  private:
182  uint8_t flags_;
183  };
184 
185  explicit EncodeTable(uint8_t flags)
186  : flags_(flags), triple2label_(1024, TripleHash(flags)) {}
187 
188  // Given an arc, encodes either input/output labels or input/costs or both.
189  Label Encode(const Arc &arc) {
190  // Encoding weights of a weighted superfinal transition could result in
191  // a clash with a true epsilon arc; to avoid this we hallucinate kNoLabel
192  // labels instead.
193  if (arc.nextstate == kNoStateId && (flags_ & kEncodeWeights)) {
194  return Encode(std::make_unique<Triple>(kNoLabel, kNoLabel, arc.weight));
195  } else {
196  return Encode(std::make_unique<Triple>(arc, flags_));
197  }
198  }
199 
200  // Given an encoded arc label, decodes back to input/output labels and costs.
201  const Triple *Decode(Label label) const {
202  if (label < 1 || label > triples_.size()) {
203  LOG(ERROR) << "EncodeTable::Decode: Unknown decode label: " << label;
204  return nullptr;
205  }
206  return triples_[label - 1].get();
207  }
208 
209  size_t Size() const { return triples_.size(); }
210 
211  static EncodeTable *Read(std::istream &strm, std::string_view source);
212 
213  bool Write(std::ostream &strm, std::string_view source) const;
214 
215  // This is masked to hide internal-only isymbol and osymbol bits.
216 
217  uint8_t Flags() const { return flags_ & kEncodeFlags; }
218 
219  const SymbolTable *InputSymbols() const { return isymbols_.get(); }
220 
221  const SymbolTable *OutputSymbols() const { return osymbols_.get(); }
222 
223  void SetInputSymbols(const SymbolTable *syms) {
224  if (syms) {
225  isymbols_.reset(syms->Copy());
226  flags_ |= kEncodeHasISymbols;
227  } else {
228  isymbols_.reset();
229  flags_ &= ~kEncodeHasISymbols;
230  }
231  }
232 
233  void SetOutputSymbols(const SymbolTable *syms) {
234  if (syms) {
235  osymbols_.reset(syms->Copy());
236  flags_ |= kEncodeHasOSymbols;
237  } else {
238  osymbols_.reset();
239  flags_ &= ~kEncodeHasOSymbols;
240  }
241  }
242 
243  private:
244  Label Encode(std::unique_ptr<Triple> triple) {
245  auto insert_result =
246  triple2label_.emplace(triple.get(), triples_.size() + 1);
247  if (insert_result.second) triples_.push_back(std::move(triple));
248  return insert_result.first->second;
249  }
250 
251  uint8_t flags_;
252  std::vector<std::unique_ptr<Triple>> triples_;
253  std::unordered_map<const Triple *, Label, TripleHash, TripleEqual>
254  triple2label_;
255  std::unique_ptr<SymbolTable> isymbols_;
256  std::unique_ptr<SymbolTable> osymbols_;
257 
258  EncodeTable(const EncodeTable &) = delete;
259  EncodeTable &operator=(const EncodeTable &) = delete;
260 };
261 
262 template <class Arc>
264  std::string_view source) {
265  EncodeTableHeader hdr;
266  if (!hdr.Read(strm, source)) return nullptr;
267  const auto flags = hdr.Flags();
268  const auto size = hdr.Size();
269  auto table = std::make_unique<EncodeTable>(flags);
270  for (int64_t i = 0; i < size; ++i) {
271  table->triples_.emplace_back(std::move(Triple::Read(strm)));
272  table->triple2label_[table->triples_.back().get()] = table->triples_.size();
273  }
274  if (flags & kEncodeHasISymbols) {
275  table->isymbols_.reset(SymbolTable::Read(strm, source));
276  }
277  if (flags & kEncodeHasOSymbols) {
278  table->osymbols_.reset(SymbolTable::Read(strm, source));
279  }
280  if (!strm) {
281  LOG(ERROR) << "EncodeTable::Read: Read failed: " << source;
282  return nullptr;
283  }
284  return table.release();
285 }
286 
287 template <class Arc>
288 bool EncodeTable<Arc>::Write(std::ostream &strm,
289  std::string_view source) const {
290  EncodeTableHeader hdr;
291  hdr.SetArcType(Arc::Type());
292  hdr.SetFlags(flags_); // Real flags, not masked ones.
293  hdr.SetSize(Size());
294  if (!hdr.Write(strm, source)) return false;
295  for (const auto &triple : triples_) triple->Write(strm);
296  if (flags_ & kEncodeHasISymbols) isymbols_->Write(strm);
297  if (flags_ & kEncodeHasOSymbols) osymbols_->Write(strm);
298  strm.flush();
299  if (!strm) {
300  LOG(ERROR) << "EncodeTable::Write: Write failed: " << source;
301  return false;
302  }
303  return true;
304 }
305 
306 } // namespace internal
307 
308 // A mapper to encode/decode weighted transducers. Encoding of an FST is used
309 // for performing classical determinization or minimization on a weighted
310 // transducer viewing it as an unweighted acceptor over encoded labels.
311 //
312 // The mapper stores the encoding in a local hash table (EncodeTable). This
313 // table is shared (and reference-counted) between the encoder and decoder.
314 // A decoder has read-only access to the EncodeTable.
315 //
316 // The EncodeMapper allows on the fly encoding of the machine. As the
317 // EncodeTable is generated the same table may by used to decode the machine
318 // on the fly. For example in the following sequence of operations
319 //
320 // Encode -> Determinize -> Decode
321 //
322 // we will use the encoding table generated during the encode step in the
323 // decode, even though the encoding is not complete.
324 template <class Arc>
326  using Label = typename Arc::Label;
327  using Weight = typename Arc::Weight;
328 
329  public:
330  explicit EncodeMapper(uint8_t flags, EncodeType type = ENCODE)
331  : flags_(flags),
332  type_(type),
333  table_(std::make_shared<internal::EncodeTable<Arc>>(flags)),
334  error_(false) {}
335 
336  EncodeMapper(const EncodeMapper &mapper)
337  : flags_(mapper.flags_),
338  type_(mapper.type_),
339  table_(mapper.table_),
340  error_(false) {}
341 
342  // Copy constructor but setting the type, typically to DECODE.
343  EncodeMapper(const EncodeMapper &mapper, EncodeType type)
344  : flags_(mapper.flags_),
345  type_(type),
346  table_(mapper.table_),
347  error_(mapper.error_) {}
348 
349  Arc operator()(const Arc &arc);
350 
352  return (type_ == ENCODE && (flags_ & kEncodeWeights))
355  }
356 
358  return MAP_CLEAR_SYMBOLS;
359  }
360 
362  return MAP_CLEAR_SYMBOLS;
363  }
364 
365  uint8_t Flags() const { return flags_; }
366 
367  uint64_t Properties(uint64_t inprops) {
368  uint64_t outprops = inprops;
369  if (error_) outprops |= kError;
370  uint64_t mask = kFstProperties;
371  if (flags_ & kEncodeLabels) {
373  }
374  if (flags_ & kEncodeWeights) {
376  (type_ == ENCODE ? kAddSuperFinalProperties
378  }
379  if (type_ == ENCODE) mask |= kIDeterministic;
380  outprops &= mask;
381  if (type_ == ENCODE) {
382  if (flags_ & kEncodeLabels) {
383  outprops |= kAcceptor;
384  }
385  if (flags_ & kEncodeWeights) {
386  outprops |= kUnweighted | kUnweightedCycles;
387  }
388  }
389  return outprops;
390  }
391 
392  EncodeType Type() const { return type_; }
393 
394  static EncodeMapper *Read(std::istream &strm, std::string_view source,
395  EncodeType type = ENCODE) {
396  auto *table = internal::EncodeTable<Arc>::Read(strm, source);
397  return table ? new EncodeMapper(table->Flags(), type, table) : nullptr;
398  }
399 
400  static EncodeMapper *Read(std::string_view source,
401  EncodeType type = ENCODE) {
402  std::ifstream strm(std::string(source),
403  std::ios_base::in | std::ios_base::binary);
404  if (!strm) {
405  LOG(ERROR) << "EncodeMapper: Can't open file: " << source;
406  return nullptr;
407  }
408  return Read(strm, source, type);
409  }
410 
411  bool Write(std::ostream &strm, std::string_view source) const {
412  return table_->Write(strm, source);
413  }
414 
415  bool Write(std::string_view source) const {
416  std::ofstream strm(std::string(source),
417  std::ios_base::out | std::ios_base::binary);
418  if (!strm) {
419  LOG(ERROR) << "EncodeMapper: Can't open file: " << source;
420  return false;
421  }
422  return Write(strm, source);
423  }
424 
425  const SymbolTable *InputSymbols() const { return table_->InputSymbols(); }
426 
427  const SymbolTable *OutputSymbols() const { return table_->OutputSymbols(); }
428 
429  void SetInputSymbols(const SymbolTable *syms) {
430  table_->SetInputSymbols(syms);
431  }
432 
433  void SetOutputSymbols(const SymbolTable *syms) {
434  table_->SetOutputSymbols(syms);
435  }
436 
437  private:
438  uint8_t flags_;
439  EncodeType type_;
440  std::shared_ptr<internal::EncodeTable<Arc>> table_;
441  bool error_;
442 
443  explicit EncodeMapper(uint8_t flags, EncodeType type,
445  : flags_(flags), type_(type), table_(table), error_(false) {}
446 
447  EncodeMapper &operator=(const EncodeMapper &) = delete;
448 };
449 
450 template <class Arc>
451 Arc EncodeMapper<Arc>::operator()(const Arc &arc) {
452  if (type_ == ENCODE) {
453  // If this arc is a hallucinated final state, and we're either not encoding
454  // weights, or we're encoding weights but this is non-final, we use an
455  // identity-encoding.
456  if (arc.nextstate == kNoStateId &&
457  ((!(flags_ & kEncodeWeights) ||
458  ((flags_ & kEncodeWeights) && arc.weight == Weight::Zero())))) {
459  return arc;
460  } else {
461  const auto label = table_->Encode(arc);
462  return Arc(label, flags_ & kEncodeLabels ? label : arc.olabel,
463  flags_ & kEncodeWeights ? Weight::One() : arc.weight,
464  arc.nextstate);
465  }
466  } else { // type_ == DECODE
467  if (arc.nextstate == kNoStateId) {
468  return arc;
469  } else {
470  if (arc.ilabel == 0) return arc;
471  if (flags_ & kEncodeLabels && arc.ilabel != arc.olabel) {
472  FSTERROR() << "EncodeMapper: Label-encoded arc has different "
473  "input and output labels";
474  error_ = true;
475  }
476  if (flags_ & kEncodeWeights && arc.weight != Weight::One()) {
477  FSTERROR() << "EncodeMapper: Weight-encoded arc has non-trivial weight";
478  error_ = true;
479  }
480  const auto triple = table_->Decode(arc.ilabel);
481  if (!triple) {
482  FSTERROR() << "EncodeMapper: Decode failed";
483  error_ = true;
484  return Arc(kNoLabel, kNoLabel, Weight::NoWeight(), arc.nextstate);
485  } else if (triple->ilabel == kNoLabel) {
486  // Hallucinated kNoLabel from a weighted superfinal transition.
487  return Arc(0, 0, triple->weight, arc.nextstate);
488  } else {
489  return Arc(triple->ilabel,
490  flags_ & kEncodeLabels ? triple->olabel : arc.olabel,
491  flags_ & kEncodeWeights ? triple->weight : arc.weight,
492  arc.nextstate);
493  }
494  }
495  }
496 }
497 
498 // Complexity: O(E + V).
499 template <class Arc>
500 inline void Encode(MutableFst<Arc> *fst, EncodeMapper<Arc> *mapper) {
501  mapper->SetInputSymbols(fst->InputSymbols());
502  mapper->SetOutputSymbols(fst->OutputSymbols());
503  ArcMap(fst, mapper);
504 }
505 
506 template <class Arc>
507 inline void Decode(MutableFst<Arc> *fst, const EncodeMapper<Arc> &mapper) {
508  ArcMap(fst, EncodeMapper<Arc>(mapper, DECODE));
509  RmFinalEpsilon(fst);
510  fst->SetInputSymbols(mapper.InputSymbols());
511  fst->SetOutputSymbols(mapper.OutputSymbols());
512 }
513 
514 // On-the-fly encoding of an input FST.
515 //
516 // Complexity:
517 //
518 // Construction: O(1)
519 // Traversal: O(e + v)
520 //
521 // where e is the number of arcs visited and v is the number of states visited.
522 // Constant time and space to visit an input state or arc is assumed and
523 // exclusive of caching.
524 template <class Arc>
525 class EncodeFst : public ArcMapFst<Arc, Arc, EncodeMapper<Arc>> {
526  public:
529 
530  EncodeFst(const Fst<Arc> &fst, Mapper *encoder)
531  : ArcMapFst<Arc, Arc, Mapper>(fst, encoder, ArcMapFstOptions()) {
532  encoder->SetInputSymbols(fst.InputSymbols());
533  encoder->SetOutputSymbols(fst.OutputSymbols());
534  }
535 
536  EncodeFst(const Fst<Arc> &fst, const Mapper &encoder)
537  : ArcMapFst<Arc, Arc, Mapper>(fst, encoder, ArcMapFstOptions()) {}
538 
539  // See Fst<>::Copy() for doc.
540  EncodeFst(const EncodeFst &fst, bool copy = false)
541  : ArcMapFst<Arc, Arc, Mapper>(fst, copy) {}
542 
543  // Makes a copy of this EncodeFst. See Fst<>::Copy() for further doc.
544  EncodeFst *Copy(bool safe = false) const override {
545  if (safe) {
546  FSTERROR() << "EncodeFst::Copy(true): Not allowed";
547  GetImpl()->SetProperties(kError, kError);
548  }
549  return new EncodeFst(*this);
550  }
551 
552  private:
555 };
556 
557 // On-the-fly decoding of an input FST.
558 //
559 // Complexity:
560 //
561 // Construction: O(1).
562 // Traversal: O(e + v)
563 //
564 // Constant time and space to visit an input state or arc is assumed and
565 // exclusive of caching.
566 template <class Arc>
567 class DecodeFst : public ArcMapFst<Arc, Arc, EncodeMapper<Arc>> {
568  public:
571 
572  DecodeFst(const Fst<Arc> &fst, const Mapper &encoder)
573  : ArcMapFst<Arc, Arc, Mapper>(fst, Mapper(encoder, DECODE),
574  ArcMapFstOptions()) {
575  GetMutableImpl()->SetInputSymbols(encoder.InputSymbols());
576  GetMutableImpl()->SetOutputSymbols(encoder.OutputSymbols());
577  }
578 
579  // See Fst<>::Copy() for doc.
580  DecodeFst(const DecodeFst &fst, bool safe = false)
581  : ArcMapFst<Arc, Arc, Mapper>(fst, safe) {}
582 
583  // Makes a copy of this DecodeFst. See Fst<>::Copy() for further doc.
584  DecodeFst *Copy(bool safe = false) const override {
585  return new DecodeFst(*this, safe);
586  }
587 
588  private:
591 };
592 
593 // Specialization for EncodeFst.
594 template <class Arc>
596  : public StateIterator<ArcMapFst<Arc, Arc, EncodeMapper<Arc>>> {
597  public:
600 };
601 
602 // Specialization for EncodeFst.
603 template <class Arc>
605  : public ArcIterator<ArcMapFst<Arc, Arc, EncodeMapper<Arc>>> {
606  public:
607  ArcIterator(const EncodeFst<Arc> &fst, typename Arc::StateId s)
608  : ArcIterator<ArcMapFst<Arc, Arc, EncodeMapper<Arc>>>(fst, s) {}
609 };
610 
611 // Specialization for DecodeFst.
612 template <class Arc>
614  : public StateIterator<ArcMapFst<Arc, Arc, EncodeMapper<Arc>>> {
615  public:
618 };
619 
620 // Specialization for DecodeFst.
621 template <class Arc>
623  : public ArcIterator<ArcMapFst<Arc, Arc, EncodeMapper<Arc>>> {
624  public:
625  ArcIterator(const DecodeFst<Arc> &fst, typename Arc::StateId s)
626  : ArcIterator<ArcMapFst<Arc, Arc, EncodeMapper<Arc>>>(fst, s) {}
627 };
628 
629 // Useful aliases when using StdArc.
630 
632 
634 
635 } // namespace fst
636 
637 #endif // FST_ENCODE_H_
typename Impl::Arc Arc
Definition: impl-to-fst.h:43
MapSymbolsAction
Definition: arc-map.h:65
void ArcMap(MutableFst< A > *fst, C *mapper)
Definition: arc-map.h:120
EncodeType Type() const
Definition: encode.h:392
bool Write(std::ostream &strm, std::string_view source) const
Definition: encode.h:288
typename ArcMapFst< Arc, Arc, EncodeMapper< Arc > >::Arc Arc
Definition: fst.h:517
constexpr uint64_t kWeightInvariantProperties
Definition: properties.h:280
constexpr int kNoLabel
Definition: fst.h:195
const SymbolTable * OutputSymbols() const
Definition: encode.h:221
constexpr uint8_t kEncodeFlags
Definition: encode.h:57
const SymbolTable * OutputSymbols() const
Definition: encode.h:427
bool Write(std::string_view source) const
Definition: encode.h:415
void Encode(MutableFst< Arc > *fst, EncodeMapper< Arc > *mapper)
Definition: encode.h:500
constexpr int32_t kEncodeDeprecatedMagicNumber
Definition: encode.h:69
ArcIterator(const DecodeFst< Arc > &fst, typename Arc::StateId s)
Definition: encode.h:625
constexpr uint8_t kEncodeHasISymbols
Definition: encode.h:63
EncodeFst(const Fst< Arc > &fst, Mapper *encoder)
Definition: encode.h:530
virtual SymbolTable * Copy() const
Definition: symbol-table.h:411
size_t Size() const
Definition: encode.h:209
DecodeFst(const Fst< Arc > &fst, const Mapper &encoder)
Definition: encode.h:572
const SymbolTable * InputSymbols() const override=0
void SetInputSymbols(const SymbolTable *syms)
Definition: encode.h:223
constexpr uint64_t kError
Definition: properties.h:52
constexpr uint64_t kRmSuperFinalProperties
Definition: properties.h:301
virtual void SetInputSymbols(const SymbolTable *isyms)=0
Triple(Label ilabel, Label olabel, Weight weight)
Definition: encode.h:124
#define LOG(type)
Definition: log.h:53
void RmFinalEpsilon(MutableFst< Arc > *fst)
MapFinalAction FinalAction() const
Definition: encode.h:351
static EncodeTable * Read(std::istream &strm, std::string_view source)
Definition: encode.h:263
DecodeFst(const DecodeFst &fst, bool safe=false)
Definition: encode.h:580
constexpr uint64_t kUnweightedCycles
Definition: properties.h:145
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: encode.h:357
typename ArcMapFst< Arc, Arc, EncodeMapper< Arc > >::Arc Arc
Definition: fst.h:408
void SetOutputSymbols(const SymbolTable *syms)
Definition: encode.h:233
static EncodeMapper * Read(std::istream &strm, std::string_view source, EncodeType type=ENCODE)
Definition: encode.h:394
DecodeFst * Copy(bool safe=false) const override
Definition: encode.h:584
constexpr int kNoStateId
Definition: fst.h:196
constexpr uint8_t kEncodeHasOSymbols
Definition: encode.h:64
EncodeType
Definition: encode.h:53
void SetArcType(std::string_view arctype)
Definition: encode.h:88
std::ostream & WriteType(std::ostream &strm, const T t)
Definition: util.h:228
#define FSTERROR()
Definition: util.h:56
EncodeFst(const Fst< Arc > &fst, const Mapper &encoder)
Definition: encode.h:536
EncodeMapper(const EncodeMapper &mapper)
Definition: encode.h:336
const SymbolTable * OutputSymbols() const override=0
EncodeMapper(const EncodeMapper &mapper, EncodeType type)
Definition: encode.h:343
MapFinalAction
Definition: arc-map.h:50
EncodeTable(uint8_t flags)
Definition: encode.h:185
EncodeMapper(uint8_t flags, EncodeType type=ENCODE)
Definition: encode.h:330
ArcIterator(const EncodeFst< Arc > &fst, typename Arc::StateId s)
Definition: encode.h:607
void Write(std::ostream &strm) const
Definition: encode.h:141
constexpr int32_t kEncodeMagicNumber
Definition: encode.h:67
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: encode.h:361
void SetSize(size_t size)
Definition: encode.h:94
Arc operator()(const Arc &arc)
Definition: encode.h:451
StateIterator(const DecodeFst< Arc > &fst)
Definition: encode.h:616
constexpr uint8_t kEncodeLabels
Definition: encode.h:55
void SetInputSymbols(const SymbolTable *syms)
Definition: encode.h:429
const Triple * Decode(Label label) const
Definition: encode.h:201
void SetFlags(uint8_t flags)
Definition: encode.h:92
size_t Size() const
Definition: encode.h:84
bool Write(std::ostream &strm, std::string_view source) const
Definition: encode.cc:69
constexpr uint64_t kIDeterministic
Definition: properties.h:69
constexpr uint64_t kOLabelInvariantProperties
Definition: properties.h:270
uint8_t Flags() const
Definition: encode.h:365
StateIterator(const EncodeFst< Arc > &fst)
Definition: encode.h:598
const SymbolTable * InputSymbols() const
Definition: encode.h:425
Label Encode(const Arc &arc)
Definition: encode.h:189
constexpr uint64_t kILabelInvariantProperties
Definition: properties.h:261
constexpr uint8_t kEncodeWeights
Definition: encode.h:56
constexpr uint64_t kFstProperties
Definition: properties.h:326
constexpr uint64_t kUnweighted
Definition: properties.h:106
virtual const SymbolTable * InputSymbols() const =0
Triple(const Arc &arc, uint8_t flags)
Definition: encode.h:128
static std::unique_ptr< Triple > Read(std::istream &strm)
Definition: encode.h:133
bool operator()(const Triple *x, const Triple *y) const
Definition: encode.h:160
bool Read(std::istream &strm, std::string_view source)
Definition: encode.cc:33
static EncodeMapper * Read(std::string_view source, EncodeType type=ENCODE)
Definition: encode.h:400
const SymbolTable * InputSymbols() const
Definition: encode.h:219
uint8_t Flags() const
Definition: encode.h:82
bool Write(std::ostream &strm, std::string_view source) const
Definition: encode.h:411
std::istream & ReadType(std::istream &strm, T *t)
Definition: util.h:80
virtual void SetOutputSymbols(const SymbolTable *osyms)=0
size_t operator()(const Triple *triple) const
Definition: encode.h:168
uint64_t Properties(uint64_t inprops)
Definition: encode.h:367
typename Arc::Label Label
Definition: encode.h:117
EncodeFst(const EncodeFst &fst, bool copy=false)
Definition: encode.h:540
typename Arc::Weight Weight
Definition: encode.h:118
static SymbolTable * Read(std::istream &strm, std::string_view source)
Definition: symbol-table.h:395
void Decode(MutableFst< Arc > *fst, const EncodeMapper< Arc > &mapper)
Definition: encode.h:507
void SetOutputSymbols(const SymbolTable *syms)
Definition: encode.h:433
constexpr uint64_t kAddSuperFinalProperties
Definition: properties.h:291
uint8_t Flags() const
Definition: encode.h:217
bool operator==(const Triple &other) const
Definition: encode.h:148
constexpr uint64_t kAcceptor
Definition: properties.h:64
EncodeFst * Copy(bool safe=false) const override
Definition: encode.h:544
const std::string & ArcType() const
Definition: encode.h:80
virtual const SymbolTable * OutputSymbols() const =0