Advertisement
Guest User

triggers.py

a guest
Aug 4th, 2015
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.14 KB | None | 0 0
  1. import re
  2. class WordTrigger(Trigger):
  3.     """Sub class of Trigger"""
  4.     def __init__(self,word):
  5.         """Initialization of the WordTrigger class"""
  6.         self.word = word.lower()
  7.     def isWordIn(self,text):
  8.         """Returns whether the object's word's present in the text"""
  9.         #Splits text with punctuations as word separators
  10.         text = re.split('[!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ ]',text.lower())
  11.         return self.word in text
  12.  
  13. class TitleTrigger(WordTrigger):
  14.         def evaluate(self,story):
  15.             """Returns whether the object's word's present in the title of the news story"""
  16.             return self.isWordIn(story.getTitle())
  17.  
  18. class SubjectTrigger(WordTrigger):
  19.         def evaluate(self,story):
  20.             """Returns whether a given word appears in the subject of the news story"""
  21.             return self.isWordIn(story.getSubject())
  22.  
  23. class SummaryTrigger(WordTrigger):
  24.         def evaluate(self,story):
  25.             """Returns whether a given word appears in the subject of the news story"""
  26.             return self.isWordIn(story.getSummary())
  27. # Composite Triggers
  28.  
  29. class NotTrigger(Trigger):
  30.     """A composite trigger whose output is opposite to that of the given trigger"""
  31.     def __init__(self,another):
  32.         self.opposite_trigger = another
  33.     def evaluate(self,story):
  34.         return not self.opposite_trigger.evaluate(story)
  35.  
  36. class AndTrigger(Trigger):
  37.     """A composite trigger which fires if and only the given two triggers evaluate to true"""
  38.     def __init__(self,first_trigger,second_trigger):
  39.         self.first_trigger = first_trigger
  40.         self.second_trigger = second_trigger
  41.  
  42.     def evaluate(self,story):
  43.         return self.first_trigger.evaluate(story) and self.second_trigger.evaluate(story)
  44.  
  45. class OrTrigger(Trigger):
  46.     """A composite trigger which triggers if one or both of the given triggers fire"""
  47.     def __init__(self,first_trigger,second_trigger):
  48.         self.first_trigger = first_trigger
  49.         self.second_trigger = second_trigger
  50.  
  51.     def evaluate(self,story):
  52.         return self.first_trigger.evaluate(story) or self.second_trigger.evaluate(story)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement