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.

189 lines
6.1 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. // Functions and classes to compute the union of two FSTs.
  19. #ifndef FST_UNION_H_
  20. #define FST_UNION_H_
  21. #include <algorithm>
  22. #include <utility>
  23. #include <vector>
  24. #include <fst/log.h>
  25. #include <fst/arc.h>
  26. #include <fst/cache.h>
  27. #include <fst/expanded-fst.h>
  28. #include <fst/float-weight.h>
  29. #include <fst/fst.h>
  30. #include <fst/impl-to-fst.h>
  31. #include <fst/mutable-fst.h>
  32. #include <fst/properties.h>
  33. #include <fst/rational.h>
  34. #include <fst/symbol-table.h>
  35. #include <fst/util.h>
  36. namespace fst {
  37. // Computes the union (sum) of two FSTs. This version writes the union to an
  38. // output MutableFst. If A transduces string x to y with weight a and B
  39. // transduces string w to v with weight b, then their union transduces x to y
  40. // with weight a and w to v with weight b.
  41. //
  42. // Complexity:
  43. //
  44. // Time: (V_2 + E_2)
  45. // Space: O(V_2 + E_2)
  46. //
  47. // where Vi is the number of states, and Ei is the number of arcs, in the ith
  48. // FST.
  49. template <class Arc>
  50. void Union(MutableFst<Arc> *fst1, const Fst<Arc> &fst2) {
  51. // Checks for symbol table compatibility.
  52. if (!CompatSymbols(fst1->InputSymbols(), fst2.InputSymbols()) ||
  53. !CompatSymbols(fst1->OutputSymbols(), fst2.OutputSymbols())) {
  54. FSTERROR() << "Union: Input/output symbol tables of 1st argument "
  55. << "do not match input/output symbol tables of 2nd argument";
  56. fst1->SetProperties(kError, kError);
  57. return;
  58. }
  59. const auto numstates1 = fst1->NumStates();
  60. const bool initial_acyclic1 =
  61. fst1->Properties(kInitialAcyclic, false) == kInitialAcyclic;
  62. const auto props1 = fst1->Properties(kFstProperties, false);
  63. const auto props2 = fst2.Properties(kFstProperties, false);
  64. const auto start2 = fst2.Start();
  65. if (start2 == kNoStateId) {
  66. if (props2 & kError) fst1->SetProperties(kError, kError);
  67. return;
  68. }
  69. if (std::optional<typename Arc::StateId> numstates2 =
  70. fst2.NumStatesIfKnown()) {
  71. fst1->ReserveStates(numstates1 + *numstates2 + (initial_acyclic1 ? 0 : 1));
  72. }
  73. for (StateIterator<Fst<Arc>> siter(fst2); !siter.Done(); siter.Next()) {
  74. const auto s1 = fst1->AddState();
  75. const auto s2 = siter.Value();
  76. fst1->SetFinal(s1, fst2.Final(s2));
  77. fst1->ReserveArcs(s1, fst2.NumArcs(s2));
  78. for (ArcIterator<Fst<Arc>> aiter(fst2, s2); !aiter.Done(); aiter.Next()) {
  79. auto arc = aiter.Value(); // Copy intended.
  80. arc.nextstate += numstates1;
  81. fst1->AddArc(s1, std::move(arc));
  82. }
  83. }
  84. const auto start1 = fst1->Start();
  85. if (start1 == kNoStateId) {
  86. fst1->SetStart(start2);
  87. fst1->SetProperties(props2, kCopyProperties);
  88. return;
  89. }
  90. if (initial_acyclic1) {
  91. fst1->AddArc(start1, Arc(0, 0, start2 + numstates1));
  92. } else {
  93. const auto nstart1 = fst1->AddState();
  94. fst1->SetStart(nstart1);
  95. fst1->AddArc(nstart1, Arc(0, 0, start1));
  96. fst1->AddArc(nstart1, Arc(0, 0, start2 + numstates1));
  97. }
  98. fst1->SetProperties(UnionProperties(props1, props2), kFstProperties);
  99. }
  100. // Same as the above but can handle arbitrarily many right-hand-side FSTs,
  101. // preallocating the states.
  102. template <class Arc>
  103. void Union(MutableFst<Arc> *fst1, const std::vector<const Fst<Arc> *> &fsts2) {
  104. // We add 1 just in case fst1 has an initial cycle.
  105. fst1->ReserveStates(1 + fst1->NumStates() + CountStates(fsts2));
  106. for (const auto *fst2 : fsts2) Union(fst1, *fst2);
  107. }
  108. // Computes the union of two FSTs, modifying the RationalFst argument.
  109. template <class Arc>
  110. void Union(RationalFst<Arc> *fst1, const Fst<Arc> &fst2) {
  111. fst1->GetMutableImpl()->AddUnion(fst2);
  112. }
  113. using UnionFstOptions = RationalFstOptions;
  114. // Computes the union (sum) of two FSTs. This version is a delayed FST. If A
  115. // transduces string x to y with weight a and B transduces string w to v with
  116. // weight b, then their union transduces x to y with weight a and w to v with
  117. // weight b.
  118. //
  119. // Complexity:
  120. //
  121. // Time: O(v_1 + e_1 + v_2 + e_2)
  122. // Space: O(v_1 + v_2)
  123. //
  124. // where vi is the number of states visited, and ei is the number of arcs
  125. // visited, in the ith FST. Constant time and space to visit an input state or
  126. // arc is assumed and exclusive of caching.
  127. template <class A>
  128. class UnionFst : public RationalFst<A> {
  129. public:
  130. using Arc = A;
  131. using StateId = typename Arc::StateId;
  132. using Weight = typename Arc::Weight;
  133. UnionFst(const Fst<Arc> &fst1, const Fst<Arc> &fst2) {
  134. GetMutableImpl()->InitUnion(fst1, fst2);
  135. }
  136. UnionFst(const Fst<Arc> &fst1, const Fst<Arc> &fst2,
  137. const UnionFstOptions &opts)
  138. : RationalFst<Arc>(opts) {
  139. GetMutableImpl()->InitUnion(fst1, fst2);
  140. }
  141. // See Fst<>::Copy() for doc.
  142. UnionFst(const UnionFst &fst, bool safe = false)
  143. : RationalFst<Arc>(fst, safe) {}
  144. // Gets a copy of this UnionFst. See Fst<>::Copy() for further doc.
  145. UnionFst *Copy(bool safe = false) const override {
  146. return new UnionFst(*this, safe);
  147. }
  148. private:
  149. using ImplToFst<internal::RationalFstImpl<Arc>>::GetImpl;
  150. using ImplToFst<internal::RationalFstImpl<Arc>>::GetMutableImpl;
  151. };
  152. // Specialization for UnionFst.
  153. template <class Arc>
  154. class StateIterator<UnionFst<Arc>> : public StateIterator<RationalFst<Arc>> {
  155. public:
  156. explicit StateIterator(const UnionFst<Arc> &fst)
  157. : StateIterator<RationalFst<Arc>>(fst) {}
  158. };
  159. // Specialization for UnionFst.
  160. template <class Arc>
  161. class ArcIterator<UnionFst<Arc>> : public ArcIterator<RationalFst<Arc>> {
  162. public:
  163. using StateId = typename Arc::StateId;
  164. ArcIterator(const UnionFst<Arc> &fst, StateId s)
  165. : ArcIterator<RationalFst<Arc>>(fst, s) {}
  166. };
  167. using StdUnionFst = UnionFst<StdArc>;
  168. } // namespace fst
  169. #endif // FST_UNION_H_