1.  

介绍

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

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

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

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

阅读本文不需要什么先决条件,任何对NLP或数据科学感兴趣的人都可以跟读。

在NLP中,什么是标识化?

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

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

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

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

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

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

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

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

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

“This is a cat.”

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

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

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

在Python中执行标识化的方法

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

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

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

单词标识化

  1. 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() 
  2. # 以空格为分隔符进行分割
  3. text.split() 
  1. 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.']'in', '2002,', 'SpaceX’s', 'mission', 'is', 'to', 'enable', 'humans', 
  2.           'to', 'become', 'a', 'spacefaring', 'civilization', 'and', 'a', 'multi-planet', 
  3.           'species', 'by', 'building', 'a', 'self-sustaining', 'city', 'on', 'Mars.', 'In', 
  4.           '2008,', 'SpaceX’s', 'Falcon', '1', 'became', 'the', 'first', 'privately', 
  5.           'developed', 'liquid-fuel', 'launch', 'vehicle', 'to', 'orbit', 'the', 'Earth.']

句子标识化:

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

  1. 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('. ') 
  2. # 以"."作为分割符进行分割 
  3. text.split('. ') 
  1. 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.']
  2.           'In 2008, SpaceX’s Falcon 1 became the first privately developed \nliquid-fuel 
  3.            launch vehicle to orbit the Earth.']

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

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

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

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

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

单词标识化

  1. import retext = """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
  2. text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
  3. species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
  4. liquid-fuel launch vehicle to orbit the Earth."""
  5. tokens = re.findall("[\w']+", text)
  6. tokens
  1. 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']'in', '2002', 'SpaceX', 's', 'mission', 'is', 'to', 'enable', 
  2.           'humans', 'to', 'become', 'a', 'spacefaring', 'civilization', 'and', 'a', 
  3.           'multi', 'planet', 'species', 'by', 'building', 'a', 'self', 'sustaining', 
  4.           'city', 'on', 'Mars', 'In', '2008', 'SpaceX', 's', 'Falcon', '1', 'became', 
  5.           'the', 'first', 'privately', 'developed', 'liquid', 'fuel', 'launch', 'vehicle', 
  6.           'to', 'orbit', 'the', 'Earth']

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

句子标识化

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

  1. import retext = """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
  2. text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
  3. species by building a self-sustaining city on, Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
  4. liquid-fuel launch vehicle to orbit the Earth."""
  5. sentences = re.compile('[.!?] ').split(text)
  6. sentences
  1. 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.']
  2.           'In 2008, SpaceX’s Falcon 1 became the first privately developed \nliquid-fuel 
  3.            launch vehicle to orbit the Earth.']

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

有兴趣阅读更多关于正则表达式的信息吗?下面的参考资料将帮助你开始学习NLP中的正则表达式:

  • https://www.analyticsvidhya.com/blog/2015/06/regular-expression-python/

  • https://www.analyticsvidhya.com/blog/2017/03/extracting-information-from-reports-using-regular-expressons-library-in-python/

3.使用NLTK进行标识化

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

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

pip install --user -U nltk

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

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

单词标识化

  1. 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)import word_tokenize 
  2. text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
  3. species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
  4. liquid-fuel launch vehicle to orbit the Earth."""
  5. word_tokenize(text)
  1. 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', '.']'in', '2002', ',', 'SpaceX', '’', 's', 'mission', 'is', 'to', 'enable', 
  2.          'humans', 'to', 'become', 'a', 'spacefaring', 'civilization', 'and', 'a', 
  3.          'multi-planet', 'species', 'by', 'building', 'a', 'self-sustaining', 'city', 'on', 
  4.          'Mars', '.', 'In', '2008', ',', 'SpaceX', '’', 's', 'Falcon', '1', 'became', 
  5.          'the', 'first', 'privately', 'developed', 'liquid-fuel', 'launch', 'vehicle', 
  6.          'to', 'orbit', 'the', 'Earth', '.']

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

句子标识化

  1. from nltk.tokenize import sent_tokenizetext = """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)import sent_tokenize
  2. text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
  3. species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
  4. liquid-fuel launch vehicle to orbit the Earth."""
  5. sent_tokenize(text)
  1. 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.']
  2.          'In 2008, SpaceX’s Falcon 1 became the first privately developed \nliquid-fuel 
  3.           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以支持英文。

单词标识化

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

句子标识化

  1. 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_listimport English
  2. # 加载英文分词器,标记器、解析器、命名实体识别和词向量
  3. nlp = English()
  4. # 创建管道 'sentencizer' 组件
  5. sbd = nlp.create_pipe('sentencizer')
  6. # 将组建添加到管道中
  7. nlp.add_pipe(sbd)
  8. text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
  9. species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
  10. liquid-fuel launch vehicle to orbit the Earth."""
  11. #  "nlp" 对象用于创建具有语言注解的文档
  12. doc = nlp(text)
  13. # 创建句子标识符列表
  14. sents_list = []
  15. for sent in doc.sents:
  16.     sents_list.append(sent.text)
  17. sents_list
  1. 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.']
  2.           'In 2008, SpaceX’s Falcon 1 became the first privately developed \nliquid-fuel 
  3.            launch vehicle to orbit the Earth.']

