FST  openfst-1.7.3
OpenFst Library
fstunion-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 // Creates the union of 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/union.h>
13 
14 int fstunion_main(int argc, char **argv) {
15  namespace s = fst::script;
18 
19  std::string usage = "Creates the union of two FSTs.\n\n Usage: ";
20  usage += argv[0];
21  usage += " in1.fst in2.fst [out.fst]\n";
22 
23  std::set_new_handler(FailedNewHandler);
24  SET_FLAGS(usage.c_str(), &argc, &argv, true);
25  if (argc < 3 || argc > 4) {
26  ShowUsage();
27  return 1;
28  }
29 
30  const std::string in1_name = strcmp(argv[1], "-") != 0 ? argv[1] : "";
31  const std::string in2_name = strcmp(argv[2], "-") != 0 ? argv[2] : "";
32  const std::string out_name =
33  argc > 3 && strcmp(argv[3], "-") != 0 ? argv[3] : "";
34 
35  if (in1_name == "" && in2_name == "") {
36  LOG(ERROR) << argv[0] << ": Can't take both inputs from standard input";
37  return 1;
38  }
39 
40  std::unique_ptr<MutableFstClass> fst1(MutableFstClass::Read(in1_name, true));
41  if (!fst1) return 1;
42 
43  std::unique_ptr<FstClass> fst2(FstClass::Read(in2_name));
44  if (!fst2) return 1;
45 
46  s::Union(fst1.get(), *fst2);
47 
48  return !fst1->Write(out_name);
49 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
#define LOG(type)
Definition: log.h:46
void FailedNewHandler()
Definition: compat.cc:20
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
void Union(RationalFst< Arc > *fst1, const Fst< Arc > &fst2)
Definition: union.h:84
int fstunion_main(int argc, char **argv)