Write a function called count_words that takes a list of the words of s as its only argument and returns a collections. 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})

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Write a function called count_words that takes a list of the words of s as its only argument and returns a collections. 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})
Transcribed Image Text:Write a function called count_words that takes a list of the words of s as its only argument and returns a collections. 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})
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education