来源:Pexels

我们常常认为在预处理文本时,去除停用词是很明智的一种操作。

的确,我同意这一做法,但是我们应该谨慎决定该去除哪类停用词。

比如说,去除停用词最常规的方法是使用NLTK停用词表。

一起来看看nltk中的停用词列表吧。

from  nltk.corpus import stopwords
print(stopwords.words('english'))

stopwords.py hosted with by GitHub

['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"]

现在,请注意这些加粗的单词。

它们有什么问题吗?

下面是一个例子:

假设我们要创建一个对产品评论进行情感分析的模型。鉴于数据集很小,因此可以自己手动标识情感态度。让我们来研究一下数据集中的一些评论。

  1. The product is really very good. — POSITIVE (这款产品真的很棒。——积极)
  2. The products seems to be good. — POSITIVE (这款产品看起来不错。——积极)
  3. Good product. I really liked it.— POSITIVE (不错的产品。我真的很喜欢。——积极)
  4. I didn’t like the product. —NEGATIVE (我不喜欢这款产品。——消极)
  5. The product is not good. — NEGATIVE (这款产品不好。——消极)

接下来,对数据进行预处理,去除所有的停用词。

现在,来看看上述范例会发生什么变化吧。

  1. product really good. — POSITIVE (产品真的很棒。——积极)
  2. products seems good. — POSITIVE (产品看起来不错。——积极)
  3. Good product. really liked. —POSITIVE (不错的产品,真的很喜欢。——积极)
  4. like product. — NEGATIVE (喜欢产品。——消极)
  5. product good. — NEGATIVE (产品好。——消极)

看看这些负面评价现在表达的含义。

可怕吧?

来源: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包对之前的样本进行预处理,可以得到如下结果:

  1. product really very good. — POSITIVE (产品真的很棒。——积极) 2.products seems good. — POSITIVE (产品看起来不错。——积极)
  2. Good product. really liked. —POSITIVE (不错的产品,真的很喜欢。——积极)
  3. not like product. — NEGATIVE (不喜欢产品。——消极)
  4. product not good. — NEGATIVE (产品不好。——消极)

如此看来,用nlppreprocess包去除停用词并进行其他预处理似乎效果不错。

你觉得的?

留言 点赞 发个朋友圈

我们一起分享AI学习与发展的干货

编译组:虞双双、李韵帷
相关链接:
https://towardsdatascience.com/why-you-should-avoid-removing-stopwords-aa7a353d2a52

如需转载,请后台留言,遵守转载规范

推荐文章阅读

ACL2018论文集50篇解读
EMNLP2017论文集28篇论文解读
2018年AI三大顶会中国学术成果全链接
ACL2017 论文集:34篇解读干货全在这里
10篇AAAI2017经典论文回顾

长按识别二维码可添加关注

读芯君爱你