Write a function create_dictionary(filename) that takes a string representing th
ID: 3703202 • Letter: W
Question
Write a function create_dictionary(filename) that takes a string representing the name of a text file, and that returns a dictionary of key-value pairs in which:
each key is a word encountered in the text file
the corresponding value is a list of words that follow the key word in the text file.
For example, the dictionary produced for the text “I love roses and carnations. I hope I get roses for my birthday.” would include the following key-value pairs, among others:
Guidelines:
You should not try to remove the punctuation from the words of the text file.
The keys of the dictionary should include every word in the fileexcept the sentence-ending words. A sentence-ending word is defined to be any word whose last character is a period ('.'), a question mark ('?'), or an exclamation point ('!'). A sentence-ending word should be included in the lists associated with the words that it follows (i.e., in the value parts of the appropriate key-value pairs), but it not appear as its own key.
If a word w1 is followed by another word w2 multiple times in the text file, then w2 should appear multiple times in the list of words associated with w1. This will allow you to capture the frequency with which word combinations appear.
In addition to the words in the file, the dictionary should include the string $ as a special key referred to as the sentence-start symbol. This symbol will be used when choosing the first word in a sentence. In the dictionary, the list of words associated with the key '$' should include:
the first word in the file
every word in the file that follows a sentence-ending word.
Doing this will ensure that the list of words associated with '$'includes all of the words that start a sentence. For example, the dictionary for the text “I scream. You scream. We all scream for ice cream.” would include the following entry for the sentence-start symbol:
You may find it helpful to consult the word_frequencies functionfrom lecture. We will also discuss some additional strategies for create_dictionary in lecture.
Examples:
To test your code, download the sample.txt file into the same directory that contains ps8pr3.py. This sample text file contains the following contents:
Once this file is in place, run your ps8pr3.py in IDLE and test your function from the Shell:
The order of the keys–or of the elements within a given key’s list of values–may not be the same as what you see above, but the elements of the lists should appear in the quantities shown above for each of the four keys 'A', 'B', 'C', and '$'.
Here are some additional files you can use for testing:
edited_mission.txt - an edited version of BU’s mission statement, and the dictionary that we derived from it.
brave.txt - lyrics from the song Brave by Sara Bareilles, and its dictionary.
Here again, the ordering that you obtain for the keys and list elements in the dictionaries may be different. In addition, we have edited the formatting of the dictionaries to make them easier to read.
Explanation / Answer
def create_dictionary(fileName):
'''
Function that creates a dictionary for the words in the file given
Input : File Name (String)
'''
#Creating an empty dictionary
dict = {};
#Opening file for reading
fp = open(fileName, "r");
#Reading data from file
data = fp.read();
#Split the data on space
data = data.split(' ');
#Create a key $ to hold the words that starts with sentences
dict['$'] = [];
#First word of sentence is added
dict['$'].append(data[0]);
#Looping over each word in the dictionary
for i in range(0,len(data)):
#If word doesn't end with .
if data[i][len(data[i])-1] != '.':
#Checking for existence of key in the dictionary
if data[i] in dict.keys():
#If already present, just append to the value list
dict[data[i]].append(data[i+1]);
else:
#If not present, create a key and value as empty list
dict[data[i]] = [];
#Append to the newly created list
dict[data[i]].append(data[i+1]);
#If word end with .
else:
#Append to the key '$'
if (i+1) < len(data):
dict['$'].append(data[i+1]);
#Printing dictionary
print(" Dictionary Created: " + str(dict));
#Calling function
create_dictionary("sample.txt");
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.