【NLP】文本预处理:删除单词停用词 您所在的位置:网站首页 删除英文单词缩写 【NLP】文本预处理:删除单词停用词

【NLP】文本预处理:删除单词停用词

2024-06-02 15:41| 来源: 网络整理| 查看: 265

作者 | Chetna Khanna

编译 | VK来源 | Towards Data Science

我们很清楚这样一个事实:计算机可以很容易地处理数字。

然而,我们掌握的大部分信息都是以文本的形式存在的。我们通过直接与他们交谈或使用短信、社交媒体帖子、电话、视频通话等方式相互交流。为了创建智能系统,我们需要利用我们拥有的丰富信息。

自然语言处理(NLP)是人工智能的一个分支,它允许机器解释人类语言。但是,机器不能直接使用,我们需要先对其进行预处理。

文本预处理是准备文本数据的过程,机器可以使用这些数据来执行分析、预测等任务。文本预处理有许多不同的步骤,但在本文中,我们将介绍停用词,为什么要删除它们,以及可以用来删除它们的不同库。

所以,让我们开始吧。

什么是停用词?

在处理自然语言之前通常被过滤掉的单词称为停用词。这些词实际上是任何语言中最常见的词(如冠词、介词、代词、连词等),不会给文本增加太多信息。英语中几个停用词的例子是“the”,“a”,“an”,“so”,“what”。

为什么要删除停用词?

停用词在任何人类语言中都是大量存在的。通过删除这些词,我们从文本中删除了底层信息,以便更加关注重要的信息。我们可以说,删除这些词语并不会对我们的任务训练产生任何负面影响。

删除停用词肯定会减少数据集的大小,从而减少训练时间,因为训练中涉及的标识数量较少。

我们总是删除停用词吗?他们总是对我们没用吗?

答案是否定的!

我们并不能总是删除停用词。停用词的删除在很大程度上取决于我们正在执行的任务和我们想要实现的目标。例如,如果我们正在训练一个可以执行情绪分析任务的模型,我们可能不会删除停用词。

影评:“The movie was not good at all.”

删除停用词后的文字:“movie good”

我们可以清楚地看到这部电影的评论是负面的。然而,在去掉停用词之后,评论变得积极起来,这不是现实。因此,删除停用词在这里可能是有问题的。

像文本分类这样的任务通常不需要停用词,因为数据集中存在的其他词更重要,并且给出了文本的一般概念。因此,我们通常会删除这些任务中的停用词。

简单地说,NLP有很多任务在去掉停用词后无法正常完成。因此,在执行此步骤之前,请先考虑一下。这里的问题是,没有一条规则是通用的,也没有一个停用词列表是通用的。

注意事项:在删除停用词之前,先研究一下你的任务和你要解决的问题,然后再做决定。

有哪些不同的库可以删除停用词?????‍♀️

自然语言处理是当今研究最多的领域之一,在这一领域有了许多革命性的发展。NLP依赖于先进的计算技术,世界各地的开发人员已经创造了许多不同的工具来处理人类语言。

在这么多的库中,有一些非常流行,并且在执行许多不同的NLP任务时有很大帮助。

下面给出了一些用于删除英语停用词的库、停用词列表以及代码。

自然语言工具包(NLTK):

NLTK是一个可以使用自然语言库。当你将开始你的NLP之旅,这是你将使用的第一个库。导入库和英语停用词列表的步骤如下:

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

输出:

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

让我们检查一下这个库有多少停用词。

print(len(sw_nltk))

输出:

179

让我们把文本中的停用词去掉。

text = "When I first met her she was very quiet. She remained quiet during the entire two hour long journey from Stony Brook to New York." words = [word for word in text.split() if word.lower() not in sw_nltk] new_text = " ".join(words) print(new_text) print("Old length: ", len(text)) print("New length: ", len(new_text))

上面的代码很简单,但我还是会向初学者解释。

我拥有一段文本,我把文本分成单词,因为停用词是一个单词列表。然后我将单词改为小写,因为停用词列表中的所有单词都是小写的。然后我创建了一个不在停用词列表中的所有单词的列表。然后将得到的列表连接起来再次组成句子。

输出:

first met quiet. remained quiet entire two hour long journey Stony Brook New York. Old length:  129 New length:  82

