FST  openfst-1.8.3
OpenFst Library
closure.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 // Functions and classes to compute the concatenative closure of an FST.
19 
20 #ifndef FST_CLOSURE_H_
21 #define FST_CLOSURE_H_
22 
23 #include <algorithm>
24 #include <vector>
25 
26 #include <fst/arc.h>
27 #include <fst/cache.h>
28 #include <fst/float-weight.h>
29 #include <fst/fst.h>
30 #include <fst/impl-to-fst.h>
31 #include <fst/mutable-fst.h>
32 #include <fst/properties.h>
33 #include <fst/rational.h>
34 
35 namespace fst {
36 
37 // Computes the concatenative closure. This version modifies its
38 // MutableFst input. If an FST transduces string x to y with weight a,
39 // then its closure transduces x to y with weight a, xx to yy with
40 // weight Times(a, a), xxx to yyy with Times(Times(a, a), a),
41 // etc. If closure_type == CLOSURE_STAR, then the empty string is
42 // transduced to itself with weight Weight::One() as well.
43 //
44 // Complexity:
45 //
46 // Time: O(V)
47 // Space: O(V)
48 //
49 // where V is the number of states.
50 template <class Arc>
51 void Closure(MutableFst<Arc> *fst, ClosureType closure_type) {
52  using Weight = typename Arc::Weight;
53  const auto props = fst->Properties(kFstProperties, false);
54  const auto start = fst->Start();
55  for (StateIterator<MutableFst<Arc>> siter(*fst); !siter.Done();
56  siter.Next()) {
57  const auto s = siter.Value();
58  const auto weight = fst->Final(s);
59  if (weight != Weight::Zero()) fst->AddArc(s, Arc(0, 0, weight, start));
60  }
61  if (closure_type == CLOSURE_STAR) {
62  fst->ReserveStates(fst->NumStates() + 1);
63  const auto nstart = fst->AddState();
64  fst->SetStart(nstart);
65  fst->SetFinal(nstart);
66  if (start != kNoStateId) fst->AddArc(nstart, Arc(0, 0, start));
67  }
68  fst->SetProperties(ClosureProperties(props, closure_type == CLOSURE_STAR),
70 }
71 
72 // Computes the concatenative closure. This version modifies its
73 // RationalFst input.
74 template <class Arc>
75 void Closure(RationalFst<Arc> *fst, ClosureType closure_type) {
76  fst->GetMutableImpl()->AddClosure(closure_type);
77 }
78 
81 
82  explicit ClosureFstOptions(const RationalFstOptions &opts,
84  : RationalFstOptions(opts), type(type) {}
85 
86  explicit ClosureFstOptions(ClosureType type = CLOSURE_STAR) : type(type) {}
87 };
88 
89 // Computes the concatenative closure. This version is a delayed FST. If an FST
90 // transduces string x to y with weight a, then its closure transduces x to y
91 // with weight a, xx to yy with weight Times(a, a), xxx to yyy with weight
92 // Times(Times(a, a), a), etc. If closure_type == CLOSURE_STAR, then the empty
93 // string is transduced to itself with weight Weight::One() as well.
94 //
95 // Complexity:
96 //
97 // Time: O(v)
98 // Space: O(v)
99 //
100 // where v is the number of states visited. Constant time and space to visit an
101 // input state or arc is assumed and exclusive of caching.
102 template <class A>
103 class ClosureFst : public RationalFst<A> {
104  public:
105  using Arc = A;
106 
107  ClosureFst(const Fst<Arc> &fst, ClosureType closure_type) {
108  GetMutableImpl()->InitClosure(fst, closure_type);
109  }
110 
112  : RationalFst<A>(opts) {
113  GetMutableImpl()->InitClosure(fst, opts.type);
114  }
115 
116  // See Fst<>::Copy() for doc.
117  ClosureFst(const ClosureFst &fst, bool safe = false)
118  : RationalFst<A>(fst, safe) {}
119 
120  // Gets a copy of this ClosureFst. See Fst<>::Copy() for further doc.
121  ClosureFst *Copy(bool safe = false) const override {
122  return new ClosureFst(*this, safe);
123  }
124 
125  private:
127  using ImplToFst<internal::RationalFstImpl<Arc>>::GetMutableImpl;
128 };
129 
130 // Specialization for ClosureFst.
131 template <class Arc>
132 class StateIterator<ClosureFst<Arc>> : public StateIterator<RationalFst<Arc>> {
133  public:
135  : StateIterator<RationalFst<Arc>>(fst) {}
136 };
137 
138 // Specialization for ClosureFst.
139 template <class Arc>
140 class ArcIterator<ClosureFst<Arc>> : public ArcIterator<RationalFst<Arc>> {
141  public:
142  using StateId = typename Arc::StateId;
143 
145  : ArcIterator<RationalFst<Arc>>(fst, s) {}
146 };
147 
148 // Useful alias when using StdArc.
150 
151 } // namespace fst
152 
153 #endif // FST_CLOSURE_H_
ClosureFst(const Fst< Arc > &fst, const ClosureFstOptions &opts)
Definition: closure.h:111
ArcIterator(const ClosureFst< Arc > &fst, StateId s)
Definition: closure.h:144
virtual uint64_t Properties(uint64_t mask, bool test) const =0
void Closure(MutableFst< Arc > *fst, ClosureType closure_type)
Definition: closure.h:51
virtual Weight Final(StateId) const =0
virtual void SetStart(StateId)=0
ClosureType type
Definition: closure.h:80
typename ReplaceFst< Arc >::Arc Arc
Definition: fst.h:408
typename Arc::StateId StateId
Definition: closure.h:142
constexpr int kNoStateId
Definition: fst.h:196
uint64_t ClosureProperties(uint64_t inprops, bool star, bool delayed=false)
Definition: properties.cc:38
virtual void SetProperties(uint64_t props, uint64_t mask)=0
virtual StateId Start() const =0
ClosureFst * Copy(bool safe=false) const override
Definition: closure.h:121
StateIterator(const ClosureFst< Arc > &fst)
Definition: closure.h:134
ClosureFst(const Fst< Arc > &fst, ClosureType closure_type)
Definition: closure.h:107
constexpr uint64_t kFstProperties
Definition: properties.h:326
virtual void AddArc(StateId, const Arc &)=0
typename ReplaceFst< Arc >::Arc Arc
Definition: cache.h:1202
ClosureType
Definition: rational.h:45
virtual StateId AddState()=0
virtual void ReserveStates(size_t)
Definition: mutable-fst.h:101
virtual void SetFinal(StateId s, Weight weight=Weight::One())=0
ClosureFstOptions(const RationalFstOptions &opts, ClosureType type=CLOSURE_STAR)
Definition: closure.h:82
internal::RationalFstImpl< A > * GetMutableImpl() const
Definition: impl-to-fst.h:125
virtual StateId NumStates() const =0
ClosureFst(const ClosureFst &fst, bool safe=false)
Definition: closure.h:117
ClosureFstOptions(ClosureType type=CLOSURE_STAR)
Definition: closure.h:86