Question

I am trying to run this code from the beginners' python book Think Python to do Markov Analysis on a text file. When I run the code provided as a solution, I get an IndexError: List index out of range from the random.py module. What do I need to change?

The code should be the answer to Exercise 8 off of Think Python, but I can't get it to work.

Traceback:

Traceback (most recent call last):
  File "test.py", line 115, in <module>
    main(*sys.argv)
  File "test.py", line 111, in main
    random_text(n)
  File "test.py", line 76, in random_text
    start = random.choice(suffix_map.keys())
  File "/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/random.py", line 274, in choice
    return seq[int(self.random() * len(seq))]  # raises IndexError if seq is empty

Edit: I am running it on a .txt file. It's the text of a book downloaded from Project Gutenburg.

Was it helpful?

Solution

The file you are running the program on doesn't have a Gutenberg header. It needs to have a line that starts with:

*END*THE SMALL PRINT!

Only text after that will be read (this occurs in the skip_gutenberg_header function). As an example, running the code on the file

Header info
*END*THE SMALL PRINT!

He was very clever, be it sweetness or be angry, ashamed or only amused, at such a stroke. She had never thought of Hannah till you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself.

produces (as one example):

a stroke. She had never thought of Hannah till you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself. at such a stroke. She had never thought of Hannah till you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself. you were never meant for me?" "I cannot make speeches, Emma:" he soon cut it all himself. cannot make speeches, Emma:" he soon cut it all himself. be it sweetness or be angry, ashamed or only amused, at such a stroke. She had never thought of
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top