我正在尝试构建一个马尔可夫生成器,它可以将任意长度的单词链作为编程练习,但我发现了一个我似乎无法修复的错误。当我运行 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)