from nltk.corpus import stopwords

This line imports the stopwords corpus from NLTK (Natural Language Toolkit), a popular Python library for natural language processing.

What it does

stopwords is a collection of common words (like "the", "is", "at", "and") that are typically filtered out during text processing because they carry little meaningful information.

Prerequisites

Before this import works, you need to download the stopwords data once:

import nltk
nltk.download('stopwords')

Common usage

from nltk.corpus import stopwords

# Get English stopwords (returns a list)
english_stops = stopwords.words('english')
print(english_stops[:10])
# ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're"]

# Convert to a set for faster lookup
stop_words = set(stopwords.words('english'))

# Filter stopwords from text
text = "This is a sample sentence showing off stopword filtration"
words = text.lower().split()
filtered = [w for w in words if w not in stop_words]
print(filtered)
# ['sample', 'sentence', 'showing', 'stopword', 'filtration']

Available languages

NLTK supports stopwords in many languages:

stopwords.words('spanish')
stopwords.words('french')
stopwords.words('german')
# etc.

Notes

  • The result of stopwords.words('english') is a list, so converting to a set is recommended for performance when checking membership.
  • NLTK's English list doesn't include some newer contractions (e.g., "aren't" is there, but "ain't" isn't) — you may need to extend it for your use case.

Let me know if you want an example of using this in a full preprocessing pipeline (tokenization → stopword removal → stemming).

Here's the same explanation in Chinese:

这行代码的作用

from nltk.corpus import stopwords 从 NLTK(Natural Language Toolkit,自然语言工具包)中导入 stopwords 语料库。NLTK 是 Python 中非常流行的自然语言处理库。

stopwords 是停用词的集合,指的是像 "the"、"is"、"at"、"and" 这类常见词。在文本处理时,这些词通常会被过滤掉,因为它们携带的信息量很小。

前置条件

在使用这行代码之前,需要先下载一次停用词数据:

import nltk
nltk.download('stopwords')

常见用法

from nltk.corpus import stopwords

# 获取英文停用词(返回的是一个列表)
english_stops = stopwords.words('english')
print(english_stops[:10])
# ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're"]

# 转换成集合,查找速度更快
stop_words = set(stopwords.words('english'))

# 从文本中过滤停用词
text = "This is a sample sentence showing off stopword filtration"
words = text.lower().split()
filtered = [w for w in words if w not in stop_words]
print(filtered)
# ['sample', 'sentence', 'showing', 'stopword', 'filtration']

支持的语言

NLTK 支持多种语言的停用词:

stopwords.words('spanish')   # 西班牙语
stopwords.words('french')    # 法语
stopwords.words('german')    # 德语
# 等等

注意:NLTK 官方并不包含中文停用词(stopwords.words('chinese') 会报错)。中文停用词需要另外准备。

中文停用词的处理方式

如果你要做中文文本处理,常见的做法有:

方式一:使用第三方库(如 jieba 或自建词表)

# 自己定义一个中文停用词表
chinese_stops = set([
    '的', '了', '和', '是', '就', '都', '而', '及', '与',
    '着', '或', '一个', '没有', '我们', '你们', '他们',
    '这', '那', '在', '也', '还', '把', '被', '让', '对'
])

text = "这是一个中文分词的示例,展示停用词过滤的效果"
words = list(text)  # 或用 jieba 分词
filtered = [w for w in words if w not in chinese_stops]
print(filtered)

方式二:配合 jieba 分词

import jieba

text = "这是一个中文分词的示例,展示停用词过滤的效果"
words = jieba.lcut(text)
filtered = [w for w in words if w not in chinese_stops]
print(filtered)
# ['中文', '分词', '示例', '展示', '停用词', '过滤', '效果']

方式三:从网上下载现成的中文停用词表

哈工大停用词表、百度停用词表、四川大学机器智能实验室停用词表都是常用的资源,可以下载后直接加载成集合使用。

小贴士

  • stopwords.words('english') 返回的是列表,做成员判断时建议转成 set,效率更高。
  • NLTK 的英文停用词表缺少一些较新的缩写词(比如 "aren't" 有,但 "ain't" 没有),实际使用时可能需要自己补充。
  • 中文没有统一的停用词标准,不同场景(搜索、情感分析、主题建模)适合用不同的停用词表。

如果你需要,我可以给你一个完整的中文文本预处理流程示例(分词 → 去停用词 → 词性标注)。

posted @ 2026-09-25 10:31  AceFenix  阅读(5)  评论(0)    收藏  举报