FST  openfst-1.7.1
OpenFst Library
fstminimize-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 // Minimizes a deterministic 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/minimize.h>
14 
15 DECLARE_double(delta);
16 DECLARE_bool(allow_nondet);
17 
18 int fstminimize_main(int argc, char **argv) {
19  namespace s = fst::script;
22 
23  string usage = "Minimizes a deterministic FST.\n\n Usage: ";
24  usage += argv[0];
25  usage += " [in.fst [out1.fst [out2.fst]]]\n";
26 
27  std::set_new_handler(FailedNewHandler);
28  SET_FLAGS(usage.c_str(), &argc, &argv, true);
29  if (argc > 4) {
30  ShowUsage();
31  return 1;
32  }
33 
34  const string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
35  const string out1_name =
36  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
37  const string out2_name =
38  (argc > 3 && strcmp(argv[3], "-") != 0) ? argv[3] : "";
39 
40  if (out1_name.empty() && out2_name.empty() && argc > 3) {
41  LOG(ERROR) << argv[0] << ": Both outputs can't be standard output.";
42  return 1;
43  }
44 
45  std::unique_ptr<MutableFstClass> fst1(MutableFstClass::Read(in_name, true));
46  if (!fst1) return 1;
47 
48  if (argc > 3) {
49  std::unique_ptr<MutableFstClass> fst2(new VectorFstClass(fst1->ArcType()));
50  s::Minimize(fst1.get(), fst2.get(), FLAGS_delta, FLAGS_allow_nondet);
51  if (!fst2->Write(out2_name)) return 1;
52  } else {
53  s::Minimize(fst1.get(), nullptr, FLAGS_delta, FLAGS_allow_nondet);
54  }
55 
56  return !fst1->Write(out1_name);
57 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
DECLARE_bool(allow_nondet)
#define LOG(type)
Definition: log.h:48
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
DECLARE_double(delta)
void Minimize(MutableFst< Arc > *fst, MutableFst< Arc > *sfst=nullptr, float delta=kShortestDelta, bool allow_nondet=false)
Definition: minimize.h:495
int fstminimize_main(int argc, char **argv)