Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem 1 Parsing all of this information from the feeds that Google/Yahoo/the New York Times/etc. gives us is no small feat. So, lets tackle an

Problem 1

Parsing all of this information from the feeds that Google/Yahoo/the New York Times/etc. gives us is no small feat. So, lets tackle an easy part of the problem first: Pretend that someone has already done the specific parsing, and has left you with variables that contain the following information for a news story:

globally unique identifier (GUID) a string that serves as a unique name for this entry

title a string

published a string

summary a string

link to more content a string

We want to store this information in an object that we can then pass around in the rest of our program. Your task, in this problem, is to write a class, NewsStory, with at least the following methods:

get_guid()

get_title()

get_published()

get_summary()

get_link()

Youll also want to write a constructor for NewsStory that takes (guid, title, published, summary, link) as arguments and stores them appropriately. The solution to this problem should be relatively short and very straightforward.

Parsing the feed

Parsing is the process of turning a data stream into a structured format that is more convenient to work with. We have provided you with code that will retrieve and parse the Google and Yahoo news feeds.

Problem 2

Implement a word trigger abstract class, WordTrigger. It should take in a string word as an argument to the classs constructor.

WordTrigger should be a subclass of Trigger. It has one new method, is_word_in. is_word_in takes in one string argument text. It returns True if the whole word word is present in text, False otherwise, as described in the above examples. This method should not be case sensitive. Implement this method.

Because this is an abstract class, we will not be directly instantiating any WordTriggers. WordTrigger should inherit its evaluate method from Trigger. We do this because now we can create subclasses of WordTrigger that use its is_word_in function. In this way, it is much like the Trigger interface, except now actual code from the class is used.

Problem 3

Implement a word trigger class, TitleTrigger that fires when a news items title contains a given word. The word should be an argument to the classs constructor. This trigger should not be case-sensitive (it should treat Intel and intel as being equal).

For example, an instance of this type of trigger could be used to generate an alert whenever the word Intel occurred in the title of a news item. Another instance could generate an alert whenever the word Microsoft occurred in the title of an item.

Think carefully about what methods should be defined in TitleTrigger and what methods should be inherited from the superclass.

Once youve implemented TitleTrigger, the TitleTrigger unit tests in our test suite should pass.

Problem 4

Implement a word trigger class, PublishedTrigger, that fires when a news items published contains a given word. The word should be an argument to the classs constructor. This trigger should not be case-sensitive.

Once youve implemented PublishedTrigger, the PublishedTrigger unit tests in our test suite should pass.

Problem 5

Implement a word trigger class, SummaryTrigger, that fires when a news items summary contains a given word. The word should be an argument to the classs constructor. This trigger should not be case-sensitive.

Once youve implemented SummaryTrigger, the SummaryTrigger unit tests in our test suite should pass.

Composite triggers

So the triggers above are mildly interesting, but we want to do better: we want to compose the earlier triggers, to set up more powerful alert rules. For instance, we may want to raise an alert only when both google and stock are present in the news item (an idea we cant express right now).

Note that these triggers are not word triggers and should not be subclasses of WordTrigger.

Problem 6

Implement a NOT trigger (NotTrigger).

This trigger should produce its output by inverting the output of another trigger. The NOT trigger should take this other trigger as an argument to its constructor (why its constructor? Because we cant change evaluate thatd break our polymorphism). So, given a trigger T and a news item x, the output of the NOT triggers evaluate method should be equivalent to not T.evaluate(x).

When this is done, the NotTrigger unit tests should pass.

Problem 7

Implement an AND trigger (AndTrigger).

This trigger should take two triggers as arguments to its constructor, and should fire on a news story only if both of the inputted triggers would fire on that item.

When this is done, the AndTrigger unit tests should pass

Problem 8

Implement an OR trigger (OrTrigger).

This trigger should take two triggers as arguments to its constructor, and should fire if either one (or both) of its inputted triggers would fire on that item.

When this is done, the OrTrigger unit tests should pass.

Phrase triggers

At this point, you have no way of writing a trigger that matches on New York City the only triggers you know how to write would be a trigger that would fire on New AND York AND City which also fires on the phrase New students at York University love the city. Its time to fix this. Since here youre asking for an exact match, we will require that the cases match, but well be a little more flexible on word matching. So, New York City will match:

  • New York City sees movie premiere
  • In the heart of New York Citys famous cafe
  • New York Cityrandomtexttoproveapointhere

but will not match:

  • I love new york city

Problem 9

Implement a phrase trigger (PhraseTrigger) that fires when a given phrase is in the title or the summary. The phrase should be an argument to the classs constructor. You may find the Python operator in helpful, as in:

>>> print("New York City" in "In the heart of New York City's famous cafe") True >>> print("New York City" in "I love new york city") False

When this is done, the PhraseTrigger unit tests should pass.

Part 3: Filtering

At this point, you can run ps5.py, and it will fetch and display Google and Yahoo news items for you in little pop-up windows. How many news items? All of them.

Right now, the code weve given you in ps5.py gets all of the feeds every minute, and displays the result. This is nice, but, remember, the goal here was to filter out only the the stories we wanted.

Problem 10

Write a function, filter_stories(stories, triggerlist) that takes in a list of news stories and a list of triggers, and returns only the stories which a trigger fires for.

