// See www.openfst.org for extensive documentation on this weighted // finite-state transducer library. // // Class to compute the intersection of two FSAs. #ifndef FST_INTERSECT_H_ #define FST_INTERSECT_H_ #include #include #include #include #include namespace fst { using IntersectOptions = ComposeOptions; template >, class Filter = SequenceComposeFilter, class StateTable = GenericComposeStateTable> struct IntersectFstOptions : public ComposeFstOptions { IntersectFstOptions() {} explicit IntersectFstOptions(const CacheOptions &opts, M *matcher1 = nullptr, M *matcher2 = nullptr, Filter *filter = nullptr, StateTable *state_table = nullptr) : ComposeFstOptions(opts, matcher1, matcher2, filter, state_table) {} }; // Computes the intersection (Hadamard product) of two FSAs. This version is a // delayed FST. Only strings that are in both automata are retained in the // result. // // The two arguments must be acceptors. One of the arguments must be // label-sorted. // // Complexity: same as ComposeFst. // // Caveats: same as ComposeFst. template class IntersectFst : public ComposeFst { public: using Arc = A; using StateId = typename Arc::StateId; using Weight = typename Arc::Weight; using ComposeFst::CreateBase; using ComposeFst::CreateBase1; using ComposeFst::Properties; IntersectFst(const Fst &fst1, const Fst &fst2, const CacheOptions &opts = CacheOptions()) : ComposeFst(CreateBase(fst1, fst2, opts)) { const bool acceptors = fst1.Properties(kAcceptor, true) && fst2.Properties(kAcceptor, true); if (!acceptors) { FSTERROR() << "IntersectFst: Input FSTs are not acceptors"; GetMutableImpl()->SetProperties(kError); } } template IntersectFst(const Fst &fst1, const Fst &fst2, const IntersectFstOptions &opts) : ComposeFst(CreateBase1(fst1, fst2, opts)) { const bool acceptors = fst1.Properties(kAcceptor, true) && fst2.Properties(kAcceptor, true); if (!acceptors) { FSTERROR() << "IntersectFst: input FSTs are not acceptors"; GetMutableImpl()->SetProperties(kError); } } // See Fst<>::Copy() for doc. IntersectFst(const IntersectFst &fst, bool safe = false) : ComposeFst(fst, safe) {} // Get a copy of this IntersectFst. See Fst<>::Copy() for further doc. IntersectFst *Copy(bool safe = false) const override { return new IntersectFst(*this, safe); } private: using ImplToFst>::GetImpl; using ImplToFst>::GetMutableImpl; }; // Specialization for IntersectFst. template class StateIterator> : public StateIterator> { public: explicit StateIterator(const IntersectFst &fst) : StateIterator>(fst) {} }; // Specialization for IntersectFst. template class ArcIterator> : public ArcIterator> { public: using StateId = typename Arc::StateId; ArcIterator(const IntersectFst &fst, StateId s) : ArcIterator>(fst, s) {} }; // Useful alias when using StdArc. using StdIntersectFst = IntersectFst; // Computes the intersection (Hadamard product) of two FSAs. This version // writes the intersection to an output MurableFst. Only strings that are in // both automata are retained in the result. // // The two arguments must be acceptors. One of the arguments must be // label-sorted. // // Complexity: same as Compose. // // Caveats: same as Compose. template void Intersect(const Fst &ifst1, const Fst &ifst2, MutableFst *ofst, const IntersectOptions &opts = IntersectOptions()) { using M = Matcher>; if (opts.filter_type == AUTO_FILTER) { CacheOptions nopts; nopts.gc_limit = 0; // Cache only the last state for fastest copy. *ofst = IntersectFst(ifst1, ifst2, nopts); } else if (opts.filter_type == SEQUENCE_FILTER) { IntersectFstOptions iopts; iopts.gc_limit = 0; // Cache only the last state for fastest copy. *ofst = IntersectFst(ifst1, ifst2, iopts); } else if (opts.filter_type == ALT_SEQUENCE_FILTER) { IntersectFstOptions> iopts; iopts.gc_limit = 0; // Cache only the last state for fastest copy. *ofst = IntersectFst(ifst1, ifst2, iopts); } else if (opts.filter_type == MATCH_FILTER) { IntersectFstOptions> iopts; iopts.gc_limit = 0; // Cache only the last state for fastest copy. *ofst = IntersectFst(ifst1, ifst2, iopts); } if (opts.connect) Connect(ofst); } } // namespace fst #endif // FST_INTERSECT_H_