subject

Python code:

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:

1) each key is a word encountered in the tex file

2) the corresponding value is a list of words that follow the key word in the text file

for example the dictionary produced the text "i love roses and carnations. i hope i get roses for my birthday." would include the following key-value pairs, among others:

'i': ['love', 'hope', 'get']
'love': ['roses']
'roses': ['and', 'for']
'my': ['birthday.']
# as well as 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 file except 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 w2should 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:
'$': ['i', 'you', 'we']
-you may find it to consult the word_frequencies function from class. we will also discuss some additional strategies for create_dictionary in class.
examples:
to test your code, download the sample. txt file into the same directory that containsps13pr4.py. this sample text file contains the following contents:
a b a. a b c. b a c. c c c.
once this file is in place, run your ps13pr4.py in idle and test your function from the shell:
> > > word_dict = create_dictionary('sample. txt')
> > > word_dict
{'a': ['b', 'b', 'c.'], 'c': ['c', 'c.'],
'b': ['a.', 'c.', 'a'], '$': ['a', 'a', 'b', 'c']}

ansver
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 23.06.2019 05:00, mariahchaparro08
Which best explains why a digital leader would join a society specializing in technology
Answers: 1
image
Computers and Technology, 24.06.2019 00:00, miguelturner
Which tool could be used to display only rows containing presidents who served two terms
Answers: 3
image
Computers and Technology, 24.06.2019 10:00, genyjoannerubiera
In which view can you see speaker notes?
Answers: 1
image
Computers and Technology, 24.06.2019 18:00, bloop3r
Why is a multiview sketch drawinf different from other sketches like isometric, two point, and oblique
Answers: 1
You know the right answer?
Python code:

write a function create_dictionary(filename) that takes a string represent...

Questions in other subjects:

Konu
Mathematics, 27.05.2020 21:04
Konu
Mathematics, 27.05.2020 21:05