來源:Pexelsapp
咱們經常認爲在預處理文本時,去除停用詞是很明智的一種操做。ide
的確,我贊成這一作法,可是咱們應該謹慎決定該去除哪類停用詞。性能
好比說,去除停用詞最常規的方法是使用NLTK停用詞表。學習
一塊兒來看看nltk中的停用詞列表吧。this
from nltk.corpus import stopwords print(stopwords.words('english'))
stopwords.py hosted with by GitHub3d
['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"]
如今,請注意這些加粗的單詞。code
它們有什麼問題嗎?blog
下面是一個例子:ci
假設咱們要建立一個對產品評論進行情感分析的模型。鑑於數據集很小,所以能夠本身手動標識情感態度。讓咱們來研究一下數據集中的一些評論。rem
接下來,對數據進行預處理,去除全部的停用詞。
如今,來看看上述範例會發生什麼變化吧。
看看這些負面評價如今表達的含義。
可怕吧?
來源:Pexels
那些正面評價彷佛並未受到影響,可是負面評價的總體意思都變了。若是咱們用這些數據去構建模型,那最後得出的結果確定不理想。
這種狀況常有發生,當去除停用詞後,句子的整個意思都會改變。
若是你使用的是基礎的NLP技術,如BOW, Count Vectorizer或TF-IDF(詞頻和逆文檔頻率),那麼去除停用詞是明智的選擇,由於在這些模型中,停用詞會帶來干擾。但若是你使用的是LSTM或其餘模型,這些模型會捕獲單詞的語義,且單詞的含義基於前文語境,那麼此時保留停用詞就十分必要了。
如今,回到最初的問題——去除停用詞真的能提升模型性能嗎?
就如我以前所說,這取決於去除的是哪類停用詞。若是不去除像I, my, me等停用詞的話,數據集就會受到更多幹擾。
來源:Pexels
那麼,有什麼解決辦法呢?能夠建立一個合適的停用詞列表,但問題是要如何在不一樣的項目中重複使用這個列表。
這就是爲何建立Python包nlppreprocess,這個包去除了全部無用的停用詞,此外,還能夠更加高效地整理文本。
發揮nlppreprocess包功能的最佳方法是將其與pandas 組合使用:
from nlppreprocess importNLP import pandas as pd nlp = NLP() df = pd.read_csv('some_file.csv') df['text'] = df['text'].apply(nlp.process)
viewrawdemo.py hosted with by GitHub
如今,若是咱們用nlppreprocess包對以前的樣本進行預處理,能夠獲得以下結果:
如此看來,用nlppreprocess包去除停用詞並進行其餘預處理彷佛效果不錯。
你以爲的?
編譯組:虞雙雙、李韻帷 相關連接: https://towardsdatascience.com/why-you-should-avoid-removing-stopwords-aa7a353d2a52 如需轉載,請後臺留言,遵照轉載規範
ACL2018論文集50篇解讀 EMNLP2017論文集28篇論文解讀 2018年AI三大頂會中國學術成果全連接 ACL2017 論文集:34篇解讀乾貨全在這裏 10篇AAAI2017經典論文回顧