Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Write a function make_vocabulary (text) that takes a string text, parses it into words, and creates a dictionary of words where keys are unique
Write a function make_vocabulary (text) that takes a string text, parses it into words, and creates a dictionary of words where keys are unique words, and their values are word occurrences. Your function should work with the following code that prints the generated dictionary. For example, if the input text is the quote from the famous English poet Lord Byron: In secret we met - In silence I grieve, That thy heart could forget, Thy spirit deceive. If I should meet thee After long years, How should I greet thee? - With silence and tears then the output of the program is: Total words in the dictionary: 27 After 1 How : 1 1:3 If : 1 In: 2 That: 1 Thy: 1 With: 1 and: 1 could: 1 deceive 1 forget 1 greet: 1 grieve: 1 heart: 1 long: 1 meet: 1 met: 1 secret : 1 should: 2 silence : 2 spirit: 1 tears: 1 thee: 2 forget 1 greet: 1 grieve 1 heart: 1 long : 1 meet met: 1 secret 1 1 should : 2 silence : 2 spirit 1 tears: 1 thee: 2 thy : 1 we: 1 years: 1 NOTE: Your program should work with the following code: #main if_name__main__': text In secret we met - In silence I grieve, That thy heart could forget, Thy spirit deceive. If I should meet thee i After long years, How should I greet thee? With silence and tears*** vocabulary - make_vocabulary(text) print('Total words in the dictionary: (len(vocabulary)}') for key in sorted(vocabulary): print(f'(key}: {vocabulary [key]}') HINTS: You may use the string method split) to parse the text into words, regular expressions to remove punctuation marks, and method get() to update the dictionary. For example, #parse text into words words text.split() #remove the punctuation marks from a word (not words!) import re word = re.sub(r'[^\w\s]", "", word) #update dictionary vocabulary [word] = vocabulary.get(word, 0) + 1
Step by Step Solution
★★★★★
3.39 Rating (152 Votes )
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