Question: Problem 2 Word Retrieval Write a function called get_words which takes in a single argument, a string representing a sentence. It should return an iterable
Problem 2 Word Retrieval
Write a function called get_words which takes in a single argument, a string representing a sentence. It should return an iterable of the unique words in the string.
- Words must be lowercase and contain at least 1 alphanumeric character (i.e. the empty string is not a word).
- Words should have all punctuation characters stripped. A punctuation character is any one defined by punctuation in Pythons string module. Punctuation should be completely removed (i.e. not replaced by spaces). Note that because of this, you may get some words which dont look like English words; this is OK.
- You may assume that the sentence only contains a mix of alphanumeric characters (a-z, A-Z, 0-9), whitespace (spaces, tabs, newlines), and punctuation as defined in string.punctuation.
Hint: Python has a built-in data structure for unique sets of words, which you may find useful. Example:
my_sentence = "Grocery list: 3 boxes Land-o-Lakes butter, Aunt Jemima's butter pancake mix" words = get_words(my_sentence) # Should output something like: ['grocery', 'list', '3', 'boxes', 'landolakes', 'butter', 'aunt', 'jemimas', 'pancake', 'mix'] # Can be any appropriate iterable # Note that 'butter' only appears once
use python 3.8
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
