Stopwords are commonly used words in a language that are usually removed from texts during natural language processing (NLP) tasks such as text classification, sentiment analysis, and topic modeling. Examples of stopwords in English include “the”, “and”, “a”, “an”, “in”, “of”, “to”, etc. The NLTK library provides a list of stopwords that you can use in your Python code. It can happen that when you’re trying to use stopwords from the NLTK library, you may run into the “NameError: name ‘stopwords’ is not defined” error.
In this tutorial, we will discuss how to fix this error by importing the stopwords module correctly. By the end of this tutorial, you will have a better understanding of how to import stopwords from the NLTK library and avoid common errors.
Why does the NameError: name 'stopwords' is not defined
occur?
This error occurs when you try to use the stopwords module from the NLTK library in your Python code, but Python cannot find the stopwords module in its namespace. This could happen if you are not correctly importing the stopwords module.
How to correctly import stopwords
?
The correct way to import the stopwords
module is as follows –
-
Make sure that you have the
nltk
module installed. Usepip show nltk
inside command prompt or terminal to check if you have thenltk
module installed or not. If it is not installed, usepip install nltk
inside the command prompt or terminal to install thenltk
module. -
Import the
nltk
module. -
Download the
stopwords
corpus using thenltk
module using the commandnltk.download('stopwords')
. This will download the stopwords corpus to your computer. - After downloading the
stopwords
corpus, you can import the stopwords module in your Python code usingfrom nltk.corpus import stopwords
The above steps will make sure that you have correctly imported stopwords
from the nltk
module. Let’s now look at an example of importing and using the stopwords from the nltk
module.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
Assuming that the nltk
module is installed.
import nltk # download the stopwords corpus nltk.download('stopwords') # import stopwords from nltk.corpus import stopwords # print the english stopwords english_stopwords = set(stopwords.words('english')) print(english_stopwords)
Output:
[nltk_data] Downloading package stopwords to [nltk_data] /Users/piyush/nltk_data... [nltk_data] Unzipping corpora/stopwords.zip. --------------------------------------------------------------------------- {'where', 'myself', 'yourselves', 'aren', "doesn't", 'once', 'do', 'from', 'each', 'have', 'now', 'your', 'doesn', 'into', 'a', "that'll", "shan't", 'then', 'or', "should've", 'she', "didn't", 'them', 'whom', 'in', 'their', 'yourself', 'before', 'up', 'for', 'wouldn', 'yours', 'what', "couldn't", 'during', 'my', 'but', 'i', 'himself', 'did', 'under', 'to', 'more', 'that', 'own', 'there', "hasn't", 'shan', 'the', 'been', "weren't", 'hers', 'is', 'ain', 'and', 'will', 'over', 'its', 'hasn', 'doing', 'theirs', 'below', 'too', "mightn't", 'all', "wasn't", 'he', 'me', 'themselves', 'how', 'only', 'between', 'these', 'should', 'was', "wouldn't", 'which', 'any', "mustn't", 'y', 'mustn', "haven't", 'couldn', 'down', 'they', 'very', 'haven', 'having', "shouldn't", "you're", 'you', 'of', 'am', 'why', 'at', 'other', "needn't", 'd', 'with', "she's", 'about', 'few', 'needn', 'some', 'shouldn', "won't", 'most', 'be', 'same', 'as', 'further', 'an', 'we', 'don', 'those', 'so', 'won', 'until', 'hadn', 'not', 'on', 're', 'both', 'her', 'wasn', "isn't", 'this', 'against', 'ma', 'who', 'off', 'm', "you've", 'ours', 'his', 'can', "aren't", 'didn', "it's", 'ourselves', 'being', 'while', 've', 'isn', 'our', 'weren', "you'd", 'herself', 'nor', 't', 'll', 'him', 'just', "hadn't", "you'll", 'again', 'does', 'mightn', 'it', 'no', 'such', 'are', "don't", 'because', 's', 'through', 'were', 'has', 'itself', 'if', 'than', 'had', 'out', 'after', 'by', 'above', 'here', 'o', 'when'}
In the above example, we are printing the set of English stopwords using the stopwords
module from nltk
. We followed the steps mentioned earlier and thus didn’t get an error.
Common Errors when importing stopwords
Let’s now look at some common scenarios that could result in errors while importing the stopwords
module.
NameError: name 'stopwords' is not defined
A common mistake people do is that they download the stopwords
corpus but forget to import it. Like in the example below –
import nltk # download the stopwords corpus nltk.download('stopwords') # print the english stopwords english_stopwords = set(stopwords.words('english')) print(english_stopwords)
Output:
[nltk_data] Downloading package stopwords to [nltk_data] /Users/piyush/nltk_data... [nltk_data] Unzipping corpora/stopwords.zip. --------------------------------------------------------------------------- NameError Traceback (most recent call last) Cell In[2], line 7 4 nltk.download('stopwords') 6 # print the english stopwords ----> 7 english_stopwords = set(stopwords.words('english')) 8 print(english_stopwords) NameError: name 'stopwords' is not defined
Using nltk.download('stopwords')
will download the stopwords corpus to your computer but in order to use it in your Python code, you still have to import the stopwords
module. You can import the stopwords
module using from nltk.corpus import stopwords
.
LookupError: resource 'stopwords' was not found
If the stopwords
corpus is not downloaded on your machine and you try to import the stopwords
, it will give you a LookupError
.
import nltk # import stopwords from nltk.corpus import stopwords # print the english stopwords english_stopwords = set(stopwords.words('english')) print(english_stopwords)
Output:
LookupError: ********************************************************************** Resource stopwords not found. Please use the NLTK Downloader to obtain the resource: >>> import nltk >>> nltk.download('stopwords') **********************************************************************
To avoid this error, make sure that the stopwords
corpus is downloaded before you import it into your code.
Conclusion
In conclusion, the “NameError name ‘stopwords’ is not defined” error can be frustrating when working with natural language processing tasks. However, by following the steps outlined in this tutorial, you can easily fix this error and continue with your NLP project. Remember to import the necessary libraries and modules, and ensure that you have installed the required packages. With these simple fixes, you can overcome the “NameError name ‘stopwords’ is not defined” error and successfully complete your NLP tasks.
You might also be interested in –