将单词添加到 nltk 非索引字表
我有一些代码可以从我的数据集中删除停用词,因为停用词列表似乎没有删除我也想要的大部分单词,我希望将单词添加到此停用词列表中,以便它将删除他们对于这个案子。 我用来删除停用词的代码是:
word_list2 = [w.strip() for w in word_list if w.strip() not in nltk.corpus.stopwords.words('english')]
我不确定添加单词的正确语法,并且似乎无法在任何地方找到正确的语法。任何帮助表示赞赏。谢谢。
I have some code that removes stop words from my data set, as the stop list doesn't seem to remove a majority of the words I would like it too, I'm looking to add words to this stop list so that it will remove them for this case.
The code i'm using to remove stop words is:
word_list2 = [w.strip() for w in word_list if w.strip() not in nltk.corpus.stopwords.words('english')]
I'm unsure of the correct syntax for adding words and can't seem to find the correct one anywhere. Any help is appreciated. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
您可以简单地使用追加方法向其中添加单词:
或扩展以附加单词列表,正如查理在评论中所建议的那样。
You can simply use the append method to add words to it:
or extend to append a list of words, as suggested by Charlie on the comments.
我在 Ubuntu 机器上的做法是,我在 root 中按 ctrl + F 切换“停用词”。它给了我一个文件夹。我走进里面,里面有不同的文件。我打开“英语”,只有128个单词。添加了我的话。保存并完成。
The way how I did on my Ubuntu machine was, I ctrl + F for "stopwords" in root. It gave me a folder. I stepped inside it which had different files. I opened "english" which had barely 128 words. Added my words to it. Saved and done.
英语停用词是 nltk/corpus/stopwords/english.txt 中的一个文件(我猜它会在这里...我这台机器上没有 nltk..最好的办法是在 nltk 存储库中搜索“english.txt”)
您只需在此文件中添加新的停用词即可。
如果您的停用词列表增加到数百个,也可以尝试查看 bloom 过滤器
The english stop words is a file within nltk/corpus/stopwords/english.txt (I guess it would be here...i dont have nltk on this machine..best thing would be to search 'english.txt within nltk repo)
You can just add your new stop words in this file.
also try looking at bloom filters if your stop word list increases to few hundreds
我总是在任何需要它的模块的顶部执行
stopset = set(nltk.corpus.stopwords.words('english'))
。然后可以轻松地向集合中添加更多单词,并且成员资格检查速度更快。I always do
stopset = set(nltk.corpus.stopwords.words('english'))
at the top of any module that needs it. Then it's easy to add more words to the set, plus membership checks are faster.也在寻找解决方案。经过一番尝试和错误后,我必须将单词添加到非索引字表中。希望这有帮助。
Was also looking for solution on this. After some trail and error I got to add words to the stoplist. Hope this helps.
我使用此代码将新的停用词添加到 python 中的 nltk 停用词列表中
I use this code for adding new stop words to nltk stop word list in python
我发现(Python 3.7、Windows 10 上的 jupyter 笔记本、企业防火墙)
创建列表并使用“附加”命令会导致整个停用词列表被附加为原始列表的元素。
这使得“停用词”成为列表的列表。
Snijesh 的答案很有效,Jayantha 的答案也很有效。
I've found (Python 3.7, jupyter notebook on Windows 10, corporate firewall)
that creating a list and using the 'append' command results in the entire stopwords list being appended as an element of the original list.
This makes 'stopwords' into a list of lists.
Snijesh's answer works well, as does Jayantha's answer.
STOP_WORDS.add(“Lol”) #根据需要添加新的停用词到语料库
STOP_WORDS.add(“Lol”) #Add new stopword into corpus as you wish