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