6

I'm getting the error NameError: name 'stopwords' is not defined for some reason, even though I have the package installed. I'm trying to do natural language processing on some feedback reviews. The dataset object is a table with two columns, Reviews (a sentence of feedback) and target variable Liked (1 or 0). Help appreciated, thanks!


Block 1

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

Output 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

Block 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**

Output 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
james
  • 63
  • 1
  • 1
  • 4
  • Do you have from nltk.corpus import stopwords ?? – Nabin Nov 25 '17 at 11:59
  • 1
    @Nabin Oh wow, it worked! Thanks very much. The tutorial I was following didn't have that, but it worked for them, strange... Thanks again! – james Nov 25 '17 at 12:10

1 Answers1

28

you just have to add the following line before using stopwords in your code:

from nltk.corpus import stopwords
Panna Das
  • 611
  • 6
  • 11
  • 2
    Thanks Panna! The tutorial I was following didn't have that so I thought it was a bit strange. – james Nov 25 '17 at 12:26
  • 2
    Don't thank me my friend, it's us helping each other :) – Panna Das Nov 25 '17 at 12:27
  • @PannaDas Hey, I know you answered this years ago lol but why doesn't NLTK automatically import "stopwords()"? – Molly Taylor Apr 17 '20 at 03:55
  • @MollyTaylor this is called explicit import, you can read up further and see other ways one can make imports in python. Another way you could've done this is as below: import nltk stpwrds = nltk.corpus.stopwords.words('english') search "Absolute vs Relative Imports in Python" on realpython for further explanation – Mahlatse Jul 27 '23 at 07:29