简体   繁体   English

如何从 nltk.corpus 的停用词中删除“他们”和“我们”?

[英]How I can remove “they” and “we” form stop word of nltk.corpus?

I know that I can update the stop word set by adding to it, but How can I remove from it some stop words I need them in my analysis is there any way to do that using python?我知道我可以通过添加来更新停用词集,但是如何从中删除一些我在分析中需要它们的停用词有没有办法使用 python 来做到这一点?

from nltk.corpus import stopwords
stop_words = stopwords.words('english')
print("stop_words :",stop_words)
stop_words_none = stop_words.remove("they")
print("stop_words without they: ",stop_words_none)

but the output is:但 output 是:

stop_words ['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', "you're", "you've", "you'll", "you'd", 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', "she's", 'her', 'hers', 'herself', 'it', "it's", 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', "that'll", 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', "don't", 'should', "should've", 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', "aren't", 'couldn', "couldn't", 'didn', "didn't", 'doesn', "doesn't", 'hadn', "hadn't", 'hasn', "hasn't", 'haven', "haven't", 'isn', "isn't", 'ma', 'mightn', "mightn't", 'mustn', "mustn't", 'needn', "needn't", 'shan', "shan't", 'shouldn', "shouldn't", 'wasn', "wasn't", 'weren', "weren't", 'won', "won't", 'wouldn', "wouldn't"]
stop_words without they: None

A list in python is a mutable object, as stated here : python 中的列表是可变的object ,如下所述:

mutable object can be changed after it is created, and an immutable object can't.可变的 object 在创建后可以更改,而不可变的 object 不能。 Objects of built-in types like (int, float, bool, str, tuple, unicode) are immutable. (int, float, bool, str, tuple, unicode) 等内置类型的对象是不可变的。 Objects of built-in types like (list, set, dict) are mutable. (list, set, dict) 等内置类型的对象是可变的。

The python list remove() method does not create a new list, it modifies the list given as argument, see here : python list remove() 方法不会创建新列表,它会修改作为参数给出的列表,请参见此处

Python list method remove() searches for the given element in the list and removes the first matching element. Python 列表方法 remove() 在列表中搜索给定元素并删除第一个匹配元素。

Return Value : This Python list method does not return any value but removes the given object from the list.返回值:此 Python 列表方法不返回任何值,但会从列表中删除给定的 object。

The following code shows that the word 'they' is indeed removed from the list:以下代码显示确实从列表中删除了“他们”一词:

from nltk.corpus import stopwords
stop_words = stopwords.words('english')

print('they' in stop_words)
#True
stop_words.remove("they")
print('they' in stop_words)
#False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM