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.

102 lines
3.3 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. // Classes and functions to remove unsuccessful paths from an FST.
  19. #ifndef FST_CONNECT_H_
  20. #define FST_CONNECT_H_
  21. #include <algorithm>
  22. #include <cstddef>
  23. #include <cstdint>
  24. #include <utility>
  25. #include <vector>
  26. #include <fst/cc-visitors.h>
  27. #include <fst/dfs-visit.h>
  28. #include <fst/fst.h>
  29. #include <fst/mutable-fst.h>
  30. #include <fst/properties.h>
  31. namespace fst {
  32. // Trims an FST, removing states and arcs that are not on successful paths.
  33. // This version modifies its input.
  34. //
  35. // Complexity:
  36. //
  37. // Time: O(V + E)
  38. // Space: O(V + E)
  39. //
  40. // where V = # of states and E = # of arcs.
  41. template <class Arc>
  42. void Connect(MutableFst<Arc> *fst) {
  43. using StateId = typename Arc::StateId;
  44. std::vector<bool> access;
  45. std::vector<bool> coaccess;
  46. uint64_t props = 0;
  47. SccVisitor<Arc> scc_visitor(nullptr, &access, &coaccess, &props);
  48. DfsVisit(*fst, &scc_visitor);
  49. std::vector<StateId> dstates;
  50. dstates.reserve(access.size());
  51. for (StateId s = 0; s < access.size(); ++s) {
  52. if (!access[s] || !coaccess[s]) dstates.push_back(s);
  53. }
  54. fst->DeleteStates(dstates);
  55. fst->SetProperties(kAccessible | kCoAccessible, kAccessible | kCoAccessible);
  56. }
  57. // Returns an acyclic FST where each SCC in the input FST has been condensed to
  58. // a single state with transitions between SCCs retained and within SCCs
  59. // dropped. Also populates 'scc' with a mapping from input to output states.
  60. template <class Arc>
  61. void Condense(const Fst<Arc> &ifst, MutableFst<Arc> *ofst,
  62. std::vector<typename Arc::StateId> *scc) {
  63. using StateId = typename Arc::StateId;
  64. ofst->DeleteStates();
  65. uint64_t props = 0;
  66. SccVisitor<Arc> scc_visitor(scc, nullptr, nullptr, &props);
  67. DfsVisit(ifst, &scc_visitor);
  68. const auto iter = std::max_element(scc->cbegin(), scc->cend());
  69. if (iter == scc->cend()) return;
  70. const StateId num_condensed_states = 1 + *iter;
  71. ofst->ReserveStates(num_condensed_states);
  72. for (StateId c = 0; c < num_condensed_states; ++c) {
  73. ofst->AddState();
  74. }
  75. for (StateId s = 0; s < scc->size(); ++s) {
  76. const auto c = (*scc)[s];
  77. if (s == ifst.Start()) ofst->SetStart(c);
  78. const auto weight = ifst.Final(s);
  79. if (weight != Arc::Weight::Zero())
  80. ofst->SetFinal(c, Plus(ofst->Final(c), weight));
  81. for (ArcIterator<Fst<Arc>> aiter(ifst, s); !aiter.Done(); aiter.Next()) {
  82. const auto &arc = aiter.Value();
  83. const auto nextc = (*scc)[arc.nextstate];
  84. if (nextc != c) {
  85. Arc condensed_arc = arc;
  86. condensed_arc.nextstate = nextc;
  87. ofst->AddArc(c, std::move(condensed_arc));
  88. }
  89. }
  90. }
  91. ofst->SetProperties(kAcyclic | kInitialAcyclic, kAcyclic | kInitialAcyclic);
  92. }
  93. } // namespace fst
  94. #endif // FST_CONNECT_H_