Exercise 5.1 (markov.py). Write a program that takes in a filename, reads each line of the file, converts the lines into a format convenient for making Markov chains, and then prints out a new sentence randomly generated from the data, based on the Markov algorithm. You may use any file you wish for test input, though alice.txt and cthulhu.txt are provided on Canvas. alice.txt is a slightly edited version of the complete text of Lewis Carroll's Alice's Adventures in Wonderland, taken from https://www.gutenberg.org/files/11/11-h/11-h.htm. cthulhu.txt is a slightly edited version of the complete text of H.P. Lovecrafts's The Call of Cthulhu, taken from http://www.hplovecraft.com/writings/texts/fiction/cc.aspx. When parsing the input file, you should ignore any blank lines. Treat the lines in the file as separate sentences. That is, if "Hello, how are you?" and "Where are my keys?" are lines in a file, then "you?" should not be followed by "Where" when generating a chain. However, "are" should be allowed to be followed by either "you?" or "my", as seen in Figure 1. When creating a new chain, the first element should always be a randomly selected first word of a line in the file. The chain should end when either: • There are no valid choices to continue the sentence with. ⚫ The sentence has reached a length of 100 words. Remember that you need to account for different frequencies of possible follow words. That is, a situation like in Figure 2 where the word "a" is followed by two "is" and one "known". You need to account for whatever word frequencies might come up within your input file. Hint 3 You may find dictionaries to be useful in implementing Markov chains. For example, the Markov chain in Figure 1 could be represented using this dictionary: 'Hello,': ['how'], 'how': ['are'], 2 'Where': ['are'], 'are': ['you', 'my'], 'you?': [], 'my': ['keys'], 'keys?': []} To generate a Markov chain, pick a random word from a list of first words. Then pick a random word that comes after the first word. Keep picking random next words until no more words are left or until the length limit is reached.
Exercise 5.1 (markov.py). Write a program that takes in a filename, reads each line of the file, converts the lines into a format convenient for making Markov chains, and then prints out a new sentence randomly generated from the data, based on the Markov algorithm. You may use any file you wish for test input, though alice.txt and cthulhu.txt are provided on Canvas. alice.txt is a slightly edited version of the complete text of Lewis Carroll's Alice's Adventures in Wonderland, taken from https://www.gutenberg.org/files/11/11-h/11-h.htm. cthulhu.txt is a slightly edited version of the complete text of H.P. Lovecrafts's The Call of Cthulhu, taken from http://www.hplovecraft.com/writings/texts/fiction/cc.aspx. When parsing the input file, you should ignore any blank lines. Treat the lines in the file as separate sentences. That is, if "Hello, how are you?" and "Where are my keys?" are lines in a file, then "you?" should not be followed by "Where" when generating a chain. However, "are" should be allowed to be followed by either "you?" or "my", as seen in Figure 1. When creating a new chain, the first element should always be a randomly selected first word of a line in the file. The chain should end when either: • There are no valid choices to continue the sentence with. ⚫ The sentence has reached a length of 100 words. Remember that you need to account for different frequencies of possible follow words. That is, a situation like in Figure 2 where the word "a" is followed by two "is" and one "known". You need to account for whatever word frequencies might come up within your input file. Hint 3 You may find dictionaries to be useful in implementing Markov chains. For example, the Markov chain in Figure 1 could be represented using this dictionary: 'Hello,': ['how'], 'how': ['are'], 2 'Where': ['are'], 'are': ['you', 'my'], 'you?': [], 'my': ['keys'], 'keys?': []} To generate a Markov chain, pick a random word from a list of first words. Then pick a random word that comes after the first word. Keep picking random next words until no more words are left or until the length limit is reached.
Chapter13: File Input And Output
Section: Chapter Questions
Problem 6PE
Related questions
Question
Instructions for a computer Python file I have to create called "Markov.py"
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps
Recommended textbooks for you
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781337671385
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage
EBK JAVA PROGRAMMING
Computer Science
ISBN:
9781305480537
Author:
FARRELL
Publisher:
CENGAGE LEARNING - CONSIGNMENT