Lab7README.md piglatin.py markov.py Test.txt calculator.py 1 2 C: > Users > fjbc2 > Downloads > =Test.txt There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itsel "Oh dear! Oh dear! I shall be late!" (when she thought it over afterwards, 3 it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); 4 but when the Rabbit actually took a watch out of its waistcoat-pocket, and looked at it, and then hurried on, 5 Alice started to her feet, for it flashed across her mind that she had never before seen 6 a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, 7 she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Attached is a text file called "Test.txt" that I've created for Python:

"There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itself, 
“Oh dear! Oh dear! I shall be late!” (when she thought it over afterwards, 
it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); 
but when the Rabbit actually took a watch out of its waistcoat-pocket, and looked at it, and then hurried on, 
Alice started to her feet, for it flashed across her mind that she had never before seen 
a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, 
she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge."

 

I'm trying to create a Markov chain with this code I have here (markov.py) but the program won't run because it doesn't have a direct path to "Test.txt".  Can you help me fix this issue? 

 

import random


def is_ending_word(word):
    return word[-1] in {'.', '!', '?'}


def read_file(filename):
    chain = {}
    first_words = []
    with open(filename, 'r') as f:
        for line in f:
            words = line.split()
            first_words.append(words[0])

            for i in range(len(words) - 1):
                if words[i] not in chain:
                    chain[words[i]] = []
                if not is_ending_word(words[i]):
                    chain[words[i]].append(words[i + 1])


                if is_ending_word(words[i + 1]):
                    if i < len(words) - 2:
                        first_words.append(words[i + 2])
                    chain[words[i + 1]] = []


            if words and words[-1] not in chain:
                chain[words[-1]] = []

    return chain, first_words


def generate_sentence(chain, start_lst):
    current = random.choice(start_lst)
    sentence = [current]
    while True:
        if chain[current]:
            current = random.choice(chain[current])
        else:
            break
        sentence.append(current)
    return ' '.join(sentence)

def main():
    chain, first_word = read_file('Test.txt')
    generated_sentence = generate_sentence(chain, first_word)
    print(generated_sentence)

if __name__ == '__main__':
    main()
Lab7README.md
piglatin.py
markov.py
Test.txt
calculator.py
1
2
C: > Users > fjbc2 > Downloads > =Test.txt
There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itsel
"Oh dear! Oh dear! I shall be late!" (when she thought it over afterwards,
3
it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural);
4
but when the Rabbit actually took a watch out of its waistcoat-pocket, and looked at it, and then hurried on,
5
Alice started to her feet, for it flashed across her mind that she had never before seen
6
a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity,
7
she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.
Transcribed Image Text:Lab7README.md piglatin.py markov.py Test.txt calculator.py 1 2 C: > Users > fjbc2 > Downloads > =Test.txt There was nothing so very remarkable in that; nor did Alice think it so very much out of the way to hear the Rabbit say to itsel "Oh dear! Oh dear! I shall be late!" (when she thought it over afterwards, 3 it occurred to her that she ought to have wondered at this, but at the time it all seemed quite natural); 4 but when the Rabbit actually took a watch out of its waistcoat-pocket, and looked at it, and then hurried on, 5 Alice started to her feet, for it flashed across her mind that she had never before seen 6 a rabbit with either a waistcoat-pocket, or a watch to take out of it, and burning with curiosity, 7 she ran across the field after it, and fortunately was just in time to see it pop down a large rabbit-hole under the hedge.
AI-Generated Solution
AI-generated content may present inaccurate or offensive content that does not represent bartleby’s views.
steps

Unlock instant AI solutions

Tap the button
to generate a solution

Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education