FST  openfst-1.8.3
OpenFst Library
rmepsilon.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 #ifndef FST_SCRIPT_RMEPSILON_H_
19 #define FST_SCRIPT_RMEPSILON_H_
20 
21 #include <cstdint>
22 #include <utility>
23 #include <vector>
24 
25 #include <fst/log.h>
26 #include <fst/arcfilter.h>
27 #include <fst/fst.h>
28 #include <fst/mutable-fst.h>
29 #include <fst/properties.h>
30 #include <fst/queue.h>
31 #include <fst/rmepsilon.h>
32 #include <fst/util.h>
33 #include <fst/weight.h>
35 #include <fst/script/fst-class.h>
38 
39 namespace fst {
40 namespace script {
41 
43  const bool connect;
45  const int64_t state_threshold;
46 
48  const WeightClass &weight_threshold,
49  int64_t state_threshold = kNoStateId, float delta = kDelta)
51  delta),
52  connect(connect),
53  weight_threshold(weight_threshold),
54  state_threshold(state_threshold) {}
55 };
56 
57 namespace internal {
58 
59 // Code to implement switching on queue types.
60 
61 template <class Arc, class Queue>
63  std::vector<typename Arc::Weight> *distance,
64  const RmEpsilonOptions &opts, Queue *queue) {
65  using Weight = typename Arc::Weight;
67  queue, opts.delta, opts.connect,
68  *opts.weight_threshold.GetWeight<Weight>(), opts.state_threshold);
69  RmEpsilon(fst, distance, ropts);
70 }
71 
72 template <class Arc>
74  using StateId = typename Arc::StateId;
75  using Weight = typename Arc::Weight;
76  std::vector<Weight> distance;
77  switch (opts.queue_type) {
78  case AUTO_QUEUE: {
79  AutoQueue<StateId> queue(*fst, &distance, EpsilonArcFilter<Arc>());
80  RmEpsilon(fst, &distance, opts, &queue);
81  return;
82  }
83  case FIFO_QUEUE: {
84  FifoQueue<StateId> queue;
85  RmEpsilon(fst, &distance, opts, &queue);
86  return;
87  }
88  case LIFO_QUEUE: {
89  LifoQueue<StateId> queue;
90  RmEpsilon(fst, &distance, opts, &queue);
91  return;
92  }
93  case SHORTEST_FIRST_QUEUE: {
94  if constexpr (IsIdempotent<Weight>::value) {
96  RmEpsilon(fst, &distance, opts, &queue);
97  } else {
98  FSTERROR() << "RmEpsilon: Bad queue type SHORTEST_FIRST_QUEUE for"
99  << " non-idempotent Weight " << Weight::Type();
100  fst->SetProperties(kError, kError);
101  }
102  return;
103  }
104  case STATE_ORDER_QUEUE: {
106  RmEpsilon(fst, &distance, opts, &queue);
107  return;
108  }
109  case TOP_ORDER_QUEUE: {
111  internal::RmEpsilon(fst, &distance, opts, &queue);
112  return;
113  }
114  default: {
115  FSTERROR() << "RmEpsilon: Unknown queue type: " << opts.queue_type;
116  fst->SetProperties(kError, kError);
117  return;
118  }
119  }
120 }
121 
122 } // namespace internal
123 
124 using FstRmEpsilonArgs = std::pair<MutableFstClass *, const RmEpsilonOptions &>;
125 
126 template <class Arc>
128  MutableFst<Arc> *fst = std::get<0>(*args)->GetMutableFst<Arc>();
129  const auto &opts = std::get<1>(*args);
130  internal::RmEpsilon(fst, opts);
131 }
132 
133 void RmEpsilon(MutableFstClass *fst, const RmEpsilonOptions &opts);
134 
135 } // namespace script
136 } // namespace fst
137 
138 #endif // FST_SCRIPT_RMEPSILON_H_
QueueType
Definition: queue.h:76
const WeightClass & weight_threshold
Definition: rmepsilon.h:44
constexpr uint64_t kError
Definition: properties.h:52
std::bool_constant<(W::Properties()&kIdempotent)!=0 > IsIdempotent
Definition: weight.h:159
constexpr int kNoStateId
Definition: fst.h:196
#define FSTERROR()
Definition: util.h:56
RmEpsilonOptions(QueueType queue_type, bool connect, const WeightClass &weight_threshold, int64_t state_threshold=kNoStateId, float delta=kDelta)
Definition: rmepsilon.h:47
virtual void SetProperties(uint64_t props, uint64_t mask)=0
const W * GetWeight() const
Definition: weight-class.h:144
const int64_t state_threshold
Definition: rmepsilon.h:45
std::pair< MutableFstClass *, const RmEpsilonOptions & > FstRmEpsilonArgs
Definition: rmepsilon.h:124
void RmEpsilon(FstRmEpsilonArgs *args)
Definition: rmepsilon.h:127
void RmEpsilon(MutableFst< Arc > *fst, std::vector< typename Arc::Weight > *distance, const RmEpsilonOptions &opts, Queue *queue)
Definition: rmepsilon.h:62
constexpr float kDelta
Definition: weight.h:133