
- Python - 文字處理
- Python - 文字處理入門
- Python - 文字處理環境
- Python - 字串不變性
- Python - 排序行
- Python - 段落重新格式化
- Python - 統計段落中的詞元
- Python - 二進位制ASCII轉換
- Python - 字串作為檔案
- Python - 反向讀取檔案
- Python - 過濾重複單詞
- Python - 從文字中提取電子郵件
- Python - 從文字中提取URL
- Python - 美化列印
- Python - 文字處理狀態機
- Python - 首字母大寫和翻譯
- Python - 詞元化
- Python - 去除停用詞
- Python - 同義詞和反義詞
- Python - 文字翻譯
- Python - 單詞替換
- Python - 拼寫檢查
- Python - WordNet介面
- Python - 語料庫訪問
- Python - 詞性標註
- Python - 塊和塊隙
- Python - 塊分類
- Python - 文字分類
- Python - 二元語法
- Python - 處理PDF
- Python - 處理Word文件
- Python - 讀取RSS Feed
- Python - 情感分析
- Python - 搜尋和匹配
- Python - 文字處理
- Python - 文字換行
- Python - 頻率分佈
- Python - 文字摘要
- Python - 詞幹提取演算法
- Python - 受約束搜尋
Python - 去除停用詞
停用詞是英語中那些不會給句子增加太多意義的詞語。在不犧牲句子意義的情況下,可以安全地忽略它們。例如,“the”、“he”、“have”等詞。這些詞已經在名為corpus的語料庫中捕獲。我們首先將其下載到我們的python環境中。
import nltk nltk.download('stopwords')
它將下載一個包含英語停用詞的檔案。
驗證停用詞
from nltk.corpus import stopwords stopwords.words('english') print stopwords.words() [620:680]
執行以上程式後,我們將得到以下輸出:
[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()
執行以上程式後,我們將得到以下輸出:
[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)
執行以上程式後,我們將得到以下輸出:
There tree near river
廣告