Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

python/jupyter notebook please Write a function called count_words that takes a list of the words of s as its only argument and returns a col

image text in transcribedpython/jupyter notebook please

Write a function called count_words that takes a list of the words of s as its only argument and returns a col lections. Counter that maps a word to the frequency that it occurred in s. Use the output of the get_words function as the input to this function. s= 'The cat in the hat ate the rat in the vat' words = get_words (s) count_words (words) Counter(\{'the': 3, 'in': 2, 'The': 1, 'cat': 1, 'hat': 1, 'ate': 1, 'rat': 1, 'vat': 1} ) Notice that this is somewhat unsatisfying because the is counted separately from The. To fix this, have your get_words function be able to lower-case all of the words before returning them. You won't want to break any previous code you wrote, though (backwards compatibility is important)), so add a new parameter to get_words with a default value: def get_words (s, do_lower=False) Now, if get_words is called the way we were using it above, nothing will change. But if we call get_words (s, do_lower=True) then get_words should lowercase the string before getting the words. You can make use of str. Lower to modify the string. When you're done, the following should work: s= 'The cat in the hat ate the rat in the vat' words = get_words (s, do_lower=True ) count_words (words) Counter(\{'the': 4 , 'in': 2 , 'cat': 1, 'hat': 1 , 'ate': 1 , 'rat': 1 , 'vat': 1} )

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions