我使用的是 Mac,我想编写一个程序来为我定义单词列表。为此,我按照 official website 中的说明安装了 NLTK。适用于 Mac/Unix 机器。 numpy 安装失败,不确定是否会影响任何内容。但无论如何,请处理 here 中的一些示例代码我做了一个简单的小程序
from nltk.corpus import wordnet
print(wordnet)
word = raw_input("Words to define? \n")
for word in word.split():
for synset in wordnet.synsets(word):
print("lexical type:", synset.lexname)
print ("Definition:", synset.definition)
我第一次这样做时,出现了一个错误,为了修复它,我在 python 提示符下运行了以下代码
import nltk
nltk.download()
我安装了几乎所有的软件包(我认为不相关的软件包),然后我再次运行了上面的代码。问题是现在我收到此错误
Traceback (most recent call last):
File "/Users/pipsqueaker/Documents/workspace/wordDefine/main/main.py", line 10, in <module>
for synset in wordnet.synsets(word):
File "/Library/Python/2.7/site-packages/nltk/corpus/util.py", line 68, in __getattr__
self.__load()
File "/Library/Python/2.7/site-packages/nltk/corpus/util.py", line 56, in __load
except LookupError: raise e
LookupError:
**********************************************************************
Resource 'corpora/wordnet' not found. Please use the NLTK
Downloader to obtain the resource: >>> nltk.download()
Searched in:
- '/Users/pipsqueaker/nltk_data'
- '/usr/share/nltk_data'
- '/usr/local/share/nltk_data'
- '/usr/lib/nltk_data'
- '/usr/local/lib/nltk_data'
**********************************************************************
我将 nltk 安装到了/usr/share/nltk_data 目录,并在 finder 中导航到它以确保它在那里,而且确实如此。我在 Eclipse 的 pydev 中使用 osx 的默认 python,语法版本为 2.7。您知道是什么原因导致了这个问题吗?
另外,我不认为这是导入错误。我能做到
import nltk
print(nltk) #this runs nice
我可以做什么来解决这个问题?
最佳答案
我认为这可能是由于路径问题造成的。我建议在使用包时使用虚拟环境和 pip 作为标准。
这里有一些很棒的注释:https://python-guide.readthedocs.org/en/latest/
即使您不确定它是否“相关”,您也可能想尝试重新安装。该错误消息可能是一般数据未找到。
关于python - NLTK 找不到所需的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14538737/