NLTK使用中NameError: name 'FreqDist' is not defined問題解決
阿新 • • 發佈:2019-02-12
在使用NLTK學習自然語言處理時,按照《Python自然語言處理》的程式碼進行頻率分佈統計,原始碼如下:
<span style="font-size:14px;">fdist1 = FreqDist(text1)</span>
但是使用時報錯如下:
Traceback (most recent call last):
File "<pyshell#33>", line 1, in <module>
fdist1 = FreqDist(text1)
NameError: name 'FreqDist' is not defined
書上給的解決方案是輸入
<span style="font-size:14px;">from nltk.book import *</span>
然而並沒有什麼卵用。。錯誤依舊
後來查到FreqDist在nltk.probability類下,於是乾脆匯入所有nltk包:
<span style="font-size:14px;">from nltk import *</span>
問題就解決啦~~
>>> from nltk import * >>> fdist1 = FreqDist(text1) >>> fdist1 FreqDist({u',': 18713, u'the': 13721, u'.': 6862, u'of': 6536, u'and': 6024, u'a': 4569, u'to': 4542, u';': 4072, u'in': 3916, u'that': 2982, ...})