我们可以清楚地看到,删除停用词将句子的长度从129缩短到82。

请注意,我将使用类似的代码来解释每个库中的停用词。

spaCy:

spaCy是一个用于高级NLP的开源软件库。这个库现在非常流行,NLP从业者使用它来完成他们的工作。

import spacy # 加载英文空间小模型 en = spacy.load('en_core_web_sm') sw_spacy = en.Defaults.stop_words print(sw_spacy)

输出

{'those', 'on', 'own', '’ve', 'yourselves', 'around', 'between', 'four', 'been', 'alone', 'off', 'am', 'then', 'other', 'can', 'regarding', 'hereafter', 'front', 'too', 'used', 'wherein', '‘ll', 'doing', 'everything', 'up', 'onto', 'never', 'either', 'how', 'before', 'anyway', 'since', 'through', 'amount', 'now', 'he', 'was', 'have', 'into', 'because', 'not', 'therefore', 'they', 'n’t', 'even', 'whom', 'it', 'see', 'somewhere', 'thereupon', 'nothing', 'whereas', 'much', 'whenever', 'seem', 'until', 'whereby', 'at', 'also', 'some', 'last', 'than', 'get', 'already', 'our', 'once', 'will', 'noone', "'m", 'that', 'what', 'thus', 'no', 'myself', 'out', 'next', 'whatever', 'although', 'though', 'which', 'would', 'therein', 'nor', 'somehow', 'whereupon', 'besides', 'whoever', 'ourselves', 'few', 'did', 'without', 'third', 'anything', 'twelve', 'against', 'while', 'twenty', 'if', 'however', 'herself', 'when', 'may', 'ours', 'six', 'done', 'seems', 'else', 'call', 'perhaps', 'had', 'nevertheless', 'where', 'otherwise', 'still', 'within', 'its', 'for', 'together', 'elsewhere', 'throughout', 'of', 'others', 'show', '’s', 'anywhere', 'anyhow', 'as', 'are', 'the', 'hence', 'something', 'hereby', 'nowhere', 'latterly', 'say', 'does', 'neither', 'his', 'go', 'forty', 'put', 'their', 'by', 'namely', 'could', 'five', 'unless', 'itself', 'is', 'nine', 'whereafter', 'down', 'bottom', 'thereby', 'such', 'both', 'she', 'become', 'whole', 'who', 'yourself', 'every', 'thru', 'except', 'very', 'several', 'among', 'being', 'be', 'mine', 'further', 'n‘t', 'here', 'during', 'why', 'with', 'just', "'s", 'becomes', '’ll', 'about', 'a', 'using', 'seeming', "'d", "'ll", "'re", 'due', 'wherever', 'beforehand', 'fifty', 'becoming', 'might', 'amongst', 'my', 'empty', 'thence', 'thereafter', 'almost', 'least', 'someone', 'often', 'from', 'keep', 'him', 'or', '‘m', 'top', 'her', 'nobody', 'sometime', 'across', '‘s', '’re', 'hundred', 'only', 'via', 'name', 'eight', 'three', 'back', 'to', 'all', 'became', 'move', 'me', 'we', 'formerly', 'so', 'i', 'whence', 'under', 'always', 'himself', 'in', 'herein', 'more', 'after', 'themselves', 'you', 'above', 'sixty', 'them', 'your', 'made', 'indeed', 'most', 'everywhere', 'fifteen', 'but', 'must', 'along', 'beside', 'hers', 'side', 'former', 'anyone', 'full', 'has', 'yours', 'whose', 'behind', 'please', 'ten', 'seemed', 'sometimes', 'should', 'over', 'take', 'each', 'same', 'rather', 'really', 'latter', 'and', 'ca', 'hereupon', 'part', 'per', 'eleven', 'ever', '‘re', 'enough', "n't", 'again', '‘d', 'us', 'yet', 'moreover', 'mostly', 'one', 'meanwhile', 'whither', 'there', 'toward', '’m', "'ve", '’d', 'give', 'do', 'an', 'quite', 'these', 'everyone', 'towards', 'this', 'cannot', 'afterwards', 'beyond', 'make', 'were', 'whether', 'well', 'another', 'below', 'first', 'upon', 'any', 'none', 'many', 'serious', 'various', 're', 'two', 'less', '‘ve'}

