PlaintextCorpusReader'对象没有'files'属性

2 投票
1 回答
3085 浏览
提问于 2025-04-18 17:35

我正在跟着一个关于NLTK的教程学习,结果在Python的交互式命令行中遇到了这个错误,而书里显示的是另外一个结果。你知道怎么解决这个问题吗?

>>> import nltk
>>> nltk.corpus.gutenberg.files()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'PlaintextCorpusReader' object has no attribute 'files'

enter image description here

1 个回答

5

这个教程已经有6年了,所以它用的是一个旧版本的 NLTK。你可以使用 nltk.corpus.gutenberg.fileids() 来得到你想要的结果。

In [22]: nltk.corpus.gutenberg.fileids()
Out[22]: 
['austen-emma.txt',
 'austen-persuasion.txt',
 'austen-sense.txt',
 'bible-kjv.txt',
 'blake-poems.txt',
 'bryant-stories.txt',
 'burgess-busterbrown.txt',
 'carroll-alice.txt',
 'chesterton-ball.txt',
 'chesterton-brown.txt',
 'chesterton-thursday.txt',
 'edgeworth-parents.txt',
 'melville-moby_dick.txt',
 'milton-paradise.txt',
 'shakespeare-caesar.txt',
 'shakespeare-hamlet.txt',
 'shakespeare-macbeth.txt',
 'whitman-leaves.txt']

撰写回答