python - 将停用词字典导入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]
提前致谢!
解决方案
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]
推荐阅读
- c# - ORA-01031 错误消息“权限不足”与实体框架 - Oracle
- angular - HttpResponse 活动中的 Cors 问题
- reactjs - 用承诺值对 for 循环做出反应
- google-apps-script - 使用谷歌应用程序脚本将数据从 sheet1 中的单元格复制到 sheet2 中的不同非连续列
- c++ - 如何调用集合上的函数
> C++ 中的容器? - python - 如何从 Excel 图表图例中删除系列 - Python Openpyxl
- java - 为什么将数组长度的计算移出递归函数会增加运行时间?
- bigdata - 从 google colab 访问本地计算机中的数据
- runtime - 如何更好地分析复杂嵌套循环的运行时间?
- javascript - JS、CSS 使用链接添加/删除类以打开/关闭 Div