// intersect.h // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // Copyright 2005-2010 Google, Inc. // Author: riley@google.com (Michael Riley) // // \file // Class to compute the intersection of two FSAs #ifndef FST_LIB_INTERSECT_H__ #define FST_LIB_INTERSECT_H__ #include #include using std::vector; #include #include namespace fst { template >, class F = SequenceComposeFilter, class T = GenericComposeStateTable > struct IntersectFstOptions : public ComposeFstOptions { explicit IntersectFstOptions(const CacheOptions &opts, M *mat1 = 0, M *mat2 = 0, F *filt = 0, T *sttable= 0) : ComposeFstOptions(opts, mat1, mat2, filt, sttable) { } IntersectFstOptions() {} }; // 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 ComposeFst::CreateBase; using ComposeFst::CreateBase1; using ComposeFst::Properties; using ImplToFst< ComposeFstImplBase >::GetImpl; using ImplToFst< ComposeFstImplBase >::SetImpl; typedef A Arc; typedef typename A::Weight Weight; typedef typename A::StateId StateId; IntersectFst(const Fst &fst1, const Fst &fst2, const CacheOptions opts = CacheOptions()) { bool acceptors = fst1.Properties(kAcceptor, true) && fst2.Properties(kAcceptor, true); SetImpl(CreateBase(fst1, fst2, opts)); if (!acceptors) { FSTERROR() << "IntersectFst: input FSTs are not acceptors"; GetImpl()->SetProperties(kError); } } template IntersectFst(const Fst &fst1, const Fst &fst2, const IntersectFstOptions &opts) { bool acceptors = fst1.Properties(kAcceptor, true) && fst2.Properties(kAcceptor, true); SetImpl(CreateBase1(fst1, fst2, opts)); if (!acceptors) { FSTERROR() << "IntersectFst: input FSTs are not acceptors"; GetImpl()->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. virtual IntersectFst *Copy(bool safe = false) const { return new IntersectFst(*this, safe); } }; // Specialization for IntersectFst. template class StateIterator< IntersectFst > : public StateIterator< ComposeFst > { public: explicit StateIterator(const IntersectFst &fst) : StateIterator< ComposeFst >(fst) {} }; // Specialization for IntersectFst. template class ArcIterator< IntersectFst > : public ArcIterator< ComposeFst > { public: typedef typename A::StateId StateId; ArcIterator(const IntersectFst &fst, StateId s) : ArcIterator< ComposeFst >(fst, s) {} }; // Useful alias when using StdArc. typedef IntersectFst StdIntersectFst; typedef ComposeOptions IntersectOptions; // 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()) { typedef Matcher< Fst > M; 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_LIB_INTERSECT_H__