FST  openfst-1.7.3
OpenFst Library
fstcompose-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 two FSTs.
5 
6 #include <cstring>
7 #include <memory>
8 #include <string>
9 
10 #include <fst/flags.h>
11 #include <fst/log.h>
12 #include <fst/script/compose.h>
13 #include <fst/script/getters.h>
14 
15 DECLARE_string(compose_filter);
16 DECLARE_bool(connect);
17 
18 int fstcompose_main(int argc, char **argv) {
19  namespace s = fst::script;
20  using fst::ComposeFilter;
21  using fst::ComposeOptions;
24 
25  std::string usage = "Composes two FSTs.\n\n Usage: ";
26  usage += argv[0];
27  usage += " in1.fst in2.fst [out.fst]\n";
28 
29  std::set_new_handler(FailedNewHandler);
30  SET_FLAGS(usage.c_str(), &argc, &argv, true);
31  if (argc < 3 || argc > 4) {
32  ShowUsage();
33  return 1;
34  }
35 
36  const std::string in1_name = strcmp(argv[1], "-") != 0 ? argv[1] : "";
37  const std::string in2_name =
38  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
39  const std::string out_name =
40  (argc > 3 && (strcmp(argv[3], "-") != 0)) ? argv[3] : "";
41 
42  if (in1_name.empty() && in2_name.empty()) {
43  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input";
44  return 1;
45  }
46 
47  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
48  if (!ifst1) return 1;
49 
50  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
51  if (!ifst2) return 1;
52 
53  if (ifst1->ArcType() != ifst2->ArcType()) {
54  LOG(ERROR) << argv[0] << ": Input FSTs must have the same arc type";
55  return 1;
56  }
57 
58  VectorFstClass ofst(ifst1->ArcType());
59 
60  ComposeFilter compose_filter;
61  if (!s::GetComposeFilter(FLAGS_compose_filter, &compose_filter)) {
62  LOG(ERROR) << argv[0] << ": Unknown or unsupported compose filter type: "
63  << FLAGS_compose_filter;
64  return 1;
65  }
66 
67  const ComposeOptions opts(FLAGS_connect, compose_filter);
68 
69  s::Compose(*ifst1, *ifst2, &ofst, opts);
70 
71  return !ofst.Write(out_name);
72 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
bool GetComposeFilter(const std::string &str, ComposeFilter *compose_filter)
Definition: getters.cc:17
#define LOG(type)
Definition: log.h:46
void FailedNewHandler()
Definition: compat.cc:20
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
DECLARE_bool(connect)
void Compose(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const ComposeOptions &opts=ComposeOptions())
Definition: compose.h:981
DECLARE_string(compose_filter)
int fstcompose_main(int argc, char **argv)
ComposeFilter
Definition: compose.h:929