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