【问题标题】:Importing stopword dictionary to python将停用词字典导入python
【发布时间】:2018-11-20 12:46:30
【问题描述】:
如何将特定的停用词词典(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]
提前致谢!
【问题讨论】:
标签:
python
nltk
stop-words
【解决方案1】:
您可以使用 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]