FST  openfst-1.7.1
OpenFst Library
pdtreverse.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 a PDT.
5 
6 #include <cstring>
7 
8 #include <memory>
9 #include <string>
10 #include <vector>
11 
12 #include <fst/flags.h>
13 #include <fst/log.h>
14 
16 #include <fst/util.h>
17 
18 DEFINE_string(pdt_parentheses, "", "PDT parenthesis label pairs");
19 
20 int main(int argc, char **argv) {
21  namespace s = fst::script;
22  using fst::ReadLabelPairs;
25 
26  string usage = "Reverse a PDT.\n\n Usage: ";
27  usage += argv[0];
28  usage += " in.pdt [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 string in_name =
38  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
39  const string out_name = argc > 2 ? argv[2] : "";
40 
41  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
42  if (!ifst) return 1;
43 
44  if (FLAGS_pdt_parentheses.empty()) {
45  LOG(ERROR) << argv[0] << ": No PDT parenthesis label pairs provided";
46  return 1;
47  }
48 
49  std::vector<s::LabelPair> parens;
50  if (!ReadLabelPairs(FLAGS_pdt_parentheses, &parens, false)) return 1;
51 
52  VectorFstClass ofst(ifst->ArcType());
53 
54  s::PdtReverse(*ifst, parens, &ofst);
55 
56  ofst.Write(out_name);
57 
58  return 0;
59 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
bool ReadLabelPairs(const string &filename, std::vector< std::pair< Label, Label >> *pairs, bool allow_negative=false)
Definition: util.h:331
int main(int argc, char **argv)
Definition: pdtreverse.cc:20
void PdtReverse(const FstClass &ifst, const std::vector< LabelPair > &parens, MutableFstClass *ofst)
Definition: pdtscript.cc:67
#define LOG(type)
Definition: log.h:48
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
DEFINE_string(pdt_parentheses,"","PDT parenthesis label pairs")