python去停用词用nltk_使用nltk删除英文停用词
一、概念
首先我們來看一下停用詞的概念,然后來介紹使用nltk如何刪除英文的停用詞:
由于一些常用字或者詞使用的頻率相當(dāng)?shù)母?#xff0c;英語中比如a,the, he等,中文中比如:我、它、個(gè)等,每個(gè)頁面幾乎都包含了這些詞匯,如果搜索引擎它們當(dāng)關(guān)鍵字進(jìn)行索引,那么所有的網(wǎng)站都會(huì)被索引,而且沒有區(qū)分度,所以一般把這些詞直接去掉,不可當(dāng)做關(guān)鍵詞。
二、使用nltk刪除英文停用詞
首先我import stopwords進(jìn)來,代碼如下:
from nltk.corpus import stopwords
words = stopwords.words('english')
print(words)
首先看看打印停用詞的結(jié)果:
['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', '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', 'should', 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', 'couldn', 'didn', 'doesn', 'hadn', 'hasn', 'haven', 'isn', 'ma', 'mightn', 'mustn', 'needn', 'shan', 'shouldn', 'wasn', 'weren', 'won', 'wouldn']
當(dāng)然在很多任務(wù)(比如對(duì)話任務(wù)中)中,停用詞還包括下面這些符合和后綴:
['!', ',' ,'.' ,'?' ,'-s' ,'-ly' ,' ', 's']
使用下面代碼,將他們加上去
for w in ['!',',','.','?','-s','-ly','','s']:
self.stopwords.add(w)
然后刪除的用法就非常容易,假如我們的語料在word_list中,我們只需要寫上下面的代碼即可!
from nltk.corpus import stopwords
for w in ['!',',','.','?','-s','-ly','','s']:
self.stopwords.add(w)
filtered_words = [word for word in word_list if word not in stopwords.words('english')]
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的python去停用词用nltk_使用nltk删除英文停用词的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android jxl.jar 使用,使
- 下一篇: MySQL运算符,函数,索引,图形化管理