FST  openfst-1.7.2
OpenFst Library
farprintstrings.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 // Outputs as strings the string FSTs in a finite-state archive.
5 
6 #include <string>
7 #include <vector>
8 
9 #include <fst/flags.h>
12 
13 DEFINE_string(filename_prefix, "", "Prefix to append to filenames");
14 DEFINE_string(filename_suffix, "", "Suffix to append to filenames");
15 DEFINE_int32(generate_filenames, 0,
16  "Generate N digit numeric filenames (def: use keys)");
17 DEFINE_string(begin_key, "",
18  "First key to extract (def: first key in archive)");
19 DEFINE_string(end_key, "", "Last key to extract (def: last key in archive)");
20 // PrintStringsMain specific flag definitions.
21 DEFINE_bool(print_key, false, "Prefix each string by its key");
22 DEFINE_bool(print_weight, false, "Suffix each string by its weight");
23 DEFINE_string(entry_type, "line",
24  "Entry type: one of : "
25  "\"file\" (one FST per file), \"line\" (one FST per line)");
26 DEFINE_string(token_type, "symbol",
27  "Token type: one of : "
28  "\"symbol\", \"byte\", \"utf8\"");
29 DEFINE_string(symbols, "", "Label symbol table");
30 DEFINE_bool(initial_symbols, true,
31  "Uses symbol table from the first Fst in archive for all entries.");
32 
33 int main(int argc, char **argv) {
34  namespace s = fst::script;
35 
36  string usage = "Print as string the string FSTs in an archive.\n\n Usage:";
37  usage += argv[0];
38  usage += " [in1.far in2.far ...]\n";
39 
40  std::set_new_handler(FailedNewHandler);
41  SET_FLAGS(usage.c_str(), &argc, &argv, true);
42  s::ExpandArgs(argc, argv, &argc, &argv);
43 
44  std::vector<string> in_fnames;
45  for (int i = 1; i < argc; ++i) in_fnames.push_back(argv[i]);
46  if (in_fnames.empty()) in_fnames.push_back("");
47 
48  const auto arc_type = s::LoadArcTypeFromFar(in_fnames[0]);
49  if (arc_type.empty()) return 1;
50 
51  fst::FarEntryType entry_type;
52  if (!s::GetFarEntryType(FLAGS_entry_type, &entry_type)) {
53  LOG(ERROR) << "Unknown or unsupported FAR entry type: " << FLAGS_entry_type;
54  return 1;
55  }
56 
57  fst::FarTokenType token_type;
58  if (!s::GetFarTokenType(FLAGS_token_type, &token_type)) {
59  LOG(ERROR) << "Unknown or unsupported FAR token type: " << FLAGS_token_type;
60  return 1;
61  }
62 
63  s::FarPrintStrings(in_fnames, arc_type, entry_type, token_type,
64  FLAGS_begin_key, FLAGS_end_key, FLAGS_print_key,
65  FLAGS_print_weight, FLAGS_symbols, FLAGS_initial_symbols,
66  FLAGS_generate_filenames, FLAGS_filename_prefix,
67  FLAGS_filename_suffix);
68 
69  return 0;
70 }
DEFINE_bool(print_key, false,"Prefix each string by its key")
void FarPrintStrings(const std::vector< string > &ifilenames, const string &arc_type, const FarEntryType entry_type, const FarTokenType token_type, const string &begin_key, const string &end_key, const bool print_key, const bool print_weight, const string &symbols_fname, const bool initial_symbols, const int32 generate_filenames, const string &filename_prefix, const string &filename_suffix)
Definition: farscript.cc:80
void ExpandArgs(int argc, char **argv, int *argcp, char ***argvp)
Definition: getters.cc:55
#define LOG(type)
Definition: log.h:48
bool GetFarTokenType(const string &str, FarTokenType *token_type)
Definition: getters.cc:42
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
FarTokenType
Definition: far.h:23
DEFINE_int32(generate_filenames, 0,"Generate N digit numeric filenames (def: use keys)")
bool GetFarEntryType(const string &str, FarEntryType *entry_type)
Definition: getters.cc:31
int main(int argc, char **argv)
DEFINE_string(filename_prefix,"","Prefix to append to filenames")
FarEntryType
Definition: far.h:21
string LoadArcTypeFromFar(const string &far_fname)
Definition: script-impl.cc:17