-
Notifications
You must be signed in to change notification settings - Fork 67
/
wordanalyzer.py
63 lines (49 loc) · 1.79 KB
/
wordanalyzer.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
# -*- coding: utf-8 -*-
from .basicanalyzer import BasicAnalyzer
class WordAnalyzer(BasicAnalyzer):
"""Analyzer to match the content of a paste by words"""
name = "WordAnalyzer"
def __init__(self, actions, words, blacklist=None, case_sensitive=False):
super().__init__(actions, "{0} ({1})".format(self.name, words))
if words is None:
self.words = []
elif isinstance(words, list):
self.words = words
else:
self.words = [words]
self.blacklist = blacklist or []
self.case_sensitive = case_sensitive
def _blacklist_word_found(self, text):
if not self.case_sensitive:
text = text.lower()
self.blacklist = [x.lower() for x in self.blacklist]
for word in self.blacklist:
if word in text:
return True
return False
def add_word(self, word):
"""
Add a word to the analyzer
:param word: Word to be added
:return:
"""
self.words.append(word)
def match(self, paste):
"""Check if the specified words are part of the paste text"""
if paste is None:
return False
paste_content = paste.body or ""
if self._blacklist_word_found(paste_content):
return False
_matches = []
if self.case_sensitive:
for word in self.words:
# Never use 'return word in paste_content' - otherwise you will
# return false before all words have been checked
if word in paste_content:
_matches.append(word)
else:
for word in self.words:
if word.lower() in paste_content.lower():
_matches.append(word)
return _matches