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