Answered step by step
Verified Expert Solution
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 createmarkovmodeltext that takes a string containing some text, and that returns a dictionary of word transitions ie 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 keyvalue 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 sentenceending words. A sentenceending word is defined to be any word whose last character is a period a question mark or an exclamation point
A sentenceending word should still be included in the lists associated with the words that it follows ie in the value parts of the appropriate keyvalue pairs but it must not appear as its own key.
If a word w is followed by another word w multiple times in the text file, then w should appear multiple times in the list of words associated with w 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 sentencestart 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 sentenceending 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 sentencestart 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 createmarkovmodeltext
model
A: BBCC: CC
B: ACA$: AABC
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 ABC 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:
editedmission.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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started