我使用下面的方法,但它總是拋出我的無效文件錯誤:從NLTK
import nltk
然後
file=open(nltk.corpus.gutenberg.words('austen-persuasion.txt'),"r").read().split().lower()
wordcount={}
for word in file:
if word not in wordcount:
wordcount[word] = 1
else:
wordcount[word] += 1
print ("The frequency of each word in the text file is as follows :")
for k,v in wordcount.items():
print (k, v)
的錯誤如下
TypeError Traceback (most recent call last)
<ipython-input-88-de499228f7ab> in <module>()
1 import nltk
----> 2 file=open(nltk.corpus.gutenberg.words('austen-persuasion.txt'),'r').read().split()
3 #file = nltk.corpus.gutenberg.words('austen-persuasion.txt')
4 wordcount={}
5
TypeError: invalid file: ['[', 'Persuasion', 'by', 'Jane', 'Austen', '1818', ...]
你不需要拆分()加速文件,ntkl功能爲你做 – patito