FST  openfst-1.7.5
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 <utility>
10 #include <vector>
11 
12 #include <fst/flags.h>
13 #include <fst/types.h>
14 #include <fst/log.h>
18 #include <fst/util.h>
19 
20 DECLARE_string(mpdt_parentheses);
21 DECLARE_bool(left_mpdt);
22 DECLARE_bool(connect);
23 DECLARE_string(compose_filter);
24 
25 int mpdtcompose_main(int argc, char **argv) {
26  namespace s = fst::script;
32 
33  std::string usage = "Compose an MPDT and an FST.\n\n Usage: ";
34  usage += argv[0];
35  usage += " in.pdt in.fst [out.mpdt]\n";
36  usage += " in.fst in.pdt [out.mpdt]\n";
37 
38  std::set_new_handler(FailedNewHandler);
39  SET_FLAGS(usage.c_str(), &argc, &argv, true);
40  if (argc < 3 || argc > 4) {
41  ShowUsage();
42  return 1;
43  }
44 
45  const std::string in1_name = strcmp(argv[1], "-") == 0 ? "" : argv[1];
46  const std::string in2_name = strcmp(argv[2], "-") == 0 ? "" : argv[2];
47  const std::string out_name =
48  argc > 3 && strcmp(argv[3], "-") != 0 ? 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_mpdt_parentheses.empty()) {
61  LOG(ERROR) << argv[0] << ": No MPDT parenthesis label pairs provided";
62  return 1;
63  }
64 
65  std::vector<std::pair<int64, int64>> parens;
66  std::vector<int64> assignments;
67  if (!ReadLabelTriples(FLAGS_mpdt_parentheses, &parens, &assignments, false)) {
68  return 1;
69  }
70 
71  VectorFstClass ofst(ifst1->ArcType());
72 
73  PdtComposeFilter compose_filter;
74  if (!s::GetPdtComposeFilter(FLAGS_compose_filter, &compose_filter)) {
75  LOG(ERROR) << argv[0] << ": Unknown or unsupported compose filter type: "
76  << FLAGS_compose_filter;
77  return 1;
78  }
79 
80  const MPdtComposeOptions opts(FLAGS_connect, compose_filter);
81 
82  s::MPdtCompose(*ifst1, *ifst2, parens, assignments, &ofst, opts,
83  FLAGS_left_mpdt);
84 
85  return !ofst.Write(out_name);
86 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
void MPdtCompose(const FstClass &ifst1, const FstClass &ifst2, const std::vector< std::pair< int64, int64 >> &parens, const std::vector< int64 > &assignments, MutableFstClass *ofst, const MPdtComposeOptions &copts, bool left_pdt)
Definition: mpdtscript.cc:25
bool ReadLabelTriples(const std::string &source, std::vector< std::pair< Label, Label >> *pairs, std::vector< Label > *assignments, bool allow_negative=false)
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:22
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
DECLARE_bool(left_mpdt)