FST  openfst-1.7.3
OpenFst Library
fstrandgen-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 // Generates random paths through an FST.
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/getters.h>
13 #include <fst/script/randgen.h>
14 
15 DECLARE_int32(max_length);
16 DECLARE_int32(npath);
17 DECLARE_int32(seed);
18 DECLARE_string(select);
19 DECLARE_bool(weighted);
20 DECLARE_bool(remove_total_weight);
21 
22 int fstrandgen_main(int argc, char **argv) {
23  namespace s = fst::script;
26 
27  std::string usage = "Generates random paths through an FST.\n\n Usage: ";
28  usage += argv[0];
29  usage += " [in.fst [out.fst]]\n";
30 
31  std::set_new_handler(FailedNewHandler);
32  SET_FLAGS(usage.c_str(), &argc, &argv, true);
33  if (argc > 3) {
34  ShowUsage();
35  return 1;
36  }
37 
38  VLOG(1) << argv[0] << ": Seed = " << FLAGS_seed;
39 
40  const std::string in_name =
41  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
42  const std::string out_name =
43  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
44 
45  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
46  if (!ifst) return 1;
47 
48  VectorFstClass ofst(ifst->ArcType());
49 
51  if (!s::GetRandArcSelection(FLAGS_select, &ras)) {
52  LOG(ERROR) << argv[0] << ": Unknown or unsupported select type "
53  << FLAGS_select;
54  return 1;
55  }
56 
57  s::RandGen(*ifst, &ofst, FLAGS_seed,
59  ras, FLAGS_max_length, FLAGS_npath, FLAGS_weighted,
60  FLAGS_remove_total_weight));
61 
62  return !ofst.Write(out_name);
63 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:121
bool GetRandArcSelection(const std::string &str, RandArcSelection *ras)
Definition: getters.cc:88
#define LOG(type)
Definition: log.h:46
int fstrandgen_main(int argc, char **argv)
void FailedNewHandler()
Definition: compat.cc:20
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:209
void RandGen(const Fst< FromArc > &ifst, MutableFst< ToArc > *ofst, const RandGenOptions< Selector > &opts)
Definition: randgen.h:728
#define VLOG(level)
Definition: log.h:47
DECLARE_string(select)
DECLARE_bool(weighted)
DECLARE_int32(max_length)