鍍金池/ 教程/ Python/ 刪除停用詞
文本翻譯
提取URL地址
處理PDF
塊分類
搜索和匹配
大寫轉(zhuǎn)換
提取電子郵件地址
字符串的不變性
文本處理狀態(tài)機(jī)
雙字母組
閱讀RSS提要
單詞替換
WordNet接口
重新格式化段落
標(biāo)記單詞
向后讀取文件
塊和裂口
美化打印數(shù)字
拼寫檢查
將二進(jìn)制轉(zhuǎn)換為ASCII
文本分類
文字換行
頻率分布
字符串作為文件
約束搜索
詞干算法
符號(hào)化
同義詞和反義詞
過濾重復(fù)的字詞
刪除停用詞
Python文本處理教程
文字摘要
段落計(jì)數(shù)令牌
語料訪問
文字改寫
文本處理簡(jiǎn)介
處理Word文檔
Python文本處理開發(fā)環(huán)境
排序行

刪除停用詞

停用詞是英語單詞,對(duì)句子沒有多大意義。 在不犧牲句子含義的情況下,可以安全地忽略它們。 例如,the, he, have等等的單詞已經(jīng)在名為語料庫的語料庫中捕獲了這些單詞。 我們首先將它下載到python環(huán)境中。如下代碼 -

import nltk
nltk.download('stopwords')

它將下載帶有英語停用詞的文件。

驗(yàn)證停用詞

from nltk.corpus import stopwords
stopwords.words('english')
print stopwords.words() [620:680]

當(dāng)運(yùn)行上面的程序時(shí),得到以下輸出 -

[u'your', u'yours', u'yourself', u'yourselves', u'he', u'him', u'his', u'himself', u'she', 
u"she's", u'her', u'hers', u'herself', u'it', u"it's", u'its', u'itself', u'they', u'them', 
u'their', u'theirs', u'themselves', u'what', u'which', u'who', u'whom', u'this', 
u'that', u"that'll", u'these', u'those', u'am', u'is', u'are', u'was', u'were', u'be',
u'been', u'being', u'have', u'has', u'had', u'having', u'do', u'does', u'did', u'doing',
u'a', u'an', u'the', u'and', u'but', u'if', u'or', u'because', u'as', u'until',
u'while', u'of', u'at']

除了英語之外,具有這些停用詞的各種語言如下。

from nltk.corpus import stopwords
print stopwords.fileids()

當(dāng)運(yùn)行上面的程序時(shí),我們得到以下輸出 -

[u'arabic', u'azerbaijani', u'danish', u'dutch', u'english', u'finnish', 
u'french', u'german', u'greek', u'hungarian', u'indonesian', u'italian', 
u'kazakh', u'nepali', u'norwegian', u'portuguese', u'romanian', u'russian',
u'spanish', u'swedish', u'turkish']

示例

參考下面的示例來說明如何從單詞列表中刪除停用詞。

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

all_words = ['There', 'is', 'a', 'tree','near','the','river']
for word in all_words: 
    if word not in en_stops:
        print(word)

當(dāng)運(yùn)行上面的程序時(shí),我們得到以下輸出 -

There
tree
near
river