Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In SpinFile.py implement a program that Reads some text from a file called 'essay.txt ' . The file.read ( ) method reads a whole file

In SpinFile.py implement a program that
Reads some text from a file called 'essay.txt'. The file.read() method reads a whole file without reading line by line. Make the text lower case and remove punctuation like we did for the ChatBot.
Take the text read from the 'essay.txt' file and use a Spinner object from the Spinner class to change some of the words. The Spinner class should return a string with some of the words changed into a synonym. The program should print "Original :" followed by the original text. Then use a loop to convert the original text three times. Each time it is changed, print "Option 1(or 2 or 3) :" followed by the changed text. Each time through the loop, change the original text to a changed version. Do not change the original and then change the changed text, then change that changed text.
In Spinner.py
Write code in the Spinner class that reads in one of the synonym files and stores a dictionary where the key is the word from a synonym file and the value is a list of synonyms for the word.
When you read the lines from the file, each line may end with a "
" at the end of the line. This newline may add extra blank lines when the program is changing your text into synonyms. You can use the strip() method to remove blank lines (and extra spaces) at the beginning and end of a string. For example, " David
".strip will return "David". You need to assign the returned result to a variable.
Think carefully about how to use split() to get the key word and the list of words value. You will want to do this in a few steps. First, split on ':' to get a string with a word and a string of synonyms. Then, think about about how to use split again to turn a string like "good,great,fine" into a list ['good','great','fine'].
The dictionary should end up looking like {'kid': ['child', 'goat']} where the key is a word we might want to replace and the list has some options to replace the word.
Write code in the Spinner class that uses the stored dictionary to look at each word in the some text.
If the word is not in the dictionary, do not change it.
If the word is in the dictionary, change it to one of its synonyms based on some random probability. To do this, pick a random number from 1 to 100. If the random number is bigger then some fixed number, like 50, then change the word. In this case, half the random numbers will be above 50 and half below, so there is a 50% chance of changing the word. You can experiment with different fixed numbers to find one that changes some words but not too many words.
Build up a new string from these words (the unchanged and changed) and return the new string. You can build up a string by starting with "" and then using + to add more letters. For example, text = "I am" followed by text += "David" would combine those to make "I amDavid". Note that I need to think about adding spaces to make this more correct.
Return the string of changed and unchanged words with a space between them.
You have some options how to implement this, but you must make a class with some stored value(s) and method(s). You should follow the patterns and examples we have used in past assignments for good code structure. The ChatBot and Chatter example is a good example to look at since it also read a file, and had text coming in and text coming out.

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

Database Driven Web Sites

Authors: Joline Morrison, Mike Morrison

2nd Edition

? 061906448X, 978-0619064488

More Books

Students also viewed these Databases questions