Question: Define the get_previous_words_dict() function which is passed a string of text as a parameter. The function returns a dictionary with keys which are all the
Define the get_previous_words_dict() function which is passed a string of 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)
Expected
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):
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
