FST  openfst-1.7.2
OpenFst Library
fstreverse-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 // Reverses the paths in an FST.
5 
6 #include <cstring>
7 
8 #include <memory>
9 #include <string>
10 
11 #include <fst/flags.h>
12 #include <fst/script/fst-class.h>
13 #include <fst/script/reverse.h>
14 
15 DECLARE_bool(require_superinitial);
16 
17 int fstreverse_main(int argc, char **argv) {
18  namespace s = fst::script;
21 
22  string usage = "Reverses the paths in an FST.\n\n Usage: ";
23  usage += argv[0];
24  usage += " [in.fst [out.fst]]\n";
25 
26  std::set_new_handler(FailedNewHandler);
27  SET_FLAGS(usage.c_str(), &argc, &argv, true);
28  if (argc > 3) {
29  ShowUsage();
30  return 1;
31  }
32 
33  const string in_name =
34  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
35  const string out_name = argc > 2 ? argv[2] : "";
36 
37  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
38  if (!ifst) return 1;
39 
40  VectorFstClass ofst(ifst->ArcType());
41 
42  s::Reverse(*ifst, &ofst, FLAGS_require_superinitial);
43 
44  return !ofst.Write(out_name);
45 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
int fstreverse_main(int argc, char **argv)
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
void Reverse(const Fst< Arc > &ifst, const std::vector< std::pair< typename Arc::Label, typename Arc::Label >> &parens, std::vector< typename Arc::Label > *assignments, MutableFst< RevArc > *ofst)
Definition: reverse.h:20
DECLARE_bool(require_superinitial)