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

NLP入门第一步6种独特的数据标记方式

toyiye 2024-06-21 12:43 10 浏览 0 评论

全文共10818字,预计学习时长21分钟


你是否对互联网上大量可用的文本数据量着迷?你是否正在寻找使用该文本数据的方法,但不知道从何下手?毕竟,机器只能识别数字,而不是人类语言中的字母。在机器学习中,这是亟待解决的棘手问题。


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


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


? 词标记(也称分词)

? 预测每个token的词性

? 文本词形还原

? 识别和删除停用词等等

本文将讨论第一步——标记。首先看看什么是标记以及NLP中需要标记的原因,并了解在Python中标记数据的六种独特方法。


本文不设前提条件,任何对NLP或数据科学感兴趣的人都能上手。


目录


1. NLP中的标记指的是什么?

2. 为什么NLP中需要标记?

3. 在Python中执行标记的不同方法

3.1 使用Python split()函数进行标记

3.2 使用正则表达式进行标记

3.3 使用NLTK进行标记

3.4 使用Spacy进行标记

3.5 使用Keras进行标记

3.6 使用Gensim进行标记



1. NLP中的标记指的是什么?


在处理文本数据时,标记是最常见的任务之一。但“标记”一词究竟意味着什么呢?

标记实质上是将句子、段落或整个文本文档分成较小的单元,例如单个单词或术语。每一个较小的单元都称为“token”。

通过下面图像,可以更直观地了解该定义:


token可以是单词、数字或标点符号。在分词中,通过定位词边界来创建较小的单元。那么词边界是什么?


词边界是一个单词的结束点和下一个单词的开头。这些token被认为是词干化和词形还原的第一步。


2. 为什么NLP中需要标记?


在此应考虑一下英语语言的特性。说出能想到的任何句子,并在阅读本节时牢记这一点。这有助于用更简单的方式理解标记的重要性。


在处理文本数据之前需要识别构成一串字符的单词,因此标记是继续使用NLP(文本数据)的最基本步骤。这很重要,因为通过分析文本中的单词可以很容易地解释文本。


举个例子,思考以下字符串:


“This is a cat.”


在标记该字符串后会发生什么?可得到['This','is','a',cat']。


这样做有很多用途,可以使用这种标记形式:

? 计算文本中的单词数

? 计算单词的频率,即特定单词的出现次数


除此之外,还可以提取更多信息。现在,是时候深入了解在NLP中标记数据的不同方法。


3. 在Python中标记的方法


本文介绍了标记文本数据的六种独特方式,并为每种方法提供了Python代码,仅供参考。

3.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."""
# Splits at space 
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."""
# Splits at '.' 
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()并未将标点符号视为单独的标记。


3.2 使用正则表达式(RegEx)进行标记


首先要理解什么是正则表达式。正则表达式基本上是一个特殊的字符序列,可以帮助匹配文本

可以使用Python中的RegEx库来处理正则表达式,该库预装了Python安装包。


现在,记住用RegEx实现词标记和句子标记。


词标记

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.3 使用NLTK进行标记


如果经常和文本数据打交道,则应使用NLTK库。NLTK是Natural Language ToolKit的缩写,是一个用Python编写的用于符号和统计自然语言处理的库。

可使用以下代码安装NLTK:

pip install --user -U nltk


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

? 词标记:使用word_tokenize()方法将句子拆分为token或单词

? 句子标记:使用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是如何将标点符号视为token了吗?对于之后的任务,需要从初始列表中删除标点符号。


句子标记

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.']


3.4 使用spaCy库进行标记


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

在Linux中安装Spacy:

pip install -U spacy

python -m spacy download en


若在其他操作系统上安装,点击该网址https://spacy.io/usage可查看更多。


那么,现在看看如何使用spaCY的强大功能来执行标记,spacy.lang.enwhich支持英语。


词标记

from spacy.lang.en import English
# Load English tokenizer, tagger, parser, NER and word vectors
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" Object is used to create documents with linguistic annotations.
my_doc = nlp(text)
# Create list of word tokens
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
# Load English tokenizer, tagger, parser, NER and word vectors
nlp = English()
# Create the pipeline 'sentencizer' component
sbd = nlp.create_pipe('sentencizer')
# Add the component to the pipeline
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" Object is used to create documents with linguistic annotations.
doc = nlp(text)
# create list of sentence tokens
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快)。可以通过 DataHack Radio了解如何创建spaCy以及可以在何处使用:


? DataHack Radio #23: Ines Montani and Matthew Honnibal – The Brains behind spaCy

传送门:https://www.analyticsvidhya.com/blog/2019/06/datahack-radio-ines-montani-matthew-honnibal-brains-behind-spacy/?utm_source=blog&utm_medium=how-get-started-nlp-6-unique-ways-perform-tokenization


以下是一个深入的spaCy入门教程:

? Natural Language Processing Made Easy – using SpaCy (in Python)

传送门:https://www.analyticsvidhya.com/blog/2017/04/natural-language-processing-made-easy-using-spacy-%E2%80%8Bin-python/?utm_source=blog&utm_medium=how-get-started-nlp-6-unique-ways-perform-tokenization


3.5 使用Keras进行标记


Keras现在是业内最热门的深度学习框架之一,是Python的开源神经网络库。Keras易于使用,可以在TensorFlow上运行。


在NLP环境中,可以使用Keras来清理平时收集的非结构化文本数据。

只需一行代码就可以在机器上安装Keras:

pip install Keras


Keras词标记使用keras.preprocessing.text类中的text_to_word_sequence方法。


词标记

from keras.preprocessing.text import text_to_word_sequence
# define
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."""
# tokenize
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会把所有字母变成小写,这可以节省相当多的时间。


3.6 使用Gensim进行标记


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


以下是安装Gensim的方法:

pip install gensim


可以使用gensim.utils类导入用于执行词标记的方法。


词标记

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))


Output : ['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']


句子标记


句子标记使用gensim.summerization.texttcleaner类中的split_sentences方法:


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”对文本进行标记,而其它库通常忽略“\n”。


留言 点赞 关注

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

编译组:陈枫、蒋馨怡

相关链接:

https://www.analyticsvidhya.com/blog/2019/07/how-get-started-nlp-6-unique-ways-perform-tokenization/

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

相关推荐

为何越来越多的编程语言使用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)是在日常开发中比较常用的两种数据格式,它们主要的作用就是用来进行数据的传...

取消回复欢迎 发表评论:

请填写验证码