1

我正在尝试构建一个马尔可夫生成器,它可以将任意长度的单词链作为编程练习,但我发现了一个我似乎无法修复的错误。当我运行 markov 函数时,列表索引超出范围。

我觉得我忽略了一些明显的东西,但我不确定是什么。回溯说错误在第 41 行,带有words[-1] = nextWords[random.randint(0, len(nextWords)-1)].

完整代码如下,如果缩进搞砸了,请见谅。

#! /usr/bin/python

# To change this template, choose Tools | Templates
# and open the template in the editor.

import random

class Markov(object):
    def __init__(self, open_file):
        self.cache = {}
    self.open_file = open_file
    open_file.seek(0)
    self.wordlist = open_file.read().split()

def get_random_list(self, length):
    i = random.randint(0, len(self.wordlist) - (length - 1))
    result = self.wordlist[i:i + length]
    return result

def find_next_word(self, words):
    candidates = []
    for i in range(len(self.wordlist) - len(words)):
        if self.wordlist[i:i + len(words)] == words and self.wordlist[i+len(words)+1] not in candidates:
            candidates.append(self.wordlist[i+len(words)+1])
    return candidates

def markov(self, length=20, chainlength=2):
    gibberish = []
    words = self.get_random_list(chainlength)
    for i in range(len(words)-1):
        gibberish.append(words[i])
    while len(gibberish) < length:
        #find candidate for next word
        nextWords = self.find_next_word(words)
        gibberish.append(words[-1])
        for i in range(len(words)):
            try:
                words[i] = words[i+1]
            except:
                pass
        words[-1] = nextWords[random.randint(0, len(nextWords)-1)]
    return " ".join(gibberish)
4

1 回答 1

7

如果words是空的,那么是的,那将会发生。尝试访问words[-1]空数组与words[0]. 添加检查以查看是否len(words) == 0. 相同的逻辑nextWords在此代码中看起来也可能为空。

于 2011-08-03T16:57:04.377 回答