からグーテンベルクテキストに、私は次のメソッドを使用していますが、それはいつも私に無効なファイル・エラーがスローされます。読むには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