FST  openfst-1.7.2
OpenFst Library
fstrandmod.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 a random FST according to a class-specific transition model.
5 
6 #include <cstdlib>
7 #include <cstring>
8 #include <ctime>
9 #include <memory>
10 #include <string>
11 
12 #include <fst/flags.h>
13 #include <fst/log.h>
15 #include <fst/fstlib.h>
16 
17 DEFINE_int32(seed, time(0), "Random seed");
18 DEFINE_int32(states, 10, "# of states");
19 DEFINE_int32(labels, 2, "# of labels");
20 DEFINE_int32(classes, 1, "# of probability distributions");
21 DEFINE_bool(transducer, false, "Output a transducer");
22 DEFINE_bool(weights, false, "Output a weighted FST");
23 
24 int main(int argc, char **argv) {
25  using fst::StdVectorFst;
26  using fst::StdArc;
27  using fst::TropicalWeight;
28  using fst::WeightGenerate;
29 
30  string usage = "Generates a random FST.\n\n Usage: ";
31  usage += argv[0];
32  usage += "[out.fst]\n";
33 
34  std::set_new_handler(FailedNewHandler);
35  SET_FLAGS(usage.c_str(), &argc, &argv, true);
36  if (argc > 2) {
37  ShowUsage();
38  return 1;
39  }
40 
41  string out_name = (argc > 1 && (strcmp(argv[1], "-") != 0)) ? argv[1] : "";
42 
43  srand(FLAGS_seed);
44 
45  int num_states = (rand() % FLAGS_states) + 1; // NOLINT
46  int num_classes = (rand() % FLAGS_classes) + 1; // NOLINT
47  int num_labels = (rand() % FLAGS_labels) + 1; // NOLINT
48 
50  using TropicalWeightGenerate = WeightGenerate<TropicalWeight>;
51  std::unique_ptr<TropicalWeightGenerate> generate(FLAGS_weights ?
52  new TropicalWeightGenerate(false) : nullptr);
54  num_classes, num_labels, FLAGS_transducer, generate.get());
55  rand_mod.Generate(&fst);
56  fst.Write(out_name);
57  return 0;
58 }
void ShowUsage(bool long_usage=true)
Definition: flags.cc:124
void Generate(StdMutableFst *fst)
Definition: randmod.h:41
VectorFst< StdArc > StdVectorFst
Definition: fst-decl.h:187
ArcTpl< TropicalWeight > StdArc
Definition: arc.h:55
void FailedNewHandler()
Definition: compat.cc:25
#define SET_FLAGS(usage, argc, argv, rmflags)
Definition: flags.h:214
TropicalWeightTpl< float > TropicalWeight
Definition: float-weight.h:244
DEFINE_bool(transducer, false,"Output a transducer")
DEFINE_int32(seed, time(0),"Random seed")
int main(int argc, char **argv)
Definition: fstrandmod.cc:24