forked from fogleman/FeedNotifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filters.py
223 lines (186 loc) · 4.95 KB
/
filters.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# Keyword Filter Parser
EXCLUDE = 0
INCLUDE = 1
ALL = 0xf
TITLE = 1
LINK = 2
AUTHOR = 4
CONTENT = 8
TYPES = {
None: INCLUDE,
'+': INCLUDE,
'-': EXCLUDE,
}
QUALIFIERS = {
None: ALL,
'title:': TITLE,
'link:': LINK,
'author:': AUTHOR,
'content:': CONTENT,
}
TYPE_STR = {
EXCLUDE: '-',
INCLUDE: '+',
}
QUALIFIER_STR = {
ALL: 'all',
TITLE: 'title',
LINK: 'link',
AUTHOR: 'author',
CONTENT: 'content',
}
class Rule(object):
def __init__(self, type, qualifier, word):
self.type = TYPES.get(type, type)
self.qualifier = QUALIFIERS.get(qualifier, qualifier)
self.word = word
def evaluate(self, item, ignore_case=True, whole_word=True):
strings = []
if self.qualifier & TITLE:
strings.append(item.title)
if self.qualifier & LINK:
strings.append(item.link)
if self.qualifier & AUTHOR:
strings.append(item.author)
if self.qualifier & CONTENT:
strings.append(item.description)
text = '\n'.join(strings)
word = self.word
if ignore_case:
text = text.lower()
word = word.lower()
if whole_word:
text = set(text.split())
if word in text:
return self.type == INCLUDE
else:
return self.type == EXCLUDE
def __str__(self):
type = TYPE_STR[self.type]
qualifier = QUALIFIER_STR[self.qualifier]
return '(%s, %s, "%s")' % (type, qualifier, self.word)
class AndRule(object):
def __init__(self, left, right):
self.left = left
self.right = right
def evaluate(self, item, ignore_case=True, whole_word=True):
a = self.left.evaluate(item, ignore_case, whole_word)
b = self.right.evaluate(item, ignore_case, whole_word)
return a and b
def __str__(self):
return '(%s and %s)' % (self.left, self.right)
class OrRule(object):
def __init__(self, left, right):
self.left = left
self.right = right
def evaluate(self, item, ignore_case=True, whole_word=True):
a = self.left.evaluate(item, ignore_case, whole_word)
b = self.right.evaluate(item, ignore_case, whole_word)
return a or b
def __str__(self):
return '(%s or %s)' % (self.left, self.right)
class NotRule(object):
def __init__(self, rule):
self.rule = rule
def evaluate(self, item, ignore_case=True, whole_word=True):
return not self.rule.evaluate(item, ignore_case, whole_word)
def __str__(self):
return '(not %s)' % (self.rule)
# Lexer Rules
reserved = {
'and': 'AND',
'or': 'OR',
'not': 'NOT',
}
tokens = [
'PLUS',
'MINUS',
'LPAREN',
'RPAREN',
'TITLE',
'LINK',
'AUTHOR',
'CONTENT',
'WORD',
] + reserved.values()
t_PLUS = r'\+'
t_MINUS = r'\-'
t_LPAREN = r'\('
t_RPAREN = r'\)'
def t_TITLE(t):
r'title:'
return t
def t_LINK(t):
r'link:'
return t
def t_AUTHOR(t):
r'author:'
return t
def t_CONTENT(t):
r'content:'
return t
def t_WORD(t):
r'(\'[^\']+\') | (\"[^\"]+\") | ([^ \n\t\r+\-()\'"]+)'
t.type = reserved.get(t.value, 'WORD')
if t.value[0] == '"' and t.value[-1] == '"':
t.value = t.value[1:-1]
if t.value[0] == "'" and t.value[-1] == "'":
t.value = t.value[1:-1]
return t
t_ignore = ' \n\t\r'
def t_error(t):
raise Exception
# Parser Rules
precedence = (
('left', 'OR'),
('left', 'AND'),
('right', 'NOT')
)
def p_filter(t):
'filter : expression'
t[0] = t[1]
def p_expression_rule(t):
'expression : rule'
t[0] = t[1]
def p_expression_and(t):
'expression : expression AND expression'
t[0] = AndRule(t[1], t[3])
def p_expression_or(t):
'expression : expression OR expression'
t[0] = OrRule(t[1], t[3])
def p_expression_not(t):
'expression : NOT expression'
t[0] = NotRule(t[2])
def p_expression_group(t):
'expression : LPAREN expression RPAREN'
t[0] = t[2]
def p_rule(t):
'rule : type qualifier WORD'
t[0] = Rule(t[1], t[2], t[3])
def p_type(t):
'''type : PLUS
| MINUS
| empty'''
t[0] = t[1]
def p_qualifier(t):
'''qualifier : TITLE
| LINK
| AUTHOR
| CONTENT
| empty'''
t[0] = t[1]
def p_empty(t):
'empty :'
pass
def p_error(t):
raise Exception
import ply.lex as lex
import ply.yacc as yacc
lexer = lex.lex()
parser = yacc.yacc()
def parse(text):
return parser.parse(text, lexer=lexer)
if __name__ == '__main__':
while True:
text = raw_input('> ')
print parse(text)