2017-11-25 1702 views
0

由於某種原因,我收到錯誤NameError: name 'stopwords' is not defined,即使我已經安裝了該軟件包。我試圖對一些反饋評論進行自然語言處理。 dataset對象是具有兩列的表格,Reviews (a sentence of feedback)和目標變量Liked (1 or 0)。謝謝,謝謝!NameError:name'stopwords'is not defined


塊1

import re 
import nltk 
nltk.download('stopwords') 

輸出1

> [nltk_data] Downloading package stopwords to 

    > [nltk_data]  /Users/user/nltk_data... 

    > [nltk_data] Package stopwords is already up-to-date! 

    > Out[14]: True 

塊2

dataset['Review'][0] 
review = re.sub('[^a-zA-Z]',' ' ,dataset['Review'][0]) 
review = review.lower() 
review = review.split() 
review = [word for word in review if not word in stopwords.words('english')] **ERROR ON THIS LINE** 

輸出2

>NameError         Traceback (most recent call last) 
<ipython-input-16-8d0ee1fd7c7f> in <module>() 
     3 review = review.lower() 
     4 review = review.split() 
----> 5 review = [word for word in review if not word in stopwords.words('english')] 

><ipython-input-16-8d0ee1fd7c7f> in <listcomp>(.0) 
     3 review = review.lower() 
     4 review = review.split() 
----> 5 review = [word for word in review if not word in stopwords.words('english')] 

>NameError: name 'stopwords' is not defined 
+0

你從nltk.corpus進口禁用詞有? – Nabin

+0

@Nabin哦哇,它的工作!非常感謝。我接下來的教程沒有,但它對他們有效,奇怪......再次感謝! – james

回答

0

你只需要在代碼中使用stopwords之前添加以下行:

from nltk.corpus import stopwords 
+0

謝謝潘娜!我接下來的教程沒有,所以我認爲這有點奇怪。 – james

+0

不要感謝我的朋友,這是我們互相幫助:) –