FST  openfst-1.7.2
OpenFst Library
mpdtreverse.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 an MPDT.
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 
17 #include <fst/util.h>
18 
19 DEFINE_string(mpdt_parentheses, "",
20  "MPDT parenthesis label pairs with assignments.");
21 
22 DEFINE_string(mpdt_new_parentheses, "",
23  "Output for reassigned parentheses and stacks");
24 
25 int main(int argc, char **argv) {
26  namespace s = fst::script;
31 
32  string usage = "Reverse an MPDT.\n\n Usage: ";
33  usage += argv[0];
34  usage += " in.pdt [out.fst]\n";
35 
36  std::set_new_handler(FailedNewHandler);
37  SET_FLAGS(usage.c_str(), &argc, &argv, true);
38  if (argc > 3) {
39  ShowUsage();
40  return 1;
41  }
42 
43  const string in_name =
44  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
45  const string out_name = argc > 2 ? argv[2] : "";
46 
47  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
48  if (!ifst) return 1;
49 
50  if (FLAGS_mpdt_parentheses.empty()) {
51  LOG(ERROR) << argv[0] << ": No MPDT parenthesis label pairs provided";
52  return 1;
53  }
54 
55  if (FLAGS_mpdt_new_parentheses.empty()) {
56  LOG(ERROR) << argv[0] << ": No MPDT output parenthesis label file provided";
57  return 1;
58  }
59 
60  std::vector<s::LabelPair> parens;
61  std::vector<int64> assignments;
62  if (!ReadLabelTriples(FLAGS_mpdt_parentheses, &parens, &assignments, false))
63  return 1;
64 
65  VectorFstClass ofst(ifst->ArcType());
66 
67  s::MPdtReverse(*ifst, parens, &assignments, &ofst);
68 
69  ofst.Write(out_name);
70 
71  if (!WriteLabelTriples(FLAGS_mpdt_new_parentheses, parens, assignments))
72  return 1;
73 
74  return 0;
75 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
DEFINE_string(mpdt_parentheses,"","MPDT parenthesis label pairs with assignments.")
void MPdtReverse(const FstClass &ifst, const std::vector< LabelPair > &parens, std::vector< int64 > *assignments, MutableFstClass *ofst)
Definition: mpdtscript.cc:46
#define LOG(type)
Definition: log.h:48
void FailedNewHandler()
Definition: compat.cc:25
bool ReadLabelTriples(const string &filename, std::vector< std::pair< Label, Label >> *pairs, std::vector< Label > *assignments, bool allow_negative=false)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
int main(int argc, char **argv)
Definition: mpdtreverse.cc:25
bool WriteLabelTriples(const string &filename, const std::vector< std::pair< Label, Label >> &pairs, const std::vector< Label > &assignments)