Question: Program must be coded using Python You will write a program that reads a book (Alice in Wonderland), and lets the user print information about

Program must be coded using Python

You will write a program that reads a book (Alice in Wonderland), and lets the user print information about words in the book. The user can print:

all the line numbers that a word appears on;

the count of occurrences of a word;

the top K most frequent words (for the user's choice of K);

quit.

First:

You will create two dictionaries: the first dictionary (word_line) has the word as the key, and the set of line numbers as the value; and the second dictionary (word_count) will have the word as the key, and the count of occurrences for the word as the value. All words appear in both dictionaries.

To accomplish this: You will read from the file (alice.txtProgram must be coded using Python You will write a program that), and extract each word from the line. (Note: split will not do this well, because of punctuation; a regular expression will do it better. See the find_all(...) command in the regular expression chapter; the option to match all words is '\w+'.) You will also keep a counter to track the line number. For each word in each line, the line number is added to the set for the entry in word_line, and the value is incremented for the entry in word_count.

Second:

You will create a list out of the keys in the dictionary word_count using the command sorted(...); the key will be word_count.get; and use the option reverse=True.

Third:

You will show the menu options, and write a function for each of the first 3 options to complete them. For options 1 and 2: Prompt for the word to match, and print the list from the dictionary for that key. If there is no match, the program should print a message to that effect. For option 3, prompt for a value of K, and print the top K words from the sorted list, and the count of their occurrences from the word_count dictionary (the word will be in the sorted word list, and that will be used as the key to get the value from word_count).

Error checking: You do not need to do any further error checking than in program 2: just ensure that the file is read otherwise print a message and quit; and ensure that only the valid menu options are processed.

*********************Sample of Alice.txt file**************************

ALICES ADVENTURES IN WONDERLAND

Lewis Carroll

THE MILLENNIUM FULCRUM EDITION 3.0

CHAPTER I. Down the Rabbit-Hole

Alice was beginning to get very tired of sitting by her sister on the bank, and of having nothing to do: once or twice she had peeped into the book her sister was reading, but it had no pictures or conversations in it, and what is the use of a book, thought Alice without pictures or conversations?

So she was considering in her own mind (as well as she could, for the hot day made her feel very sleepy and stupid), whether the pleasure of making a daisy-chain would be worth the trouble of getting up and picking the daisies, when suddenly a White Rabbit with pink eyes ran close by her.

There was nothing so VERY remarkable in that; nor did Alice think it so VERY much out of the way to hear the Rabbit say to itself, Oh dear! Oh dear! I shall be late! (when she thought it over afterwards, it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); but when the Rabbit actually TOOK A WATCH OUT OF ITS WAISTCOAT-POCKET, and looked at it, and then hurried on, Alice started to her feet, for it flashed across her mind that she had never before seen a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.

In another moment down went Alice after it, never once considering how in the world she was to get out again.

The rabbit-hole went straight on like a tunnel for some way, and then dipped suddenly down, so suddenly that Alice had not a moment to think about stopping herself before she found herself falling down a very deep well.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!