名单很长。让我们检查一下这个库有多少停用词。

print(len(sw_spacy))

输出:

326

哇,326!让我们把前一篇文本中的停用词去掉。

words = [word for word in text.split() if word.lower() not in sw_spacy] new_text = " ".join(words) print(new_text) print("Old length: ", len(text)) print("New length: ", len(new_text))

输出:

met quiet. remained quiet entire hour lomg journey Stony Brook New York. Old length:  129 New length:  72

我们可以清楚地看到,删除停用词将句子的长度从129缩短到72,甚至比NLTK还要短,因为spaCy库中的停用词比NLTK多。不过,在本例中,结果非常相似。

Gensim:

Gensim(Generate-Similar)是一个使用现代统计机器学习的开源软件库。根据Wikipedia的说法,Gensim被设计成使用数据流和增量在线算法来处理大型文本集合,这与其他大多数只针对内存处理的机器学习软件包不同。

import gensim from gensim.parsing.preprocessing import remove_stopwords, STOPWORDS print(STOPWORDS)

输出:

frozenset({'those', 'on', 'own', 'yourselves', 'ie', 'around', 'between', 'four', 'been', 'alone', 'off', 'am', 'then', 'other', 'can', 'cry', 'regarding', 'hereafter', 'front', 'too', 'used', 'wherein', 'doing', 'everything', 'up', 'never', 'onto', 'how', 'either', 'before', 'anyway', 'since', 'through', 'amount', 'now', 'he', 'cant', 'was', 'con', 'have', 'into', 'because', 'inc', 'not', 'therefore', 'they', 'even', 'whom', 'it', 'see', 'somewhere', 'interest', 'thereupon', 'thick', 'nothing', 'whereas', 'much', 'whenever', 'find', 'seem', 'until', 'whereby', 'at', 'ltd', 'fire', 'also', 'some', 'last', 'than', 'get', 'already', 'our', 'doesn', 'once', 'will', 'noone', 'that', 'what', 'thus', 'no', 'myself', 'out', 'next', 'whatever', 'although', 'though', 'etc', 'which', 'would', 'therein', 'nor', 'somehow', 'whereupon', 'besides', 'whoever', 'thin', 'ourselves', 'few', 'did', 'third', 'without', 'twelve', 'anything', 'against', 'while', 'twenty', 'if', 'however', 'found', 'herself', 'when', 'may', 'six', 'ours', 'done', 'seems', 'else', 'call', 'perhaps', 'had', 'nevertheless', 'fill', 'where', 'otherwise', 'still', 'within', 'its', 'for', 'together', 'elsewhere', 'throughout', 'of', 'eg', 'others', 'show', 'sincere', 'anywhere', 'anyhow', 'as', 'are', 'the', 'hence', 'something', 'hereby', 'nowhere', 'latterly', 'de', 'say', 'does', 'neither', 'his', 'go', 'forty', 'put', 'their', 'by', 'namely', 'km', 'could', 'five', 'unless', 'itself', 'is', 'nine', 'whereafter', 'down', 'bottom', 'thereby', 'such', 'both', 'she', 'become', 'whole', 'who', 'yourself', 'every', 'thru', 'except', 'very', 'several', 'among', 'being', 'be', 'mine', 'further', 'here', 'during', 'why', 'with', 'just', 'becomes', 'about', 'a', 'co', 'using', 'seeming', 'due', 'wherever', 'beforehand', 'detail', 'fifty', 'becoming', 'might', 'amongst', 'my', 'empty', 'thence', 'thereafter', 'almost', 'least', 'someone', 'often', 'from', 'keep', 'him', 'or', 'top', 'her', 'didn', 'nobody', 'sometime', 'across', 'hundred', 'only', 'via', 'name', 'eight', 'three', 'back', 'to', 'all', 'became', 'move', 'me', 'we', 'formerly', 'so', 'i', 'whence', 'describe', 'under', 'always', 'himself', 'more', 'herein', 'in', 'after', 'themselves', 'you', 'them', 'above', 'sixty', 'hasnt', 'your', 'made', 'everywhere', 'indeed', 'most', 'kg', 'fifteen', 'but', 'must', 'along', 'beside', 'hers', 'computer', 'side', 'former', 'full', 'anyone', 'has', 'yours', 'whose', 'behind', 'please', 'mill', 'amoungst', 'ten', 'seemed', 'sometimes', 'should', 'over', 'take', 'each', 'don', 'same', 'rather', 'really', 'latter', 'and', 'part', 'hereupon', 'per', 'eleven', 'ever', 'enough', 'again', 'us', 'yet', 'moreover', 'mostly', 'one', 'meanwhile', 'whither', 'there', 'toward', 'give', 'system', 'do', 'quite', 'an', 'these', 'everyone', 'towards', 'this', 'bill', 'cannot', 'un', 'afterwards', 'beyond', 'make', 'were', 'whether', 'well', 'another', 'below', 'first', 'upon', 'any', 'none', 'many', 'various', 'serious', 're', 'two', 'less', 'couldnt'})

