1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
4 #ifndef FST_SCRIPT_RMEPSILON_H_
5 #define FST_SCRIPT_RMEPSILON_H_
10 #include <fst/queue.h>
11 #include <fst/rmepsilon.h>
12 #include <fst/script/fst-class.h>
13 #include <fst/script/shortest-distance.h>
14 #include <fst/script/weight-class.h>
20 struct RmEpsilonOptions : public ShortestDistanceOptions {
22 const WeightClass &weight_threshold;
23 const int64 state_threshold;
25 RmEpsilonOptions(QueueType queue_type, bool connect,
26 const WeightClass &weight_threshold,
27 int64 state_threshold = kNoStateId,
29 : ShortestDistanceOptions(queue_type, EPSILON_ARC_FILTER, kNoStateId,
32 weight_threshold(weight_threshold),
33 state_threshold(state_threshold) {}
38 // Code to implement switching on queue types.
40 template <class Arc, class Queue>
41 void RmEpsilon(MutableFst<Arc> *fst,
42 std::vector<typename Arc::Weight> *distance,
43 const RmEpsilonOptions &opts,
45 using Weight = typename Arc::Weight;
46 const fst::RmEpsilonOptions<Arc, Queue> ropts(queue, opts.delta,
47 opts.connect, *opts.weight_threshold.GetWeight<Weight>(),
48 opts.state_threshold);
49 RmEpsilon(fst, distance, ropts);
53 void RmEpsilon(MutableFst<Arc> *fst,
54 const RmEpsilonOptions &opts) {
55 using StateId = typename Arc::StateId;
56 using Weight = typename Arc::Weight;
57 std::vector<Weight> distance;
58 switch (opts.queue_type) {
60 AutoQueue<StateId> queue(*fst, &distance, EpsilonArcFilter<Arc>());
61 RmEpsilon(fst, &distance, opts, &queue);
65 FifoQueue<StateId> queue;
66 RmEpsilon(fst, &distance, opts, &queue);
70 LifoQueue<StateId> queue;
71 RmEpsilon(fst, &distance, opts, &queue);
74 case SHORTEST_FIRST_QUEUE: {
75 NaturalShortestFirstQueue<StateId, Weight> queue(distance);
76 RmEpsilon(fst, &distance, opts, &queue);
79 case STATE_ORDER_QUEUE: {
80 StateOrderQueue<StateId> queue;
81 RmEpsilon(fst, &distance, opts, &queue);
84 case TOP_ORDER_QUEUE: {
85 TopOrderQueue<StateId> queue(*fst, EpsilonArcFilter<Arc>());
86 internal::RmEpsilon(fst, &distance, opts, &queue);
90 FSTERROR() << "RmEpsilon: Unknown queue type: "
92 fst->SetProperties(kError, kError);
98 } // namespace internal
100 using RmEpsilonArgs = std::pair<MutableFstClass *, const RmEpsilonOptions &>;
103 void RmEpsilon(RmEpsilonArgs *args) {
104 MutableFst<Arc> *fst = std::get<0>(*args)->GetMutableFst<Arc>();
105 const auto &opts = std::get<1>(*args);
106 internal::RmEpsilon(fst, opts);
109 void RmEpsilon(MutableFstClass *fst, const RmEpsilonOptions &opts);
111 } // namespace script
114 #endif // FST_SCRIPT_RMEPSILON_H_