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