又是一张长长的列表。让我们检查一下这个库有多少停用词。

print(len(STOPWORDS))

输出:

337

嗯!类似的计算为spaCy。让我们把文本中的停用词去掉。

new_text = remove_stopwords(text) print(new_text) print("Old length: ", len(text)) print("New length: ", len(new_text))

我们可以看到,使用Gensim库删除停用词非常简单。

输出:

When I met quiet. She remained quiet entire hour long journey Stony Brook New York. Old length:  129 New length:  83

去掉了停用词,句子的长度从129缩短到83。我们可以看到,尽管spaCy和Gensim中停用词的长度是相似的,但结果却是完全不同的。

Scikit-Learn:

Scikit学习不需要介绍。它是一个免费的Python机器学习库。它可能是最强大的机器学习库。

from sklearn.feature_extraction.text import ENGLISH_STOP_WORDS print(ENGLISH_STOP_WORDS)

输出:

frozenset({'those', 'on', 'own', 'yourselves', 'ie', 'around', 'between', 'four', 'been', 'alone', 'off', 'am', 'then', 'other', 'can', 'cry', 'hereafter', 'front', 'too', 'wherein', 'everything', 'up', 'onto', 'never', 'either', 'how', 'before', 'anyway', 'since', 'through', 'amount', 'now', 'he', 'cant', 'was', 'con', 'have', 'into', 'because', 'inc', 'not', 'therefore', 'they', 'even', 'whom', 'it', 'see', 'somewhere', 'interest', 'thereupon', 'nothing', 'thick', 'whereas', 'much', 'whenever', 'find', 'seem', 'until', 'whereby', 'at', 'ltd', 'fire', 'also', 'some', 'last', 'than', 'get', 'already', 'our', 'once', 'will', 'noone', 'that', 'what', 'thus', 'no', 'myself', 'out', 'next', 'whatever', 'although', 'though', 'etc', 'which', 'would', 'therein', 'nor', 'somehow', 'whereupon', 'besides', 'whoever', 'thin', 'ourselves', 'few', 'third', 'without', 'anything', 'twelve', 'against', 'while', 'twenty', 'if', 'however', 'found', 'herself', 'when', 'may', 'ours', 'six', 'done', 'seems', 'else', 'call', 'perhaps', 'had', 'nevertheless', 'fill', 'where', 'otherwise', 'still', 'within', 'its', 'for', 'together', 'elsewhere', 'throughout', 'of', 'eg', 'others', 'show', 'sincere', 'anywhere', 'anyhow', 'as', 'are', 'the', 'hence', 'something', 'hereby', 'nowhere', 'de', 'latterly', 'neither', 'his', 'go', 'forty', 'put', 'their', 'by', 'namely', 'could', 'five', 'itself', 'is', 'nine', 'whereafter', 'down', 'bottom', 'thereby', 'such', 'both', 'she', 'become', 'whole', 'who', 'yourself', 'every', 'thru', 'except', 'very', 'several', 'among', 'being', 'be', 'mine', 'further', 'here', 'during', 'why', 'with', 'becomes', 'about', 'a', 'co', 'seeming', 'due', 'wherever', 'beforehand', 'detail', 'fifty', 'becoming', 'might', 'amongst', 'my', 'empty', 'thence', 'thereafter', 'almost', 'least', 'someone', 'often', 'from', 'keep', 'him', 'or', 'top', 'her', 'nobody', 'sometime', 'across', 'hundred', 'only', 'via', 'name', 'eight', 'three', 'back', 'to', 'all', 'became', 'move', 'me', 'we', 'formerly', 'so', 'i', 'whence', 'describe', 'under', 'always', 'himself', 'in', 'herein', 'more', 'after', 'themselves', 'you', 'above', 'sixty', 'them', 'hasnt', 'your', 'made', 'indeed', 'most', 'everywhere', 'fifteen', 'but', 'must', 'along', 'beside', 'hers', 'side', 'former', 'anyone', 'full', 'has', 'yours', 'whose', 'behind', 'please', 'amoungst', 'mill', 'ten', 'seemed', 'sometimes', 'should', 'over', 'take', 'each', 'same', 'rather', 'latter', 'and', 'hereupon', 'part', 'per', 'eleven', 'ever', 'enough', 'again', 'us', 'yet', 'moreover', 'mostly', 'one', 'meanwhile', 'whither', 'there', 'toward', 'give', 'system', 'do', 'an', 'these', 'everyone', 'towards', 'this', 'bill', 'cannot', 'un', 'afterwards', 'beyond', 'were', 'whether', 'well', 'another', 'below', 'first', 'upon', 'any', 'none', 'many', 'serious', 're', 'two', 'couldnt', 'less'})

