FST  openfst-1.8.3
OpenFst Library
arcfilter.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 // Function objects to restrict which arcs are traversed in an FST.
19 
20 #ifndef FST_ARCFILTER_H_
21 #define FST_ARCFILTER_H_
22 
23 #include <fst/fst.h>
24 #include <fst/util.h>
25 
26 namespace fst {
27 
28 // True for all arcs.
29 template <class Arc>
30 class AnyArcFilter {
31  public:
32  bool operator()(const Arc &arc) const { return true; }
33 };
34 
35 // True for (input/output) epsilon arcs.
36 template <class Arc>
38  public:
39  bool operator()(const Arc &arc) const {
40  return arc.ilabel == 0 && arc.olabel == 0;
41  }
42 };
43 
44 // True for input epsilon arcs.
45 template <class Arc>
47  public:
48  bool operator()(const Arc &arc) const { return arc.ilabel == 0; }
49 };
50 
51 // True for output epsilon arcs.
52 template <class Arc>
54  public:
55  bool operator()(const Arc &arc) const { return arc.olabel == 0; }
56 };
57 
58 // True if specified label matches (doesn't match) when keep_match is
59 // true (false).
60 template <class Arc>
62  public:
63  using Label = typename Arc::Label;
64 
65  explicit LabelArcFilter(Label label, bool match_input = true,
66  bool keep_match = true)
67  : label_(label), match_input_(match_input), keep_match_(keep_match) {}
68 
69  bool operator()(const Arc &arc) const {
70  const bool match = (match_input_ ? arc.ilabel : arc.olabel) == label_;
71  return keep_match_ ? match : !match;
72  }
73 
74  private:
75  const Label label_;
76  const bool match_input_;
77  const bool keep_match_;
78 };
79 
80 // True if specified labels match (don't match) when keep_match is true (false).
81 template <class Arc>
83  public:
84  using Label = typename Arc::Label;
85 
86  explicit MultiLabelArcFilter(bool match_input = true, bool keep_match = true)
87  : match_input_(match_input), keep_match_(keep_match) {}
88 
89  bool operator()(const Arc &arc) const {
90  const Label label = match_input_ ? arc.ilabel : arc.olabel;
91  const bool match = labels_.Find(label) != labels_.End();
92  return keep_match_ ? match : !match;
93  }
94 
95  void AddLabel(Label label) { labels_.Insert(label); }
96 
97  private:
99  const bool match_input_;
100  const bool keep_match_;
101 };
102 
103 } // namespace fst
104 
105 #endif // FST_ARCFILTER_H_
void AddLabel(Label label)
Definition: arcfilter.h:95
typename Arc::Label Label
Definition: arcfilter.h:84
bool operator()(const Arc &arc) const
Definition: arcfilter.h:48
MultiLabelArcFilter(bool match_input=true, bool keep_match=true)
Definition: arcfilter.h:86
LabelArcFilter(Label label, bool match_input=true, bool keep_match=true)
Definition: arcfilter.h:65
bool operator()(const Arc &arc) const
Definition: arcfilter.h:89
typename Arc::Label Label
Definition: arcfilter.h:63
bool operator()(const Arc &arc) const
Definition: arcfilter.h:32
bool operator()(const Arc &arc) const
Definition: arcfilter.h:55
bool operator()(const Arc &arc) const
Definition: arcfilter.h:39
bool operator()(const Arc &arc) const
Definition: arcfilter.h:69