百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程字典 > 正文

如何开始学习NLP 6种用来标识化的方法

toyiye 2024-06-21 12:15 9 浏览 0 评论

介绍

你对互联网上的大量文本数据着迷吗?你是否正在寻找处理这些文本数据的方法,但不确定从哪里开始?毕竟,机器识别的是数字,而不是我们语言中的字母。在机器学习中,这可能是一个棘手的问题。

那么,我们如何操作和处理这些文本数据来构建模型呢?答案就在自然语言处理(NLP)的奇妙世界中。

解决一个NLP问题是一个多阶段的过程。在进入建模阶段之前,我们需要首先处理非结构化文本数据。处理数据包括以下几个关键步骤:

  • 标识化
  • 预测每个单词的词性
  • 词形还原
  • 识别和删除停止词,等等

在本文中,我们将讨论第一步—标识化。我们将首先了解什么是标识化,以及为什么在NLP中需要标识化。然后,我们将研究在Python中进行标识化的六种独特方法。

在NLP中,什么是标识化?

标识化是处理文本数据时最常见的任务之一。但是标识化(tokenization)具体是什么意思呢?

标识化(tokenization)本质上是将短语、句子、段落或整个文本文档分割成更小的单元,例如单个单词或术语。每个较小的单元都称为标识符(token)

看看下面这张图片,你就能理解这个定义了:

标识符可以是单词、数字或标点符号。在标识化中,通过定位单词边界创建更小的单元。等等,可能你又有疑问,什么是单词边界呢?

单词边界是一个单词的结束点和下一个单词的开始。而这些标识符被认为是词干提取(stemming)和词形还原(lemmatization )的第一步。

为什么在NLP中需要标识化?

在这里,我想让你们思考一下英语这门语言。想一句任何你能想到的一个英语句子,然后在你接下去读这部分的时候,把它记在心里。这将帮助你更容易地理解标识化的重要性。

在处理一种自然语言之前,我们需要识别组成字符串的单词,这就是为什么标识化是处理NLP(文本数据)的最基本步骤。这一点很重要,因为通过分析文本中的单词可以很容易地解释文本的含义。

让我们举个例子,以下面的字符串为例:

“This is a cat.”

