
EBK JAVA PROGRAMMING
9th Edition
ISBN: 9781337671385
Author: FARRELL
Publisher: CENGAGE LEARNING - CONSIGNMENT
expand_more
expand_more
format_list_bulleted
Question
Instructions for a computer Python file I have to create called "Markov.py"

Transcribed Image Text: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.](https://content.bartleby.com/qna-images/question/645f5f13-b198-4aa8-9dad-4f403fda1bca/ac7e8948-ea73-4287-b786-644b0d9444ea/iboi6uf_thumbnail.png)
Transcribed Image Text: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.
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by stepSolved in 2 steps

Knowledge Booster
Similar questions
- (Program) Write a program that tests the effectiveness of the rand() library function. Start by initializing 10 counters, such as zerocount, onecount, twocount, and so forth, to 0. Then generate a large number of pseudorandom integers between 0 and 9. Each time 0 occurs, increment zerocount; when 1 occurs, increment onecount; and so on. Finally, display the number of 0s, 1s, 2s, and so on that occurred and the percentage of time they occurred.arrow_forwardThe implementation of a queue in an array, as given in this chapter, uses the variable count to determine whether the queue is empty or full. You can also use the variable count to return the number of elements in the queue. On the other hand, class linkedQueueType does not use such a variable to keep track of the number of elements in the queue. Redefine the class linkedQueueType by adding the variable count to keep track of the number of elements in the queue. Modify the definitions of the functions addQueue and deleteQueue as necessary. Add the function queueCount to return the number of elements in the queue. Also, write a program to test various operations of the class you defined.arrow_forward1. Consider the following incomplete C++ program: #include int main() { … } a. Write a statement that includes the header files fstream, string, and iomanip in this program. b. Write statements that declare inFile to be an ifstream variable and outFile to be an ofstream variable. c. The program will read data from the file inData.txt and write output to the file outData.txt. Write statements to open both of these files, associate inFile with inData.txt, and associate outFile with outData.txt. d. Suppose that the file inData.txt contains the following data: Giselle Robinson Accounting 5600 5 30 450 9 75 1.5 The first line contains a person's first name, last name, and the department the person works in. In the second line, the first number represents the monthly gross salary, the bonus (as a percent), and the taxes (as a percent). The third line contains the distance traveled and the traveling time. The fourth line contains the number of coffee cups sold and the cost of each coffee cup. Write statements so that after the program executes, the contents of the file outData.txt are as shown below. If necessary, declare additional variables. Your statements should be general enough so that if the content of the input file changes and the program is run again (without editing and recompiling), it outputs the appropriate results. Name: Giselle Robinson, Department: Accounting Monthly Gross Salary: $5600.00, Monthly Bonus: 5.00, Taxes: 30.000 Paycheck: $4116.00 Distance Traveled 450.00 miles, Traveling Time: 9.00 hours Average Speed: 50.00 miles per hour Number of coffee Cups Sold: 75, Cost: $1.50 per cup Sales Amount = $112.50 e. Write statements that close the input and output files. f. Write a C++ program that tests the statements in parts a through e.arrow_forward
arrow_back_ios
arrow_forward_ios
Recommended textbooks for you
- EBK JAVA PROGRAMMINGComputer ScienceISBN:9781337671385Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENTC++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage LearningC++ for Engineers and ScientistsComputer ScienceISBN:9781133187844Author:Bronson, Gary J.Publisher:Course Technology Ptr
- Microsoft Visual C#Computer ScienceISBN:9781337102100Author:Joyce, Farrell.Publisher:Cengage Learning,Programming Logic & Design ComprehensiveComputer ScienceISBN:9781337669405Author:FARRELLPublisher:CengageEBK JAVA PROGRAMMINGComputer ScienceISBN:9781305480537Author:FARRELLPublisher:CENGAGE LEARNING - CONSIGNMENT

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