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