0

初心者向けの Python ブック Think Python からこのコードを実行して、テキスト ファイルでマルコフ分析を実行しようとしています。解決策として提供されたコードを実行すると、random.py モジュールから IndexError: List index out of range が発生します。何を変更する必要がありますか?

このコードはThink Pythonの演習 8 の答えになるはずですが、うまくいきません。

トレースバック:

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

編集: .txt ファイルで実行しています。Project Gutenburg からダウンロードした本のテキストです。

4

1 に答える 1

0

プログラムを実行しているファイルには Gutenberg ヘッダーがありません。次で始まる行が必要です。

*END*THE SMALL PRINT!

その後のテキストのみが読み取られます (これはskip_gutenberg_header関数で発生します)。例として、ファイルでコードを実行します

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.

(一例として):

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
于 2012-08-11T22:08:35.577 に答える