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.

204 lines
6.5 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. // Class to compute the intersection of two FSAs.
  19. #ifndef FST_INTERSECT_H_
  20. #define FST_INTERSECT_H_
  21. #include <algorithm>
  22. #include <vector>
  23. #include <fst/log.h>
  24. #include <fst/arc.h>
  25. #include <fst/cache.h>
  26. #include <fst/compose-filter.h>
  27. #include <fst/compose.h>
  28. #include <fst/connect.h>
  29. #include <fst/float-weight.h>
  30. #include <fst/fst.h>
  31. #include <fst/impl-to-fst.h>
  32. #include <fst/matcher.h>
  33. #include <fst/mutable-fst.h>
  34. #include <fst/properties.h>
  35. #include <fst/state-table.h>
  36. #include <fst/util.h>
  37. namespace fst {
  38. using IntersectOptions = ComposeOptions;
  39. template <class Arc, class M = Matcher<Fst<Arc>>,
  40. class Filter = SequenceComposeFilter<M>,
  41. class StateTable =
  42. GenericComposeStateTable<Arc, typename Filter::FilterState>>
  43. struct IntersectFstOptions
  44. : public ComposeFstOptions<Arc, M, Filter, StateTable> {
  45. IntersectFstOptions() = default;
  46. explicit IntersectFstOptions(const CacheOptions &opts, M *matcher1 = nullptr,
  47. M *matcher2 = nullptr, Filter *filter = nullptr,
  48. StateTable *state_table = nullptr)
  49. : ComposeFstOptions<Arc, M, Filter, StateTable>(opts, matcher1, matcher2,
  50. filter, state_table) {}
  51. };
  52. // Computes the intersection (Hadamard product) of two FSAs. This version is a
  53. // delayed FST. Only strings that are in both automata are retained in the
  54. // result.
  55. //
  56. // The two arguments must be acceptors. One of the arguments must be
  57. // label-sorted.
  58. //
  59. // Complexity: same as ComposeFst.
  60. //
  61. // Caveats: same as ComposeFst.
  62. template <class A>
  63. class IntersectFst : public ComposeFst<A> {
  64. public:
  65. using Arc = A;
  66. using StateId = typename Arc::StateId;
  67. using Weight = typename Arc::Weight;
  68. using ComposeFst<A>::CreateBase;
  69. using ComposeFst<A>::CreateBase1;
  70. using ComposeFst<A>::Properties;
  71. IntersectFst(const Fst<Arc> &fst1, const Fst<Arc> &fst2,
  72. const CacheOptions &opts = CacheOptions())
  73. : ComposeFst<Arc>(CreateBase(fst1, fst2, opts)) {
  74. const bool acceptors =
  75. fst1.Properties(kAcceptor, true) && fst2.Properties(kAcceptor, true);
  76. if (!acceptors) {
  77. FSTERROR() << "IntersectFst: Input FSTs are not acceptors";
  78. GetMutableImpl()->SetProperties(kError);
  79. }
  80. }
  81. template <class M, class Filter, class StateTable>
  82. IntersectFst(const Fst<Arc> &fst1, const Fst<Arc> &fst2,
  83. const IntersectFstOptions<Arc, M, Filter, StateTable> &opts)
  84. : ComposeFst<Arc>(CreateBase1(fst1, fst2, opts)) {
  85. const bool acceptors =
  86. fst1.Properties(kAcceptor, true) && fst2.Properties(kAcceptor, true);
  87. if (!acceptors) {
  88. FSTERROR() << "IntersectFst: input FSTs are not acceptors";
  89. GetMutableImpl()->SetProperties(kError);
  90. }
  91. }
  92. // See Fst<>::Copy() for doc.
  93. IntersectFst(const IntersectFst &fst, bool safe = false)
  94. : ComposeFst<Arc>(fst, safe) {}
  95. // Get a copy of this IntersectFst. See Fst<>::Copy() for further doc.
  96. IntersectFst *Copy(bool safe = false) const override {
  97. return new IntersectFst(*this, safe);
  98. }
  99. private:
  100. using ImplToFst<internal::ComposeFstImplBase<A>>::GetImpl;
  101. using ImplToFst<internal::ComposeFstImplBase<A>>::GetMutableImpl;
  102. };
  103. // Specialization for IntersectFst.
  104. template <class Arc>
  105. class StateIterator<IntersectFst<Arc>> : public StateIterator<ComposeFst<Arc>> {
  106. public:
  107. explicit StateIterator(const IntersectFst<Arc> &fst)
  108. : StateIterator<ComposeFst<Arc>>(fst) {}
  109. };
  110. // Specialization for IntersectFst.
  111. template <class Arc>
  112. class ArcIterator<IntersectFst<Arc>> : public ArcIterator<ComposeFst<Arc>> {
  113. public:
  114. using StateId = typename Arc::StateId;
  115. ArcIterator(const IntersectFst<Arc> &fst, StateId s)
  116. : ArcIterator<ComposeFst<Arc>>(fst, s) {}
  117. };
  118. // Useful alias when using StdArc.
  119. using StdIntersectFst = IntersectFst<StdArc>;
  120. // Computes the intersection (Hadamard product) of two FSAs. This version
  121. // writes the intersection to an output MurableFst. Only strings that are in
  122. // both automata are retained in the result.
  123. //
  124. // The two arguments must be acceptors. One of the arguments must be
  125. // label-sorted.
  126. //
  127. // Complexity: same as Compose.
  128. //
  129. // Caveats: same as Compose.
  130. template <class Arc>
  131. void Intersect(const Fst<Arc> &ifst1, const Fst<Arc> &ifst2,
  132. MutableFst<Arc> *ofst,
  133. const IntersectOptions &opts = IntersectOptions()) {
  134. using M = Matcher<Fst<Arc>>;
  135. // In each case, we cache only the last state for fastest copy.
  136. switch (opts.filter_type) {
  137. case AUTO_FILTER: {
  138. CacheOptions nopts;
  139. nopts.gc_limit = 0;
  140. *ofst = IntersectFst<Arc>(ifst1, ifst2, nopts);
  141. break;
  142. }
  143. case SEQUENCE_FILTER: {
  144. IntersectFstOptions<Arc> iopts;
  145. iopts.gc_limit = 0;
  146. *ofst = IntersectFst<Arc>(ifst1, ifst2, iopts);
  147. break;
  148. }
  149. case ALT_SEQUENCE_FILTER: {
  150. IntersectFstOptions<Arc, M, AltSequenceComposeFilter<M>> iopts;
  151. iopts.gc_limit = 0;
  152. *ofst = IntersectFst<Arc>(ifst1, ifst2, iopts);
  153. break;
  154. }
  155. case MATCH_FILTER: {
  156. IntersectFstOptions<Arc, M, MatchComposeFilter<M>> iopts;
  157. iopts.gc_limit = 0;
  158. *ofst = IntersectFst<Arc>(ifst1, ifst2, iopts);
  159. break;
  160. }
  161. case NO_MATCH_FILTER: {
  162. IntersectFstOptions<Arc, M, NoMatchComposeFilter<M>> iopts;
  163. iopts.gc_limit = 0;
  164. *ofst = IntersectFst<Arc>(ifst1, ifst2, iopts);
  165. break;
  166. }
  167. case NULL_FILTER: {
  168. IntersectFstOptions<Arc, M, NullComposeFilter<M>> iopts;
  169. iopts.gc_limit = 0;
  170. *ofst = IntersectFst<Arc>(ifst1, ifst2, iopts);
  171. break;
  172. }
  173. case TRIVIAL_FILTER: {
  174. IntersectFstOptions<Arc, M, TrivialComposeFilter<M>> iopts;
  175. iopts.gc_limit = 0;
  176. *ofst = IntersectFst<Arc>(ifst1, ifst2, iopts);
  177. break;
  178. }
  179. }
  180. if (opts.connect) Connect(ofst);
  181. }
  182. } // namespace fst
  183. #endif // FST_INTERSECT_H_