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

Another question on Computers and Technology

question
Computers and Technology, 22.06.2019 04:30
Which of the statements below is true? the formatting, standard, and drawing commands are unavailable. the formatting, standard, and drawing commands have been used. the formatting, standard, and drawing toolbars are displayed. the formatting, standard, and drawing toolbars are hidden.
Answers: 1
question
Computers and Technology, 23.06.2019 23:40
Which of the following calculates the total from the adjacent cell through the first nonnumeric cell by default, using the sum function in its formula? -average -autosum -counta -max
Answers: 1
question
Computers and Technology, 24.06.2019 00:00
Visualizing a game of “tag” to remember the meaning of contagious
Answers: 3
question
Computers and Technology, 24.06.2019 00:30
Which boolean operator enables you to exclude a search term? a} not b} and c} or d} plus
Answers: 1
You know the right answer?
Python code:

write a function create_dictionary(filename) that takes a string represent...
Questions
Questions on the website: 13722360