Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- class WordTrigger(Trigger):
- """Sub class of Trigger"""
- def __init__(self,word):
- """Initialization of the WordTrigger class"""
- self.word = word.lower()
- def isWordIn(self,text):
- """Returns whether the object's word's present in the text"""
- #Splits text with punctuations as word separators
- text = re.split('[!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ ]',text.lower())
- return self.word in text
- class TitleTrigger(WordTrigger):
- def evaluate(self,story):
- """Returns whether the object's word's present in the title of the news story"""
- return self.isWordIn(story.getTitle())
- class SubjectTrigger(WordTrigger):
- def evaluate(self,story):
- """Returns whether a given word appears in the subject of the news story"""
- return self.isWordIn(story.getSubject())
- class SummaryTrigger(WordTrigger):
- def evaluate(self,story):
- """Returns whether a given word appears in the subject of the news story"""
- return self.isWordIn(story.getSummary())
- # Composite Triggers
- class NotTrigger(Trigger):
- """A composite trigger whose output is opposite to that of the given trigger"""
- def __init__(self,another):
- self.opposite_trigger = another
- def evaluate(self,story):
- return not self.opposite_trigger.evaluate(story)
- class AndTrigger(Trigger):
- """A composite trigger which fires if and only the given two triggers evaluate to true"""
- def __init__(self,first_trigger,second_trigger):
- self.first_trigger = first_trigger
- self.second_trigger = second_trigger
- def evaluate(self,story):
- return self.first_trigger.evaluate(story) and self.second_trigger.evaluate(story)
- class OrTrigger(Trigger):
- """A composite trigger which triggers if one or both of the given triggers fire"""
- def __init__(self,first_trigger,second_trigger):
- self.first_trigger = first_trigger
- self.second_trigger = second_trigger
- def evaluate(self,story):
- return self.first_trigger.evaluate(story) or self.second_trigger.evaluate(story)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement