繁体   English   中英

Pycharm无法识别nltk(与Anaconda一起安装)

[英]Pycharm doesn't recognize nltk (installed with Anaconda)

我正在使用PyCharm编写使用nltk软件包的程序。 我的第一行是:

 from nltk import word_tokenize, sent_tokenize

我在PyCharm的2.7 Python环境(我正在处理的环境)中导入了nltk包,如下所示: 屏幕快照PyCharm无法识别nltk软件包

但是,PyCharm无法识别from nltk..行。 它变灰了; 它还显示此错误:

This inspection detects names that should resolve but don't. Due to dynamic dispatch and duck typing, this is possible in a limited but useful number of cases. Top-level and class-level items are supported better than instance items.

这是我的代码:

from nltk import word_tokenize, sent_tokenize

annot1 = [(500L, u'[they seldom desire anything unless it belongs to     others]')]
annot2 = (500L, u'[they seldom desire anything unless it belongs to others]')

def scope_match(annot1, annot2):
    tokens1 = annot2[1].encode('utf-8')
    print type(tokens1)
    for string in tokens1:
        tokens2 = nltk.word_tokenize(string)
        print 'these are the tokens: ', tokens2
        new2 = [a.strip('[]').encode('utf-8') for a in tokens2]
        print new2

scope_agr = scope_match(annot1, annot2)
print scope_agr

运行代码时,出现以下错误:C:\\ Users \\ nepal \\ Anaconda3 \\ envs \\ py27 \\ python.exe / Users / nepal / PycharmProjects / ScopeCue / ScopeComparison / scope-compare-inter-annotation-agreement-TEST .py

Traceback (most recent call last):
  File "C:/Users/nepal/PycharmProjects/ScopeCue/ScopeComparison/scope-compare- inter-annotation-agreement-TEST.py", line 1, in <module>
from nltk import word_tokenize, sent_tokenize
ImportError: cannot import name word_tokenize

Process finished with exit code 1`

有人可以指导我解决这个问题吗? 非常感谢。

您的导入错误表明找到了模块nltk ,但其中不包含word_tokenize 99%的时间表示您已在脚本所在的目录中创建了文件nltk.py

实际上,您似乎是例外之一- nltk在注释中发布的最后一个错误跟踪显示,您已经创建了一个完整的nltk包(带有__init__.py的文件夹)! 摆脱它或重命名它,以便python可以找到真正的nltk

我通过从终端使用nltk.download()下载完整的nltk软件包来解决此问题。

因此,我打开了一个新的python会话,然后执行了以下操作:

import nltk

nltk.download()

一个新的窗口打开,问我是否要下载,我接受了。 现在运行良好。

我想知道Anaconda是否不安装完整的nltk软件包吗?....在尝试此解决方案之前,我使用Anaconda重新安装了两次( conda install -c anaconda nltk=3.2.1 )。 但是似乎使用该命令并不能获得整个nltk软件包...

无论如何,我希望它能帮助下一个人。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM