使用nltk用n-gram模型创建新句子

时间:2015-11-08 18:31:30

标签: python nlp nltk auto-generate n-gram

我从我的文本文件中制作了2和3克模型。

from nltk import *
text = open('Alice in Wonderland.txt', 'r').read()
table = string.maketrans('', '')
text = text.translate(table, string.punctuation)
tokens = word_tokenize(text.lower())
bigram = nltk.bigrams(tokens)
trigram = nltk.trigrams(tokens)

但是如何使用这些模型生成新句子?

1 个答案:

答案 0 :(得分:2)

目前,NLTK的generate()函数已被弃用,因为它已被破坏,请参阅https://github.com/nltk/nltk/issues/1180

但最先进的替代方案是使用Recurrent Neural Nets进行文本生成,例如: https://github.com/karpathy/char-rnn注意:与传统的基于Ngram的隐马尔可夫模型不同,char-RNN不使用ngrams信息。)

或者,您可以实现自己的隐马尔可夫模型,请参阅http://fulmicoton.com/posts/shannon-markov/