Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write code in python This problem requires you to prompt the user for a sentence and then construct two lists: one that contains each unique
Write code in python
This problem requires you to prompt the user for a sentence and then construct two lists: one that contains each unique word, and one that contains the number of times that word occurred in the sentence. Create these as parallel lists where the indices match between the lists. The index for the word in the words list should match the index for that words count in the occurrences list. This problem will have you practice with strings, lists, and loops.
The input prompt should be as follows:
"Enter your sentence:
n
You will need to remove any punctuation from the sentence. As a simplification, you can assume that any punctuation will be at the beginning or end of a word, and you only need to worry about the characters Extra spaces should also be removed.
You will also need to make sure that all of the words are converted to lower case for purposes of counting their frequency and the final list.
Hint: There is punctuation in the middle of the sentence such as commas. These will need to be removed. It can be useful to split up the string to then process each word in the sentence to remove punctuation.
For example, if the user entered the sentence "The cow jumped over the moon.", your program would create two lists to keep track of the words and the word occurrences. After calculating the word frequencies, print out the words as well as their frequencies in the following format:
Word.........Frequency
the.................
cow.................
jumped..............
over................
moon................
Here, the words from the word list are left aligned spaces with a fill character of a period and the words frequency from the occurrences list is right aligned spaces with a period fill character as well. Between the header line and the data from the sentence is a line of underscores
NOTE that the word the is in the final word list only once in lower case, but is in the sentence twice and thus counted twice in the frequency list. Also note that the period at the end of the sentence should not be included.
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