你认为我们对这个字符串进行标识化之后会发生什么?是的,我们将得到[' This ', ' is ', ' a ', cat ']。

这样做有很多用途,我们可以使用这个标识符形式:

  • 计数文本中出现的单词总数
  • 计数单词出现的频率,也就是某个单词出现的次数

之外,还有其他用途。我们可以提取更多的信息,这些信息将在以后的文章中详细讨论。现在,是我们深入研究本文的主要内容的时候了——在NLP中进行标识化的不同方法。

在Python中执行标识化的方法

我们将介绍对英文文本数据进行标识化的六种独特方法。我已经为每个方法提供了Python代码,所以你可以在自己的机器上运行示例用来学习。

1.使用python的split()函数进行标识化

让我们从split()方法开始,因为它是最基本的方法。它通过指定的分隔符分割给定的字符串后返回字符串列表。默认情况下,split()是以一个或多个空格作为分隔符。我们可以把分隔符换成任何东西。让我们来看看。

单词标识化

text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
liquid-fuel launch vehicle to orbit the Earth."""
# 以空格为分隔符进行分割
text.split() 
Output : ['Founded', 'in', '2002,', 'SpaceX’s', 'mission', 'is', 'to', 'enable', 'humans', 
 'to', 'become', 'a', 'spacefaring', 'civilization', 'and', 'a', 'multi-planet', 
 'species', 'by', 'building', 'a', 'self-sustaining', 'city', 'on', 'Mars.', 'In', 
 '2008,', 'SpaceX’s', 'Falcon', '1', 'became', 'the', 'first', 'privately', 
 'developed', 'liquid-fuel', 'launch', 'vehicle', 'to', 'orbit', 'the', 'Earth.']

句子标识化:

这类似于单词标识化。这里,我们在分析中研究句子的结构。一个句子通常以句号(.)结尾,所以我们可以用"."作为分隔符来分割字符串:

text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
liquid-fuel launch vehicle to orbit the Earth."""
# 以"."作为分割符进行分割 
text.split('. ') 
Output : ['Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring 
 civilization and a multi-planet \nspecies by building a self-sustaining city on 
 Mars', 
 'In 2008, SpaceX’s Falcon 1 became the first privately developed \nliquid-fuel 
 launch vehicle to orbit the Earth.']

使用Python的split()方法的一个主要缺点是一次只能使用一个分隔符。另一件需要注意的事情是——在单词标识化中,split()没有将标点符号视为单独的标识符。

2.使用正则表达式(RegEx)进行标识化

正则表达式是什么?它基本上是一个特殊的字符序列,使用该序列作为模式帮助你匹配或查找其他字符串或字符串集。

我们可以使用Python中的re库来处理正则表达式。这个库预安装在Python安装包中。

现在,让我们记住正则表达式并执行单词标识化和句子标识化。

单词标识化

import re
text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
liquid-fuel launch vehicle to orbit the Earth."""
tokens = re.findall("[\w']+", text)
tokens
Output : ['Founded', 'in', '2002', 'SpaceX', 's', 'mission', 'is', 'to', 'enable', 
 'humans', 'to', 'become', 'a', 'spacefaring', 'civilization', 'and', 'a', 
 'multi', 'planet', 'species', 'by', 'building', 'a', 'self', 'sustaining', 
 'city', 'on', 'Mars', 'In', '2008', 'SpaceX', 's', 'Falcon', '1', 'became', 
 'the', 'first', 'privately', 'developed', 'liquid', 'fuel', 'launch', 'vehicle', 
 'to', 'orbit', 'the', 'Earth']

re.findall()函数的作用是查找与传递给它的模式匹配的所有单词,并将其存储在列表中。\w表示“任何字符”,通常表示字母数字和下划线(_)。+表示任意出现次数。因此[\w']+表示代码应该找到所有的字母数字字符,直到遇到任何其他字符为止。

句子标识化

要执行句子标识化,可以使用re.split()函数,将通过传递一个模式给函数将文本分成句子。

import re
text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
species by building a self-sustaining city on, Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
liquid-fuel launch vehicle to orbit the Earth."""
sentences = re.compile('[.!?] ').split(text)
sentences
Output : ['Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring 
 civilization and a multi-planet \nspecies by building a self-sustaining city on 
 Mars.', 
 'In 2008, SpaceX’s Falcon 1 became the first privately developed \nliquid-fuel 
 launch vehicle to orbit the Earth.']

这里,我们相比split()方法上有一个优势,因为我们可以同时传递多个分隔符。在上面的代码中,我们使用了的re.compile()函数,并传递一个模式[.?!]。这意味着一旦遇到这些字符,句子就会被分割开来。

3.使用NLTK进行标识化

NLTK是Natural Language ToolKit的缩写,是用Python编写的用于符号和统计自然语言处理的库。

你可以使用以下命令安装NLTK:

pip install --user -U nltk

NLTK包含一个名为tokenize()的模块,它可以进一步划分为两个子类别:

  • Word tokenize:我们使用word_tokenize()方法将一个句子分割成标识符
  • Sentence tokenize:我们使用sent_tokenize()方法将文档或段落分割成句子

让我们一个一个来看是怎么操作的。

单词标识化

from nltk.tokenize import word_tokenize 
text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
liquid-fuel launch vehicle to orbit the Earth."""
word_tokenize(text)
Output: ['Founded', 'in', '2002', ',', 'SpaceX', '’', 's', 'mission', 'is', 'to', 'enable', 
 'humans', 'to', 'become', 'a', 'spacefaring', 'civilization', 'and', 'a', 
 'multi-planet', 'species', 'by', 'building', 'a', 'self-sustaining', 'city', 'on', 
 'Mars', '.', 'In', '2008', ',', 'SpaceX', '’', 's', 'Falcon', '1', 'became', 
 'the', 'first', 'privately', 'developed', 'liquid-fuel', 'launch', 'vehicle', 
 'to', 'orbit', 'the', 'Earth', '.']

注意到NLTK是如何考虑将标点符号作为标识符的吗?因此,对于之后的任务,我们需要从初始列表中删除这些标点符号。

句子标识化

from nltk.tokenize import sent_tokenize
text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
liquid-fuel launch vehicle to orbit the Earth."""
sent_tokenize(text)
Output: ['Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring 
 civilization and a multi-planet \nspecies by building a self-sustaining city on 
 Mars.', 
 'In 2008, SpaceX’s Falcon 1 became the first privately developed \nliquid-fuel 
 launch vehicle to orbit the Earth.']

4.使用`spaCy`库进行标识化

我喜欢spaCy这个库,我甚至不记得上次我在做NLP项目时没有使用它是什么时候了。是的,它就是那么有用。

spaCy是一个用于高级自然语言处理(NLP)的开源库。它支持超过49种语言,并具有最快的的计算速度。

在Linux上安装Spacy的命令:

pip install -U spacy

python -m spacy download en

要在其他操作系统上安装它,可以通过下面链接查看:

https://spacy.io/usage

所以,让我们看看如何利用spaCy的神奇之处来进行标识化。我们将使用spacy.lang.en以支持英文。

单词标识化

from spacy.lang.en import English
# 加载英文分词器,标记器、解析器、命名实体识别和词向量
nlp = English()
text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
liquid-fuel launch vehicle to orbit the Earth."""
#"nlp" 对象用于创建具有语言注解的文档
my_doc = nlp(text)
# 创建单词标识符列表
token_list = []
for token in my_doc:
 token_list.append(token.text)
token_list
Output : ['Founded', 'in', '2002', ',', 'SpaceX', '’s', 'mission', 'is', 'to', 'enable', 
 'humans', 'to', 'become', 'a', 'spacefaring', 'civilization', 'and', 'a', 
 'multi', '-', 'planet', '\n', 'species', 'by', 'building', 'a', 'self', '-', 
 'sustaining', 'city', 'on', 'Mars', '.', 'In', '2008', ',', 'SpaceX', '’s', 
 'Falcon', '1', 'became', 'the', 'first', 'privately', 'developed', '\n', 
 'liquid', '-', 'fuel', 'launch', 'vehicle', 'to', 'orbit', 'the', 'Earth', '.']

句子标识化

from spacy.lang.en import English
# 加载英文分词器,标记器、解析器、命名实体识别和词向量
nlp = English()
# 创建管道 'sentencizer' 组件
sbd = nlp.create_pipe('sentencizer')
# 将组建添加到管道中
nlp.add_pipe(sbd)
text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
liquid-fuel launch vehicle to orbit the Earth."""
# "nlp" 对象用于创建具有语言注解的文档
doc = nlp(text)
# 创建句子标识符列表
sents_list = []
for sent in doc.sents:
 sents_list.append(sent.text)
sents_list
Output : ['Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring 
 civilization and a multi-planet \nspecies by building a self-sustaining city on 
 Mars.', 
 'In 2008, SpaceX’s Falcon 1 became the first privately developed \nliquid-fuel 
 launch vehicle to orbit the Earth.']

在执行NLP任务时,与其他库相比,spaCy的速度相当快(是的,甚至相较于NLTK)。

5.使用Keras进行标识化

Keras是目前业界最热门的深度学习框架之一。它是Python的一个开源神经网络库。Keras非常容易使用,也可以运行在TensorFlow之上。

在NLP上下文中,我们可以使用Keras处理我们通常收集到的非结构化文本数据。

在你的机子上,只需要一行代码就可以在机器上安装Keras:

pip install Keras

让我们开始进行实验,要使用Keras执行单词标记化,我们使用keras.preprocessing.text类中的text_to_word_sequence方法.

让我们看看keras是怎么做的。

单词标识化

from keras.preprocessing.text import text_to_word_sequence
# 文本数据
text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
liquid-fuel launch vehicle to orbit the Earth."""
# 标识化
result = text_to_word_sequence(text)
result
Output : ['founded', 'in', '2002', 'spacex’s', 'mission', 'is', 'to', 'enable', 'humans', 
 'to', 'become', 'a', 'spacefaring', 'civilization', 'and', 'a', 'multi', 
 'planet', 'species', 'by', 'building', 'a', 'self', 'sustaining', 'city', 'on', 
 'mars', 'in', '2008', 'spacex’s', 'falcon', '1', 'became', 'the', 'first', 
 'privately', 'developed', 'liquid', 'fuel', 'launch', 'vehicle', 'to', 'orbit', 
 'the', 'earth']

Keras在进行标记之前将所有字母转换成小写。你可以想象,这为我们节省了很多时间!

6.使用Gensim进行标识化

我们介绍的最后一个标识化方法是使用Gensim库。它是一个用于无监督主题建模和自然语言处理的开源库,旨在从给定文档中自动提取语义主题。

下面我们在机器上安装Gensim:

pip install gensim

我们可以用gensim.utils类导入用于执行单词标识化的tokenize方法。

单词标识化

from gensim.utils import tokenize
text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
liquid-fuel launch vehicle to orbit the Earth."""
list(tokenize(text))
Outpur : ['Founded', 'in', 'SpaceX', 's', 'mission', 'is', 'to', 'enable', 'humans', 'to', 
 'become', 'a', 'spacefaring', 'civilization', 'and', 'a', 'multi', 'planet', 
 'species', 'by', 'building', 'a', 'self', 'sustaining', 'city', 'on', 'Mars', 
 'In', 'SpaceX', 's', 'Falcon', 'became', 'the', 'first', 'privately', 
 'developed', 'liquid', 'fuel', 'launch', 'vehicle', 'to', 'orbit', 'the', 
 'Earth']

句子标识化

from gensim.summarization.textcleaner import split_sentences
text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
liquid-fuel launch vehicle to orbit the Earth."""
result = split_sentences(text)
result
Output : ['Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring 
 civilization and a multi-planet ', 
 'species by building a self-sustaining city on Mars.', 
 'In 2008, SpaceX’s Falcon 1 became the first privately developed ', 
 'liquid-fuel launch vehicle to orbit the Earth.']

你可能已经注意到,Gensim对标点符号非常严格。每当遇到标点符号时,它就会分割。在句子分割中,Gensim在遇到\n时会分割文本,而其他库则是忽略它。

总结

标识化是整个处理NLP任务中的一个关键步骤。如果不先处理文本,我们就不能简单地进入模型构建部分。

在本文中,对于给定的英文文本,我们使用了六种不同的标识化方法(单词和句子)。当然,还有其他的方法,但是这些方法已经足够让你开始进行标识化了。

相关推荐

为何越来越多的编程语言使用JSON(为什么编程)

JSON是JavascriptObjectNotation的缩写,意思是Javascript对象表示法,是一种易于人类阅读和对编程友好的文本数据传递方法,是JavaScript语言规范定义的一个子...

何时在数据库中使用 JSON(数据库用json格式存储)

在本文中,您将了解何时应考虑将JSON数据类型添加到表中以及何时应避免使用它们。每天?分享?最新?软件?开发?,Devops,敏捷?,测试?以及?项目?管理?最新?,最热门?的?文章?,每天?花?...

MySQL 从零开始:05 数据类型(mysql数据类型有哪些,并举例)

前面的讲解中已经接触到了表的创建,表的创建是对字段的声明,比如:上述语句声明了字段的名称、类型、所占空间、默认值和是否可以为空等信息。其中的int、varchar、char和decimal都...

JSON对象花样进阶(json格式对象)

一、引言在现代Web开发中,JSON(JavaScriptObjectNotation)已经成为数据交换的标准格式。无论是从前端向后端发送数据,还是从后端接收数据,JSON都是不可或缺的一部分。...

深入理解 JSON 和 Form-data(json和formdata提交区别)

在讨论现代网络开发与API设计的语境下,理解客户端和服务器间如何有效且可靠地交换数据变得尤为关键。这里,特别值得关注的是两种主流数据格式:...

JSON 语法(json 语法 priority)

JSON语法是JavaScript语法的子集。JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔花括号保存对象方括号保存数组JS...

JSON语法详解(json的语法规则)

JSON语法规则JSON语法是JavaScript对象表示法语法的子集。数据在名称/值对中数据由逗号分隔大括号保存对象中括号保存数组注意:json的key是字符串,且必须是双引号,不能是单引号...

MySQL JSON数据类型操作(mysql的json)

概述mysql自5.7.8版本开始,就支持了json结构的数据存储和查询,这表明了mysql也在不断的学习和增加nosql数据库的有点。但mysql毕竟是关系型数据库,在处理json这种非结构化的数据...

JSON的数据模式(json数据格式示例)

像XML模式一样,JSON数据格式也有Schema,这是一个基于JSON格式的规范。JSON模式也以JSON格式编写。它用于验证JSON数据。JSON模式示例以下代码显示了基本的JSON模式。{"...

前端学习——JSON格式详解(后端json格式)

JSON(JavaScriptObjectNotation)是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。它基于JavaScriptProgrammingLa...

什么是 JSON:详解 JSON 及其优势(什么叫json)

现在程序员还有谁不知道JSON吗?无论对于前端还是后端,JSON都是一种常见的数据格式。那么JSON到底是什么呢?JSON的定义...

PostgreSQL JSON 类型:处理结构化数据

PostgreSQL提供JSON类型,以存储结构化数据。JSON是一种开放的数据格式,可用于存储各种类型的值。什么是JSON类型?JSON类型表示JSON(JavaScriptO...

JavaScript:JSON、三种包装类(javascript 包)

JOSN:我们希望可以将一个对象在不同的语言中进行传递,以达到通信的目的,最佳方式就是将一个对象转换为字符串的形式JSON(JavaScriptObjectNotation)-JS的对象表示法...

Python数据分析 只要1分钟 教你玩转JSON 全程干货

Json简介:Json,全名JavaScriptObjectNotation,JSON(JavaScriptObjectNotation(记号、标记))是一种轻量级的数据交换格式。它基于J...

比较一下JSON与XML两种数据格式?(json和xml哪个好)

JSON(JavaScriptObjectNotation)和XML(eXtensibleMarkupLanguage)是在日常开发中比较常用的两种数据格式,它们主要的作用就是用来进行数据的传...

取消回复欢迎 发表评论:

请填写验证码