我在Python下的NLTK,尤其是.generate()方法有問題。在Python的NLTK中從自定義文本生成隨機句子?
生成(個體,長度= 100)
打印隨機文本,使用三元語言模型生成。
參數:
* length (int) - The length of text to generate (default=100)
這裏是我在嘗試的簡化版本。
import nltk
words = 'The quick brown fox jumps over the lazy dog'
tokens = nltk.word_tokenize(words)
text = nltk.Text(tokens)
print text.generate(3)
這將總是產生
Building ngram index...
The quick brown
None
至於反對建立一個隨機短語出來的話。
這裏是我的輸出,當我做
print text.generate()
Building ngram index...
The quick brown fox jumps over the lazy dog fox jumps over the lazy
dog dog The quick brown fox jumps over the lazy dog dog brown fox
jumps over the lazy dog over the lazy dog The quick brown fox jumps
over the lazy dog fox jumps over the lazy dog lazy dog The quick brown
fox jumps over the lazy dog the lazy dog The quick brown fox jumps
over the lazy dog jumps over the lazy dog over the lazy dog brown fox
jumps over the lazy dog quick brown fox jumps over the lazy dog The
None
再次開始接觸相同的文字,但後來改變它。我也嘗試使用Orwell's 1984的第一章。再次,總是開始於前3個標記(其中一個是這種情況下的空間)並且然後繼續隨機生成文本。
我在這裏做錯了什麼?
NLTK使用單詞的上下文來確定它們的使用。舉例來說,他們在NLTK中有「Moby Dick」的全部文本。使用generate生成Meville聲音句子。所以,除非你知道我沒有的東西,否則我認爲你不想訴諸文字,因爲最初的語境很重要。 – 2009-07-19 15:56:49
你是對的。如果你打亂了你所說的話,你就會失去卦三圍的信息。 – Mastermind 2009-07-20 17:07:50