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, 23.06.2019 01:00
Petrică, tânăr licean în clasa a ix-a, a primit în dar de la părinţii săi un cont bancar pentru micile sale cheltuieli curente. el este pasionat de internet banking şi îşi verifică cu grijă toate tranzacţiile efectuate. pentru creşterea securităţii tranzacţiilor online, banca îi furnizează lui petrică un număr pe care el va trebui să îl modifice, obţinând un număr tan – număr de autentificare a tranzacţiei (transaction authentication number). regula de obţinere a numărului tan este următoarea: se formează cel mai mic număr par din toate cifrele numărului furnizat de bancă. cerinţă cunoscând numărul n furnizat de bancă, să se determine numărul tan obţinut de petrică. date de intrare fişierul tan.in conţine pe prima linie numărul natural n cu semnificaţia din enunţ. date de ieşire fişierul de ieşire tan.out va conţine o singură linie pe care va fi scris numărul tan cerut. restricţii • 0 < n < 18*1018 • n are cel puţin o cifră pară • numărul tan obţinut nu poate conţine zerouri nesemnificative
Answers: 2
question
Computers and Technology, 23.06.2019 17:00
1. which of the following is not an example of an objective question? a. multiple choice. b. essay. c. true/false. d. matching 2. why is it important to recognize the key word in the essay question? a. it will provide the answer to the essay. b. it will show you a friend's answer. c. it will provide you time to look for the answer. d. it will guide you on which kind of answer is required.
Answers: 1
question
Computers and Technology, 23.06.2019 21:50
Description: write function lastfirst() that takes one argument—a list of strings of the format "lastname, firstname" —and returns a list consisting of two lists: (a) a list of all the last names (b) a list of all the first names
Answers: 2
question
Computers and Technology, 24.06.2019 10:00
Which two technologies support the building of single-page applications?
Answers: 2
You know the right answer?
Python code:

write a function create_dictionary(filename) that takes a string represent...
Questions
question
Mathematics, 17.02.2021 22:40
question
Business, 17.02.2021 22:40
question
English, 17.02.2021 22:40
question
English, 17.02.2021 22:40
question
Mathematics, 17.02.2021 22:40
question
Medicine, 17.02.2021 22:40
question
Chemistry, 17.02.2021 22:40
Questions on the website: 13722361