【发布时间】:2016-12-17 19:50:24
【问题描述】:
我正在使用 PyCharm 编写一个使用 nltk 包的程序。我的第一行是:
from nltk import word_tokenize, sent_tokenize
我在 PyCharm 的 2.7 Python 环境(我正在使用的环境)中导入了 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`
有人可以指导我解决这个问题吗?提前非常感谢。
【问题讨论】:
-
通常该错误意味着存在有条件定义的变量,因此如果导入语句显示为灰色,则 PyCharm 认为这是一条注释,然后给您一个错误,因为
word_tokenize未定义跨度> -
文件中是否可能有奇怪的非打印字符?您能否在此处发布代码 - 然后从 SO 复制回 PyCharm 并查看它是否有效? (有时有效 - 摆脱奇怪的字符)
-
我现在添加了代码。非常感谢您的帮助!
-
(请同时包含完整的回溯)它对我有用......我得到了错误
nltk is undefined,因为您直接导入word_tokenize,然后尝试使用nltk.word_tokenize,但导入部分工作。 -
我现在添加了完整的错误代码。很抱歉,这看起来很奇怪!