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.

82 lines
3.0 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_RELABEL_H_
  18. #define FST_SCRIPT_RELABEL_H_
  19. #include <algorithm>
  20. #include <cstdint>
  21. #include <string>
  22. #include <tuple>
  23. #include <utility>
  24. #include <vector>
  25. #include <fst/mutable-fst.h>
  26. #include <fst/relabel.h>
  27. #include <fst/symbol-table.h>
  28. #include <fst/script/fst-class.h>
  29. namespace fst {
  30. namespace script {
  31. using FstRelabelArgs1 =
  32. std::tuple<MutableFstClass *, const SymbolTable *, const SymbolTable *,
  33. const std::string &, bool, const SymbolTable *,
  34. const SymbolTable *, const std::string &, bool>;
  35. template <class Arc>
  36. void Relabel(FstRelabelArgs1 *args) {
  37. MutableFst<Arc> *ofst = std::get<0>(*args)->GetMutableFst<Arc>();
  38. Relabel(ofst, std::get<1>(*args), std::get<2>(*args), std::get<3>(*args),
  39. std::get<4>(*args), std::get<5>(*args), std::get<6>(*args),
  40. std::get<7>(*args), std::get<8>(*args));
  41. }
  42. using FstRelabelArgs2 =
  43. std::tuple<MutableFstClass *,
  44. const std::vector<std::pair<int64_t, int64_t>> &,
  45. const std::vector<std::pair<int64_t, int64_t>> &>;
  46. template <class Arc>
  47. void Relabel(FstRelabelArgs2 *args) {
  48. MutableFst<Arc> *ofst = std::get<0>(*args)->GetMutableFst<Arc>();
  49. using LabelPair = std::pair<typename Arc::Label, typename Arc::Label>;
  50. // In case the MutableFstClass::Label is not the same as Arc::Label,
  51. // make a copy.
  52. std::vector<LabelPair> typed_ipairs(std::get<1>(*args).size());
  53. std::copy(std::get<1>(*args).begin(), std::get<1>(*args).end(),
  54. typed_ipairs.begin());
  55. std::vector<LabelPair> typed_opairs(std::get<2>(*args).size());
  56. std::copy(std::get<2>(*args).begin(), std::get<2>(*args).end(),
  57. typed_opairs.begin());
  58. Relabel(ofst, typed_ipairs, typed_opairs);
  59. }
  60. void Relabel(MutableFstClass *ofst, const SymbolTable *old_isymbols,
  61. const SymbolTable *new_isymbols,
  62. const std::string &unknown_isymbol, bool attach_new_isymbols,
  63. const SymbolTable *old_osymbols, const SymbolTable *new_osymbols,
  64. const std::string &unknown_osymbol, bool attach_new_osymbols);
  65. void Relabel(MutableFstClass *ofst,
  66. const std::vector<std::pair<int64_t, int64_t>> &ipairs,
  67. const std::vector<std::pair<int64_t, int64_t>> &opairs);
  68. } // namespace script
  69. } // namespace fst
  70. #endif // FST_SCRIPT_RELABEL_H_