FST  openfst-1.8.3
OpenFst Library
difference.h
Go to the documentation of this file.
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 difference between two FSAs.
19 
20 #ifndef FST_DIFFERENCE_H_
21 #define FST_DIFFERENCE_H_
22 
23 #include <memory>
24 
25 #include <fst/log.h>
26 #include <fst/arc.h>
27 #include <fst/cache.h>
28 #include <fst/complement.h>
29 #include <fst/compose-filter.h>
30 #include <fst/compose.h>
31 #include <fst/connect.h>
32 #include <fst/float-weight.h>
33 #include <fst/fst.h>
34 #include <fst/impl-to-fst.h>
35 #include <fst/matcher.h>
36 #include <fst/mutable-fst.h>
37 #include <fst/properties.h>
38 #include <fst/state-table.h>
39 #include <fst/util.h>
40 
41 namespace fst {
42 
43 template <class Arc, class M = Matcher<Fst<Arc>>,
44  class Filter = SequenceComposeFilter<M>,
45  class StateTable =
46  GenericComposeStateTable<Arc, typename Filter::FilterState>>
48  : public ComposeFstOptions<Arc, M, Filter, StateTable> {
50  M *matcher1 = nullptr, M *matcher2 = nullptr,
51  Filter *filter = nullptr,
52  StateTable *state_table = nullptr)
53  : ComposeFstOptions<Arc, M, Filter, StateTable>(opts, matcher1, matcher2,
54  filter, state_table) {}
55 };
56 
57 // Computes the difference between two FSAs. This version is a delayed FST.
58 // Only strings that are in the first automaton but not in second are retained
59 // in the result.
60 //
61 // The first argument must be an acceptor; the second argument must be an
62 // unweighted, epsilon-free, deterministic acceptor. One of the arguments must
63 // be label-sorted.
64 //
65 // Complexity: same as ComposeFst.
66 //
67 // Caveats: same as ComposeFst.
68 template <class A>
69 class DifferenceFst : public ComposeFst<A> {
70  public:
71  using Arc = A;
72  using Weight = typename Arc::Weight;
73  using StateId = typename Arc::StateId;
74 
76 
77  // A - B = A ^ B'.
78  DifferenceFst(const Fst<Arc> &fst1, const Fst<Arc> &fst2,
79  const CacheOptions &opts = CacheOptions())
80  : ComposeFst<Arc>(CreateDifferenceImplWithCacheOpts(fst1, fst2, opts)) {
81  if (!fst1.Properties(kAcceptor, true)) {
82  FSTERROR() << "DifferenceFst: 1st argument not an acceptor";
83  GetImpl()->SetProperties(kError, kError);
84  }
85  }
86 
87  template <class Matcher, class Filter, class StateTable>
89  const Fst<Arc> &fst1, const Fst<Arc> &fst2,
91  : ComposeFst<Arc>(
92  CreateDifferenceImplWithDifferenceOpts(fst1, fst2, opts)) {
93  if (!fst1.Properties(kAcceptor, true)) {
94  FSTERROR() << "DifferenceFst: 1st argument not an acceptor";
95  GetImpl()->SetProperties(kError, kError);
96  }
97  }
98 
99  // See Fst<>::Copy() for doc.
100  DifferenceFst(const DifferenceFst &fst, bool safe = false)
101  : ComposeFst<Arc>(fst, safe) {}
102 
103  // Get a copy of this DifferenceFst. See Fst<>::Copy() for further doc.
104  DifferenceFst *Copy(bool safe = false) const override {
105  return new DifferenceFst(*this, safe);
106  }
107 
108  private:
111 
112  static std::shared_ptr<Impl> CreateDifferenceImplWithCacheOpts(
113  const Fst<Arc> &fst1, const Fst<Arc> &fst2, const CacheOptions &opts) {
114  using RM = RhoMatcher<Matcher<Fst<A>>>;
115  ComplementFst<Arc> cfst(fst2);
117  CacheOptions(), new RM(fst1, MATCH_NONE),
119  return CreateBase1(fst1, cfst, copts);
120  }
121 
122  template <class Matcher, class Filter, class StateTable>
123  static std::shared_ptr<Impl> CreateDifferenceImplWithDifferenceOpts(
124  const Fst<Arc> &fst1, const Fst<Arc> &fst2,
126  using RM = RhoMatcher<Matcher>;
127  ComplementFst<Arc> cfst(fst2);
128  ComposeFstOptions<Arc, RM> copts(opts);
129  copts.matcher1 = new RM(fst1, MATCH_NONE, kNoLabel, MATCHER_REWRITE_ALWAYS,
130  opts.matcher1);
131  copts.matcher2 = new RM(cfst, MATCH_INPUT, ComplementFst<Arc>::kRhoLabel,
133  return CreateBase1(fst1, cfst, copts);
134  }
135 };
136 
137 // Specialization for DifferenceFst.
138 template <class Arc>
140  : public StateIterator<ComposeFst<Arc>> {
141  public:
143  : StateIterator<ComposeFst<Arc>>(fst) {}
144 };
145 
146 // Specialization for DifferenceFst.
147 template <class Arc>
148 class ArcIterator<DifferenceFst<Arc>> : public ArcIterator<ComposeFst<Arc>> {
149  public:
150  using StateId = typename Arc::StateId;
151 
153  : ArcIterator<ComposeFst<Arc>>(fst, s) {}
154 };
155 
157 
158 // Useful alias when using StdArc.
160 
162 
163 // Computes the difference between two FSAs. This version writes the difference
164 // to an output MutableFst. Only strings that are in the first automaton but not
165 // in the second are retained in the result.
166 //
167 // The first argument must be an acceptor; the second argument must be an
168 // unweighted, epsilon-free, deterministic acceptor. One of the arguments must
169 // be label-sorted.
170 //
171 // Complexity: same as Compose.
172 //
173 // Caveats: same as Compose.
174 template <class Arc>
175 void Difference(const Fst<Arc> &ifst1, const Fst<Arc> &ifst2,
176  MutableFst<Arc> *ofst,
177  const DifferenceOptions &opts = DifferenceOptions()) {
178  using M = Matcher<Fst<Arc>>;
179  // In each case, we cache only the last state for fastest copy.
180  switch (opts.filter_type) {
181  case AUTO_FILTER: {
182  CacheOptions nopts;
183  nopts.gc_limit = 0;
184  *ofst = DifferenceFst<Arc>(ifst1, ifst2, nopts);
185  break;
186  }
187  case SEQUENCE_FILTER: {
189  dopts.gc_limit = 0;
190  *ofst = DifferenceFst<Arc>(ifst1, ifst2, dopts);
191  break;
192  }
193  case ALT_SEQUENCE_FILTER: {
195  dopts.gc_limit = 0;
196  *ofst = DifferenceFst<Arc>(ifst1, ifst2, dopts);
197  break;
198  }
199  case MATCH_FILTER: {
201  dopts.gc_limit = 0;
202  *ofst = DifferenceFst<Arc>(ifst1, ifst2, dopts);
203  break;
204  }
205  case NO_MATCH_FILTER: {
207  dopts.gc_limit = 0;
208  *ofst = DifferenceFst<Arc>(ifst1, ifst2, dopts);
209  break;
210  }
211  case NULL_FILTER: {
213  dopts.gc_limit = 0;
214  *ofst = DifferenceFst<Arc>(ifst1, ifst2, dopts);
215  break;
216  }
217  case TRIVIAL_FILTER: {
219  dopts.gc_limit = 0;
220  *ofst = DifferenceFst<Arc>(ifst1, ifst2, dopts);
221  break;
222  }
223  }
224  if (opts.connect) Connect(ofst);
225 }
226 
227 } // namespace fst
228 
229 #endif // FST_DIFFERENCE_H_
typename Arc::Weight Weight
Definition: difference.h:72
DifferenceFst(const DifferenceFst &fst, bool safe=false)
Definition: difference.h:100
StateIterator(const DifferenceFst< Arc > &fst)
Definition: difference.h:142
typename ComposeFst< Arc >::Arc Arc
Definition: fst.h:517
constexpr int kNoLabel
Definition: fst.h:195
CacheOptions(bool gc=FST_FLAGS_fst_default_cache_gc, size_t gc_limit=FST_FLAGS_fst_default_cache_gc_limit)
Definition: cache.h:54
virtual uint64_t Properties(uint64_t mask, bool test) const =0
DifferenceFst(const Fst< Arc > &fst1, const Fst< Arc > &fst2, const DifferenceFstOptions< Arc, Matcher, Filter, StateTable > &opts)
Definition: difference.h:88
constexpr uint64_t kError
Definition: properties.h:52
void Connect(MutableFst< Arc > *fst)
Definition: connect.h:47
typename ComposeFst< Arc >::Arc Arc
Definition: fst.h:408
void Difference(const Fst< Arc > &ifst1, const Fst< Arc > &ifst2, MutableFst< Arc > *ofst, const DifferenceOptions &opts=DifferenceOptions())
Definition: difference.h:175
#define FSTERROR()
Definition: util.h:56
typename Arc::StateId StateId
Definition: difference.h:73
ComposeOptions DifferenceOptions
Definition: difference.h:156
DifferenceFstOptions(const CacheOptions &opts=CacheOptions(), M *matcher1=nullptr, M *matcher2=nullptr, Filter *filter=nullptr, StateTable *state_table=nullptr)
Definition: difference.h:49
DifferenceFst * Copy(bool safe=false) const override
Definition: difference.h:104
DifferenceFst(const Fst< Arc > &fst1, const Fst< Arc > &fst2, const CacheOptions &opts=CacheOptions())
Definition: difference.h:78
size_t gc_limit
Definition: cache.h:52
ArcIterator(const DifferenceFst< Arc > &fst, StateId s)
Definition: difference.h:152
constexpr uint64_t kAcceptor
Definition: properties.h:64
StateTable * state_table
Definition: compose.h:68