FST  openfst-1.7.3
OpenFst Library
fstarcsort-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 // Sorts arcs of 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/arcsort.h>
13 #include <fst/script/getters.h>
14 
15 DECLARE_string(sort_type);
16 
17 int fstarcsort_main(int argc, char **argv) {
18  namespace s = fst::script;
20 
21  std::string usage = "Sorts arcs of an FST.\n\n Usage: ";
22  usage += argv[0];
23  usage += " [in.fst [out.fst]]\n";
24 
25  std::set_new_handler(FailedNewHandler);
26  SET_FLAGS(usage.c_str(), &argc, &argv, true);
27 
28  if (argc > 3) {
29  ShowUsage();
30  return 1;
31  }
32 
33  const std::string in_name =
34  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
35  const std::string out_name =
36  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
37 
38  std::unique_ptr<MutableFstClass> fst(MutableFstClass::Read(in_name, true));
39  if (!fst) return 1;
40 
41  s::ArcSortType sort_type;
42  if (!s::GetArcSortType(FLAGS_sort_type, &sort_type)) {
43  LOG(ERROR) << argv[0] << ": Unknown or unsupported sort type: "
44  << FLAGS_sort_type;
45  return 1;
46  }
47 
48  s::ArcSort(fst.get(), sort_type);
49 
50  return !fst->Write(out_name);
51 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
int fstarcsort_main(int argc, char **argv)
#define LOG(type)
Definition: log.h:46
bool GetArcSortType(const std::string &str, ArcSortType *sort_type)
Definition: getters.cc:6
void FailedNewHandler()
Definition: compat.cc:20
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
void ArcSort(MutableFst< Arc > *fst, Compare comp)
Definition: arcsort.h:87
DECLARE_string(sort_type)