2012-11-24 2 views
0

안녕하세요. 간단한 질문입니다. 이 프로그램을 사용하여 코퍼스 (이 경우 책의 일부)에서 임의의 텍스트를 생성하려고합니다.속성 오류? 프로그램이 실행에 가까워졌습니다

나는 나의 코퍼스있는 텍스트 파일이 있습니다 (이것은 소개하고, 여기에 모든 일을 게시하지 않습니다)

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'이있다"무엇 여기 잘못 가고있어? 감사.

+2

file_to_words는 Markov 클래스의 일부로 들여 쓰기되지 않습니다. 그것은 알몸 기능입니다. – Keith

답변

2

file_to_words 메서드를 들여 쓰기해야 Markov 클래스의 일부가됩니다. 지금 당장 가지고있는 방법은 Class 함수의 모듈 수준 함수입니다. file_to_words 방법 (def 행 포함)의 모든 항목을 오른쪽으로 4 칸 옮깁니다.

업데이트 : 다른 모든 방법에도 동일하게 적용됩니다. 파이썬은 범위를 나타 내기 위해 공백/들여 쓰기를 사용합니다.

+0

고마워 .. 지금은 바보 같아. – user1378618

+0

이봐, 우린 모두 한 번 해냈다. 그것이 당신이 배우는 방법입니다. – Whatang

+0

마지막 질문 이제 왜 프로그램을 실행시켜야 단어 출력을 생성하지 못하는지 설명 할 수 있습니까? 나는 기본적으로 여기에 프로그램을 가져 가서 재미로 실행하려고하지만, 그의 결과와 일치시킬 수 없다 ?? http://agiliq.com/blog/2009/06/generating-pseudo-random-text-with-markov-chains-u/ – user1378618

1

게시 한 코드에서 들여 쓰기 때문에 init을 제외한 모든 메소드가 Markov 클래스에 속하지 않습니다.