FST  openfst-1.7.1
OpenFst Library
mpdtexpand.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 // Expands a (bounded-stack) MPDT as an FST.
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 DEFINE_bool(connect, true, "Trim output?");
22 DEFINE_bool(keep_parentheses, false, "Keep PDT parentheses in result?");
23 
24 int main(int argc, char **argv) {
25  namespace s = fst::script;
30 
31  string usage = "Expand a (bounded-stack) MPDT as an FST.\n\n Usage: ";
32  usage += argv[0];
33  usage += " in.pdt [out.fst]\n";
34 
35  std::set_new_handler(FailedNewHandler);
36  SET_FLAGS(usage.c_str(), &argc, &argv, true);
37  if (argc > 3) {
38  ShowUsage();
39  return 1;
40  }
41 
42  const string in_name =
43  (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
44  const string out_name = argc > 2 ? argv[2] : "";
45 
46  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
47  if (!ifst) return 1;
48 
49  if (FLAGS_mpdt_parentheses.empty()) {
50  LOG(ERROR) << argv[0] << ": No MPDT parenthesis label pairs provided";
51  return 1;
52  }
53 
54  std::vector<s::LabelPair> parens;
55  std::vector<int64> assignments;
56  if (!ReadLabelTriples(FLAGS_mpdt_parentheses, &parens, &assignments, false))
57  return 1;
58 
59  VectorFstClass ofst(ifst->ArcType());
60 
61  const MPdtExpandOptions opts(FLAGS_connect, FLAGS_keep_parentheses);
62 
63  s::MPdtExpand(*ifst, parens, assignments, &ofst, opts);
64 
65  ofst.Write(out_name);
66 
67  return 0;
68 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
DEFINE_string(mpdt_parentheses,"","MPDT parenthesis label pairs with assignments")
DEFINE_bool(connect, true,"Trim output?")
#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
void MPdtExpand(const FstClass &ifst, const std::vector< LabelPair > &parens, const std::vector< int64 > &assignments, MutableFstClass *ofst, const MPdtExpandOptions &opts)
Definition: mpdtscript.cc:33
int main(int argc, char **argv)
Definition: mpdtexpand.cc:24