Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a function create _ markov _ model ( text ) that takes a string containing some text, and that returns a dictionary of word

Write a function create_markov_model(text) that takes a string containing some text, and that returns a dictionary of word transitions (i.e., which words follow another word within the text). The format of this dictionary is as follows:
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 original text.
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:
'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 in the text these are important~
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 still 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 must 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 text
every word in the text 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']
Examples:
To test your function, begin with the following sample text:
>>> text ='A B A. A B C. B A C. C C C.'
>>> model = create_markov_model(text)
>>> model
{'A': ['B','B','C.'],'C': ['C','C.'],
'B': ['A.','C.','A'],'$': ['A','A','B','C']}
The order of the keys or of the elements within a given keys 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 '$'.
You can also test your function by using all of the text from any text file. Here are some additional files you can use for testing:
edited_mission.txt - an edited version of BUs mission statement, and the dictionary that we derived from it.
brave.txt - lyrics from the song Brave by Sara Bareilles, and its dictionary.
first act of Romeo and Juliet - the text of the first act of the classic tragedy Romeo and Juliet, by William Shakespeare.
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.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions