使用 NLTK 过滤停用词
从 Token 列表中去除噪声
使用 NLTK 过滤停用词 是 CoddyKit 上的免费 NLP Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 NLP Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 NLP Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Let NLTK Do the Heavy Lifting
Building your own stopword list is fine, but NLTK already ships a curated one for many languages. Let us put it to work on a token list.
Grab the Data First
NLTK keeps word lists as downloadable data. You fetch the stopwords package once, then it stays on your machine.
import nltk
nltk.download("stopwords")Load the English List
Now import the corpus and ask for English. You get back a plain list of words you can inspect or filter against.
from nltk.corpus import stopwords
stops = stopwords.words("english")
print(len(stops))Convert It to a Set
The list works, but a set makes membership checks much faster. Wrap it once and reuse it for every token.
stops = set(stopwords.words("english"))Filter With a Comprehension
A list comprehension keeps only the words that are not stopwords. This single line is the heart of stopword removal.
tokens = ["the", "quick", "brown", "fox"]
clean = [w for w in tokens if w not in stops]
print(clean)Mind the Case
The list is lowercase, so The will not match the. Lowercase your tokens first, or you will leave capitalized stopwords behind.
clean = [w for w in tokens if w.lower() not in stops]See the Difference
Before filtering you might have ten tokens; after, only the meaningful four remain. That shrink is the noise you just dropped.
Other Languages Too
NLTK is not English-only. Swap the argument to pull a stopword list for Spanish, German, French, and many more.
spanish = set(stopwords.words("spanish"))Customize the List
The list is just a set, so you can add your own domain noise to it with normal set operations before filtering.
stops.add("subject")
stops.update(["http", "www"])Or Keep a Few Back
Want to protect a word like not? Just remove it from the set so filtering never strips it out.
stops.discard("not")Filter Once, Reuse Often
Build your stops set a single time at startup, not inside a loop. Rebuilding it for every document wastes real time.
Quick Check
One detail trips up almost everyone the first time.
Recap
You can now filter tokens against NLTK stopwords: download once, build a lowercase set, and keep only words not in it. Mind the case.
常见问题解答
「使用 NLTK 过滤停用词」课时是免费的吗?
是的 — 「使用 NLTK 过滤停用词」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 NLP Academy 课程的其余内容,请升级到 CoddyKit PRO。 NLP Academy 课程共包含 4 节课。
「使用 NLTK 过滤停用词」这节课中我会学到什么?
从 Token 列表中去除噪声 你通过在浏览器中直接运行的动手代码来练习 NLP Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 NLP Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 NLP Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 NLTK 过滤停用词」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 NLP Academy 课中编写并运行代码吗?
能。每节 NLP Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 什么是停用词
- 使用 NLTK 过滤停用词
- 去除标点符号与其他符号
- 构建可复用的清理文本函数