在执行NLP任务时,与其他库相比,spaCy的速度相当快(是的,甚至相较于NLTK)。我鼓励你收听下面的DataHack Radio播客,以了解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的一个更深入的教程:

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

5.使用Keras进行标识化

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

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

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

pip install Keras

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

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

单词标识化

  1. 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)resultimport text_to_word_sequence
  2. # 文本数据
  3. text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
  4. species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
  5. liquid-fuel launch vehicle to orbit the Earth."""
  6. # 标识化
  7. result = text_to_word_sequence(text)
  8. result
  1. 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']'in', '2002', 'spacex’s', 'mission', 'is', 'to', 'enable', 'humans', 
  2.           'to', 'become', 'a', 'spacefaring', 'civilization', 'and', 'a', 'multi', 
  3.           'planet', 'species', 'by', 'building', 'a', 'self', 'sustaining', 'city', 'on', 
  4.           'mars', 'in', '2008', 'spacex’s', 'falcon', '1', 'became', 'the', 'first', 
  5.           'privately', 'developed', 'liquid', 'fuel', 'launch', 'vehicle', 'to', 'orbit', 
  6.           'the', 'earth']

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

6.使用Gensim进行标识化

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

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

pip install gensim

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

单词标识化

  1. from gensim.utils import tokenizetext = """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))import tokenize
  2. text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
  3. species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
  4. liquid-fuel launch vehicle to orbit the Earth."""
  5. list(tokenize(text))
  1. 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']'in', 'SpaceX', 's', 'mission', 'is', 'to', 'enable', 'humans', 'to', 
  2.           'become', 'a', 'spacefaring', 'civilization', 'and', 'a', 'multi', 'planet', 
  3.           'species', 'by', 'building', 'a', 'self', 'sustaining', 'city', 'on', 'Mars', 
  4.           'In', 'SpaceX', 's', 'Falcon', 'became', 'the', 'first', 'privately', 
  5.           'developed', 'liquid', 'fuel', 'launch', 'vehicle', 'to', 'orbit', 'the', 
  6.           'Earth']

句子标识化

  1. from gensim.summarization.textcleaner import split_sentencestext = """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)resultimport split_sentences
  2. text = """Founded in 2002, SpaceX’s mission is to enable humans to become a spacefaring civilization and a multi-planet 
  3. species by building a self-sustaining city on Mars. In 2008, SpaceX’s Falcon 1 became the first privately developed 
  4. liquid-fuel launch vehicle to orbit the Earth."""
  5. result = split_sentences(text)
  6. result
  1. 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.']
  2.           'species by building a self-sustaining city on Mars.', 
  3.           'In 2008, SpaceX’s Falcon 1 became the first privately developed ', 
  4.           'liquid-fuel launch vehicle to orbit the Earth.']

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

总结

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

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

[1]: 有部分中文将其翻译为分词,但中文文本和英文文本在分词上有所差别,且在本文中,不只演示将英文文本段落分割成单词,还演示将其分割成句子,所以在本文中将其翻译为标识化而不是分词。

欢迎关注磐创博客资源汇总站:

http://docs.panchuang.net/

欢迎关注PyTorch官方中文教程站:

http://pytorch.panchuang.net/

