FST  openfst-1.7.1
OpenFst Library
mpdtcompose.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 // Composes an MPDT and 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 
18 #include <fst/util.h>
19 
20 DEFINE_string(mpdt_parentheses, "",
21  "MPDT parenthesis label pairs with assignments");
22 DEFINE_bool(left_mpdt, true, "Is the first argument the MPDT?");
23 DEFINE_bool(connect, true, "Trim output?");
24 DEFINE_string(compose_filter, "paren",
25  "Composition filter, one of: \"expand\", \"expand_paren\", "
26  "\"paren\"");
27 
28 int main(int argc, char **argv) {
29  namespace s = fst::script;
35 
36  string usage = "Compose an MPDT and an FST.\n\n Usage: ";
37  usage += argv[0];
38  usage += " in.pdt in.fst [out.mpdt]\n";
39  usage += " in.fst in.pdt [out.mpdt]\n";
40 
41  std::set_new_handler(FailedNewHandler);
42  SET_FLAGS(usage.c_str(), &argc, &argv, true);
43  if (argc < 3 || argc > 4) {
44  ShowUsage();
45  return 1;
46  }
47 
48  const string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
49  const string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
50  const string out_name = argc > 3 ? argv[3] : "";
51 
52  if (in1_name.empty() && in2_name.empty()) {
53  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input.";
54  return 1;
55  }
56 
57  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
58  if (!ifst1) return 1;
59  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
60  if (!ifst2) return 1;
61 
62  if (FLAGS_mpdt_parentheses.empty()) {
63  LOG(ERROR) << argv[0] << ": No MPDT parenthesis label pairs provided";
64  return 1;
65  }
66 
67  std::vector<s::LabelPair> parens;
68  std::vector<int64> assignments;
69  if (!ReadLabelTriples(FLAGS_mpdt_parentheses, &parens, &assignments, false))
70  return 1;
71 
72  VectorFstClass ofst(ifst1->ArcType());
73 
74  PdtComposeFilter compose_filter;
75  if (!s::GetPdtComposeFilter(FLAGS_compose_filter, &compose_filter)) {
76  LOG(ERROR) << argv[0] << ": Unknown or unsupported compose filter type: "
77  << FLAGS_compose_filter;
78  return 1;
79  }
80 
81  const MPdtComposeOptions opts(FLAGS_connect, compose_filter);
82 
83  s::MPdtCompose(*ifst1, *ifst2, parens, assignments, &ofst, opts,
84  FLAGS_left_mpdt);
85 
86  ofst.Write(out_name);
87 
88  return 0;
89 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
DEFINE_string(mpdt_parentheses,"","MPDT parenthesis label pairs with assignments")
void MPdtCompose(const FstClass &ifst1, const FstClass &ifst2, const std::vector< LabelPair > &parens, const std::vector< int64 > &assignments, MutableFstClass *ofst, const MPdtComposeOptions &copts, bool left_pdt)
Definition: mpdtscript.cc:22
int main(int argc, char **argv)
Definition: mpdtcompose.cc:28
DEFINE_bool(left_mpdt, true,"Is the first argument the MPDT?")
#define LOG(type)
Definition: log.h:48
PdtComposeFilter
Definition: compose.h:436
bool GetPdtComposeFilter(const string &str, PdtComposeFilter *cf)
Definition: getters.cc:9
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