FST  openfst-1.7.2
OpenFst Library
farcreate.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 DEFINE_string(key_prefix, "", "Prefix to append to keys");
15 DEFINE_string(key_suffix, "", "Suffix to append to keys");
16 DEFINE_int32(generate_keys, 0,
17  "Generate N digit numeric keys (def: use file basenames)");
18 DEFINE_string(far_type, "default",
19  "FAR file format type: one of: \"default\", "
20  "\"stlist\", \"sttable\"");
21 DEFINE_bool(file_list_input, false,
22  "Each input file contains a list of files to be processed");
23 
24 int main(int argc, char **argv) {
25  namespace s = fst::script;
26 
27  string usage = "Creates a finite-state archive from input FSTs.\n\n Usage:";
28  usage += argv[0];
29  usage += " [in1.fst [[in2.fst ...] out.far]]\n";
30 
31  std::set_new_handler(FailedNewHandler);
32  SET_FLAGS(usage.c_str(), &argc, &argv, true);
33  s::ExpandArgs(argc, argv, &argc, &argv);
34 
35  std::vector<string> in_fnames;
36  if (FLAGS_file_list_input) {
37  for (int i = 1; i < argc - 1; ++i) {
38  std::ifstream istrm(argv[i]);
39  string str;
40  while (getline(istrm, str)) in_fnames.push_back(str);
41  }
42  } else {
43  for (int i = 1; i < argc - 1; ++i)
44  in_fnames.push_back(argv[i]);
45  }
46  if (in_fnames.empty())
47  in_fnames.push_back(argc == 2 && strcmp(argv[1], "-") != 0 ? argv[1] : "");
48 
49  string out_fname =
50  argc > 2 && strcmp(argv[argc - 1], "-") != 0 ? argv[argc - 1] : "";
51 
52  const auto arc_type = s::LoadArcTypeFromFst(in_fnames[0]);
53  if (arc_type.empty()) return 1;
54 
55  const auto far_type = s::GetFarType(FLAGS_far_type);
56 
57  s::FarCreate(in_fnames, out_fname, arc_type, FLAGS_generate_keys, far_type,
58  FLAGS_key_prefix, FLAGS_key_suffix);
59 
60  return 0;
61 }
FarType GetFarType(const string &str)
Definition: getters.cc:19
void ExpandArgs(int argc, char **argv, int *argcp, char ***argvp)
Definition: getters.cc:55
DEFINE_bool(file_list_input, false,"Each input file contains a list of files to be processed")
void FarCreate(const std::vector< string > &in_fnames, const string &out_fname, const string &arc_type, const int32 generate_keys, const FarType &far_type, const string &key_prefix, const string &key_suffix)
Definition: farscript.cc:29
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
DEFINE_int32(generate_keys, 0,"Generate N digit numeric keys (def: use file basenames)")
DEFINE_string(key_prefix,"","Prefix to append to keys")
int main(int argc, char **argv)
Definition: farcreate.cc:24
string LoadArcTypeFromFst(const string &fst_fname)
Definition: script-impl.cc:31