Answered step by step
Verified Expert Solution
Link Copied!

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

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 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.
2 Functions in words.py
In this python file words.py you can see three functions defined.
def load string from file(filename):
#code is missing here
def text to words(the text):
#code is missing here
def remove adjacent dups(xs):
#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 file("sawyer.txt")
>>> wds = words.text to words(file content)
>>> print(wds[:10])
['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:
>>> wds2= words.text to words("this is a different example yes!")
>>> print(wds2)
['this','is','a', 'different', 'example', 'yes']
>>> wds2.sort()
>>> print(wds2)
['a', 'different', 'example', 'is', 'this', 'yes']
>>> wds3= words.text to words("yes this is a a different example yes!")
>>> wds3.sort()
>>> print(wds3)
['a','a', 'different', 'example', 'is', 'this', 'yes', 'yes']
>>> wds3= words.remove adjacent dups(wds3)
>>> print(wds3)
['a', 'different', 'example', 'is', 'this', 'yes']
Notice that I used a list method sort() in the above code. In the example
2
>>> wds3.sort()
The sort() method is able to sort the words maintained on the list wds3 in place.
3 You need to implement your own wp.py based on the given wp skeleton.py
Here is the partial code in wp skeleton.py :
import words class WordProcessor: def
init (self): def process(self, 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 process(self, filename):
#some code applying a function in words module file content =
words.load string from file(filename)
#more code should follow
The other part of the given code is for testing purpose. Feel free to extend the testing code.
wp = WordProcessor() wp.process("brooks.txt")
#display the number of words in the text file print(wp.getWordcount())
#display the vocabulary of words in the text file print(wp.getVocabulary())
Based on the content of brooks.txt, which is a textual file, the output when running wp.py will
be like
$ python3 wp.py
31
['and','be', 'conceal', 'continue', 'flowcharts', 'i','ll','me', 'mystified',
'need', 'obvious', 'shall', 'show', 't', 'tables', 'they', 'to', 'usually', 'won', 'your']
image text in transcribed

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_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

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

Get Started

Students also viewed these Databases questions

Question

7. Identify six intercultural communication dialectics.

Answered: 1 week ago