FST  openfst-1.7.3
OpenFst Library
mpdtcompose-main.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 #include <memory>
8 #include <string>
9 #include <vector>
10 
11 #include <fst/flags.h>
12 #include <fst/log.h>
16 #include <fst/util.h>
17 
18 DECLARE_string(mpdt_parentheses);
19 DECLARE_bool(left_mpdt);
20 DECLARE_bool(connect);
21 DECLARE_string(compose_filter);
22 
23 int mpdtcompose_main(int argc, char **argv) {
24  namespace s = fst::script;
30 
31  std::string usage = "Compose an MPDT and an FST.\n\n Usage: ";
32  usage += argv[0];
33  usage += " in.pdt in.fst [out.mpdt]\n";
34  usage += " in.fst in.pdt [out.mpdt]\n";
35 
36  std::set_new_handler(FailedNewHandler);
37  SET_FLAGS(usage.c_str(), &argc, &argv, true);
38  if (argc < 3 || argc > 4) {
39  ShowUsage();
40  return 1;
41  }
42 
43  const std::string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
44  const std::string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
45  const std::string out_name =
46  argc > 3 && strcmp(argv[3], "-") != 0 ? argv[3] : "";
47 
48  if (in1_name.empty() && in2_name.empty()) {
49  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input.";
50  return 1;
51  }
52 
53  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
54  if (!ifst1) return 1;
55  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
56  if (!ifst2) return 1;
57 
58  if (FLAGS_mpdt_parentheses.empty()) {
59  LOG(ERROR) << argv[0] << ": No MPDT parenthesis label pairs provided";
60  return 1;
61  }
62 
63  std::vector<s::LabelPair> parens;
64  std::vector<int64> assignments;
65  if (!ReadLabelTriples(FLAGS_mpdt_parentheses, &parens, &assignments, false)) {
66  return 1;
67  }
68 
69  VectorFstClass ofst(ifst1->ArcType());
70 
71  PdtComposeFilter compose_filter;
72  if (!s::GetPdtComposeFilter(FLAGS_compose_filter, &compose_filter)) {
73  LOG(ERROR) << argv[0] << ": Unknown or unsupported compose filter type: "
74  << FLAGS_compose_filter;
75  return 1;
76  }
77 
78  const MPdtComposeOptions opts(FLAGS_connect, compose_filter);
79 
80  s::MPdtCompose(*ifst1, *ifst2, parens, assignments, &ofst, opts,
81  FLAGS_left_mpdt);
82 
83  return !ofst.Write(out_name);
84 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
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 mpdtcompose_main(int argc, char **argv)
DECLARE_string(mpdt_parentheses)
#define LOG(type)
Definition: log.h:46
PdtComposeFilter
Definition: compose.h:436
bool GetPdtComposeFilter(const std::string &str, PdtComposeFilter *cf)
Definition: getters.cc:9
void FailedNewHandler()
Definition: compat.cc:20
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
DECLARE_bool(left_mpdt)
bool ReadLabelTriples(const std::string &filename, std::vector< std::pair< Label, Label >> *pairs, std::vector< Label > *assignments, bool allow_negative=false)