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.

77 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_DETERMINIZE_H_
  18. #define FST_SCRIPT_DETERMINIZE_H_
  19. #include <cstdint>
  20. #include <tuple>
  21. #include <fst/determinize.h>
  22. #include <fst/fst.h>
  23. #include <fst/mutable-fst.h>
  24. #include <fst/script/fst-class.h>
  25. #include <fst/script/weight-class.h>
  26. namespace fst {
  27. namespace script {
  28. struct DeterminizeOptions {
  29. const float delta;
  30. const WeightClass &weight_threshold;
  31. const int64_t state_threshold;
  32. const int64_t subsequential_label;
  33. const DeterminizeType det_type;
  34. const bool increment_subsequential_label;
  35. DeterminizeOptions(float delta, const WeightClass &weight_threshold,
  36. int64_t state_threshold = kNoStateId,
  37. int64_t subsequential_label = 0,
  38. DeterminizeType det_type = DETERMINIZE_FUNCTIONAL,
  39. bool increment_subsequential_label = false)
  40. : delta(delta),
  41. weight_threshold(weight_threshold),
  42. state_threshold(state_threshold),
  43. subsequential_label(subsequential_label),
  44. det_type(det_type),
  45. increment_subsequential_label(increment_subsequential_label) {}
  46. };
  47. using FstDeterminizeArgs =
  48. std::tuple<const FstClass &, MutableFstClass *, const DeterminizeOptions &>;
  49. template <class Arc>
  50. void Determinize(FstDeterminizeArgs *args) {
  51. using Weight = typename Arc::Weight;
  52. const Fst<Arc> &ifst = *std::get<0>(*args).GetFst<Arc>();
  53. MutableFst<Arc> *ofst = std::get<1>(*args)->GetMutableFst<Arc>();
  54. const auto &opts = std::get<2>(*args);
  55. const auto weight_threshold = *opts.weight_threshold.GetWeight<Weight>();
  56. const fst::DeterminizeOptions<Arc> detargs(
  57. opts.delta, weight_threshold, opts.state_threshold,
  58. opts.subsequential_label, opts.det_type,
  59. opts.increment_subsequential_label);
  60. Determinize(ifst, ofst, detargs);
  61. }
  62. void Determinize(const FstClass &ifst, MutableFstClass *ofst,
  63. const DeterminizeOptions &opts);
  64. } // namespace script
  65. } // namespace fst
  66. #endif // FST_SCRIPT_DETERMINIZE_H_