FST  openfst-1.8.3
OpenFst Library
randgen.h
Go to the documentation of this file.
1 // Copyright 2005-2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the 'License');
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an 'AS IS' BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // See www.openfst.org for extensive documentation on this weighted
16 // finite-state transducer library.
17 
18 #ifndef FST_SCRIPT_RANDGEN_H_
19 #define FST_SCRIPT_RANDGEN_H_
20 
21 #include <cstdint>
22 #include <random>
23 #include <tuple>
24 
25 #include <fst/fst.h>
26 #include <fst/mutable-fst.h>
27 #include <fst/randgen.h>
28 #include <fst/script/fst-class.h>
29 #include <fst/script/script-impl.h>
30 
31 namespace fst {
32 namespace script {
33 
34 using FstRandGenArgs =
35  std::tuple<const FstClass &, MutableFstClass *,
37 
38 template <class Arc>
39 void RandGen(FstRandGenArgs *args) {
40  const Fst<Arc> &ifst = *std::get<0>(*args).GetFst<Arc>();
41  MutableFst<Arc> *ofst = std::get<1>(*args)->GetMutableFst<Arc>();
42  const auto &opts = std::get<2>(*args);
43  const uint64_t seed = std::get<3>(*args);
44  switch (opts.selector) {
46  const UniformArcSelector<Arc> selector(seed);
48  selector, opts.max_length, opts.npath, opts.weighted,
49  opts.remove_total_weight);
50  RandGen(ifst, ofst, ropts);
51  return;
52  }
54  const FastLogProbArcSelector<Arc> selector(seed);
56  selector, opts.max_length, opts.npath, opts.weighted,
57  opts.remove_total_weight);
58  RandGen(ifst, ofst, ropts);
59  return;
60  }
62  const LogProbArcSelector<Arc> selector(seed);
64  selector, opts.max_length, opts.npath, opts.weighted,
65  opts.remove_total_weight);
66  RandGen(ifst, ofst, ropts);
67  return;
68  }
69  }
70 }
71 
72 void RandGen(const FstClass &ifst, MutableFstClass *ofst,
75  uint64_t seed = std::random_device()());
76 
77 } // namespace script
78 } // namespace fst
79 
80 #endif // FST_SCRIPT_RANDGEN_H_
void RandGen(FstRandGenArgs *args)
Definition: randgen.h:39
std::tuple< const FstClass &, MutableFstClass *, const RandGenOptions< RandArcSelection > &, uint64_t > FstRandGenArgs
Definition: randgen.h:36