首页 > 解决方案 > 将停用词字典导入python

问题描述

如何将特定的停用词词典(excel 表)导入 Python 并将其另外运行到 nltk 停用词列表中?目前我的停用词部分如下所示:

# filter out stop words
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
words = [w for w in words if not w in stop_words]

提前致谢!

标签: pythonnltkstop-words

解决方案


pandas您可以使用该库导入 Excel 工作表。此示例假定您的停用词位于第一列,每行一个单词。然后,创建nltk停用词和您自己的停用词的联合:

import pandas as pd
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
# check pandas docs for more info on usage of read_excel
custom_words = pd.read_excel('your_file.xlsx', header=None, names=['mywords'])
# union of two sets
stop_words = stop_words | set(custom_words['mywords'])
words = [w for w in words if not w in stop_words]

推荐阅读