FST  openfst-1.7.1
OpenFst Library
fstrmepsilon-main.cc
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Removes epsilons from an FST.
5 
6 #include <cstring>
7 
8 #include <memory>
9 #include <string>
10 
11 #include <fst/flags.h>
12 #include <fst/log.h>
13 #include <fst/script/getters.h>
14 #include <fst/script/rmepsilon.h>
15 
16 DECLARE_bool(connect);
17 DECLARE_double(delta);
18 DECLARE_int64(nstate);
19 DECLARE_string(queue_type);
20 DECLARE_string(weight);
21 
22 int fstrmepsilon_main(int argc, char **argv) {
23  namespace s = fst::script;
26 
27  string usage = "Removes epsilons from an FST.\n\n Usage: ";
28  usage += argv[0];
29  usage += " [in.fst [out.fst]]\n";
30 
31  std::set_new_handler(FailedNewHandler);
32  SET_FLAGS(usage.c_str(), &argc, &argv, true);
33  if (argc > 3) {
34  ShowUsage();
35  return 1;
36  }
37 
38  const string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
39  const string out_name = argc > 2 ? argv[2] : "";
40 
41  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
42  if (!fst) return 1;
43 
44  const auto weight_threshold =
45  FLAGS_weight.empty() ? WeightClass::Zero(fst->WeightType())
46  : WeightClass(fst->WeightType(), FLAGS_weight);
47 
48  fst::QueueType queue_type;
49  if (!s::GetQueueType(FLAGS_queue_type, &queue_type)) {
50  LOG(ERROR) << argv[0]
51  << ": Unknown or unsupported queue type: " << FLAGS_queue_type;
52  return 1;
53  }
54 
55  const s::RmEpsilonOptions opts(queue_type, FLAGS_connect, weight_threshold,
56  FLAGS_nstate, FLAGS_delta);
57 
58  s::RmEpsilon(fst.get(), opts);
59 
60  return !fst->Write(out_name);
61 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
void RmEpsilon(MutableFst< Arc > *fst, std::vector< typename Arc::Weight > *distance, const RmEpsilonOptions< Arc, Queue > &opts)
Definition: rmepsilon.h:192
QueueType
Definition: queue.h:55
#define LOG(type)
Definition: log.h:48
int fstrmepsilon_main(int argc, char **argv)
DECLARE_bool(connect)
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
DECLARE_string(queue_type)
DECLARE_double(delta)
DECLARE_int64(nstate)
bool GetQueueType(const string &str, QueueType *queue_type)
Definition: getters.cc:99