Advertisement
jacmoe

PS6

Nov 15th, 2012
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.51 KB | None | 0 0
  1. #======================
  2. # Part 1
  3. # Data structure design
  4. #======================
  5.  
  6. # Problem 1
  7.  
  8. class NewsStory(object):
  9.     def __init__(self, guid, title, subject, summary, link):
  10.         self.guid = guid
  11.         self.title = title
  12.         self.subject = subject
  13.         self.summary = summary
  14.         self.link = link
  15.  
  16.     def getGuid(self):
  17.         return self.guid
  18.    
  19.     def getTitle(self):
  20.         return self.title
  21.    
  22.     def getSubject(self):
  23.         return self.subject
  24.    
  25.     def getSummary(self):
  26.         return self.summary
  27.    
  28.     def getLink(self):
  29.         return self.link
  30.  
  31. #======================
  32. # Part 2
  33. # Triggers
  34. #======================
  35.  
  36. class Trigger(object):
  37.     def evaluate(self, story):
  38.         """
  39.        Returns True if an alert should be generated
  40.        for the given news item, or False otherwise.
  41.        """
  42.         raise NotImplementedError
  43.  
  44. # Whole Word Triggers
  45. # Problems 2-5
  46.  
  47. class WordTrigger(Trigger):
  48.     def __init__(self, word):
  49.         self.word = word.lower()
  50.  
  51.     def isWordIn(self, text):
  52.         the_text = text[:]
  53.         the_text = the_text.lower()
  54.         for i in range(0, len(string.punctuation)):
  55.             the_text = string.replace(the_text, string.punctuation[i], ' ')
  56.         the_text = the_text.split(' ')
  57.         return self.word in the_text
  58.  
  59.  
  60. class TitleTrigger(WordTrigger):
  61.     def evaluate(self, story):
  62.         """
  63.        Returns True if an alert should be generated
  64.        for the given news item, or False otherwise.
  65.        """
  66.         return self.isWordIn(story.getTitle())
  67.  
  68.  
  69. class SubjectTrigger(WordTrigger):
  70.     def evaluate(self, story):
  71.         """
  72.        Returns True if an alert should be generated
  73.        for the given news item, or False otherwise.
  74.        """
  75.         return self.isWordIn(story.getSubject())
  76.  
  77. class SummaryTrigger(WordTrigger):
  78.     def evaluate(self, story):
  79.         """
  80.        Returns True if an alert should be generated
  81.        for the given news item, or False otherwise.
  82.        """
  83.         return self.isWordIn(story.getSummary())
  84.  
  85. # Composite Triggers
  86. # Problems 6-8
  87.  
  88. class NotTrigger(Trigger):
  89.     def __init__(self, T):
  90.         self.T = T
  91.  
  92.     def evaluate(self, story):
  93.         """
  94.        Returns True if an alert should be generated
  95.        for the given news item, or False otherwise.
  96.        """
  97.         return not self.T.evaluate(story)
  98.  
  99. class AndTrigger(Trigger):
  100.     def __init__(self, T1, T2):
  101.         self.T1 = T1
  102.         self.T2 = T2
  103.  
  104.     def evaluate(self, story):
  105.         """
  106.        Returns True if an alert should be generated
  107.        for the given news item, or False otherwise.
  108.        """
  109.         return self.T1.evaluate(story) and self.T2.evaluate(story)
  110.  
  111. class OrTrigger(Trigger):
  112.     def __init__(self, T1, T2):
  113.         self.T1 = T1
  114.         self.T2 = T2
  115.  
  116.     def evaluate(self, story):
  117.         """
  118.        Returns True if an alert should be generated
  119.        for the given news item, or False otherwise.
  120.        """
  121.         return self.T1.evaluate(story) or self.T2.evaluate(story)
  122.  
  123.  
  124. # Phrase Trigger
  125. # Question 9
  126.  
  127. class PhraseTrigger(Trigger):
  128.     def __init__(self, phrase):
  129.         self.phrase = phrase
  130.     def evaluate(self, story):
  131.         """
  132.        Returns True if an alert should be generated
  133.        for the given news item, or False otherwise.
  134.        """
  135.         if self.phrase in story.getTitle():
  136.             return True
  137.         elif self.phrase in story.getSubject():
  138.             return True
  139.         elif self.phrase in story.getSummary():
  140.             return True
  141.         return False
  142.  
  143. #======================
  144. # Part 3
  145. # Filtering
  146. #======================
  147.  
  148. def filterStories(stories, triggerlist):
  149.     """
  150.    Takes in a list of NewsStory instances.
  151.  
  152.    Returns: a list of only the stories for which a trigger in triggerlist fires.
  153.    """
  154.     storyList = []    
  155.     for story in stories:
  156.         for trigger in triggerlist:
  157.             if trigger.evaluate(story):
  158.                 storyList.append(story)
  159.     return storyList
  160.  
  161. #======================
  162. # Part 4
  163. # User-Specified Triggers
  164. #======================
  165.  
  166. def makeTrigger(triggerMap, triggerType, params, name):
  167.     """
  168.    Takes in a map of names to trigger instance, the type of trigger to make,
  169.    and the list of parameters to the constructor, and adds a new trigger
  170.    to the trigger map dictionary.
  171.  
  172.    triggerMap: dictionary with names as keys (strings) and triggers as values
  173.    triggerType: string indicating the type of trigger to make (ex: "TITLE")
  174.    params: list of strings with the inputs to the trigger constructor (ex: ["world"])
  175.    name: a string representing the name of the new trigger (ex: "t1")
  176.  
  177.    Modifies triggerMap, adding a new key-value pair for this trigger.
  178.  
  179.    Returns: None
  180.    """
  181.     if triggerType == 'SUMMARY':
  182.         trigger = SummaryTrigger(params[0])
  183.     elif triggerType == 'SUBJECT':
  184.         trigger = SubjectTrigger(params[0])
  185.     elif triggerType == 'TITLE':
  186.         trigger = TitleTrigger(params[0])
  187.     elif triggerType == 'PHRASE':
  188.         phrase = (' ').join(params)
  189.         trigger = PhraseTrigger(phrase)
  190.     elif triggerType == 'AND':
  191.         trigger = AndTrigger(triggerMap[params[0]], triggerMap[params[1]])
  192.     elif triggerType == 'OR':
  193.         trigger = OrTrigger(triggerMap[params[0]], triggerMap[params[1]])
  194.     elif triggerType == 'NOT':
  195.         trigger = NotTrigger(triggerMap[params[0]])
  196.     else:
  197.         return None
  198.     triggerMap[name] = trigger
  199.     return trigger
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement