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.

175 lines
5.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_MAP_H_
  18. #define FST_SCRIPT_MAP_H_
  19. #include <cstdint>
  20. #include <memory>
  21. #include <tuple>
  22. #include <utility>
  23. #include <fst/arc-map.h>
  24. #include <fst/arc.h>
  25. #include <fst/fst.h>
  26. #include <fst/state-map.h>
  27. #include <fst/vector-fst.h>
  28. #include <fst/script/arg-packs.h>
  29. #include <fst/script/fst-class.h>
  30. #include <fst/script/weight-class.h>
  31. namespace fst {
  32. namespace script {
  33. template <class M>
  34. std::unique_ptr<Fst<typename M::ToArc>> ArcMap(
  35. const Fst<typename M::FromArc> &fst, const M &mapper) {
  36. using ToArc = typename M::ToArc;
  37. auto ofst = std::make_unique<VectorFst<ToArc>>();
  38. ArcMap(fst, ofst.get(), mapper);
  39. return ofst;
  40. }
  41. template <class M>
  42. std::unique_ptr<Fst<typename M::ToArc>> StateMap(
  43. const Fst<typename M::FromArc> &fst, const M &mapper) {
  44. using ToArc = typename M::ToArc;
  45. auto ofst = std::make_unique<VectorFst<ToArc>>();
  46. StateMap(fst, ofst.get(), mapper);
  47. return ofst;
  48. }
  49. enum class MapType : uint8_t {
  50. ARC_SUM,
  51. ARC_UNIQUE,
  52. IDENTITY,
  53. INPUT_EPSILON,
  54. INVERT,
  55. OUTPUT_EPSILON,
  56. PLUS,
  57. POWER,
  58. QUANTIZE,
  59. RMWEIGHT,
  60. SUPERFINAL,
  61. TIMES,
  62. TO_LOG,
  63. TO_LOG64,
  64. TO_STD
  65. };
  66. using FstMapInnerArgs =
  67. std::tuple<const FstClass &, MapType, float, double, const WeightClass &>;
  68. using FstMapArgs = WithReturnValue<std::unique_ptr<FstClass>, FstMapInnerArgs>;
  69. template <class Arc>
  70. void Map(FstMapArgs *args) {
  71. using Weight = typename Arc::Weight;
  72. const Fst<Arc> &ifst = *std::get<0>(args->args).GetFst<Arc>();
  73. const auto map_type = std::get<1>(args->args);
  74. switch (map_type) {
  75. case MapType::ARC_SUM: {
  76. auto ofst = StateMap(ifst, ArcSumMapper<Arc>(ifst));
  77. args->retval = std::make_unique<FstClass>(std::move(ofst));
  78. return;
  79. }
  80. case MapType::ARC_UNIQUE: {
  81. auto ofst = StateMap(ifst, ArcUniqueMapper<Arc>(ifst));
  82. args->retval = std::make_unique<FstClass>(std::move(ofst));
  83. return;
  84. }
  85. case MapType::IDENTITY: {
  86. auto ofst = ArcMap(ifst, IdentityArcMapper<Arc>());
  87. args->retval = std::make_unique<FstClass>(std::move(ofst));
  88. return;
  89. }
  90. case MapType::INPUT_EPSILON: {
  91. auto ofst = ArcMap(ifst, InputEpsilonMapper<Arc>());
  92. args->retval = std::make_unique<FstClass>(std::move(ofst));
  93. return;
  94. }
  95. case MapType::INVERT: {
  96. auto ofst = ArcMap(ifst, InvertWeightMapper<Arc>());
  97. args->retval = std::make_unique<FstClass>(std::move(ofst));
  98. return;
  99. }
  100. case MapType::OUTPUT_EPSILON: {
  101. auto ofst = ArcMap(ifst, OutputEpsilonMapper<Arc>());
  102. args->retval = std::make_unique<FstClass>(std::move(ofst));
  103. return;
  104. }
  105. case MapType::PLUS: {
  106. const auto weight = *std::get<4>(args->args).GetWeight<Weight>();
  107. auto ofst = ArcMap(ifst, PlusMapper<Arc>(weight));
  108. args->retval = std::make_unique<FstClass>(std::move(ofst));
  109. return;
  110. }
  111. case MapType::POWER: {
  112. const auto power = std::get<3>(args->args);
  113. auto ofst = ArcMap(ifst, PowerMapper<Arc>(power));
  114. args->retval = std::make_unique<FstClass>(std::move(ofst));
  115. return;
  116. }
  117. case MapType::QUANTIZE: {
  118. const auto delta = std::get<2>(args->args);
  119. auto ofst = ArcMap(ifst, QuantizeMapper<Arc>(delta));
  120. args->retval = std::make_unique<FstClass>(std::move(ofst));
  121. return;
  122. }
  123. case MapType::RMWEIGHT: {
  124. auto ofst = ArcMap(ifst, RmWeightMapper<Arc>());
  125. args->retval = std::make_unique<FstClass>(std::move(ofst));
  126. return;
  127. }
  128. case MapType::SUPERFINAL: {
  129. auto ofst = ArcMap(ifst, SuperFinalMapper<Arc>());
  130. args->retval = std::make_unique<FstClass>(std::move(ofst));
  131. return;
  132. }
  133. case MapType::TIMES: {
  134. const auto weight = *std::get<4>(args->args).GetWeight<Weight>();
  135. auto ofst = ArcMap(ifst, TimesMapper<Arc>(weight));
  136. args->retval = std::make_unique<FstClass>(std::move(ofst));
  137. return;
  138. }
  139. case MapType::TO_LOG: {
  140. auto ofst = ArcMap(ifst, WeightConvertMapper<Arc, LogArc>());
  141. args->retval = std::make_unique<FstClass>(std::move(ofst));
  142. return;
  143. }
  144. case MapType::TO_LOG64: {
  145. auto ofst = ArcMap(ifst, WeightConvertMapper<Arc, Log64Arc>());
  146. args->retval = std::make_unique<FstClass>(std::move(ofst));
  147. return;
  148. }
  149. case MapType::TO_STD: {
  150. auto ofst = ArcMap(ifst, WeightConvertMapper<Arc, StdArc>());
  151. args->retval = std::make_unique<FstClass>(std::move(ofst));
  152. return;
  153. }
  154. }
  155. }
  156. std::unique_ptr<FstClass> Map(const FstClass &ifst,
  157. MapType map_type, float delta,
  158. double power,
  159. const WeightClass &weight);
  160. } // namespace script
  161. } // namespace fst
  162. #endif // FST_SCRIPT_MAP_H_