FST  openfst-1.7.3
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;
25 
26  std::string usage = "Removes epsilons from an FST.\n\n Usage: ";
27  usage += argv[0];
28  usage += " [in.fst [out.fst]]\n";
29 
30  std::set_new_handler(FailedNewHandler);
31  SET_FLAGS(usage.c_str(), &argc, &argv, true);
32  if (argc > 3) {
33  ShowUsage();
34  return 1;
35  }
36 
37  const std::string in_name =
38  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
39  const std::string out_name =
40  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
41 
42  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
43  if (!fst) return 1;
44 
45  const auto weight_threshold =
46  FLAGS_weight.empty() ? WeightClass::Zero(fst->WeightType())
47  : WeightClass(fst->WeightType(), FLAGS_weight);
48 
49  fst::QueueType queue_type;
50  if (!s::GetQueueType(FLAGS_queue_type, &queue_type)) {
51  LOG(ERROR) << argv[0]
52  << ": Unknown or unsupported queue type: " << FLAGS_queue_type;
53  return 1;
54  }
55 
56  const s::RmEpsilonOptions opts(queue_type, FLAGS_connect, weight_threshold,
57  FLAGS_nstate, FLAGS_delta);
58 
59  s::RmEpsilon(fst.get(), opts);
60 
61  return !fst->Write(out_name);
62 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
void RmEpsilon(MutableFst< Arc > *fst, std::vector< typename Arc::Weight > *distance, const RmEpsilonOptions< Arc, Queue > &opts)
Definition: rmepsilon.h:192
QueueType
Definition: queue.h:56
#define LOG(type)
Definition: log.h:46
int fstrmepsilon_main(int argc, char **argv)
DECLARE_bool(connect)
void FailedNewHandler()
Definition: compat.cc:20
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
DECLARE_string(queue_type)
bool GetQueueType(const std::string &str, QueueType *queue_type)
Definition: getters.cc:101
DECLARE_double(delta)
DECLARE_int64(nstate)