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.

112 lines
3.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. //
  18. // Conversions to/from PowerWeight and SparsePowerWeight.
  19. #ifndef FST_POWER_WEIGHT_MAPPERS_H_
  20. #define FST_POWER_WEIGHT_MAPPERS_H_
  21. namespace fst {
  22. // Converts FromWeight to ToPowerWeight (with one component).
  23. // ToPowerWeight may be PowerWeight or SparsePowerWeight.
  24. template <class FromWeight_, class ToPowerWeight>
  25. class ToPowerWeightMapper {
  26. public:
  27. using FromWeight = FromWeight_;
  28. using ToWeight = ToPowerWeight;
  29. using Index = typename ToPowerWeight::Index;
  30. explicit ToPowerWeightMapper(Index index = 0) : index_(index) {}
  31. ToPowerWeight operator()(const FromWeight &w) const {
  32. return ToPowerWeight(index_, w.Value());
  33. }
  34. private:
  35. const Index index_;
  36. };
  37. // Converts FromPowerWeight to ToWeight. Uses only one component.
  38. // FromPowerWeight may be PowerWeight or SparsePowerWeight.
  39. template <class FromPowerWeight, class ToWeight_>
  40. class FromPowerWeightMapper {
  41. public:
  42. using FromWeight = FromPowerWeight;
  43. using ToWeight = ToWeight_;
  44. using Index = typename FromPowerWeight::Index;
  45. explicit FromPowerWeightMapper(Index index = 0) : index_(index) {}
  46. ToWeight operator()(const FromPowerWeight &w) const {
  47. return ToWeight(w.Value(index_));
  48. }
  49. private:
  50. const Index index_;
  51. };
  52. // Projects to one dimension of the weight vector, filling the indices
  53. // with `default_weight`.
  54. // PowerWeightT may be PowerWeight or SparsePowerWeight.
  55. template <class PowerWeightT>
  56. class ProjectPowerWeightMapper {
  57. public:
  58. using FromWeight = PowerWeightT;
  59. using ToWeight = PowerWeightT;
  60. using Index = typename PowerWeightT::Index;
  61. using ComponentWeight = typename PowerWeightT::Weight;
  62. explicit ProjectPowerWeightMapper(
  63. Index from_index = 0, Index to_index = 0,
  64. const ComponentWeight &default_weight = ComponentWeight::Zero())
  65. : from_index_(from_index),
  66. to_index_(to_index),
  67. default_weight_(default_weight) {}
  68. PowerWeightT operator()(const PowerWeightT &w) const {
  69. return PowerWeightT(to_index_, w.Value(from_index_), default_weight_);
  70. }
  71. private:
  72. const Index from_index_;
  73. const Index to_index_;
  74. const ComponentWeight default_weight_;
  75. };
  76. // Applies a transformation function to each weight vector.
  77. // PowerWeightT may be PowerWeight or SparsePowerWeight.
  78. template <class PowerWeightT, class TransformFn_>
  79. class TransformPowerWeightMapper {
  80. public:
  81. using TransformFn = TransformFn_;
  82. using FromWeight = PowerWeightT;
  83. using ToWeight = PowerWeightT;
  84. explicit TransformPowerWeightMapper(
  85. const TransformFn &transform = TransformFn())
  86. : transform_(transform) {}
  87. PowerWeightT operator()(const PowerWeightT &w) const { return transform_(w); }
  88. private:
  89. TransformFn transform_;
  90. };
  91. } // namespace fst
  92. #endif // FST_POWER_WEIGHT_MAPPERS_H_