NLTK 找不到所需目录
我在用Mac电脑,想写个程序来定义一些单词。为此,我按照官方网站上给Mac/Unix机器的说明安装了NLTK。安装numpy的时候失败了,不太确定这会不会影响其他东西。不过,我根据这里的一些示例代码,写了一个简单的程序。
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
我该怎么做才能解决这个问题呢?
1 个回答
3
我觉得这可能是路径问题导致的。我建议在使用软件包时,标准做法是使用虚拟环境和pip。
这里有一些很好的笔记:https://python-guide.readthedocs.org/en/latest/
即使你不确定它是否“相关”,你也可以尝试重新安装一下。错误信息可能只是一个通用的“数据未找到”的提示。