Answered step by step
Verified Expert Solution
Link Copied!

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..................2
cow..................1
jumped...............1
over.................1
moon.................1
Here, the words from the word list are left aligned 10 spaces with a fill character of a period and the words frequency from the occurrences list is right aligned 12 spaces with a period fill character as well. Between the header line and the data from the sentence is a line of 22 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

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

MySQL Crash Course A Hands On Introduction To Database Development

Authors: Rick Silva

1st Edition

1718503008, 978-1718503007

More Books

Students also viewed these Databases questions

Question

Provide examples of KPIs in Human Capital Management.

Answered: 1 week ago

Question

What are OLAP Cubes?

Answered: 1 week ago