Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #======================
- # Part 1
- # Data structure design
- #======================
- # Problem 1
- class NewsStory(object):
- def __init__(self, guid, title, subject, summary, link):
- self.guid = guid
- self.title = title
- self.subject = subject
- self.summary = summary
- self.link = link
- def getGuid(self):
- return self.guid
- def getTitle(self):
- return self.title
- def getSubject(self):
- return self.subject
- def getSummary(self):
- return self.summary
- def getLink(self):
- return self.link
- #======================
- # Part 2
- # Triggers
- #======================
- class Trigger(object):
- def evaluate(self, story):
- """
- Returns True if an alert should be generated
- for the given news item, or False otherwise.
- """
- raise NotImplementedError
- # Whole Word Triggers
- # Problems 2-5
- class WordTrigger(Trigger):
- def __init__(self, word):
- self.word = word.lower()
- def isWordIn(self, text):
- the_text = text[:]
- the_text = the_text.lower()
- for i in range(0, len(string.punctuation)):
- the_text = string.replace(the_text, string.punctuation[i], ' ')
- the_text = the_text.split(' ')
- return self.word in the_text
- class TitleTrigger(WordTrigger):
- def evaluate(self, story):
- """
- Returns True if an alert should be generated
- for the given news item, or False otherwise.
- """
- return self.isWordIn(story.getTitle())
- class SubjectTrigger(WordTrigger):
- def evaluate(self, story):
- """
- Returns True if an alert should be generated
- for the given news item, or False otherwise.
- """
- return self.isWordIn(story.getSubject())
- class SummaryTrigger(WordTrigger):
- def evaluate(self, story):
- """
- Returns True if an alert should be generated
- for the given news item, or False otherwise.
- """
- return self.isWordIn(story.getSummary())
- # Composite Triggers
- # Problems 6-8
- class NotTrigger(Trigger):
- def __init__(self, T):
- self.T = T
- def evaluate(self, story):
- """
- Returns True if an alert should be generated
- for the given news item, or False otherwise.
- """
- return not self.T.evaluate(story)
- class AndTrigger(Trigger):
- def __init__(self, T1, T2):
- self.T1 = T1
- self.T2 = T2
- def evaluate(self, story):
- """
- Returns True if an alert should be generated
- for the given news item, or False otherwise.
- """
- return self.T1.evaluate(story) and self.T2.evaluate(story)
- class OrTrigger(Trigger):
- def __init__(self, T1, T2):
- self.T1 = T1
- self.T2 = T2
- def evaluate(self, story):
- """
- Returns True if an alert should be generated
- for the given news item, or False otherwise.
- """
- return self.T1.evaluate(story) or self.T2.evaluate(story)
- # Phrase Trigger
- # Question 9
- class PhraseTrigger(Trigger):
- def __init__(self, phrase):
- self.phrase = phrase
- def evaluate(self, story):
- """
- Returns True if an alert should be generated
- for the given news item, or False otherwise.
- """
- if self.phrase in story.getTitle():
- return True
- elif self.phrase in story.getSubject():
- return True
- elif self.phrase in story.getSummary():
- return True
- return False
- #======================
- # Part 3
- # Filtering
- #======================
- def filterStories(stories, triggerlist):
- """
- Takes in a list of NewsStory instances.
- Returns: a list of only the stories for which a trigger in triggerlist fires.
- """
- storyList = []
- for story in stories:
- for trigger in triggerlist:
- if trigger.evaluate(story):
- storyList.append(story)
- return storyList
- #======================
- # Part 4
- # User-Specified Triggers
- #======================
- def makeTrigger(triggerMap, triggerType, params, name):
- """
- Takes in a map of names to trigger instance, the type of trigger to make,
- and the list of parameters to the constructor, and adds a new trigger
- to the trigger map dictionary.
- triggerMap: dictionary with names as keys (strings) and triggers as values
- triggerType: string indicating the type of trigger to make (ex: "TITLE")
- params: list of strings with the inputs to the trigger constructor (ex: ["world"])
- name: a string representing the name of the new trigger (ex: "t1")
- Modifies triggerMap, adding a new key-value pair for this trigger.
- Returns: None
- """
- if triggerType == 'SUMMARY':
- trigger = SummaryTrigger(params[0])
- elif triggerType == 'SUBJECT':
- trigger = SubjectTrigger(params[0])
- elif triggerType == 'TITLE':
- trigger = TitleTrigger(params[0])
- elif triggerType == 'PHRASE':
- phrase = (' ').join(params)
- trigger = PhraseTrigger(phrase)
- elif triggerType == 'AND':
- trigger = AndTrigger(triggerMap[params[0]], triggerMap[params[1]])
- elif triggerType == 'OR':
- trigger = OrTrigger(triggerMap[params[0]], triggerMap[params[1]])
- elif triggerType == 'NOT':
- trigger = NotTrigger(triggerMap[params[0]])
- else:
- return None
- triggerMap[name] = trigger
- return trigger
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement