FST  openfst-1.7.9
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_uint64(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;
24  using fst::RandGenOptions;
27 
28  std::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 std::string in_name =
42  (argc > 1 && strcmp(argv[1], "-") != 0) ? argv[1] : "";
43  const std::string out_name =
44  (argc > 2 && strcmp(argv[2], "-") != 0) ? argv[2] : "";
45 
46  std::unique_ptr<FstClass> ifst(FstClass::Read(in_name));
47  if (!ifst) return 1;
48 
49  VectorFstClass ofst(ifst->ArcType());
50 
52  if (!s::GetRandArcSelection(FLAGS_select, &ras)) {
53  LOG(ERROR) << argv[0] << ": Unknown or unsupported select type "
54  << FLAGS_select;
55  return 1;
56  }
57 
58  s::RandGen(*ifst, &ofst,
59  RandGenOptions<s::RandArcSelection>(ras, FLAGS_max_length,
60  FLAGS_npath, FLAGS_weighted,
61  FLAGS_remove_total_weight),
62  FLAGS_seed);
63 
64  return !ofst.Write(out_name);
65 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:123
bool GetRandArcSelection(const std::string &str, RandArcSelection *ras)
Definition: getters.cc:102
#define LOG(type)
Definition: log.h:46
int fstrandgen_main(int argc, char **argv)
void FailedNewHandler()
Definition: compat.cc:22
DECLARE_uint64(seed)
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:216
void RandGen(const Fst< FromArc > &ifst, MutableFst< ToArc > *ofst, const RandGenOptions< Selector > &opts)
Definition: randgen.h:734
#define VLOG(level)
Definition: log.h:47
DECLARE_string(select)
DECLARE_bool(weighted)
DECLARE_int32(max_length)