FST  openfst-1.7.2
OpenFst Library
fstprune-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 // Prunes states and arcs of an FST w.r.t. the shortest path weight.
5 
6 #include <cstring>
7 
8 #include <memory>
9 #include <string>
10 
11 #include <fst/flags.h>
12 #include <fst/script/prune.h>
13 
14 DECLARE_double(delta);
15 DECLARE_int64(nstate);
16 DECLARE_string(weight);
17 
18 int fstprune_main(int argc, char **argv) {
19  namespace s = fst::script;
22 
23  string usage = "Prunes states and arcs of an FST.\n\n Usage: ";
24  usage += argv[0];
25  usage += " [in.fst [out.fst]]\n";
26 
27  std::set_new_handler(FailedNewHandler);
28  SET_FLAGS(usage.c_str(), &argc, &argv, true);
29  if (argc > 3) {
30  ShowUsage();
31  return 1;
32  }
33 
34  const string in_name = (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
35  const string out_name = argc > 2 ? argv[2] : "";
36 
37  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
38  if (!fst) return 1;
39 
40  const auto weight_threshold =
41  FLAGS_weight.empty() ? WeightClass::Zero(fst->WeightType())
42  : WeightClass(fst->WeightType(), FLAGS_weight);
43 
44  s::Prune(fst.get(), weight_threshold, FLAGS_nstate, FLAGS_delta);
45 
46  return !fst->Write(out_name);
47 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
void FailedNewHandler()
Definition: compat.cc:25
int fstprune_main(int argc, char **argv)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
void Prune(MutableFst< Arc > *fst, const PruneOptions< Arc, ArcFilter > &opts=PruneOptions< Arc, ArcFilter >())
Definition: prune.h:99
DECLARE_int64(nstate)
DECLARE_double(delta)
DECLARE_string(weight)