You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 lines
2.6 KiB

  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. #ifndef FST_SCRIPT_RANDGEN_H_
  18. #define FST_SCRIPT_RANDGEN_H_
  19. #include <cstdint>
  20. #include <random>
  21. #include <tuple>
  22. #include <fst/fst.h>
  23. #include <fst/mutable-fst.h>
  24. #include <fst/randgen.h>
  25. #include <fst/script/fst-class.h>
  26. #include <fst/script/script-impl.h>
  27. namespace fst {
  28. namespace script {
  29. using FstRandGenArgs =
  30. std::tuple<const FstClass &, MutableFstClass *,
  31. const RandGenOptions<RandArcSelection> &, uint64_t>;
  32. template <class Arc>
  33. void RandGen(FstRandGenArgs *args) {
  34. const Fst<Arc> &ifst = *std::get<0>(*args).GetFst<Arc>();
  35. MutableFst<Arc> *ofst = std::get<1>(*args)->GetMutableFst<Arc>();
  36. const auto &opts = std::get<2>(*args);
  37. const uint64_t seed = std::get<3>(*args);
  38. switch (opts.selector) {
  39. case RandArcSelection::UNIFORM: {
  40. const UniformArcSelector<Arc> selector(seed);
  41. const RandGenOptions<UniformArcSelector<Arc>> ropts(
  42. selector, opts.max_length, opts.npath, opts.weighted,
  43. opts.remove_total_weight);
  44. RandGen(ifst, ofst, ropts);
  45. return;
  46. }
  47. case RandArcSelection::FAST_LOG_PROB: {
  48. const FastLogProbArcSelector<Arc> selector(seed);
  49. const RandGenOptions<FastLogProbArcSelector<Arc>> ropts(
  50. selector, opts.max_length, opts.npath, opts.weighted,
  51. opts.remove_total_weight);
  52. RandGen(ifst, ofst, ropts);
  53. return;
  54. }
  55. case RandArcSelection::LOG_PROB: {
  56. const LogProbArcSelector<Arc> selector(seed);
  57. const RandGenOptions<LogProbArcSelector<Arc>> ropts(
  58. selector, opts.max_length, opts.npath, opts.weighted,
  59. opts.remove_total_weight);
  60. RandGen(ifst, ofst, ropts);
  61. return;
  62. }
  63. }
  64. }
  65. void RandGen(const FstClass &ifst, MutableFstClass *ofst,
  66. const RandGenOptions<RandArcSelection> &opts =
  67. RandGenOptions<RandArcSelection>(RandArcSelection::UNIFORM),
  68. uint64_t seed = std::random_device()());
  69. } // namespace script
  70. } // namespace fst
  71. #endif // FST_SCRIPT_RANDGEN_H_