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