Last Updated on 2021-08-30 by Clay
Problem
Today, when I was processing the English corpus, I used NLTK, a classic natural language processing tool in Python, to segment the text again. But when I use the following program to use the following program to import stopwords:
from nltk.corpus import stopwords
Yes, I encountered a strange error message:
ImportError: cannot import name 'stopwords'
This is a strange problem. I also specially opened a new virtual environment to try to reinstall NLTK, and found that it should be no problem to directly use the above code to import, the only possibility is that someone accidentally stored it in the stopword in the nltk package was accidentally deleted…
the magic thing is that this has happened for the second time, so I have the experience of the last time and quickly found a solution. Rather, many people’s problems with stopwords are more bizarre and difficult to solve than mine… Hahaha.
Solution
At present, most of the problems I have encountered are that I cannot find the stopwords in the nltk package, so I just need to use the program to download it again.
import nltk nltk.download('stopwords')
Output:
[nltk_data] Downloading package stopwords to
[nltk_data] /home/clay/nltk_data…
[nltk_data] Unzipping corpora/stopwords.zip.
True
Seeing the above message, it probably means that the download was successful. Next, let’s try it right away to see if it works normally.
from nltk.corpus import stopwords print(stopwords.words('english'))
Output:
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
If a lot of words are printed, it is likely that the disabled words can be successfully used.
References
- https://stackoverflow.com/questions/26693736/nltk-and-stopwords-fail-lookuperror
- https://github.com/heroku/heroku-buildpack-python/issues/685
I am having this issue ” NameError: name ‘STOPWORDs’ is not defined” when using python with Jupyter notebook and I have already installed below libraries NLTK,
import re
import nltk
nltk.download(‘stopwords’)
from nltk.corpus import stopwords
print(stopwords.words(‘english’))
Could anyone please advise.
Thanks.
I was trying to run the following:
# Create stopword list:
Stopwords = set(STOPWORDs)
stopwords.update([“br”, “href”,”good”,”great”])
## good and great removed because they were included in negative sentiment
pos = ” “.join(review for review in positive.Summary)
wordcloud2 = WordCloud(stopwords=stopwords).generate(pos)
plt.imshow(wordcloud2, interpolation=’bilinear’)
plt.axis(“off”)
plt.show()
But am having this error: NameError: name ‘STOPWORDs’ is not defined.
You need to assign something to define the variable `STOPWORDS`. For example:
nltk.download(‘stopwords’)
from nltk.corpus import stopwords
# Define `STOPWORDS`
STOPWORDs = stopwords.words('english')