Search code examples
pythonpython-3.xnltkcorpus

Read in gutenberg text from NLTK


I am using the following method but it always throws me invalid file error:

import nltk

then

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)

The error is as follows

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', ...]

Solution

  • As @patito mentioned in the comment, you don't need to use read and you also don't need to use split, as nltk is reading it in as a list of words. You can see that for yourself:

    >>> file = nltk.corpus.gutenberg.words('austen-persuasion.txt')
    >>> file[0:10]
    [u'[', u'Persuasion', u'by', u'Jane', u'Austen', u'1818', u']', u'Chapter', u'1', u'Sir']
    

    You will also need to fix the indentations in your word count, but otherwise it will work for you.