After completing Problem 10, you can try running ps5.py, and various RSS news items should pop up, filtered by some hard-coded triggers defined for you in some code near the bottom. The code runs an infinite loop, checking the RSS feed for new stories every 60 seconds.

Part 4. User-Specified Triggers

Right now, your triggers are specified in your Python code, and to change them, you have to edit your program. This is very user-unfriendly. (Imagine if you had to edit the source code of your web browser every time you wanted to add a bookmark!)

Instead, we want you to read your trigger configuration from a triggers.txt file, every time your application starts, and use the triggers specified there.

Consider the following example configuration file:

# subject trigger named t1 t1 SUBJECT world # title trigger named t2 t2 TITLE Intel # phrase trigger named t3 t3 PHRASE New York City # composite trigger named t4 t4 AND t2 t3 # the trigger set contains t1 and t4 ADD t1 t4

The example file specifies that four triggers should be created, and that two of those triggers should be added to the trigger set:

  • A trigger that fires when a subject contains the word world (t1).
  • A trigger that fires when the title contains the word intel and the news item contains the phrase New York City somewhere (t4).

The two other triggers (t2 and t3) are created but not added to the trigger set directly. They are used as arguments for the composite AND triggers definition.

Each line in this file does one of the following:

  • is blank
  • is a comment (begins with a #)
  • defines a named trigger
  • adds triggers to the trigger set.

Each type of line is described below.

Blank: blank lines are ignored. A line that consists only of whitespace is a blank line.

Comments: Any line that begins with a # character is ignored.

Trigger definitions: Lines that do not begin with the keyword ADD define named triggers. The first element in a trigger definition is the name of the trigger. The name can be any combination of letters without spaces, except for ADD. The second element of a trigger definition is a keyword (e.g., TITLE, PHRASE, etc.) that specifies the kind of trigger being defined. The remaining elements of the definition are the trigger arguments. What arguments are required depends on the trigger type:

  • TITLE: a single word.
  • PUBLISHED: a single word.
  • SUMMARY: a single word.
  • NOT: the name of the trigger that will be NOTd.
  • AND: the names of the two other triggers that will be ANDd.
  • OR: the names of the two other triggers that will be ORd.
  • PHRASE: a phrase.

Trigger addition: A trigger definition should create a trigger and associate it with a name but should not automatically add that trigger to the trigger set. One or more ADD lines in the .txt file will specify which triggers should be in the trigger set. An addition line begins with the ADD keyword. Following ADD are the names of one or more previously defined triggers. These triggers will be added to the the trigger set.

there are some important information :

for news question

#root = Tk() #root.withdraw()

# The Popup class class Popup: def __init__(self): self.root = tkinter.Tk() self.root.withdraw() """ self.root.mainloop() def drawALot(): self.root = Tk() self.root.withdraw() self.root.mainloop() thread.start_new_thread(drawALot, ()) """ def start(self): self.root.mainloop()

def _makeTheWindow(self, item): """ Private method that does the actual window drawing """ root = tkinter.Toplevel() root.wm_title("News Alert") w = root.winfo_screenwidth()/20 h = root.winfo_screenheight()/4 title = tkinter.Text(root, padx=5, pady=5, height=3, wrap=tkinter.WORD, bg="white") title.tag_config("title", foreground="black", font=("helvetica", 12, "bold")) title.tag_config("published", foreground="black", font=("helvetica", 12, "bold")) title.insert(tkinter.INSERT, "Title: %s" % item.get_title(), "title") title.insert(tkinter.INSERT, " Published: ", "published") title.insert(tkinter.INSERT, item.get_published().rstrip(), "published") title.config(state=tkinter.DISABLED, relief = tkinter.FLAT) title.grid(sticky=tkinter.W+tkinter.E) summary = tkinter.Text(root, padx=10, pady=5, height=15, wrap=tkinter.WORD, bg="white") summary.tag_config("text", foreground="black", font=("helvetica", 12)) summary.insert(tkinter.INSERT, item.get_summary().lstrip(), "text") summary.config(state=tkinter.DISABLED, relief = tkinter.FLAT) summary.grid(sticky=tkinter.W+tkinter.E) link = tkinter.Text(root, padx=5, pady=5, height=4, bg="white") link.tag_config("url", foreground="blue", font=("helvetica", 10, "bold")) link.insert(tkinter.INSERT, item.get_link(), "url") link.config(state=tkinter.DISABLED, relief=tkinter.FLAT) link.grid(sticky=tkinter.W+tkinter.E)

def newWindow(self, item): """ Displays a popup with the contents of the NewsStory newsitem """ self.root.after(0, self._makeTheWindow, item)

trigger file

# trigger file - if you've done through part 11 but no stories are popping # up, you should edit this file to contain triggers that will fire on current # news stories!

# title trigger named t1 t1 TITLE NFL

# summary trigger named t2 t2 SUMMARY Japan

# phrase trigger named t3 t3 PHRASE Supreme Court

# composite trigger named t4 t4 AND t2 t3

# the trigger set contains t1 and t4 ADD t1 t4

please use python! thank you verymuch

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions

Question

explain what is meant by experiential learning

Answered: 1 week ago

Question

identify the main ways in which you learn

Answered: 1 week ago