又是一张长长的列表。让我们检查一下这个库有多少停用词。

print(len(ENGLISH_STOP_WORDS))

输出:

318

让我们把文本中的停用词去掉。

words = [word for word in text.split() if word.lower() not in ENGLISH_STOP_WORDS] new_text = " ".join(words) print(new_text) print("Old length: ", len(text)) print("New length: ", len(new_text))

输出:

met quiet. remained quiet entire hour long journey Stony Brook New York. Old length:  129 New length:  72

去掉了停用词,句子的长度从129缩短到72。我们可以看到sciketlearn和spaCy产生了相同的结果。

我能把我自己的停用词加到列表上吗?

是的,我们还可以在这些库中提供的停用词列表中添加自定义停用词,以达到我们的目的。

下面是将一些自定义停用词添加到NLTK停用词列表的代码:

sw_nltk.extend(['first', 'second', 'third', 'me']) print(len(sw_nltk))

输出:

183

我们可以看到NLTK停用词的长度现在是183而不是179。现在,我们可以使用相同的代码从文本中删除停用词。

我可以从预先制作的列表中删除停用词吗?

是的,如果我们愿意,我们也可以从这些库中可用的列表中删除停用词。

以下是使用NLTK库的代码:

sw_nltk.remove('not')

停用词“not”现在从停用词列表中删除。

根据所使用的库,你可以执行相关操作,从预定义列表中添加或删除停用词。我指出这一点是因为NLTK返回一个停用词列表,而其他库返回一组停用词。

如果我们不想使用这些库,我们也可以创建自己的自定义停用词列表并在任务中使用它。这通常是当我们在我们的领域有专业知识,也就是当我们知道哪些词是停用词。

看看下面的代码,看看这是多么简单。

# 创建自定义停用词列表 my_stop_words = ['her','me','i','she','it'] words = [word for word in text.split() if word.lower() not in my_stop_words] new_text = " ".join(words) print(new_text) print("Old length: ", len(text)) print("New length: ", len(new_text))

输出:

When first met was very quiet. remained quiet during the entire two hour long journey from Stony Brook to New York. Old length:  129 New length:  115

以类似的方式,你可以根据任务创建停用词列表并使用它。

我们在本文中观察到,不同的库有不同的停用词集合,我们可以清楚地说,停用词是任何语言中使用频率最高的词。

尽管可以使用这些库中的任何一个从文本中删除停用词,但是对于整个文本预处理任务,使用同一个库是非常明智的。

往期精彩回顾 适合初学者入门人工智能的路线及资料下载机器学习及深度学习笔记等资料打印机器学习在线手册深度学习笔记专辑《统计学习方法》的代码复现专辑 AI基础下载机器学习的数学基础专辑温州大学《机器学习课程》视频 本站qq群851320808,加入微信群请扫码:


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有