FST  openfst-1.7.2
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 
8 #include <memory>
9 #include <string>
10 
11 #include <fst/flags.h>
12 #include <fst/log.h>
13 #include <fst/script/compose.h>
14 #include <fst/script/getters.h>
15 
16 DECLARE_string(compose_filter);
17 DECLARE_bool(connect);
18 
19 int fstcompose_main(int argc, char **argv) {
20  namespace s = fst::script;
21  using fst::ComposeFilter;
22  using fst::ComposeOptions;
25 
26  string usage = "Composes two FSTs.\n\n Usage: ";
27  usage += argv[0];
28  usage += " in1.fst in2.fst [out.fst]\n";
29 
30  std::set_new_handler(FailedNewHandler);
31  SET_FLAGS(usage.c_str(), &argc, &argv, true);
32  if (argc < 3 || argc > 4) {
33  ShowUsage();
34  return 1;
35  }
36 
37  const string in1_name = strcmp(argv[1], "-") != 0 ? argv[1] : "";
38  const string in2_name =
39  (argc > 2 && (strcmp(argv[2], "-") != 0)) ? argv[2] : "";
40  const string out_name = argc > 3 ? 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:124
#define LOG(type)
Definition: log.h:48
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
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
bool GetComposeFilter(const string &str, ComposeFilter *compose_filter)
Definition: getters.cc:17