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

时间:2019-10-24 10:59:01

标签: python nltk

我知道我可以通过添加停用词集来对其进行更新,但是如何从分析中删除一些我需要使用的停用词,有没有办法使用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)

但是输出是:

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

1 个答案:

答案 0 :(得分:0)

Python中的列表是一个可变对象,如here所述:

  

可变对象创建后可以更改,而不可变对象不能更改。内置类型(int,float,bool,str,tuple,unicode)的对象是不可变的。内置类型的对象((列表,集合,字典)是可变的。

python list remove()方法不会创建新列表,它会修改作为参数给出的列表,请参见here

  

Python列表方法remove()在列表中搜索给定的元素,并删除第一个匹配的元素。

     

返回值:此Python列表方法不会返回任何值,但会从列表中删除给定的对象。

以下代码显示确实从列表中删除了“他们”一词:

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