FST  openfst-1.7.9
OpenFst Library
farcreate-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 a finite-state archive from input FSTs.
5 
6 #include <string>
7 #include <vector>
8 
9 #include <fst/flags.h>
12 #include <fstream>
13 
14 DECLARE_string(key_prefix);
15 DECLARE_string(key_suffix);
16 DECLARE_int32(generate_keys);
17 DECLARE_string(far_type);
18 DECLARE_bool(file_list_input);
19 
20 int farcreate_main(int argc, char **argv) {
21  namespace s = fst::script;
22 
23  std::string usage =
24  "Creates a finite-state archive from input FSTs.\n\n Usage:";
25  usage += argv[0];
26  usage += " [in1.fst [[in2.fst ...] out.far]]\n";
27 
28  std::set_new_handler(FailedNewHandler);
29  SET_FLAGS(usage.c_str(), &argc, &argv, true);
30  s::ExpandArgs(argc, argv, &argc, &argv);
31 
32  std::vector<std::string> in_sources;
33  if (FLAGS_file_list_input) {
34  for (int i = 1; i < argc - 1; ++i) {
35  std::ifstream istrm(argv[i]);
36  std::string str;
37  while (std::getline(istrm, str)) in_sources.push_back(str);
38  }
39  } else {
40  for (int i = 1; i < argc - 1; ++i)
41  in_sources.push_back(strcmp(argv[i], "-") != 0 ? argv[i] : "");
42  }
43  if (in_sources.empty()) {
44  // argc == 1 || argc == 2. This cleverly handles both the no-file case
45  // and the one (input) file case together.
46  // TODO(jrosenstock): This probably shouldn't happen for the
47  // --file_list_input case.
48  in_sources.push_back(argc == 2 && strcmp(argv[1], "-") != 0 ? argv[1] : "");
49  }
50 
51  // argc <= 2 means the file (if any) is an input file, so write to stdout.
52  const std::string out_source =
53  argc > 2 && strcmp(argv[argc - 1], "-") != 0 ? argv[argc - 1] : "";
54 
55  const auto arc_type = s::LoadArcTypeFromFst(in_sources[0]);
56  if (arc_type.empty()) return 1;
57 
58  fst::FarType far_type;
59  if (!s::GetFarType(FLAGS_far_type, &far_type)) {
60  LOG(ERROR) << "Unknown or unsupported FAR type: " << FLAGS_far_type;
61  return 1;
62  }
63 
64  s::FarCreate(in_sources, out_source, arc_type, FLAGS_generate_keys, far_type,
65  FLAGS_key_prefix, FLAGS_key_suffix);
66 
67  return 0;
68 }
DECLARE_int32(generate_keys)
bool GetFarType(const std::string &str, FarType *far_type)
Definition: getters.cc:19
void ExpandArgs(int argc, char **argv, int *argcp, char ***argvp)
Definition: getters.cc:45
int farcreate_main(int argc, char **argv)
#define LOG(type)
Definition: log.h:46
std::string LoadArcTypeFromFst(const std::string &fst_source)
Definition: script-impl.cc:31
void FailedNewHandler()
Definition: compat.cc:22
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:216
FarType
Definition: far.h:69
DECLARE_string(key_prefix)
void FarCreate(const std::vector< std::string > &in_sources, const std::string &out_source, const std::string &arc_type, const int32 generate_keys, const FarType &far_type, const std::string &key_prefix, const std::string &key_suffix)
Definition: farscript.cc:34
DECLARE_bool(file_list_input)