Skip to main content
 首页 » 编程设计

python之使用 nltk 使用 n-gram 模型制作新句子

2024年10月24日16kevingrace

我从我的文本文件中制作了 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) 

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

请您参考如下方法:

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

但最先进的替代方法是使用递归神经网络生成文本,例如https://github.com/karpathy/char-rnn (注意:与传统的基于 Ngram 的隐马尔可夫模型不同,char-RNN 不使用 ngram 信息。)

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