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