Answered step by step
Verified Expert Solution
Question
1 Approved Answer
1 Description In this assignment, you will define a WordProcessor class based on functions defined in a given module, which is stored in a file
Description
In this assignment, you will define a WordProcessor class based on functions defined in a given module, which is stored in a file words.py Based on your WordProcessor definition, your WordProcessor object should be able to process a textual file by counting the number of words and extracting the vocabulary of words in the file content. A word, in this assignment, is defined as a sequence of adjacent letters separated by punctuations, numbers, or white spaces. There is a
function text to words in words.py capable of identifying words from a string. You can apply it in your class definition to extract words, if necessary. Note that the vocabulary extracted by the WordProcessor object must consist of words without duplicates. This implies you have to remove repeated words when building the vocabulary. The function remove adjacent dups defined in words.py should be helpful for your vocabulary construction. In addition to words.py I also provide a skeleton code file wp skeleton.py on which you can
define your WordProcessor class.
Functions in words.py
In this python file words.py you can see three functions defined.
def load string from filefilename:
#code is missing here
def text to wordsthe text:
#code is missing here
def remove adjacent dupsxs:
#code is missing here
You should practice using the three functions in your Python shell. The following I list some
examples I used in my Python shell.
file content words.load string from filesawyertxt
wds words.text to wordsfile content
printwds:
the 'adventures', of 'tom', 'sawyer', 'twain', 'mark', 'electronic', 'text', 'center'
Notice that I have a text file sawyer.txt saved in my current working directory. More
examples:
wds words.text to wordsthis is a different example yes!"
printwds
thisisa 'different', 'example', 'yes'
wdssort
printwds
a 'different', 'example', is 'this', 'yes'
wds words.text to wordsyes this is a a different example yes!"
wdssort
printwds
aa 'different', 'example', is 'this', 'yes', 'yes'
wds words.remove adjacent dupswds
printwds
a 'different', 'example', is 'this', 'yes'
Notice that I used a list method sort in the above code. In the example
wdssort
The sort method is able to sort the words maintained on the list wds in place.
You need to implement your own wppy based on the given wp skeleton.py
Here is the partial code in wp skeleton.py :
import words class WordProcessor: def
init self: def processself filename:
#more code here for other methods
When you define the class methods, you can apply the three functions defined in the words.py
You should import words in your class definition so that you can apply the functions. For
example, you can
import words class
WordProcessor:
#some code here
def processself filename:
#some code applying a function in words module file content
words.load string from filefilename
#more code should follow
The other part of the given code is for testing purpose. Feel free to extend the testing code.
wp WordProcessor wpprocessbrookstxt
#display the number of words in the text file printwpgetWordcount
#display the vocabulary of words in the text file printwpgetVocabulary
Based on the content of brooks.txt which is a textual file, the output when running wppy will
be like
$ python wppy
andbe 'conceal', 'continue', 'flowcharts', illme 'mystified',
'need', 'obvious', 'shall', 'show', t 'tables', 'they', to 'usually', 'won', 'your'
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started