使用Python中的NLTK和spaCy删除停用词与文本标准化
概述
了解如何在Python中删除停用词与文本标准化,这些是自然语言处理的基本技术
探索不同的方法来删除停用词,以及讨论文本标准化技术,如词干化(stemming)和词形还原(lemmatization)
在Python中使用NLTK,spaCy和Gensim库进行去除停用词和文本标准化
介绍
多样化的自然语言处理(NLP)是真的很棒,我们以前从未想象过的事情现在只是几行代码就可做到。这真的令人开心。
但使用文本数据会带来一系列挑战。机器在处理原始文本方面有着较大的困难。在使用NLP技术处理文本数据之前,我们需要执行一些称为预处理的步骤。
错过了这些步骤,我们会得到一个不好的模型。这些是你需要在代码,框架和项目中加入的基本NLP技术。
我们将讨论如何使用一些非常流行的NLP库(NLTK,spaCy,Gensim和TextBlob)删除停用词并在Python中执行文本标准化。
目录
什么是停用词?
为什么我们需要删除停用词?
我们何时应该删除停用词?
删除停用词的不同方法
使用NLTK
使用spaCy
使用Gensim
文本标准化简介
什么是词干化和词形还原?
执行词干化和词形还原的方法
使用NLTK
使用spaCy
使用TextBlob
什么是停用词?
在任何自然语言中停用词是最常用的词。为了分析文本数据和构建NLP模型,这些停用词可能对构成文档的意义没有太多价值。
通常,英语文本中使用的最常用词是"the","is","in","for","where","when","to","at"等。
考虑这个文本,"There is a pen on the table"。现在,单词"is","a","on"和"the"在解析它时对语句没有任何意义。而像"there","book"和"table"这样的词是关键词,并告诉我们这句话是什么意思。
一般来说在去除停用词之前要执行分词操作。
以下是一份停用词列表,可能对你有用
a about after all also always am an and any are at be been being but by came can cant come
为什么我们需要删除停用词?
这是一个你必须考虑到的非常重要的问题
在NLP中删除停用词并不是一项严格的规则。这取决于我们正在进行的任务。对于文本分类等(将文本分类为不同的类别)任务,从给定文本中删除或排除停用词,可以更多地关注定义文本含义的词。
正如我们在上一节中看到的那样,单词there,book要比单词is,on来得更加有意义。
但是,在机器翻译和文本摘要等任务中,却不建议删除停用词。
以下是删除停用词的几个主要好处:
在删除停用词时,数据集大小减小,训练模型的时间也减少
删除停用词可能有助于提高性能,因为只剩下更少且唯一有意义的词。因此,它可以提高分类准确性
甚至像Google这样的搜索引擎也会删除停用词,以便从数据库中快速地检索数据
我们什么时候应该删除停用词?
我把它归纳为两个部分:删除停用词的情况以及当我们避免删除停用词的情况。
删除停用词
我们可以在执行以下任务时删除停用词:
文本分类
垃圾邮件过滤
语言分类
体裁(Genre)分类
标题生成
自动标记(Auto-Tag)生成
避免删除停用词
机器翻译
语言建模
文本摘要
问答(QA)系统
删除停用词的不同方法
1.使用NLTK删除停用词
NLTK是文本预处理的自然语言工具包。这是我最喜欢的Python库之一。NLTK有16种不同语言的停用词列表。
你可以使用以下代码查看NLTK中的停用词列表:
import nltk
from nltk.corpus import stopwords
set(stopwords.words('english'))
现在,要使用NLTK删除停用词,你可以使用以下代码块
# 下面的代码是使用nltk从句子中去除停用词
# 导入包
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
set(stopwords.words('english'))
# 例句
text = """He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and
fishery rihgts at once. He was the more ready to do this becuase the rights had become much less valuable, and he had
indeed the vaguest idea where the wood and river in question were."""
# 停用词集合
stop_words = set(stopwords.words('english'))
# 分词
word_tokens = word_tokenize(text)
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
print("\n\nOriginal Sentence \n\n")
print(" ".join(word_tokens))
print("\n\nFiltered Sentence \n\n")
print(" ".join(filtered_sentence))
这是我们分词后的句子:
He determined to drop his litigation with the monastry, and relinguish his claims to the
wood-cuting and fishery rihgts at once. He was the more ready to do this becuase the rights
had become much less valuable, and he had indeed the vaguest idea where the wood and river
in question were.
删除停用词后:
He determined drop litigation monastry, relinguish claims wood-cuting fishery rihgts. He
ready becuase rights become much less valuable, indeed vaguest idea wood river question.
请注意,文本的大小几乎减少到一半!你能想象一下删除停用词的用处吗?
2.使用spaCy删除停用词
spaCy是NLP中功能最多,使用最广泛的库之一。我们可以使用SpaCy快速有效地从给定文本中删除停用词。它有一个自己的停用词列表,可以从spacy.lang.en.stop_words类导入。
以下是在Python中使用spaCy删除停用词的方法:
from spacy.lang.en import English
# 加载英语分词器、标记器、解析器、NER和单词向量
nlp = English()
text = """He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and
fishery rihgts at once. He was the more ready to do this becuase the rights had become much less valuable, and he had
indeed the vaguest idea where the wood and river in question were."""
# "nlp"对象用于创建具有语言注释的文档。
my_doc = nlp(text)
# 构建词列表
token_list = []
for token in my_doc:
token_list.append(token.text)
from spacy.lang.en.stop_words import STOP_WORDS
# 去除停用词后创建单词列表
filtered_sentence =[]
for word in token_list:
lexeme = nlp.vocab[word]
if lexeme.is_stop == False:
filtered_sentence.append(word)
print(token_list)
print(filtered_sentence)
这是我们在分词后获得的列表:
He determined to drop his litigation with the monastry and relinguish his claims to the
wood-cuting and \n fishery rihgts at once. He was the more ready to do this becuase the
rights had become much less valuable, and he had \n indeed the vaguest idea where the wood
and river in question were.
删除停用词后的列表:
determined drop litigation monastry, relinguish claims wood-cuting \n fishery rihgts. ready
becuase rights become valuable, \n vaguest idea wood river question
需要注意的一点是,去除停用词并不会删除标点符号或换行符,我们需要手动删除它们。
3.使用Gensim删除停用词
Gensim是一个非常方便的库,可以处理NLP任务。在预处理时,gensim也提供了去除停用词的方法。我们可以从类gensim.parsing.preprocessing轻松导入remove_stopwords方法。
尝试使用Gensim去除停用词:
# 以下代码使用Gensim去除停用词
from gensim.parsing.preprocessing import remove_stopwords
# pass the sentence in the remove_stopwords function
result = remove_stopwords("""He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and fishery rihgts at once. He was the more ready to do this becuase the rights had become much less valuable,
and he had indeed the vaguest idea where the wood and river in question were.""")
print('\n\n Filtered Sentence \n\n')
print(result)
He determined drop litigation monastry, relinguish claims wood-cuting fishery rihgts once.
He ready becuase rights valuable, vaguest idea wood river question were.
使用gensim去除停用词时,我们可以直接在原始文本上进行。在删除停用词之前无需执行分词。这可以节省我们很多时间。
文本标准化(text normalization)简介
在任何自然语言中,根据情况,可以以多种形式书写或说出单词。这就是语言的精美之处。例如:
Lisa ate the food and washed the dishes.
They were eating noodles at a cafe.
Don’t you want to eat before we leave?
We have just eaten our breakfast.
It also eats fruit and vegetables.
在所有这些句子中,我们可以看到"eat"这个词有多种形式。对我们来说,很容易理解"eat"就是这里具体的活动。所以对我们来说,无论是'eat','ate'还是'eaten'都没关系,因为我们知道发生了什么。
不幸的是,机器并非如此。他们区别对待这些词。因此,我们需要将它们标准化为它们的根词,在我们的例子中是"eat"。
因此,文本标准化是将单词转换为单个规范形式的过程。这可以通过两个过程来实现,即词干化(stemming)和词形还原(lemmatization)。让我们详细了解它们的含义。
什么是词干化和词形还原?
词干化和词形还原只是单词的标准化,这意味着将单词缩减为其根形式。
在大多数自然语言中,根词可以有许多变体。例如,"play"一词可以用作"playing","played","plays"等。你可以想到类似的例子(并且有很多)。
词干化
让我们先了解词干化:
词干化是一种文本标准化技术,它通过考虑可以在该词中找到的公共前缀或后缀列表来切断单词的结尾或开头。
这是一个基于规则的基本过程,从单词中删除后缀("ing","ly","es","s"等)
词形还原
另一方面,词形还原是一种结构化的程序,用于获得单词的根形式。它利用了词汇(词汇的字典重要性程度)和形态分析(词汇结构和语法关系)。
为什么我们需要执行词干化或词形还原?
让我们考虑以下两句话:
He was driving
He went for a drive
我们可以很容易地说两句话都传达了相同的含义,即过去的驾驶活动。机器将以不同的方式处理两个句子。因此,为了使文本可以理解,我们需要执行词干化或词形还原。
文本标准化的另一个好处是它减少了文本数据中词典的大小。这有助于缩短机器学习模型的训练时间。
我们应该选择哪一个?
词干化算法通过从词中剪切后缀或前缀来工作。词形还原是一种更强大的操作,因为它考虑了词的形态分析。
词形还原返回词根,词根是其所有变形形式的根词。
我们可以说词干化是一种快速但不那么好的方法,可以将词语切割成词根形式,而另一方面,词形还原是一种智能操作,它使用由深入的语言知识创建的词典。因此,词形还原有助于形成更好的效果。
执行文本标准化的方法
1.使用NLTK进行文本标准化
NLTK库有许多令人惊奇的方法来执行不同的数据预处理步骤。有些方法如PorterStemmer()和WordNetLemmatizer()分别执行词干化和词形还原。
让我们看看他们的实际效果。
词干化
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer
set(stopwords.words('english'))
text = """He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and
fishery rihgts at once. He was the more ready to do this becuase the rights had become much less valuable, and he had
indeed the vaguest idea where the wood and river in question were."""
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(text)
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
Stem_words = []
ps =PorterStemmer()
for w in filtered_sentence:
rootWord=ps.stem(w)
Stem_words.append(rootWord)
print(filtered_sentence)
print(Stem_words)
He determined drop litigation monastry, relinguish claims wood-cuting fishery rihgts. He
ready becuase rights become much less valuable, indeed vaguest idea wood river question.
He determin drop litig monastri, relinguish claim wood-cut fisheri rihgt. He readi becuas
right become much less valuabl, inde vaguest idea wood river question.
我们在这里就可以很清晰看到不同点了,我们继续对这段文本执行词形还原
词形还原
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import nltk
from nltk.stem import WordNetLemmatizer
set(stopwords.words('english'))
text = """He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and
fishery rihgts at once. He was the more ready to do this becuase the rights had become much less valuable, and he had
indeed the vaguest idea where the wood and river in question were."""
stop_words = set(stopwords.words('english'))
word_tokens = word_tokenize(text)
filtered_sentence = []
for w in word_tokens:
if w not in stop_words:
filtered_sentence.append(w)
print(filtered_sentence)
lemma_word = []
import nltk
from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()
for w in filtered_sentence:
word1 = wordnet_lemmatizer.lemmatize(w, pos = "n")
word2 = wordnet_lemmatizer.lemmatize(word1, pos = "v")
word3 = wordnet_lemmatizer.lemmatize(word2, pos = ("a"))
lemma_word.append(word3)
print(lemma_word)
He determined drop litigation monastry, relinguish claims wood-cuting fishery rihgts. He
ready becuase rights become much less valuable, indeed vaguest idea wood river question.
He determined drop litigation monastry, relinguish claim wood-cuting fishery rihgts. He
ready becuase right become much le valuable, indeed vaguest idea wood river question.
在这里,v表示动词,a代表形容词和n代表名词。该词根提取器(lemmatizer)仅与lemmatize方法的pos参数匹配的词语进行词形还原。
词形还原基于词性标注(POS标记)完成。
2.使用spaCy进行文本标准化
正如我们之前看到的,spaCy是一个优秀的NLP库。它提供了许多工业级方法来执行词形还原。不幸的是,spaCy没有用于词干化(stemming)的方法。要执行词形还原,请查看以下代码:
#确保使用"python -m spacy download en"下载英语模型
import en_core_web_sm
nlp = en_core_web_sm.load()
doc = nlp(u"""He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and
fishery rihgts at once. He was the more ready to do this becuase the rights had become much less valuable, and he had
indeed the vaguest idea where the wood and river in question were.""")
lemma_word1 = []
for token in doc:
lemma_word1.append(token.lemma_)
lemma_word1
-PRON- determine to drop -PRON- litigation with the monastry, and relinguish -PRON- claim
to the wood-cuting and \n fishery rihgts at once. -PRON- be the more ready to do this
becuase the right have become much less valuable, and -PRON- have \n indeed the vague idea
where the wood and river in question be.
这里-PRON-是代词的符号,可以使用正则表达式轻松删除。spaCy的好处是我们不必传递任何pos参数来执行词形还原。
3.使用TextBlob进行文本标准化
TextBlob是一个专门用于预处理文本数据的Python库。它基于NLTK库。我们可以使用TextBlob来执行词形还原。但是,TextBlob中没有用于词干化的模块。
那么让我们看看如何在Python中使用TextBlob执行词形还原:
# from textblob lib import Word method
from textblob import Word
text = """He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and
fishery rihgts at once. He was the more ready to do this becuase the rights had become much less valuable, and he had
indeed the vaguest idea where the wood and river in question were."""
lem = []
for i in text.split():
word1 = Word(i).lemmatize("n")
word2 = Word(word1).lemmatize("v")
word3 = Word(word2).lemmatize("a")
lem.append(Word(word3).lemmatize())
print(lem)
He determine to drop his litigation with the monastry, and relinguish his claim to the
wood-cuting and fishery rihgts at once. He wa the more ready to do this becuase the right
have become much le valuable, and he have indeed the vague idea where the wood and river
in question were.
就像我们在NLTK小节中看到的那样,TextBlob也使用POS标记来执行词形还原。
结束
停用词在情绪分析,问答系统等问题中反而起着重要作用。这就是为什么删除停用词可能会严重影响我们模型的准确性。
欢迎关注磐创博客资源汇总站:
http://docs.panchuang.net/
欢迎关注PyTorch官方中文教程站:
http://pytorch.panchuang.net/
使用Python中的NLTK和spaCy删除停用词与文本标准化的更多相关文章
- python使用jieba实现中文文档分词和去停用词
分词工具的选择: 现在对于中文分词,分词工具有很多种,比如说:jieba分词.thulac.SnowNLP等.在这篇文档中,笔者使用的jieba分词,并且基于python3环境,选择jieba分词的理 ...
- python中list用法及遍历删除元素
列表(list)是python的基本数据结构,list中每一个元素都分配一个位置索引,可以通过索引访问元素值,list不要求数据项有相同的数据类型. list初始化 list由一个方括号加内部由逗号分 ...
- python 语料处理(从文件夹中读取文件夹中文件,分词,去停用词,去单个字)
# -*- coding:utf8 -*- import os import jieba def splitSentence(inputFile): fin = open(inputFile, 'r' ...
- python利用jieba进行中文分词去停用词
中文分词(Chinese Word Segmentation) 指的是将一个汉字序列切分成一个一个单独的词. 分词模块jieba,它是python比较好用的分词模块.待分词的字符串可以是 unicod ...
- python中的del
python中的del,只删除变量,不删除数据,具体表现为: a=1,c=a,del a,(c=1) 和 a = [1,2,3,4,5] b= a[0] del a[0] print a ([2,3, ...
- Python核心技术与实战——十三|Python中参数传递机制
我们在前面的章节里学习了Python的函数基础以及应用,那么现在想一想:传参,也就是把一些参数从一个函数传递到另一个函数,从而使其执行相应的任务,这个过程的底层是如何工作的,原理又是怎样的呢? 在实际 ...
- python中List添加、删除元素的几种方法
一.python中List添加元素的几种方法 List 是 Python 中常用的数据类型,它一个有序集合,即其中的元素始终保持着初始时的定义的顺序(除非你对它们进行排序或其他修改操作).在Pytho ...
- python中删除某个元素的3种方法
python中关于删除list中的某个元素,一般有三种方法:remove.pop.del 1.remove: 删除单个元素,删除首个符合条件的元素,按值删除 举例说明: >>> st ...
- python中的join.set ,copy以及删除注意事项:
1 ,join : 将对象以字符串的方式拼接成一个整体 for E : li = ["李白", "是", "诗仙"] s = & ...
随机推荐
- Java后端完整学习路线及资源记录
Java后端完整学习路线及资源记录 Java语法基础 书籍教程: 视频教程: IDEA的使用 视频教程:JAVA开发利器-IntelliJ IDEA使用教程 Servlet和Web基础 书籍教程: 视 ...
- datatable某列不排序、和自定义搜索、给数据里面加属性
datatable中如果不想对前几列进行排序,使用以下代码: $('#informationList').DataTable({ //对0,1,2列不排序 "columnDefs" ...
- PhalApi 2.7 开发快速上手
PhalApi是一款国人制作的PHP纯后端框架.它的开发相当简单,同时也具备文档生成等特色功能.下面,我通过简单的几点,让你可以快速入门使用该框架的开发. 建议使用PHPStorm作为IDE,代码提示 ...
- SQL语句中in 与 exists的区别
SQL语句中in 与 exists的区别 SQL中EXISTS检查是否有结果,判断是否有记录,返回的是一个布尔型(true/false); IN是对结果值进行比较,判断一个字段是否存在于几个值的范围中 ...
- 从零认识 DOM (一): 对象及继承关系
先上图为敬! 说明: 图中包括了大部分 DOM 接口及对象, 其中: 青色背景为接口, 蓝色背景为类, 灰色背景表示为 ECMAScript 中的对象 忽略了一部分对象, 包括: HTML/SVG 的 ...
- django 从零开始 5 数据库模型创建
进入应用项目下的models.py文件 自带一个导入的包 from django.db import models 使用这个包创建models模型 我这是要创建一个图站 ,所以模型设置并不复杂(路径配 ...
- C语言程序设计(十二) 结构体和共用体
第十二章 结构体和共用体 当需要表示复杂对象时,仅使用几个基本数据类型显然是不够的 根本的解决方法是允许用户自定义数据类型 构造数据类型(复合数据类型)允许用户根据实际需要利用已有的基本数据类型来构造 ...
- Hadoop集群搭建(四)~centos6.8关闭防火墙
一.centos关闭防火墙 1,关闭防火墙.service iptables stop 2,关闭防火墙开机自启.chkconfig iptables off 3,查看防火墙状态.service ipt ...
- PostgreSQL查询表以及字段的备注
目录 查询所有表名称以及字段含义 查看所有表名 查看表名和备注 查看特定表名备注 查看特定表名字段 查询所有表名称以及字段含义 select c.relname 表名,cast(obj_descrip ...
- Gorm 预加载及输出处理(二)- 查询输出处理
上一篇<Gorm 预加载及输出处理(一)- 预加载应用>中留下的三个问题: 如何自定义输出结构,只输出指定字段? 如何自定义字段名,并去掉空值字段? 如何自定义时间格式? 这一篇先解决前两 ...