NLPer入门指南 | 完美第一步的更多相关文章

  1. 【OpenCV入门指南】第一篇 安装OpenCV

    http://blog.csdn.net/morewindows/article/details/8225783/ win10下vs2015配置Opencv3.1.0过程详解(转) http://ww ...

  2. OKR新手入门指南 (第一部分)

    什么是OKR? OKR(目标和关键结果)是Google和其他公司使用的目标系统.这是一个简单的工具,围绕可衡量的目标进行调整和互动. OKR:Google的目标设定方法 与传统的规划方法有何不同? O ...

  3. github入门教程:第一步

    [git教程] 以前在网上找过一些,见 http://www.wojilu.com/Forum1/Topic/702  我自己会一边学,一边写教程,过程中有不明白的,会跟大家请教交流.   ----- ...

  4. opencv入门指南(转载)

    转载链接:http://blog.csdn.net/morewindows/article/details/8426318 网上的总结的一些用openncv的库来做的事: 下面列出OpenCV入门指南 ...

  5. Kaggle初学者五步入门指南,七大诀窍助你享受竞赛

    Kaggle 是一个流行的数据科学竞赛平台,已被谷歌收购,参阅<业界 | 谷歌云官方正式宣布收购数据科学社区 Kaggle>.作为一个竞赛平台,Kaggle 对于初学者来说可能有些难度.毕 ...

  6. [EntLib]微软企业库5.0 学习之路——第一步、基本入门

    话说在大学的时候帮老师做项目的时候就已经接触过企业库了但是当初一直没明白为什么要用这个,只觉得好麻烦啊,竟然有那么多的乱七八糟的配置(原来我不知道有配置工具可以进行配置,请原谅我的小白). 直到去年在 ...

  7. (大数据工程师学习路径)第一步 Linux 基础入门----文件系统操作与磁盘管理

    介绍 本节的文件系统操作的内容十分简单,只会包含几个命令的几个参数的讲解,但掌握这些也将对你在学习后续其他内容的过程中有极大帮助. 因为本课程的定位为入门基础,尽快上手,故没有打算涉及太多理论内容,前 ...

  8. Django入门第一步:构建一个简单的Django项目

    Django入门第一步:构建一个简单的Django项目 1.简介 Django是一个功能完备的Python Web框架,可用于构建复杂的Web应用程序.在本文中,将通过示例跳入并学习Django.您将 ...

  9. Newbe.Claptrap 框架入门,第一步 —— 开发环境准备

    Newbe.Claptrap 框架依托于一些关键性的基础组件和一些可选的辅助组件.本篇我们来介绍一下如何准备一个开发环境. Newbe.Claptrap 是一个用于轻松应对并发问题的分布式开发框架.如 ...

随机推荐

  1. CSS——NO.2(CSS样式的基本知识)

    */ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...

  2. C++扬帆远航——10(求π)

    /* * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:π.cpp * 作者:常轩 * 微信公众号:Worldhello ...

  3. windows下用Python把pdf文件转化为图片

    依赖:PyMuPDF(pip install pymupdf) # -*- coding: utf-8 -*- """ 1.安装库 pip install pymupdf ...

  4. volatile和synchronized到底啥区别?多图文讲解告诉你

    你有一个思想,我有一个思想,我们交换后,一个人就有两个思想 If you can NOT explain it simply, you do NOT understand it well enough ...

  5. 正式学习MVC 01

    1.新建项目 点击创建新项目,选择ASP.NET web应用程序,对项目进行命名后点击创建. 截图如下: 取消勾选HTTPS配置 可选择空 + mvc 或直接选定MVC 2.目录结构分析 1) App ...

  6. js监听离开或刷新页面时的弹窗提示

    一.看图 二.使用场景. 填写表单时内容,当离开页面或者刷新的时候回丢失页面的内容,因此人性化的设计该有一个提示.所以这样的功能也就应用而生了. 三.思路. 1,页面内容改变.2,离开或刷新浏览器触发 ...

  7. 零基础HTML及CSS编码总结

    任务目的 针对设计稿样式进行合理的HTML架构,包括以下但不限于: * 掌握常用HTML标签的含义.用法 能够基于设计稿来合理规划HTML文档结构 理解语义化,合理地使用HTML标签来构建页面 掌握基 ...

  8. JS模块规范:AMD,CMD,CommonJS

    浅析JS模块规范 随着JS模块化编程的发展,处理模块之间的依赖关系成为了维护的关键. AMD,CMD,CommonJS是目前最常用的三种模块化书写规范. CommonJS CommonJS规范是诞生比 ...

  9. 如何在Flutter中使用flutter_markdown

    很多博客,论坛都支持markdown语法,flutter也有支持markdown语法的插件flutter_markdown 安装依赖 dependencies: flutter: sdk: flutt ...

  10. cocos2d-x android 入门

    前一段时间使用传统方式做了一个CS软件,发现 UI 显示的比较慢,突发奇起,开始研究起来 GPU 加速,最后开始学习 cocos2dx. 开发环境以最新的 Cocos2d-x 3.17.1 Andro ...