안녕하세요. 간단한 질문입니다. 이 프로그램을 사용하여 코퍼스 (이 경우 책의 일부)에서 임의의 텍스트를 생성하려고합니다.속성 오류? 프로그램이 실행에 가까워졌습니다
나는 나의 코퍼스있는 텍스트 파일이 있습니다 (이것은 소개하고, 여기에 모든 일을 게시하지 않습니다)
The Project Gutenberg EBook of My Man Jeeves, by P. G. Wodehouse
#27 in our series by P. G. Wodehouse
Copyright laws are changing all over the world. Be sure to check the
copyright laws for your country before downloading or redistributing
this or any other Project Gutenberg eBook.
This header should be the first thing seen when viewing this Project
Gutenberg file. Please do not remove it. Do not change or edit the
header without written permission.
Please read the "legal small print," and other information about the
eBook and Project Gutenberg at the bottom of this file. Included is
important information about your specific rights and restrictions in
how the file may be used. You can also find out about how to make a
donation to Project Gutenberg, and how to get involved.
etc etc etc
다음 내가 노력하고 클래스가 사용할 수 있습니다 :
오류를 제공import random
class Markov(object):
def __init__(self, open_file):
self.cache = {}
self.open_file = open_file
self.words = self.file_to_words()
self.word_size = len(self.words)
self.database()
def file_to_words(self):
self.open_file.seek(0)
data = self.open_file.read()
words = data.split()
return words
def triples(self):
""" Generates triples from the given data string. So if our string were
"What a lovely day", we'd generate (What, a, lovely) and then
(a, lovely, day).
"""
if len(self.words) < 3:
return
for i in range(len(self.words) - 2):
yield (self.words[i], self.words[i+1], self.words[i+2])
def database(self):
for w1, w2, w3 in self.triples():
key = (w1, w2)
if key in self.cache:
self.cache[key].append(w3)
else:
self.cache[key] = [w3]
def generate_markov_text(self, size=25):
seed = random.randint(0, self.word_size-3)
seed_word, next_word = self.words[seed], self.words[seed+1]
w1, w2 = seed_word, next_word
gen_words = []
for i in xrange(size):
gen_words.append(w1)
w1, w2 = w2, random.choice(self.cache[(w1, w2)])
gen_words.append(w2)
return ' '.join(gen_words)
마지막 주는 :
import Class
file_ = open('derp.txt')
markov = Class.Markov(file_)
markov.generate_markov_text()
" '마르코프'목적이없는 속성 'file_to_words'이있다"무엇 여기 잘못 가고있어? 감사.
file_to_words는 Markov 클래스의 일부로 들여 쓰기되지 않습니다. 그것은 알몸 기능입니다. – Keith