// difference.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. // // // \file // Class to compute the difference between two FSAs #ifndef FST_LIB_DIFFERENCE_H__ #define FST_LIB_DIFFERENCE_H__ #include "fst/lib/compose.h" #include "fst/lib/complement.h" namespace fst { template struct DifferenceFstOptions : public ComposeFstOptions { }; // Computes the difference between two FSAs. This version is a delayed // Fst. Only strings that are in the first automaton but not in second // are retained in the result. // // The first argument must be an acceptor; the second argument must be // an unweighted, epsilon-free, deterministic acceptor. One of the // arguments must be label-sorted. // // Complexity: same as ComposeFst. // // Caveats: same as ComposeFst. template class DifferenceFst : public ComposeFst { public: using ComposeFst::Impl; typedef A Arc; typedef typename A::Weight Weight; typedef typename A::StateId StateId; // A - B = A ^ B'. DifferenceFst(const Fst &fst1, const Fst &fst2) : ComposeFst(fst1, ComplementFst(fst2), ComposeFstOptions()) { if (!fst1.Properties(kAcceptor, true)) LOG(FATAL) << "DifferenceFst: 1st argument not an acceptor"; uint64 props1 = fst1.Properties(kFstProperties, false); uint64 props2 = fst2.Properties(kFstProperties, false); Impl()->SetProperties(DifferenceProperties(props1, props2), kCopyProperties); } template DifferenceFst(const Fst &fst1, const Fst &fst2, const DifferenceFstOptions &opts) : ComposeFst(fst1, ComplementFst(fst2), ComposeFstOptions(opts)) { if (!fst1.Properties(kAcceptor, true)) LOG(FATAL) << "DifferenceFst: 1st argument not an acceptor"; uint64 props1 = fst1.Properties(kFstProperties, false); uint64 props2 = fst2.Properties(kFstProperties, false); Impl()->SetProperties(DifferenceProperties(props1, props2), kCopyProperties); } DifferenceFst(const DifferenceFst &fst) : ComposeFst(fst) {} virtual DifferenceFst *Copy() const { return new DifferenceFst(*this); } }; // Specialization for DifferenceFst. template class StateIterator< DifferenceFst > : public StateIterator< ComposeFst > { public: explicit StateIterator(const DifferenceFst &fst) : StateIterator< ComposeFst >(fst) {} }; // Specialization for DifferenceFst. template class ArcIterator< DifferenceFst > : public ArcIterator< ComposeFst > { public: typedef typename A::StateId StateId; ArcIterator(const DifferenceFst &fst, StateId s) : ArcIterator< ComposeFst >(fst, s) {} }; // Useful alias when using StdArc. typedef DifferenceFst StdDifferenceFst; typedef ComposeOptions DifferenceOptions; // Computes the difference between two FSAs. This version is writes // the difference to an output MutableFst. Only strings that are in // the first automaton but not in second are retained in the result. // // The first argument must be an acceptor; the second argument must be // an unweighted, epsilon-free, deterministic acceptor. One of the // arguments must be label-sorted. // // Complexity: same as Compose. // // Caveats: same as Compose. template void Difference(const Fst &ifst1, const Fst &ifst2, MutableFst *ofst, const DifferenceOptions &opts = DifferenceOptions()) { DifferenceFstOptions<> nopts; nopts.gc_limit = 0; // Cache only the last state for fastest copy. *ofst = DifferenceFst(ifst1, ifst2, nopts); if (opts.connect) Connect(ofst); } } // namespace fst #endif // FST_LIB_DIFFERENCE_H__