Computers and Technology

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']}

answer
Answers: 2

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 23:30, kenken2583
Jaina and tomas are being considered as new tenants in an apartment. the landlord looks at their creditworthiness because he wants to be sure his new tenant pays the rent on time and in full. the table below summarizes the information that was on their applications. application information questions jaina tomas how many years have you had your job? 5 2 what is your monthly salary? $1,850 $2,500 how many credit cards do you have? 4 1 how much debt do you have? $13,000 $7,000 how many times were you late with payments on credit cards in the past year? 5 1 who will the landlord decide to be more creditworthy and why? tomas because the ratio of his debt to income is less. jaina because she has had her job longer, which makes her look more stable. jaina because she has more credit cards available to her. tomas because he makes more money per month.
Answers: 2
image
Computers and Technology, 23.06.2019 00:40, QueenKy6050
Consider the following statements: struct nametype{string first; string last; }; struct coursetype{string name; int callnum; int credits; char grade; }; struct studenttype{nametype name; double gpa; coursetype course; }; studenttype student; studenttype classlist[100]; coursetype course; nametype name; mark the following statements as valid or invalid. if a statement is invalid, explain why. a.) student. course. callnum = "csc230"; b.) cin > > student. name; c.) classlist[0] = name; d.) classlist[1].gpa = 3.45; e.) name = classlist[15].name; f.) student. name = name; g.) cout < < classlist[10] < < endl; h.) for (int j = 0; j < 100; j++)classlist[j].name = name; i.) classlist. course. credits = 3; j.) course = studenttype. course;
Answers: 1
image
Computers and Technology, 23.06.2019 04:40, Koriunaa
The narrative structure of the popular movies can be broken down into
Answers: 3
image
Computers and Technology, 23.06.2019 15:30, taapeters
Brian wants to conduct an online search with a certain phrase. he intends to use the words books that belong to the 1800s in his search. how should he use the word that in his search?
Answers: 1
Do you know the correct answer?
Python code:

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

Questions in other subjects:

Konu
Mathematics, 15.01.2021 22:20
Konu
English, 15.01.2021 22:20