2012-11-24 35 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) 

最後主要是給出了錯誤:「‘馬氏’對象有沒有屬性‘file_to_words’」

import Class 
file_ = open('derp.txt') 
markov = Class.Markov(file_) 
markov.generate_markov_text() 

什麼這裏錯了嗎?謝謝。

+2

您file_to_words不縮進,使其成爲馬爾可夫類的一部分。這是一個裸體功能。 – Keith

回答

2

您需要縮進file_to_words方法,以使其成爲Markov類的一部分。你現在擁有它的方式是Class函數中的模塊級函數。將file_to_words方法(包括def行)中的所有內容移動到右側4個空格處。

更新:對於所有其他方法也是如此。 Python使用空格/縮進來表示範圍。

+0

謝謝..現在我覺得自己像一個白癡。 – user1378618

+0

嘿,我們都做了一次。這就是你學習的方式。 – Whatang

+0

最後一個問題你能解釋爲什麼現在我運行它的程序不會生成單詞輸出嗎?我基本上只是在這裏的程序,並試圖運行它的樂趣,但我無法匹配他的輸出? http://agiliq.com/blog/2009/06/generating-pseudo-random-text-with-markov-chains-u/ – user1378618

1

從您發佈的代碼中,除init以外的所有方法由於縮進而不屬於Markov類。