-
Notifications
You must be signed in to change notification settings - Fork 0
/
pset7-P3-Filtering.py
62 lines (42 loc) · 1.44 KB
/
pset7-P3-Filtering.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
# PART 3: FILTERING
class WordTrigger(Trigger):
def __init__(self,word):
self.word = word
def isWordIn(self,text):
word = self.word
word = word.lower()
text = text.lower()
import string
for x in string.punctuation:
word = word.replace(x,"")
text = text.replace(x,"")
word = word.split()
text = text.split()
for word in word:
if word not in text:
return False
else:return True
class TitleTrigger(WordTrigger):
def evaluate(self,story):
return self.isWordIn(story.getTitle())
class SubjectTrigger(WordTrigger):
def evaluate(self,story):
return self.isWordIn(story.getSubject())
class SummaryTrigger(WordTrigger):
def evaluate(self,story):
return self.isWordIn(story.getSummary())
class PhraseTrigger(Trigger):
def __init__(self,phrase):
self.phrase = phrase
def evaluate(self,story):
return self.phrase in story.getSubject() or \
self.phrase in story.getTitle() or \
self.phrase in story.getSummary()
def filterStories(stories, triggerlist):
result = []
for story in stories:
for trigger in triggerlist:
if trigger.evaluate(story):
if story not in result:
result.append(story)
return result