Python 2到3端口 - 还有一个错误

时间:2015-04-07 02:12:54

标签: python-2.7 python-3.x

我正在移植一个名为markovgenerator的程序,我在网上找到了从Python2到Python3的程序。这一切似乎都很好。

以下是代码:(Python3版本)

import random

class Markov:
    def __init__(self, file, size):
        self.size = size
        self.starts = []
        self.cache = {}
        self.file_to_words(file)
        self.parse_words()

    def file_to_words(self, file):
        file.seek(0)
        data = file.read()
        self.words = data.split("\n")

    def tuples(self, word):
        if len(word) < self.size - 1:
            return
        word = word + "\n"
        for i in range(len(word) - self.size):
            yield (word[i:i+self.size], word[i+self.size])


    def parse_words(self):
        for word in self.words:
            self.starts.append(word[:self.size])
            for key, next in self.tuples(word):
                if key in self.cache:
                    self.cache[key].append(next)
                else:
                    self.cache[key] = [next]

    def generate_word(self):
        key = random.choice(self.starts)
        word = key
        next = random.choice(self.cache[key])
        while next != "\n":
            word = word + next
            key = key[1:] + next
            next = random.choice(self.cache[key])
        return word

from optparse import OptionParser

def main():
    parser = OptionParser()
    parser.add_option('-p', type='int', dest='prev_num', default=3,
                  help='number of previous letters to base chain on')
    parser.add_option('-n', type='int', dest='num', default=5,
                  help='number of generated words')
    parser.add_option('-s', '--source-text', type='string',
                  default='wordlist-en.txt', dest='source',
                  help='file to use as basis for generating the words')
    (options, args) = parser.parse_args()

    file = open('alice.txt')
    markov = Markov(file, options.prev_num)
    file.close()
    for i in range(options.num):
        print(markov.generate_word())

if __name__ == '__main__':
    main()

除非我收到此错误:

next = random.choice(self.cache[key])
KeyError: ''

错误出现在“generate_word()”函数中 它必须从翻译到3.任何想法?我不明白为什么我得到一个关键错误,因为我把钥匙传递到其他地方没问题。

感谢您的帮助!!!

1 个答案:

答案 0 :(得分:1)

修复了该错误,并忽略了输出的任何空行:

import random

class Markov:
    def __init__(self, file, size):
        self.size = size
        self.starts = []
        self.cache = {}
        self.file_to_words(file)
        self.parse_words()
        self.cache[''] = '\n'

    def file_to_words(self, file):
        file.seek(0)
        data = file.read()
        self.words = data.split("\n")

    def tuples(self, word):
        if len(word) < self.size - 1:
            return
        word = word + "\n"
        for i in range(len(word) - self.size):
            yield (word[i:i+self.size], word[i+self.size])


    def parse_words(self):
        for word in self.words:
            self.starts.append(word[:self.size])
            for key, next in self.tuples(word):
                if key in self.cache:
                    self.cache[key].append(next)
                else:
                    self.cache[key] = [next]

    def generate_word(self):
        key = random.choice(self.starts)
        word = key
        next = random.choice(self.cache[key])
        while not next == "\n":
            word = word + next
            key = key[1:] + next
            next = random.choice(self.cache[key])
        return word

from optparse import OptionParser

def main():
    parser = OptionParser()
    parser.add_option('-p', type='int', dest='prev_num', default=3,
                  help='number of previous letters to base chain on')
    parser.add_option('-n', type='int', dest='num', default=5,
                  help='number of generated words')
    parser.add_option('-s', '--source-text', type='string',
                  default='wordlist-en.txt', dest='source',
                  help='file to use as basis for generating the words')
    (options, args) = parser.parse_args()

    file = open('alice.txt')
    markov = Markov(file, options.prev_num)
    file.close()
    iters = 0
    while iters < options.num:
        word = markov.generate_word()
        if word != '\n' and word != '':
          print(word)
          iters += 1


if __name__ == '__main__':
    main()

出于某种原因,当您尝试在字典''中使用{1}}时,字符串KeyError正在注册为单词。我尝试删除它的所有内容都导致程序中断,因此我在cache添加了一行,将__init__的下一个单词设置为'',当我们看到时,通过退出给出预期结果换行符。

如果此代码有任何问题,请告诉我,我将很乐意解决此问题。