NLP
Stopwords
Remove common words with NLTK and spaCy.
By EZ4Code Team
stopwordsfiltering
Code
import nltk
from nltk.corpus import stopwords
nltk.download("stopwords", quiet=True)
en_stop = set(stopwords.words("english"))
text = "this is a simple test of removing stopwords"
filtered = [w for w in text.split() if w.lower() not in en_stop]
print(filtered) # ['simple', 'test', 'removing', 'stopwords']
# Multi-language
print(len(stopwords.words("french")), len(stopwords.words("german")))
# spaCy stopwords
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The quick brown fox jumps over the lazy dog")
spacy_filtered = [t.text for t in doc if not t.is_stop and not t.is_punct]
print(spacy_filtered)
# Extend with custom stopwords
custom = en_stop | {"said", "say", "says"}Explanation
Stopwords are common words that add little semantic value and are typically removed before analysis. NLTK ships language-specific lists, while spaCy marks each token with is_stop. Extending the built-in list with domain-specific noise such as 'said' in news text keeps downstream features focused.
More NLP Snippets
Tokenization
Tokenize text with NLTK, spaCy, and regex.
Stemming and Lemmatization
Reduce words to roots with Porter, Snowball, and lemmatizers.
TF-IDF
Vectorize text with TF-IDF and n-grams.
Word2Vec
Train and use word embeddings with gensim.
Named Entity Recognition
Extract entities with spaCy and transformers.
Sentiment Analysis
Score text polarity with VADER and transformers.