[英]Pycharm doesn't recognize nltk (installed with Anaconda)
我正在使用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`
有人可以指導我解決這個問題嗎? 非常感謝。
您的導入錯誤表明找到了模塊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.