【问题标题】:Why are the stop words not updating even after adding stop words to the english.txt file?为什么即使在english.txt 文件中添加停用词后停用词也不会更新?
【发布时间】:2017-12-15 05:19:44
【问题描述】:

我在 Python 中使用 stop_words 包。目录路径usr/local/lib/python2.7/dist-packages/stop_words/stop-words 中的english.txt 文件中的停用词的原始数量是174,我又添加了几个,列表变成了218

我使用以下命令来获取停用词

from stop_words import get_stop_words

en_stop = get_stop_words('en')

len(en_stop) 仍然显示174。请你能告诉我如何使更改反映出来吗?

【问题讨论】:

  • 不确定,但您不应该像那样修改系统文件。它们将在升级期间被覆盖。相反,使用get_stop_words 从原始包中获取库存列表,然后将您自己的添加到其中。
  • @Thomas 我有很多停用词要添加,我希望它们永久地被规范为停用词。该列表没有一些非常常见的词,例如“虽然”和“尽管”。

标签: python python-2.7 stop-words


【解决方案1】:

您不应该在文件中添加停用词。要添加停用词,您应该创建一个要添加的单词列表,然后使用setunion 函数创建一个新列表。

en_stop = set(get_stop_words('en'))
new_stop = {'newstopword'}
en_stop = en_stop.union(new_stop)

【讨论】:

  • 谢谢!它有帮助。
【解决方案2】:

要在 stop_words 模块中包含单词,首先使用命令“python -v”找到这些模块所在的位置。 它将显示类似'/usr/local/lib/python2.7/site-packages/stop_words-2015.2.23.1-py2.7.egg/stop_words/stop-words'的位置,在这些目录中有很多文件,包括英文。 txt 和其他。在english.txt 中添加一些你想输入的单词,然后导入模块。 get_stop_words 的长度发生了变化。

【讨论】:

  • 谢谢,但如果您阅读我的问题正文,您可能会发现我已经完成了您发布的内容。没有stop_words-2015.2.23.1-py2.7.egg 这样的目录,只有stop_wordsstop_words-2015.2.23.1-py2.7.egg-info
  • 命令的输出是什么:-- 'python -v' 然后输入 'from stop_words import get_stop_words'
  • 建议用户直接修改库时要小心,这根本不是一个好习惯。然而,OP 可以复制模块并自己制作,或者在他的 python 代码中扩展列表
猜你喜欢
  • 2012-09-27
  • 1970-01-01
  • 2015-09-13
  • 2020-09-07
  • 1970-01-01
  • 2011-09-22
  • 2011-08-30
相关资源
最近更新 更多