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.

71 lines
2.4 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_DISAMBIGUATE_H_
  18. #define FST_SCRIPT_DISAMBIGUATE_H_
  19. #include <cstdint>
  20. #include <tuple>
  21. #include <utility>
  22. #include <fst/disambiguate.h>
  23. #include <fst/fst.h>
  24. #include <fst/mutable-fst.h>
  25. #include <fst/script/fst-class.h>
  26. #include <fst/script/weight-class.h>
  27. namespace fst {
  28. namespace script {
  29. struct DisambiguateOptions {
  30. const float delta;
  31. const WeightClass &weight_threshold;
  32. const int64_t state_threshold;
  33. const int64_t subsequential_label;
  34. DisambiguateOptions(float delta, const WeightClass &weight_threshold,
  35. int64_t state_threshold = kNoStateId,
  36. int64_t subsequential_label = 0)
  37. : delta(delta),
  38. weight_threshold(weight_threshold),
  39. state_threshold(state_threshold),
  40. subsequential_label(subsequential_label) {}
  41. };
  42. using FstDisambiguateArgs = std::tuple<const FstClass &, MutableFstClass *,
  43. const DisambiguateOptions &>;
  44. template <class Arc>
  45. void Disambiguate(FstDisambiguateArgs *args) {
  46. using Weight = typename Arc::Weight;
  47. const Fst<Arc> &ifst = *std::get<0>(*args).GetFst<Arc>();
  48. MutableFst<Arc> *ofst = std::get<1>(*args)->GetMutableFst<Arc>();
  49. const auto &opts = std::get<2>(*args);
  50. const auto weight_threshold = *opts.weight_threshold.GetWeight<Weight>();
  51. const fst::DisambiguateOptions<Arc> disargs(opts.delta, weight_threshold,
  52. opts.state_threshold,
  53. opts.subsequential_label);
  54. Disambiguate(ifst, ofst, disargs);
  55. }
  56. void Disambiguate(const FstClass &ifst, MutableFstClass *ofst,
  57. const DisambiguateOptions &opts);
  58. } // namespace script
  59. } // namespace fst
  60. #endif // FST_SCRIPT_DISAMBIGUATE_H_