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