FST  openfst-1.8.2.post1
OpenFst Library
arcfilter.h
Go to the documentation of this file.
1 // Copyright 2005-2020 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 
24 #include <fst/fst.h>
25 #include <fst/util.h>
26 
27 
28 namespace fst {
29 
30 // True for all arcs.
31 template <class Arc>
32 class AnyArcFilter {
33  public:
34  bool operator()(const Arc &arc) const { return true; }
35 };
36 
37 // True for (input/output) epsilon arcs.
38 template <class Arc>
40  public:
41  bool operator()(const Arc &arc) const {
42  return arc.ilabel == 0 && arc.olabel == 0;
43  }
44 };
45 
46 // True for input epsilon arcs.
47 template <class Arc>
49  public:
50  bool operator()(const Arc &arc) const { return arc.ilabel == 0; }
51 };
52 
53 // True for output epsilon arcs.
54 template <class Arc>
56  public:
57  bool operator()(const Arc &arc) const { return arc.olabel == 0; }
58 };
59 
60 // True if specified label matches (doesn't match) when keep_match is
61 // true (false).
62 template <class Arc>
64  public:
65  using Label = typename Arc::Label;
66 
67  explicit LabelArcFilter(Label label, bool match_input = true,
68  bool keep_match = true)
69  : label_(label), match_input_(match_input), keep_match_(keep_match) {}
70 
71  bool operator()(const Arc &arc) const {
72  const bool match = (match_input_ ? arc.ilabel : arc.olabel) == label_;
73  return keep_match_ ? match : !match;
74  }
75 
76  private:
77  const Label label_;
78  const bool match_input_;
79  const bool keep_match_;
80 };
81 
82 // True if specified labels match (don't match) when keep_match is true (false).
83 template <class Arc>
85  public:
86  using Label = typename Arc::Label;
87 
88  explicit MultiLabelArcFilter(bool match_input = true, bool keep_match = true)
89  : match_input_(match_input), keep_match_(keep_match) {}
90 
91  bool operator()(const Arc &arc) const {
92  const Label label = match_input_ ? arc.ilabel : arc.olabel;
93  const bool match = labels_.Find(label) != labels_.End();
94  return keep_match_ ? match : !match;
95  }
96 
97  void AddLabel(Label label) { labels_.Insert(label); }
98 
99  private:
101  const bool match_input_;
102  const bool keep_match_;
103 };
104 
105 } // namespace fst
106 
107 #endif // FST_ARCFILTER_H_
void AddLabel(Label label)
Definition: arcfilter.h:97
typename Arc::Label Label
Definition: arcfilter.h:86
bool operator()(const Arc &arc) const
Definition: arcfilter.h:50
MultiLabelArcFilter(bool match_input=true, bool keep_match=true)
Definition: arcfilter.h:88
LabelArcFilter(Label label, bool match_input=true, bool keep_match=true)
Definition: arcfilter.h:67
bool operator()(const Arc &arc) const
Definition: arcfilter.h:91
typename Arc::Label Label
Definition: arcfilter.h:65
bool operator()(const Arc &arc) const
Definition: arcfilter.h:34
bool operator()(const Arc &arc) const
Definition: arcfilter.h:57
bool operator()(const Arc &arc) const
Definition: arcfilter.h:41
bool operator()(const Arc &arc) const
Definition: arcfilter.h:71