Question
#-------------------------------------------------- # 7777777777777777777777777777777777777777777777777 # get_previous_words_dict() #-------------------------------------------------- #-------------------------------------------------- Define the get_previous_words_dict() function which text as a parameter. The function returns a dictionary with keys which
#-------------------------------------------------- # 7777777777777777777777777777777777777777777777777 # get_previous_words_dict() #-------------------------------------------------- #-------------------------------------------------- """
Define the get_previous_words_dict() function which text as a parameter. The function returns a dictionary with keys which are all the unique words from the text, and, the corresponding values which are lists of all the unique words in the text which come before the key word. Note that in each corresponding list of previous words, the same word should not appear more than once.
Notes: the first word in the sentence will initially have the empty string as its
previous word, you can assume that the text is all in lower case and contains no
punctuation the testing code makes use of the print_dict_in_key_order(a_dict) which
prints the dictionary pairs in sorted key order. each list of previous words must be sorted into ascending order.
For example, the following code:
sentence = 'a man we saw saw a saw' previous_words_dict = get_previous_words_dict(sentence) print_dict_in_key_order(previous_words_dict) print()
sentence = 'my favourite painting is the painting i did of my dog in that painting in my den' previous_words_dict = get_previous_words_dict(sentence) print_dict_in_key_order(previous_words_dict)
prints:
a : ['', 'saw'] man : ['a'] saw : ['a', 'saw', 'we'] we : ['man']
den : ['my'] did : ['i'] dog : ['my'] favourite : ['my'] i : ['painting'] in : ['dog', 'painting'] is : ['painting']
my : ['', 'in', 'of'] of : ['did'] painting : ['favourite', 'that', 'the'] that : ['in'] the : ['is'] """
def get_previous_words_dict(text):
return {}
Step by Step Solution
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