// See www.openfst.org for extensive documentation on this weighted // finite-state transducer library. // // Class to compute the difference between two FSAs. #ifndef FST_DIFFERENCE_H_ #define FST_DIFFERENCE_H_ #include #include #include #include namespace fst { template >, class Filter = SequenceComposeFilter, class StateTable = GenericComposeStateTable> struct DifferenceFstOptions : public ComposeFstOptions { explicit DifferenceFstOptions(const CacheOptions &opts = CacheOptions(), M *matcher1 = nullptr, M *matcher2 = nullptr, Filter *filter = nullptr, StateTable *state_table = nullptr) : ComposeFstOptions(opts, matcher1, matcher2, filter, state_table) {} }; // 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 Arc = A; using Weight = typename Arc::Weight; using StateId = typename Arc::StateId; using ComposeFst::CreateBase1; // A - B = A ^ B'. DifferenceFst(const Fst &fst1, const Fst &fst2, const CacheOptions &opts = CacheOptions()) : ComposeFst(CreateDifferenceImplWithCacheOpts(fst1, fst2, opts)) { if (!fst1.Properties(kAcceptor, true)) { FSTERROR() << "DifferenceFst: 1st argument not an acceptor"; GetImpl()->SetProperties(kError, kError); } } template DifferenceFst( const Fst &fst1, const Fst &fst2, const DifferenceFstOptions &opts) : ComposeFst( CreateDifferenceImplWithDifferenceOpts(fst1, fst2, opts)) { if (!fst1.Properties(kAcceptor, true)) { FSTERROR() << "DifferenceFst: 1st argument not an acceptor"; GetImpl()->SetProperties(kError, kError); } } // See Fst<>::Copy() for doc. DifferenceFst(const DifferenceFst &fst, bool safe = false) : ComposeFst(fst, safe) {} // Get a copy of this DifferenceFst. See Fst<>::Copy() for further doc. DifferenceFst *Copy(bool safe = false) const override { return new DifferenceFst(*this, safe); } private: using Impl = internal::ComposeFstImplBase; using ImplToFst::GetImpl; static std::shared_ptr CreateDifferenceImplWithCacheOpts( const Fst &fst1, const Fst &fst2, const CacheOptions &opts) { using RM = RhoMatcher>>; ComplementFst cfst(fst2); ComposeFstOptions copts( CacheOptions(), new RM(fst1, MATCH_NONE), new RM(cfst, MATCH_INPUT, ComplementFst::kRhoLabel)); return CreateBase1(fst1, cfst, copts); } template static std::shared_ptr CreateDifferenceImplWithDifferenceOpts( const Fst &fst1, const Fst &fst2, const DifferenceFstOptions &opts) { using RM = RhoMatcher; ComplementFst cfst(fst2); ComposeFstOptions copts(opts); copts.matcher1 = new RM(fst1, MATCH_NONE, kNoLabel, MATCHER_REWRITE_ALWAYS, opts.matcher1); copts.matcher2 = new RM(cfst, MATCH_INPUT, ComplementFst::kRhoLabel, MATCHER_REWRITE_ALWAYS, opts.matcher2); return CreateBase1(fst1, cfst, copts); } }; // Specialization for DifferenceFst. template class StateIterator> : public StateIterator> { public: explicit StateIterator(const DifferenceFst &fst) : StateIterator>(fst) {} }; // Specialization for DifferenceFst. template class ArcIterator> : public ArcIterator> { public: using StateId = typename Arc::StateId; ArcIterator(const DifferenceFst &fst, StateId s) : ArcIterator>(fst, s) {} }; using DifferenceOptions = ComposeOptions; // Useful alias when using StdArc. using StdDifferenceFst = DifferenceFst; using DifferenceOptions = ComposeOptions; // Computes the difference between two FSAs. This version writes the difference // to an output MutableFst. Only strings that are in the first automaton but not // in the 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()) { using M = Matcher>; if (opts.filter_type == AUTO_FILTER) { CacheOptions nopts; nopts.gc_limit = 0; // Cache only the last state for fastest copy. *ofst = DifferenceFst(ifst1, ifst2, nopts); } else if (opts.filter_type == SEQUENCE_FILTER) { DifferenceFstOptions dopts; dopts.gc_limit = 0; // Cache only the last state for fastest copy. *ofst = DifferenceFst(ifst1, ifst2, dopts); } else if (opts.filter_type == ALT_SEQUENCE_FILTER) { DifferenceFstOptions> dopts; dopts.gc_limit = 0; // Cache only the last state for fastest copy. *ofst = DifferenceFst(ifst1, ifst2, dopts); } else if (opts.filter_type == MATCH_FILTER) { DifferenceFstOptions> dopts; dopts.gc_limit = 0; // Cache only the last state for fastest copy. *ofst = DifferenceFst(ifst1, ifst2, dopts); } if (opts.connect) Connect(ofst); } } // namespace fst #endif // FST_DIFFERENCE_H_