1 // arcfilter.h
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 // Copyright 2005-2010 Google, Inc.
16 // Author: riley@google.com (Michael Riley)
17 //
18 // \file
19 // Function objects to restrict which arcs are traversed in an FST.
20 
21 #ifndef FST_LIB_ARCFILTER_H__
22 #define FST_LIB_ARCFILTER_H__
23 
24 
25 #include <fst/fst.h>
26 #include <fst/util.h>
27 
28 
29 namespace fst {
30 
31 // True for all arcs.
32 template <class A>
33 class AnyArcFilter {
34 public:
operator()35   bool operator()(const A &arc) const { return true; }
36 };
37 
38 
39 // True for (input/output) epsilon arcs.
40 template <class A>
41 class EpsilonArcFilter {
42 public:
operator()43   bool operator()(const A &arc) const {
44     return arc.ilabel == 0 && arc.olabel == 0;
45   }
46 };
47 
48 
49 // True for input epsilon arcs.
50 template <class A>
51 class InputEpsilonArcFilter {
52 public:
operator()53   bool operator()(const A &arc) const {
54     return arc.ilabel == 0;
55   }
56 };
57 
58 
59 // True for output epsilon arcs.
60 template <class A>
61 class OutputEpsilonArcFilter {
62 public:
operator()63   bool operator()(const A &arc) const {
64     return arc.olabel == 0;
65   }
66 };
67 
68 
69 // True if specified labels match (don't match) when keep_match is
70 // true (false).
71 template <class A>
72 class MultiLabelArcFilter {
73 public:
74   typedef typename A::Label Label;
75 
76   MultiLabelArcFilter(bool match_input = true, bool keep_match = true)
match_input_(match_input)77     :  match_input_(match_input),
78        keep_match_(keep_match) {}
79 
80 
operator()81   bool operator()(const A &arc) const {
82     Label label = match_input_ ? arc.ilabel : arc.olabel;
83     bool match = labels_.Find(label) != labels_.End();
84     return keep_match_ ? match : !match;
85   }
86 
AddLabel(Label label)87   void AddLabel(Label label) {
88     labels_.Insert(label);
89   }
90 
91 private:
92   CompactSet<Label, kNoLabel> labels_;
93   bool match_input_;
94   bool keep_match_;
95 };
96 
97 }  // namespace fst
98 
99 #endif  // FST_LIB_ARCFILTER_H__
100