FST  openfst-1.7.1
OpenFst Library
arcfilter.h
Go to the documentation of this file.
1 // See www.openfst.org for extensive documentation on this weighted
2 // finite-state transducer library.
3 //
4 // Function objects to restrict which arcs are traversed in an FST.
5 
6 #ifndef FST_ARCFILTER_H_
7 #define FST_ARCFILTER_H_
8 
9 
10 #include <fst/fst.h>
11 #include <fst/util.h>
12 
13 
14 namespace fst {
15 
16 // True for all arcs.
17 template <class Arc>
18 class AnyArcFilter {
19  public:
20  bool operator()(const Arc &arc) const { return true; }
21 };
22 
23 // True for (input/output) epsilon arcs.
24 template <class Arc>
26  public:
27  bool operator()(const Arc &arc) const {
28  return arc.ilabel == 0 && arc.olabel == 0;
29  }
30 };
31 
32 // True for input epsilon arcs.
33 template <class Arc>
35  public:
36  bool operator()(const Arc &arc) const { return arc.ilabel == 0; }
37 };
38 
39 // True for output epsilon arcs.
40 template <class Arc>
42  public:
43  bool operator()(const Arc &arc) const { return arc.olabel == 0; }
44 };
45 
46 // True if specified label matches (doesn't match) when keep_match is
47 // true (false).
48 template <class Arc>
50  public:
51  using Label = typename Arc::Label;
52 
53  explicit LabelArcFilter(Label label, bool match_input = true,
54  bool keep_match = true)
55  : label_(label), match_input_(match_input), keep_match_(keep_match) {}
56 
57  bool operator()(const Arc &arc) const {
58  const bool match = (match_input_ ? arc.ilabel : arc.olabel) == label_;
59  return keep_match_ ? match : !match;
60  }
61 
62  private:
63  const Label label_;
64  const bool match_input_;
65  const bool keep_match_;
66 };
67 
68 // True if specified labels match (don't match) when keep_match is true (false).
69 template <class Arc>
71  public:
72  using Label = typename Arc::Label;
73 
74  explicit MultiLabelArcFilter(bool match_input = true, bool keep_match = true)
75  : match_input_(match_input), keep_match_(keep_match) {}
76 
77  bool operator()(const Arc &arc) const {
78  const Label label = match_input_ ? arc.ilabel : arc.olabel;
79  const bool match = labels_.Find(label) != labels_.End();
80  return keep_match_ ? match : !match;
81  }
82 
83  void AddLabel(Label label) { labels_.Insert(label); }
84 
85  private:
87  const bool match_input_;
88  const bool keep_match_;
89 };
90 
91 } // namespace fst
92 
93 #endif // FST_ARCFILTER_H_
void AddLabel(Label label)
Definition: arcfilter.h:83
typename Arc::Label Label
Definition: arcfilter.h:72
bool operator()(const Arc &arc) const
Definition: arcfilter.h:36
MultiLabelArcFilter(bool match_input=true, bool keep_match=true)
Definition: arcfilter.h:74
LabelArcFilter(Label label, bool match_input=true, bool keep_match=true)
Definition: arcfilter.h:53
bool operator()(const Arc &arc) const
Definition: arcfilter.h:77
typename Arc::Label Label
Definition: arcfilter.h:51
bool operator()(const Arc &arc) const
Definition: arcfilter.h:20
bool operator()(const Arc &arc) const
Definition: arcfilter.h:43
bool operator()(const Arc &arc) const
Definition: arcfilter.h:27
bool operator()(const Arc &arc) const
Definition: arcfilter.h:57