使用 n-gram 模型 NLTK 预测下一个词

问题描述 投票:0回答:0

我正在尝试使用 NLTK 运行 N-Gram 语言建模代码,该代码取自 https://www.geeksforgeeks.org/n-gram-language-modelling-with-nltk/。但它抛出一个错误。

# generate frequency of n-grams
freq_bi = FreqDist(bigram)
freq_tri = FreqDist(trigram)

d = defaultdict(Counter)
for a, b, c in freq_tri:
    if(a != None and b!= None and c!= None):
    d[a, b] += freq_tri[a, b, c]

我得到的错误如下,

`AttributeError                            Traceback (most recent call last)
<ipython-input-12-ae7c0728f2d6> in <module>
      3     print(freq_tri[a,b,c])
      4     if(a != None and b!= None and c!= None):
----> 5       d[a, b] += freq_tri[a, b, c]
AttributeError: 'int' object has no attribute 'items' `

完整代码可在site

python nlp nltk n-gram defaultdict
© www.soinside.com 2019 - 2024. All rights reserved.