FST  openfst-1.7.2
OpenFst Library
fstintersect-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 // Intersects 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/getters.h>
14 #include <fst/script/intersect.h>
15 
16 DECLARE_string(compose_filter);
17 DECLARE_bool(connect);
18 
19 int fstintersect_main(int argc, char **argv) {
20  namespace s = fst::script;
21  using fst::ComposeFilter;
24 
25  string usage = "Intersects two FSAs.\n\n Usage: ";
26  usage += argv[0];
27  usage += " in1.fst in2.fst [out.fst]\n";
28  usage += " Flags: connect\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 = strcmp(argv[2], "-") == 0 ? "" : argv[2];
39  const string out_name = argc > 3 ? argv[3] : "";
40 
41  if (in1_name.empty() && in2_name.empty()) {
42  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input";
43  return 1;
44  }
45 
46  std::unique_ptr<FstClass> ifst1(FstClass::Read(in1_name));
47  if (!ifst1) return 1;
48  std::unique_ptr<FstClass> ifst2(FstClass::Read(in2_name));
49  if (!ifst2) return 1;
50 
51  VectorFstClass ofst(ifst1->ArcType());
52 
53  ComposeFilter compose_filter;
54  if (!s::GetComposeFilter(FLAGS_compose_filter, &compose_filter)) {
55  LOG(ERROR) << argv[0] << ": Unknown or unsupported compose filter type: "
56  << FLAGS_compose_filter;
57  return 1;
58  }
59 
60  const fst::IntersectOptions opts(FLAGS_connect, compose_filter);
61 
62  s::Intersect(*ifst1, *ifst2, &ofst, opts);
63 
64  return !ofst.Write(out_name);
65 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
int fstintersect_main(int argc, char **argv)
#define LOG(type)
Definition: log.h:48
void Intersect(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const IntersectOptions &opts=IntersectOptions())
Definition: intersect.h:127
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
DECLARE_bool(connect)
DECLARE_string(compose_filter)
ComposeFilter
Definition: compose.h:929
bool GetComposeFilter(const string &str, ComposeFilter *compose_filter)
Definition: getters.cc:17