FST  openfst-1.7.9
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 #include <memory>
8 #include <string>
9 
10 #include <fst/flags.h>
11 #include <fst/log.h>
12 #include <fst/script/getters.h>
13 #include <fst/script/rmepsilon.h>
14 
15 DECLARE_bool(connect);
16 DECLARE_double(delta);
17 DECLARE_int64(nstate);
18 DECLARE_string(queue_type);
19 DECLARE_string(weight);
20 
21 int fstrmepsilon_main(int argc, char **argv) {
22  namespace s = fst::script;
23  using fst::QueueType;
26 
27  std::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 std::string in_name =
39  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
40  const std::string out_name =
41  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
42 
43  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
44  if (!fst) return 1;
45 
46  const auto weight_threshold =
47  FLAGS_weight.empty() ? WeightClass::Zero(fst->WeightType())
48  : WeightClass(fst->WeightType(), FLAGS_weight);
49 
50  QueueType queue_type;
51  if (!s::GetQueueType(FLAGS_queue_type, &queue_type)) {
52  LOG(ERROR) << argv[0]
53  << ": Unknown or unsupported queue type: " << FLAGS_queue_type;
54  return 1;
55  }
56 
57  const s::RmEpsilonOptions opts(queue_type, FLAGS_connect, weight_threshold,
58  FLAGS_nstate, FLAGS_delta);
59 
60  s::RmEpsilon(fst.get(), opts);
61 
62  return !fst->Write(out_name);
63 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:123
void RmEpsilon(MutableFst< Arc > *fst, std::vector< typename Arc::Weight > *distance, const RmEpsilonOptions< Arc, Queue > &opts)
Definition: rmepsilon.h:198
QueueType
Definition: queue.h:57
#define LOG(type)
Definition: log.h:46
int fstrmepsilon_main(int argc, char **argv)
DECLARE_bool(connect)
void FailedNewHandler()
Definition: compat.cc:22
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:216
DECLARE_string(queue_type)
bool GetQueueType(const std::string &str, QueueType *queue_type)
Definition: getters.cc:115
DECLARE_double(delta)
DECLARE_int64(nstate)