FST  openfst-1.7.2
OpenFst Library
arc-map.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 // Class to map over/transform arcs e.g., change semirings or
5 // implement project/invert. Consider using when operation does
6 // not change the number of arcs (except possibly superfinal arcs).
7 
8 #ifndef FST_ARC_MAP_H_
9 #define FST_ARC_MAP_H_
10 
11 #include <string>
12 #include <unordered_map>
13 #include <utility>
14 
15 #include <fst/log.h>
16 
17 #include <fst/cache.h>
18 #include <fst/mutable-fst.h>
19 
20 
21 namespace fst {
22 
23 // Determines how final weights are mapped.
25  // A final weight is mapped into a final weight. An error is raised if this
26  // is not possible.
28  // A final weight is mapped to an arc to the superfinal state when the result
29  // cannot be represented as a final weight. The superfinal state will be
30  // added only if it is needed.
32  // A final weight is mapped to an arc to the superfinal state unless the
33  // result can be represented as a final weight of weight Zero(). The
34  // superfinal state is always added (if the input is not the empty FST).
36 };
37 
38 // Determines how symbol tables are mapped.
40  // Symbols should be cleared in the result by the map.
42  // Symbols should be copied from the input FST by the map.
44  // Symbols should not be modified in the result by the map itself.
45  // (They may set by the mapper).
47 };
48 
49 // The ArcMapper interfaces defines how arcs and final weights are mapped.
50 // This is useful for implementing operations that do not change the number of
51 // arcs (except possibly superfinal arcs).
52 //
53 // template <class A, class B>
54 // class ArcMapper {
55 // public:
56 // using FromArc = A;
57 // using ToArc = B;
58 //
59 // // Maps an arc type FromArc to arc type ToArc.
60 // ToArc operator()(const FromArc &arc);
61 //
62 // // Specifies final action the mapper requires (see above).
63 // // The mapper will be passed final weights as arcs of the form
64 // // Arc(0, 0, weight, kNoStateId).
65 // MapFinalAction FinalAction() const;
66 //
67 // // Specifies input symbol table action the mapper requires (see above).
68 // MapSymbolsAction InputSymbolsAction() const;
69 //
70 // // Specifies output symbol table action the mapper requires (see above).
71 // MapSymbolsAction OutputSymbolsAction() const;
72 //
73 // // This specifies the known properties of an FST mapped by this mapper. It
74 // takes as argument the input FSTs's known properties.
75 // uint64 Properties(uint64 props) const;
76 // };
77 //
78 // The ArcMap functions and classes below will use the FinalAction()
79 // method of the mapper to determine how to treat final weights, e.g., whether
80 // to add a superfinal state. They will use the Properties() method to set the
81 // result FST properties.
82 //
83 // We include a various map versions below. One dimension of variation is
84 // whether the mapping mutates its input, writes to a new result FST, or is an
85 // on-the-fly FST. Another dimension is how we pass the mapper. We allow passing
86 // the mapper by pointer for cases that we need to change the state of the
87 // user's mapper. This is the case with the EncodeMapper, which is reused
88 // during decoding. We also include map versions that pass the mapper by value
89 // or const reference when this suffices.
90 
91 // Maps an arc type A using a mapper function object C, passed
92 // by pointer. This version modifies its Fst input.
93 template <class A, class C>
94 void ArcMap(MutableFst<A> *fst, C *mapper) {
95  using FromArc = A;
96  using ToArc = A;
97  using Weight = typename FromArc::Weight;
98  if (mapper->InputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
99  fst->SetInputSymbols(nullptr);
100  }
101  if (mapper->OutputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
102  fst->SetOutputSymbols(nullptr);
103  }
104  if (fst->Start() == kNoStateId) return;
105  const auto props = fst->Properties(kFstProperties, false);
106  const auto final_action = mapper->FinalAction();
107  auto superfinal = kNoStateId;
108  if (final_action == MAP_REQUIRE_SUPERFINAL) {
109  superfinal = fst->AddState();
110  fst->SetFinal(superfinal, Weight::One());
111  }
112  for (StateIterator<MutableFst<FromArc>> siter(*fst); !siter.Done();
113  siter.Next()) {
114  const auto state = siter.Value();
115  for (MutableArcIterator<MutableFst<FromArc>> aiter(fst, state);
116  !aiter.Done(); aiter.Next()) {
117  const auto &arc = aiter.Value();
118  aiter.SetValue((*mapper)(arc));
119  }
120  switch (final_action) {
121  case MAP_NO_SUPERFINAL:
122  default: {
123  const FromArc arc(0, 0, fst->Final(state), kNoStateId);
124  const auto final_arc = (*mapper)(arc);
125  if (final_arc.ilabel != 0 || final_arc.olabel != 0) {
126  FSTERROR() << "ArcMap: Non-zero arc labels for superfinal arc";
127  fst->SetProperties(kError, kError);
128  }
129  fst->SetFinal(state, final_arc.weight);
130  break;
131  }
132  case MAP_ALLOW_SUPERFINAL: {
133  if (state != superfinal) {
134  const FromArc arc(0, 0, fst->Final(state), kNoStateId);
135  auto final_arc = (*mapper)(arc);
136  if (final_arc.ilabel != 0 || final_arc.olabel != 0) {
137  // Add a superfinal state if not already done.
138  if (superfinal == kNoStateId) {
139  superfinal = fst->AddState();
140  fst->SetFinal(superfinal, Weight::One());
141  }
142  final_arc.nextstate = superfinal;
143  fst->AddArc(state, std::move(final_arc));
144  fst->SetFinal(state, Weight::Zero());
145  } else {
146  fst->SetFinal(state, final_arc.weight);
147  }
148  }
149  break;
150  }
151  case MAP_REQUIRE_SUPERFINAL: {
152  if (state != superfinal) {
153  const FromArc arc(0, 0, fst->Final(state), kNoStateId);
154  const auto final_arc = (*mapper)(arc);
155  if (final_arc.ilabel != 0 || final_arc.olabel != 0 ||
156  final_arc.weight != Weight::Zero()) {
157  fst->AddArc(state, ToArc(final_arc.ilabel, final_arc.olabel,
158  final_arc.weight, superfinal));
159  }
160  fst->SetFinal(state, Weight::Zero());
161  }
162  break;
163  }
164  }
165  }
166  fst->SetProperties(mapper->Properties(props), kFstProperties);
167 }
168 
169 // Maps an arc type A using a mapper function object C, passed by value. This
170 // version modifies its FST input.
171 template <class A, class C>
172 void ArcMap(MutableFst<A> *fst, C mapper) {
173  ArcMap(fst, &mapper);
174 }
175 
176 // Maps an arc type A to an arc type B using mapper function object C,
177 // passed by pointer. This version writes the mapped input FST to an
178 // output MutableFst.
179 template <class A, class B, class C>
180 void ArcMap(const Fst<A> &ifst, MutableFst<B> *ofst, C *mapper) {
181  using FromArc = A;
182  using StateId = typename FromArc::StateId;
183  ofst->DeleteStates();
184  if (mapper->InputSymbolsAction() == MAP_COPY_SYMBOLS) {
185  ofst->SetInputSymbols(ifst.InputSymbols());
186  } else if (mapper->InputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
187  ofst->SetInputSymbols(nullptr);
188  }
189  if (mapper->OutputSymbolsAction() == MAP_COPY_SYMBOLS) {
190  ofst->SetOutputSymbols(ifst.OutputSymbols());
191  } else if (mapper->OutputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
192  ofst->SetOutputSymbols(nullptr);
193  }
194  const auto iprops = ifst.Properties(kCopyProperties, false);
195  if (ifst.Start() == kNoStateId) {
196  if (iprops & kError) ofst->SetProperties(kError, kError);
197  return;
198  }
199  const auto final_action = mapper->FinalAction();
200  if (ifst.Properties(kExpanded, false)) {
201  ofst->ReserveStates(
202  CountStates(ifst) + (final_action == MAP_NO_SUPERFINAL ? 0 : 1));
203  }
204  // Adds all states.
205  for (StateIterator<Fst<A>> siter(ifst); !siter.Done(); siter.Next()) {
206  ofst->AddState();
207  }
208  StateId superfinal = kNoStateId;
209  if (final_action == MAP_REQUIRE_SUPERFINAL) {
210  superfinal = ofst->AddState();
211  ofst->SetFinal(superfinal, B::Weight::One());
212  }
213  for (StateIterator<Fst<A>> siter(ifst); !siter.Done(); siter.Next()) {
214  StateId s = siter.Value();
215  if (s == ifst.Start()) ofst->SetStart(s);
216  ofst->ReserveArcs(
217  s, ifst.NumArcs(s) + (final_action != MAP_NO_SUPERFINAL ? 1 : 0));
218  for (ArcIterator<Fst<A>> aiter(ifst, s); !aiter.Done(); aiter.Next()) {
219  ofst->AddArc(s, (*mapper)(aiter.Value()));
220  }
221  switch (final_action) {
222  case MAP_NO_SUPERFINAL:
223  default: {
224  B final_arc = (*mapper)(A(0, 0, ifst.Final(s), kNoStateId));
225  if (final_arc.ilabel != 0 || final_arc.olabel != 0) {
226  FSTERROR() << "ArcMap: Non-zero arc labels for superfinal arc";
227  ofst->SetProperties(kError, kError);
228  }
229  ofst->SetFinal(s, final_arc.weight);
230  break;
231  }
232  case MAP_ALLOW_SUPERFINAL: {
233  B final_arc = (*mapper)(A(0, 0, ifst.Final(s), kNoStateId));
234  if (final_arc.ilabel != 0 || final_arc.olabel != 0) {
235  // Add a superfinal state if not already done.
236  if (superfinal == kNoStateId) {
237  superfinal = ofst->AddState();
238  ofst->SetFinal(superfinal, B::Weight::One());
239  }
240  final_arc.nextstate = superfinal;
241  ofst->AddArc(s, std::move(final_arc));
242  ofst->SetFinal(s, B::Weight::Zero());
243  } else {
244  ofst->SetFinal(s, final_arc.weight);
245  }
246  break;
247  }
248  case MAP_REQUIRE_SUPERFINAL: {
249  B final_arc = (*mapper)(A(0, 0, ifst.Final(s), kNoStateId));
250  if (final_arc.ilabel != 0 || final_arc.olabel != 0 ||
251  final_arc.weight != B::Weight::Zero()) {
252  ofst->AddArc(s, B(final_arc.ilabel, final_arc.olabel,
253  final_arc.weight, superfinal));
254  }
255  ofst->SetFinal(s, B::Weight::Zero());
256  break;
257  }
258  }
259  }
260  const auto oprops = ofst->Properties(kFstProperties, false);
261  ofst->SetProperties(mapper->Properties(iprops) | oprops, kFstProperties);
262 }
263 
264 // Maps an arc type A to an arc type B using mapper function
265 // object C, passed by value. This version writes the mapped input
266 // Fst to an output MutableFst.
267 template <class A, class B, class C>
268 void ArcMap(const Fst<A> &ifst, MutableFst<B> *ofst, C mapper) {
269  ArcMap(ifst, ofst, &mapper);
270 }
271 
273  // ArcMapFst default caching behaviour is to do no caching. Most mappers are
274  // cheap and therefore we save memory by not doing caching.
276 
277  explicit ArcMapFstOptions(const CacheOptions &opts) : CacheOptions(opts) {}
278 };
279 
280 template <class A, class B, class C>
281 class ArcMapFst;
282 
283 namespace internal {
284 
285 // Implementation of delayed ArcMapFst.
286 template <class A, class B, class C>
287 class ArcMapFstImpl : public CacheImpl<B> {
288  public:
289  using Arc = B;
290  using StateId = typename Arc::StateId;
291  using Weight = typename Arc::Weight;
292 
293  using FstImpl<B>::SetType;
297 
299  using CacheImpl<B>::HasArcs;
302  using CacheImpl<B>::PushArc;
303  using CacheImpl<B>::SetArcs;
306 
307  friend class StateIterator<ArcMapFst<A, B, C>>;
308 
309  ArcMapFstImpl(const Fst<A> &fst, const C &mapper,
310  const ArcMapFstOptions &opts)
311  : CacheImpl<B>(opts),
312  fst_(fst.Copy()),
313  mapper_(new C(mapper)),
314  own_mapper_(true),
315  superfinal_(kNoStateId),
316  nstates_(0) {
317  Init();
318  }
319 
320  ArcMapFstImpl(const Fst<A> &fst, C *mapper, const ArcMapFstOptions &opts)
321  : CacheImpl<B>(opts),
322  fst_(fst.Copy()),
323  mapper_(mapper),
324  own_mapper_(false),
325  superfinal_(kNoStateId),
326  nstates_(0) {
327  Init();
328  }
329 
331  : CacheImpl<B>(impl),
332  fst_(impl.fst_->Copy(true)),
333  mapper_(new C(*impl.mapper_)),
334  own_mapper_(true),
335  superfinal_(kNoStateId),
336  nstates_(0) {
337  Init();
338  }
339 
340  ~ArcMapFstImpl() override {
341  if (own_mapper_) delete mapper_;
342  }
343 
345  if (!HasStart()) SetStart(FindOState(fst_->Start()));
346  return CacheImpl<B>::Start();
347  }
348 
350  if (!HasFinal(s)) {
351  switch (final_action_) {
352  case MAP_NO_SUPERFINAL:
353  default: {
354  const auto final_arc =
355  (*mapper_)(A(0, 0, fst_->Final(FindIState(s)), kNoStateId));
356  if (final_arc.ilabel != 0 || final_arc.olabel != 0) {
357  FSTERROR() << "ArcMapFst: Non-zero arc labels for superfinal arc";
358  SetProperties(kError, kError);
359  }
360  SetFinal(s, final_arc.weight);
361  break;
362  }
363  case MAP_ALLOW_SUPERFINAL: {
364  if (s == superfinal_) {
365  SetFinal(s, Weight::One());
366  } else {
367  const auto final_arc =
368  (*mapper_)(A(0, 0, fst_->Final(FindIState(s)), kNoStateId));
369  if (final_arc.ilabel == 0 && final_arc.olabel == 0) {
370  SetFinal(s, final_arc.weight);
371  } else {
372  SetFinal(s, Weight::Zero());
373  }
374  }
375  break;
376  }
377  case MAP_REQUIRE_SUPERFINAL: {
378  SetFinal(s, s == superfinal_ ? Weight::One() : Weight::Zero());
379  break;
380  }
381  }
382  }
383  return CacheImpl<B>::Final(s);
384  }
385 
386  size_t NumArcs(StateId s) {
387  if (!HasArcs(s)) Expand(s);
388  return CacheImpl<B>::NumArcs(s);
389  }
390 
392  if (!HasArcs(s)) Expand(s);
394  }
395 
397  if (!HasArcs(s)) Expand(s);
399  }
400 
401  uint64 Properties() const override { return Properties(kFstProperties); }
402 
403  // Sets error if found, and returns other FST impl properties.
404  uint64 Properties(uint64 mask) const override {
405  if ((mask & kError) && (fst_->Properties(kError, false) ||
406  (mapper_->Properties(0) & kError))) {
407  SetProperties(kError, kError);
408  }
409  return FstImpl<Arc>::Properties(mask);
410  }
411 
413  if (!HasArcs(s)) Expand(s);
415  }
416 
417  void Expand(StateId s) {
418  // Add exiting arcs.
419  if (s == superfinal_) {
420  SetArcs(s);
421  return;
422  }
423  for (ArcIterator<Fst<A>> aiter(*fst_, FindIState(s)); !aiter.Done();
424  aiter.Next()) {
425  auto aarc = aiter.Value();
426  aarc.nextstate = FindOState(aarc.nextstate);
427  PushArc(s, (*mapper_)(aarc));
428  }
429 
430  // Check for superfinal arcs.
431  if (!HasFinal(s) || Final(s) == Weight::Zero()) {
432  switch (final_action_) {
433  case MAP_NO_SUPERFINAL:
434  default:
435  break;
436  case MAP_ALLOW_SUPERFINAL: {
437  auto final_arc =
438  (*mapper_)(A(0, 0, fst_->Final(FindIState(s)), kNoStateId));
439  if (final_arc.ilabel != 0 || final_arc.olabel != 0) {
440  if (superfinal_ == kNoStateId) superfinal_ = nstates_++;
441  final_arc.nextstate = superfinal_;
442  PushArc(s, std::move(final_arc));
443  }
444  break;
445  }
446  case MAP_REQUIRE_SUPERFINAL: {
447  const auto final_arc =
448  (*mapper_)(A(0, 0, fst_->Final(FindIState(s)), kNoStateId));
449  if (final_arc.ilabel != 0 || final_arc.olabel != 0 ||
450  final_arc.weight != B::Weight::Zero()) {
451  EmplaceArc(s, final_arc.ilabel, final_arc.olabel, final_arc.weight,
452  superfinal_);
453  }
454  break;
455  }
456  }
457  }
458  SetArcs(s);
459  }
460 
461  private:
462  void Init() {
463  SetType("map");
464  if (mapper_->InputSymbolsAction() == MAP_COPY_SYMBOLS) {
465  SetInputSymbols(fst_->InputSymbols());
466  } else if (mapper_->InputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
467  SetInputSymbols(nullptr);
468  }
469  if (mapper_->OutputSymbolsAction() == MAP_COPY_SYMBOLS) {
470  SetOutputSymbols(fst_->OutputSymbols());
471  } else if (mapper_->OutputSymbolsAction() == MAP_CLEAR_SYMBOLS) {
472  SetOutputSymbols(nullptr);
473  }
474  if (fst_->Start() == kNoStateId) {
475  final_action_ = MAP_NO_SUPERFINAL;
476  SetProperties(kNullProperties);
477  } else {
478  final_action_ = mapper_->FinalAction();
479  uint64 props = fst_->Properties(kCopyProperties, false);
480  SetProperties(mapper_->Properties(props));
481  if (final_action_ == MAP_REQUIRE_SUPERFINAL) superfinal_ = 0;
482  }
483  }
484 
485  // Maps from output state to input state.
486  StateId FindIState(StateId s) {
487  if (superfinal_ == kNoStateId || s < superfinal_) {
488  return s;
489  } else {
490  return s - 1;
491  }
492  }
493 
494  // Maps from input state to output state.
495  StateId FindOState(StateId is) {
496  auto os = is;
497  if (!(superfinal_ == kNoStateId || is < superfinal_)) ++os;
498  if (os >= nstates_) nstates_ = os + 1;
499  return os;
500  }
501 
502  std::unique_ptr<const Fst<A>> fst_;
503  C *mapper_;
504  const bool own_mapper_;
505  MapFinalAction final_action_;
506  StateId superfinal_;
507  StateId nstates_;
508 };
509 
510 } // namespace internal
511 
512 // Maps an arc type A to an arc type B using Mapper function object
513 // C. This version is a delayed FST.
514 template <class A, class B, class C>
515 class ArcMapFst : public ImplToFst<internal::ArcMapFstImpl<A, B, C>> {
516  public:
517  using Arc = B;
518  using StateId = typename Arc::StateId;
519  using Weight = typename Arc::Weight;
520 
522  using State = typename Store::State;
524 
525  friend class ArcIterator<ArcMapFst<A, B, C>>;
526  friend class StateIterator<ArcMapFst<A, B, C>>;
527 
528  ArcMapFst(const Fst<A> &fst, const C &mapper, const ArcMapFstOptions &opts)
529  : ImplToFst<Impl>(std::make_shared<Impl>(fst, mapper, opts)) {}
530 
531  ArcMapFst(const Fst<A> &fst, C *mapper, const ArcMapFstOptions &opts)
532  : ImplToFst<Impl>(std::make_shared<Impl>(fst, mapper, opts)) {}
533 
534  ArcMapFst(const Fst<A> &fst, const C &mapper)
535  : ImplToFst<Impl>(
536  std::make_shared<Impl>(fst, mapper, ArcMapFstOptions())) {}
537 
538  ArcMapFst(const Fst<A> &fst, C *mapper)
539  : ImplToFst<Impl>(
540  std::make_shared<Impl>(fst, mapper, ArcMapFstOptions())) {}
541 
542  // See Fst<>::Copy() for doc.
543  ArcMapFst(const ArcMapFst<A, B, C> &fst, bool safe = false)
544  : ImplToFst<Impl>(fst, safe) {}
545 
546  // Get a copy of this ArcMapFst. See Fst<>::Copy() for further doc.
547  ArcMapFst<A, B, C> *Copy(bool safe = false) const override {
548  return new ArcMapFst<A, B, C>(*this, safe);
549  }
550 
551  inline void InitStateIterator(StateIteratorData<B> *data) const override;
552 
553  void InitArcIterator(StateId s, ArcIteratorData<B> *data) const override {
554  GetMutableImpl()->InitArcIterator(s, data);
555  }
556 
557  protected:
560 
561  private:
562  ArcMapFst &operator=(const ArcMapFst &) = delete;
563 };
564 
565 // Specialization for ArcMapFst.
566 //
567 // This may be derived from.
568 template <class A, class B, class C>
569 class StateIterator<ArcMapFst<A, B, C>> : public StateIteratorBase<B> {
570  public:
571  using StateId = typename B::StateId;
572 
574  : impl_(fst.GetImpl()),
575  siter_(*impl_->fst_),
576  s_(0),
577  superfinal_(impl_->final_action_ == MAP_REQUIRE_SUPERFINAL) {
578  CheckSuperfinal();
579  }
580 
581  bool Done() const final { return siter_.Done() && !superfinal_; }
582 
583  StateId Value() const final { return s_; }
584 
585  void Next() final {
586  ++s_;
587  if (!siter_.Done()) {
588  siter_.Next();
589  CheckSuperfinal();
590  } else if (superfinal_) {
591  superfinal_ = false;
592  }
593  }
594 
595  void Reset() final {
596  s_ = 0;
597  siter_.Reset();
598  superfinal_ = impl_->final_action_ == MAP_REQUIRE_SUPERFINAL;
599  CheckSuperfinal();
600  }
601 
602  private:
603  void CheckSuperfinal() {
604  if (impl_->final_action_ != MAP_ALLOW_SUPERFINAL || superfinal_) return;
605  if (!siter_.Done()) {
606  const auto final_arc =
607  (*impl_->mapper_)(A(0, 0, impl_->fst_->Final(s_), kNoStateId));
608  if (final_arc.ilabel != 0 || final_arc.olabel != 0) superfinal_ = true;
609  }
610  }
611 
613  StateIterator<Fst<A>> siter_;
614  StateId s_;
615  bool superfinal_; // True if there is a superfinal state and not done.
616 };
617 
618 // Specialization for ArcMapFst.
619 template <class A, class B, class C>
620 class ArcIterator<ArcMapFst<A, B, C>>
621  : public CacheArcIterator<ArcMapFst<A, B, C>> {
622  public:
623  using StateId = typename A::StateId;
624 
626  : CacheArcIterator<ArcMapFst<A, B, C>>(fst.GetMutableImpl(), s) {
627  if (!fst.GetImpl()->HasArcs(s)) fst.GetMutableImpl()->Expand(s);
628  }
629 };
630 
631 template <class A, class B, class C>
633  StateIteratorData<B> *data) const {
634  data->base = new StateIterator<ArcMapFst<A, B, C>>(*this);
635 }
636 
637 // Utility Mappers.
638 
639 // Mapper that returns its input.
640 template <class A>
642  public:
643  using FromArc = A;
644  using ToArc = A;
645 
646  constexpr ToArc operator()(const FromArc &arc) const { return arc; }
647 
648  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
649 
651  return MAP_COPY_SYMBOLS;
652  }
653 
655  return MAP_COPY_SYMBOLS;
656  }
657 
658  constexpr uint64 Properties(uint64 props) const { return props; }
659 };
660 
661 // Mapper that converts all input symbols to epsilon.
662 template <class A>
664  public:
665  using FromArc = A;
666  using ToArc = A;
667 
668  constexpr ToArc operator()(const FromArc &arc) const {
669  return ToArc(0, arc.olabel, arc.weight, arc.nextstate);
670  }
671 
672  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
673 
675  return MAP_CLEAR_SYMBOLS;
676  }
677 
679  return MAP_COPY_SYMBOLS;
680  }
681 
682  constexpr uint64 Properties(uint64 props) const {
683  return (props & kSetArcProperties) | kIEpsilons | kILabelSorted;
684  }
685 };
686 
687 // Mapper that converts all output symbols to epsilon.
688 template <class A>
690  public:
691  using FromArc = A;
692  using ToArc = A;
693 
694  constexpr ToArc operator()(const FromArc &arc) const {
695  return ToArc(arc.ilabel, 0, arc.weight, arc.nextstate);
696  }
697 
698  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
699 
701  return MAP_COPY_SYMBOLS;
702  }
703 
705  return MAP_CLEAR_SYMBOLS;
706  }
707 
708  constexpr uint64 Properties(uint64 props) const {
709  return (props & kSetArcProperties) | kOEpsilons | kOLabelSorted;
710  }
711 };
712 
713 // Mapper that returns its input with final states redirected to a single
714 // super-final state.
715 template <class A>
717  public:
718  using FromArc = A;
719  using ToArc = A;
720  using Label = typename FromArc::Label;
721  using Weight = typename FromArc::Weight;;
722 
723  // Arg allows setting super-final label.
724  explicit SuperFinalMapper(Label final_label = 0)
725  : final_label_(final_label) {}
726 
727  ToArc operator()(const FromArc &arc) const {
728  // Super-final arc.
729  if (arc.nextstate == kNoStateId && arc.weight != Weight::Zero()) {
730  return ToArc(final_label_, final_label_, arc.weight, kNoStateId);
731  } else {
732  return arc;
733  }
734  }
735 
736  constexpr MapFinalAction FinalAction() const {
737  return MAP_REQUIRE_SUPERFINAL;
738  }
739 
741  return MAP_COPY_SYMBOLS;
742  }
743 
745  return MAP_COPY_SYMBOLS;
746  }
747 
748  uint64 Properties(uint64 props) const {
749  if (final_label_ == 0) {
750  return props & kAddSuperFinalProperties;
751  } else {
752  return props & kAddSuperFinalProperties &
754  }
755  }
756 
757  private:
758  Label final_label_;
759 };
760 
761 // Mapper that leaves labels and nextstate unchanged and constructs a new weight
762 // from the underlying value of the arc weight. If no weight converter is
763 // explictly specified, requires that there is a WeightConvert class
764 // specialization that converts the weights.
765 template <class A, class B,
768  public:
769  using FromArc = A;
770  using ToArc = B;
771  using Converter = C;
772  using FromWeight = typename FromArc::Weight;
773  using ToWeight = typename ToArc::Weight;
774 
775  constexpr explicit WeightConvertMapper(const Converter &c = Converter())
776  : convert_weight_(c) {}
777 
778  constexpr ToArc operator()(const FromArc &arc) const {
779  return ToArc(arc.ilabel, arc.olabel, convert_weight_(arc.weight),
780  arc.nextstate);
781  }
782 
783  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
784 
786  return MAP_COPY_SYMBOLS;
787  }
788 
790  return MAP_COPY_SYMBOLS;
791  }
792 
793  constexpr uint64 Properties(uint64 props) const { return props; }
794 
795  private:
796  const Converter convert_weight_;
797 };
798 
799 // Non-precision-changing weight conversions; consider using more efficient
800 // Cast method instead.
801 
803 
805 
806 // Precision-changing weight conversions.
807 
809 
811 
813 
815 
816 // Mapper from A to GallicArc<A>.
817 template <class A, GallicType G = GALLIC_LEFT>
819  public:
820  using FromArc = A;
822 
824  using AW = typename FromArc::Weight;
825  using GW = typename ToArc::Weight;
826 
827  ToArc operator()(const FromArc &arc) const {
828  // Super-final arc.
829  if (arc.nextstate == kNoStateId && arc.weight != AW::Zero()) {
830  return ToArc(0, 0, GW(SW::One(), arc.weight), kNoStateId);
831  // Super-non-final arc.
832  } else if (arc.nextstate == kNoStateId) {
833  return ToArc(0, 0, GW::Zero(), kNoStateId);
834  // Epsilon label.
835  } else if (arc.olabel == 0) {
836  return ToArc(arc.ilabel, arc.ilabel, GW(SW::One(), arc.weight),
837  arc.nextstate);
838  // Regular label.
839  } else {
840  return ToArc(arc.ilabel, arc.ilabel, GW(SW(arc.olabel), arc.weight),
841  arc.nextstate);
842  }
843  }
844 
845  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
846 
848  return MAP_COPY_SYMBOLS;
849  }
850 
852  return MAP_CLEAR_SYMBOLS;
853  }
854 
855  uint64 Properties(uint64 props) const {
856  return ProjectProperties(props, true) & kWeightInvariantProperties;
857  }
858 };
859 
860 // Mapper from GallicArc<A> to A.
861 template <class A, GallicType G = GALLIC_LEFT>
863  public:
865  using ToArc = A;
866 
867  using Label = typename ToArc::Label;
868  using AW = typename ToArc::Weight;
869  using GW = typename FromArc::Weight;
870 
871  explicit FromGallicMapper(Label superfinal_label = 0)
872  : superfinal_label_(superfinal_label), error_(false) {}
873 
874  ToArc operator()(const FromArc &arc) const {
875  // 'Super-non-final' arc.
876  if (arc.nextstate == kNoStateId && arc.weight == GW::Zero()) {
877  return A(arc.ilabel, 0, AW::Zero(), kNoStateId);
878  }
879  Label l = kNoLabel;
880  AW weight;
881  if (!Extract(arc.weight, &weight, &l) || arc.ilabel != arc.olabel) {
882  FSTERROR() << "FromGallicMapper: Unrepresentable weight: " << arc.weight
883  << " for arc with ilabel = " << arc.ilabel
884  << ", olabel = " << arc.olabel
885  << ", nextstate = " << arc.nextstate;
886  error_ = true;
887  }
888  if (arc.ilabel == 0 && l != 0 && arc.nextstate == kNoStateId) {
889  return ToArc(superfinal_label_, l, weight, arc.nextstate);
890  } else {
891  return ToArc(arc.ilabel, l, weight, arc.nextstate);
892  }
893  }
894 
895  constexpr MapFinalAction FinalAction() const { return MAP_ALLOW_SUPERFINAL; }
896 
898  return MAP_COPY_SYMBOLS;
899  }
900 
902  return MAP_CLEAR_SYMBOLS;
903  }
904 
905  uint64 Properties(uint64 inprops) const {
906  uint64 outprops = inprops & kOLabelInvariantProperties &
908  if (error_) outprops |= kError;
909  return outprops;
910  }
911 
912  private:
913  template <GallicType GT>
914  static bool Extract(const GallicWeight<Label, AW, GT> &gallic_weight,
915  typename A::Weight *weight, typename A::Label *label) {
917  const GW &w1 = gallic_weight.Value1();
918  const AW &w2 = gallic_weight.Value2();
919  typename GW::Iterator iter1(w1);
920  const Label l = w1.Size() == 1 ? iter1.Value() : 0;
921  if (l == kStringInfinity || l == kStringBad || w1.Size() > 1) return false;
922  *label = l;
923  *weight = w2;
924  return true;
925  }
926 
927  static bool Extract(const GallicWeight<Label, AW, GALLIC> &gallic_weight,
928  typename A::Weight *weight, typename A::Label *label) {
929  if (gallic_weight.Size() > 1) return false;
930  if (gallic_weight.Size() == 0) {
931  *label = 0;
932  *weight = A::Weight::Zero();
933  return true;
934  }
935  return Extract<GALLIC_RESTRICT>(gallic_weight.Back(), weight, label);
936  }
937 
938  const Label superfinal_label_;
939  mutable bool error_;
940 };
941 
942 // Mapper from GallicArc<A> to A.
943 template <class A, GallicType G = GALLIC_LEFT>
945  public:
947  using ToArc = A;
948 
949  using Label = typename ToArc::Label;
950  using StateId = typename ToArc::StateId;
951  using AW = typename ToArc::Weight;
952  using GW = typename FromArc::Weight;
954 
956  : fst_(fst),
957  lmax_(0),
958  osymbols_(fst->OutputSymbols()),
959  isymbols_(nullptr),
960  error_(false) {
961  fst_->DeleteStates();
962  state_ = fst_->AddState();
963  fst_->SetStart(state_);
964  fst_->SetFinal(state_, AW::One());
965  if (osymbols_) {
966  string name = osymbols_->Name() + "_from_gallic";
967  fst_->SetInputSymbols(new SymbolTable(name));
968  isymbols_ = fst_->MutableInputSymbols();
969  const int64 zero = 0;
970  isymbols_->AddSymbol(osymbols_->Find(zero), 0);
971  } else {
972  fst_->SetInputSymbols(nullptr);
973  }
974  }
975 
976  ToArc operator()(const FromArc &arc) {
977  // Super-non-final arc.
978  if (arc.nextstate == kNoStateId && arc.weight == GW::Zero()) {
979  return ToArc(arc.ilabel, 0, AW::Zero(), kNoStateId);
980  }
981  SW w1 = arc.weight.Value1();
982  AW w2 = arc.weight.Value2();
983  Label l;
984  if (w1.Size() == 0) {
985  l = 0;
986  } else {
987  auto insert_result = map_.insert(std::make_pair(w1, kNoLabel));
988  if (!insert_result.second) {
989  l = insert_result.first->second;
990  } else {
991  l = ++lmax_;
992  insert_result.first->second = l;
993  StringWeightIterator<SW> iter1(w1);
994  StateId n;
995  string s;
996  for (size_t i = 0, p = state_; i < w1.Size();
997  ++i, iter1.Next(), p = n) {
998  n = i == w1.Size() - 1 ? state_ : fst_->AddState();
999  fst_->AddArc(p, ToArc(i ? 0 : l, iter1.Value(), AW::One(), n));
1000  if (isymbols_) {
1001  if (i) s = s + "_";
1002  s = s + osymbols_->Find(iter1.Value());
1003  }
1004  }
1005  if (isymbols_) isymbols_->AddSymbol(s, l);
1006  }
1007  }
1008  if (l == kStringInfinity || l == kStringBad || arc.ilabel != arc.olabel) {
1009  FSTERROR() << "GallicToNewSymbolMapper: Unrepresentable weight: " << l;
1010  error_ = true;
1011  }
1012  return ToArc(arc.ilabel, l, w2, arc.nextstate);
1013  }
1014 
1015  constexpr MapFinalAction FinalAction() const { return MAP_ALLOW_SUPERFINAL; }
1016 
1018  return MAP_COPY_SYMBOLS;
1019  }
1020 
1022  return MAP_CLEAR_SYMBOLS;
1023  }
1024 
1025  uint64 Properties(uint64 inprops) const {
1026  uint64 outprops = inprops & kOLabelInvariantProperties &
1028  if (error_) outprops |= kError;
1029  return outprops;
1030  }
1031 
1032  private:
1033  class StringKey {
1034  public:
1035  size_t operator()(const SW &x) const { return x.Hash(); }
1036  };
1037 
1038  using Map = std::unordered_map<SW, Label, StringKey>;
1039 
1040  MutableFst<ToArc> *fst_;
1041  Map map_;
1042  Label lmax_;
1043  StateId state_;
1044  const SymbolTable *osymbols_;
1045  SymbolTable *isymbols_;
1046  mutable bool error_;
1047 };
1048 
1049 // TODO(kbg): Add common base class for those mappers which do nothing except
1050 // mutate their weights.
1051 
1052 // Mapper to add a constant to all weights.
1053 template <class A>
1054 class PlusMapper {
1055  public:
1056  using FromArc = A;
1057  using ToArc = A;
1058  using Weight = typename FromArc::Weight;
1059 
1060  constexpr explicit PlusMapper(Weight weight) : weight_(std::move(weight)) {}
1061 
1062  ToArc operator()(const FromArc &arc) const {
1063  if (arc.weight == Weight::Zero()) return arc;
1064  return ToArc(arc.ilabel, arc.olabel, Plus(arc.weight, weight_),
1065  arc.nextstate);
1066  }
1067 
1068  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
1069 
1071  return MAP_COPY_SYMBOLS;
1072  }
1073 
1075  return MAP_COPY_SYMBOLS;
1076  }
1077 
1078  constexpr uint64 Properties(uint64 props) const {
1079  return props & kWeightInvariantProperties;
1080  }
1081 
1082  private:
1083  const Weight weight_;
1084 };
1085 
1086 // Mapper to (right) multiply a constant to all weights.
1087 template <class A>
1089  public:
1090  using FromArc = A;
1091  using ToArc = A;
1092  using Weight = typename FromArc::Weight;
1093 
1094  constexpr explicit TimesMapper(Weight weight) : weight_(std::move(weight)) {}
1095 
1096  ToArc operator()(const FromArc &arc) const {
1097  if (arc.weight == Weight::Zero()) return arc;
1098  return ToArc(arc.ilabel, arc.olabel, Times(arc.weight, weight_),
1099  arc.nextstate);
1100  }
1101 
1102  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
1103 
1105  return MAP_COPY_SYMBOLS;
1106  }
1107 
1109  return MAP_COPY_SYMBOLS;
1110  }
1111 
1112  constexpr uint64 Properties(uint64 props) const {
1113  return props & kWeightInvariantProperties;
1114  }
1115 
1116  private:
1117  const Weight weight_;
1118 };
1119 
1120 // Mapper to take all weights to a constant power. The power argument is stored
1121 // as a double, so if there is a floating-point power implementation for this
1122 // weight type, it will take precedence. Otherwise, the power argument's 53 bits
1123 // of integer precision will be implicitly converted to a size_t and the default
1124 // power implementation (iterated multiplication) will be used instead.
1125 template <class A>
1127  public:
1128  using FromArc = A;
1129  using ToArc = A;
1130  using Weight = typename FromArc::Weight;
1131 
1132  explicit PowerMapper(double power) : power_(power) {}
1133 
1134  ToArc operator()(const FromArc &arc) const {
1135  return ToArc(arc.ilabel, arc.olabel, Power(arc.weight, power_),
1136  arc.nextstate);
1137  }
1138 
1139  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
1140 
1142  return MAP_COPY_SYMBOLS;
1143  }
1144 
1146  return MAP_COPY_SYMBOLS;
1147  }
1148 
1149  constexpr uint64 Properties(uint64 props) const {
1150  return props & kWeightInvariantProperties;
1151  }
1152 
1153  private:
1154  const double power_;
1155 };
1156 
1157 // Mapper to reciprocate all non-Zero() weights.
1158 template <class A>
1160  public:
1161  using FromArc = A;
1162  using ToArc = A;
1163  using Weight = typename FromArc::Weight;
1164 
1165  ToArc operator()(const FromArc &arc) const {
1166  if (arc.weight == Weight::Zero()) return arc;
1167  return ToArc(arc.ilabel, arc.olabel, Divide(Weight::One(), arc.weight),
1168  arc.nextstate);
1169  }
1170 
1171  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
1172 
1174  return MAP_COPY_SYMBOLS;
1175  }
1176 
1178  return MAP_COPY_SYMBOLS;
1179  }
1180 
1181  constexpr uint64 Properties(uint64 props) const {
1182  return props & kWeightInvariantProperties;
1183  }
1184 };
1185 
1186 // Mapper to map all non-Zero() weights to One().
1187 template <class A, class B = A>
1189  public:
1190  using FromArc = A;
1191  using ToArc = B;
1192  using FromWeight = typename FromArc::Weight;
1193  using ToWeight = typename ToArc::Weight;
1194 
1195  ToArc operator()(const FromArc &arc) const {
1196  return ToArc(arc.ilabel, arc.olabel,
1197  arc.weight != FromWeight::Zero() ?
1198  ToWeight::One() : ToWeight::Zero(),
1199  arc.nextstate);
1200  }
1201 
1202  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
1203 
1205  return MAP_COPY_SYMBOLS;
1206  }
1207 
1209  return MAP_COPY_SYMBOLS;
1210  }
1211 
1212  constexpr uint64 Properties(uint64 props) const {
1213  return (props & kWeightInvariantProperties) | kUnweighted;
1214  }
1215 };
1216 
1217 // Mapper to quantize all weights.
1218 template <class A, class B = A>
1220  public:
1221  using FromArc = A;
1222  using ToArc = B;
1223  using FromWeight = typename FromArc::Weight;
1224  using ToWeight = typename ToArc::Weight;
1225 
1226  QuantizeMapper() : delta_(kDelta) {}
1227 
1228  explicit QuantizeMapper(float d) : delta_(d) {}
1229 
1230  ToArc operator()(const FromArc &arc) const {
1231  return ToArc(arc.ilabel, arc.olabel, arc.weight.Quantize(delta_),
1232  arc.nextstate);
1233  }
1234 
1235  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
1236 
1238  return MAP_COPY_SYMBOLS;
1239  }
1240 
1242  return MAP_COPY_SYMBOLS;
1243  }
1244 
1245  constexpr uint64 Properties(uint64 props) const {
1246  return props & kWeightInvariantProperties;
1247  }
1248 
1249  private:
1250  const float delta_;
1251 };
1252 
1253 // Mapper from A to B under the assumption:
1254 //
1255 // B::Weight = A::Weight::ReverseWeight
1256 // B::Label == A::Label
1257 // B::StateId == A::StateId
1258 //
1259 // The weight is reversed, while the label and nextstate are preserved.
1260 template <class A, class B>
1262  public:
1263  using FromArc = A;
1264  using ToArc = B;
1265 
1266  constexpr ToArc operator()(const FromArc &arc) const {
1267  return ToArc(arc.ilabel, arc.olabel, arc.weight.Reverse(), arc.nextstate);
1268  }
1269 
1270  constexpr MapFinalAction FinalAction() const { return MAP_NO_SUPERFINAL; }
1271 
1273  return MAP_COPY_SYMBOLS;
1274  }
1275 
1277  return MAP_COPY_SYMBOLS;
1278  }
1279 
1280  constexpr uint64 Properties(uint64 props) const { return props; }
1281 };
1282 
1283 } // namespace fst
1284 
1285 #endif // FST_ARC_MAP_H_
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:897
typename Impl::Arc Arc
Definition: fst.h:871
ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:874
MapSymbolsAction
Definition: arc-map.h:39
ssize_t NumOutputEpsilons(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
Definition: expanded-fst.h:99
ArcMapFstImpl(const ArcMapFstImpl< A, B, C > &impl)
Definition: arc-map.h:330
void ArcMap(MutableFst< A > *fst, C *mapper)
Definition: arc-map.h:94
constexpr uint64 kAddSuperFinalProperties
Definition: properties.h:266
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:1272
typename FromArc::Weight Weight
Definition: arc-map.h:721
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:847
ExpectationWeight< X1, X2 > Divide(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2, DivideType typ=DIVIDE_ANY)
ArcMapFst(const Fst< A > &fst, const C &mapper)
Definition: arc-map.h:534
GallicToNewSymbolsMapper(MutableFst< ToArc > *fst)
Definition: arc-map.h:955
constexpr int kNoLabel
Definition: fst.h:179
Label ilabel
Definition: arc.h:102
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:672
uint64_t uint64
Definition: types.h:32
ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:727
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:845
QuantizeMapper(float d)
Definition: arc-map.h:1228
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:785
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:851
ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:1165
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:1173
virtual size_t NumArcs(StateId) const =0
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:704
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:1208
typename ToArc::Weight ToWeight
Definition: arc-map.h:1193
const Label & Value() const
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:1241
constexpr ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:694
constexpr int kStringBad
Definition: string-weight.h:23
size_t Hash() const
uint64 Properties(uint64 inprops) const
Definition: arc-map.h:905
constexpr ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:778
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:895
constexpr uint64 Properties(uint64 props) const
Definition: arc-map.h:1245
Arc::Weight Final(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
Definition: expanded-fst.h:82
virtual void AddArc(StateId, const Arc &arc)=0
ArcMapFst(const ArcMapFst< A, B, C > &fst, bool safe=false)
Definition: arc-map.h:543
constexpr ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:1266
Weight Final(StateId s)
Definition: arc-map.h:349
typename FromArc::Weight Weight
Definition: arc-map.h:1058
ArcMapFstImpl(const Fst< A > &fst, const C &mapper, const ArcMapFstOptions &opts)
Definition: arc-map.h:309
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:1015
void InitStateIterator(StateIteratorData< B > *data) const override
Definition: arc-map.h:632
typename FromArc::Label Label
Definition: arc-map.h:720
ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:1062
constexpr uint64 kILabelSorted
Definition: properties.h:75
typename ToArc::Weight ToWeight
Definition: arc-map.h:1224
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:1139
virtual void SetInputSymbols(const SymbolTable *isyms)=0
typename FromArc::Weight Weight
Definition: arc-map.h:1092
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:1021
size_t NumArcs(StateId s)
Definition: arc-map.h:386
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:1108
virtual Weight Final(StateId) const =0
ExpectationWeight< X1, X2 > Times(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2)
typename ToArc::Weight GW
Definition: arc-map.h:825
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:1202
SetType
Definition: set-weight.h:37
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:1235
virtual void SetStart(StateId)=0
constexpr uint64 Properties(uint64 props) const
Definition: arc-map.h:1078
ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:1096
ssize_t NumArcs(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
Definition: expanded-fst.h:88
Label olabel
Definition: arc.h:103
ArcMapFstImpl(const Fst< A > &fst, C *mapper, const ArcMapFstOptions &opts)
Definition: arc-map.h:320
ToArc operator()(const FromArc &arc)
Definition: arc-map.h:976
size_t NumInputEpsilons(StateId s)
Definition: arc-map.h:391
typename FromArc::Weight Weight
Definition: arc-map.h:1130
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:1102
constexpr uint64 kFstProperties
Definition: properties.h:301
constexpr uint64 kCopyProperties
Definition: properties.h:138
constexpr int kNoStateId
Definition: fst.h:180
FromGallicMapper(Label superfinal_label=0)
Definition: arc-map.h:871
constexpr uint64 kExpanded
Definition: properties.h:27
uint64 Properties(uint64 mask) const override
Definition: arc-map.h:404
ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:1230
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:1145
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:1204
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:1171
constexpr TimesMapper(Weight weight)
Definition: arc-map.h:1094
virtual uint64 Properties(uint64 mask, bool test) const =0
constexpr uint64 kSetArcProperties
Definition: properties.h:199
ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:1134
int64_t int64
Definition: types.h:27
typename Arc::Weight Weight
Definition: arc-map.h:291
#define FSTERROR()
Definition: util.h:35
constexpr uint64 kUnweighted
Definition: properties.h:87
constexpr uint64 Properties(uint64 props) const
Definition: arc-map.h:1149
uint64 ProjectProperties(uint64 inprops, bool project_input)
Definition: properties.cc:177
MapFinalAction
Definition: arc-map.h:24
typename Arc::Weight Weight
Definition: fst.h:873
constexpr PlusMapper(Weight weight)
Definition: arc-map.h:1060
virtual void ReserveArcs(StateId s, size_t n)
Definition: mutable-fst.h:79
constexpr int kStringInfinity
Definition: string-weight.h:22
StateIteratorBase< Arc > * base
Definition: fst.h:351
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:654
virtual void SetFinal(StateId, Weight)=0
ssize_t NumInputEpsilons(const ExpandedFst< Arc > &fst, typename Arc::StateId s)
Definition: expanded-fst.h:93
typename FromArc::Weight Weight
Definition: arc-map.h:1163
uint64 Properties(uint64 props) const
Definition: arc-map.h:855
StateIterator(const ArcMapFst< A, B, C > &fst)
Definition: arc-map.h:573
uint64 Properties(uint64 inprops) const
Definition: arc-map.h:1025
constexpr ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:668
typename FirstCacheStore< VectorCacheStore< CacheState< Arc > > >::State State
Definition: cache.h:648
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:678
constexpr uint64 Properties(uint64 props) const
Definition: arc-map.h:793
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:744
constexpr uint64 kOLabelSorted
Definition: properties.h:80
constexpr uint64 kIEpsilons
Definition: properties.h:65
constexpr uint64 Properties(uint64 props) const
Definition: arc-map.h:682
uint64 Properties(uint64 props) const
Definition: arc-map.h:748
constexpr WeightConvertMapper(const Converter &c=Converter())
Definition: arc-map.h:775
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:1070
size_t NumOutputEpsilons(StateId s)
Definition: arc-map.h:396
ArcMapFst(const Fst< A > &fst, const C &mapper, const ArcMapFstOptions &opts)
Definition: arc-map.h:528
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:1237
ExpectationWeight< X1, X2 > Plus(const ExpectationWeight< X1, X2 > &w1, const ExpectationWeight< X1, X2 > &w2)
virtual StateId Start() const =0
constexpr uint64 Properties(uint64 props) const
Definition: arc-map.h:1280
ArcMapFst(const Fst< A > &fst, C *mapper)
Definition: arc-map.h:538
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:1074
virtual void ReserveStates(StateId n)
Definition: mutable-fst.h:76
typename ToArc::StateId StateId
Definition: arc-map.h:950
typename FromArc::Weight GW
Definition: arc-map.h:869
typename FromArc::Weight GW
Definition: arc-map.h:952
typename ToArc::Weight AW
Definition: arc-map.h:868
constexpr uint64 kOLabelInvariantProperties
Definition: properties.h:245
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:698
typename Arc::StateId StateId
Definition: fst.h:872
constexpr uint64 kWeightInvariantProperties
Definition: properties.h:255
constexpr uint64 Properties(uint64 props) const
Definition: arc-map.h:708
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:1068
typename FromArc::Weight FromWeight
Definition: arc-map.h:772
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:674
virtual const SymbolTable * InputSymbols() const =0
Arc::StateId CountStates(const Fst< Arc > &fst)
Definition: expanded-fst.h:154
ArcIterator(const ArcMapFst< A, B, C > &fst, StateId s)
Definition: arc-map.h:625
void InitArcIterator(StateId s, ArcIteratorData< B > *data) const override
Definition: arc-map.h:553
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:783
ArcMapFst< A, B, C > * Copy(bool safe=false) const override
Definition: arc-map.h:547
typename ToArc::Label Label
Definition: arc-map.h:949
constexpr uint64 kError
Definition: properties.h:33
virtual StateId AddState()=0
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:1276
size_t Size() const
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:740
constexpr uint64 Properties(uint64 props) const
Definition: arc-map.h:1112
typename ToArc::Label Label
Definition: arc-map.h:867
SuperFinalMapper(Label final_label=0)
Definition: arc-map.h:724
ArcMapFst(const Fst< A > &fst, C *mapper, const ArcMapFstOptions &opts)
Definition: arc-map.h:531
constexpr uint64 kNullProperties
Definition: properties.h:131
virtual void DeleteStates(const std::vector< StateId > &)=0
typename Arc::StateId StateId
Definition: arc-map.h:290
StateId nextstate
Definition: arc.h:105
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:1104
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:1017
virtual void SetOutputSymbols(const SymbolTable *osyms)=0
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:648
typename FromArc::Weight AW
Definition: arc-map.h:824
Weight weight
Definition: arc.h:104
void Expand(const Fst< Arc > &ifst, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &parens, const std::vector< typename Arc::Label > &assignments, MutableFst< Arc > *ofst, const MPdtExpandOptions &opts)
Definition: expand.h:302
ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:1195
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:736
constexpr TropicalWeightTpl< T > Power(const TropicalWeightTpl< T > &w, V n)
Definition: float-weight.h:377
typename FromArc::Weight FromWeight
Definition: arc-map.h:1192
internal::ArcMapFstImpl< A, B, C > * GetMutableImpl() const
Definition: fst.h:947
ArcMapFstOptions(const CacheOptions &opts)
Definition: arc-map.h:277
constexpr ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:646
constexpr uint64 Properties(uint64 props) const
Definition: arc-map.h:658
constexpr MapFinalAction FinalAction() const
Definition: arc-map.h:1270
constexpr uint64 kILabelInvariantProperties
Definition: properties.h:236
uint64 Properties() const override
Definition: arc-map.h:401
constexpr float kDelta
Definition: weight.h:109
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:1177
constexpr uint64 kOEpsilons
Definition: properties.h:70
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:789
constexpr uint64 Properties(uint64 props) const
Definition: arc-map.h:1181
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:650
constexpr uint64 Properties(uint64 props) const
Definition: arc-map.h:1212
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:1141
constexpr MapSymbolsAction InputSymbolsAction() const
Definition: arc-map.h:700
constexpr MapSymbolsAction OutputSymbolsAction() const
Definition: arc-map.h:901
PowerMapper(double power)
Definition: arc-map.h:1132
ToArc operator()(const FromArc &arc) const
Definition: arc-map.h:827
void InitArcIterator(StateId s, ArcIteratorData< B > *data)
Definition: arc-map.h:412
const internal::ArcMapFstImpl< A, B, C > * GetImpl() const
Definition: fst.h:945
const StringWeight< Label, GallicStringType(G)> & Value1() const
Definition: pair-weight.h:76
typename ToArc::Weight AW
Definition: arc-map.h:951
virtual void SetProperties(uint64 props, uint64 mask)=0
typename ToArc::Weight ToWeight
Definition: arc-map.h:773
virtual const SymbolTable * OutputSymbols() const =0
void Expand(StateId s)
Definition: arc-map.h:417
typename FromArc::Weight FromWeight
Definition: arc-map.h:1223