天天看點

單元識别碼是什麼意思_NLPer入門指南 | 完美第一步

介紹

你對網際網路上的大量文本資料着迷嗎?你是否正在尋找處理這些文本資料的方法,但不确定從哪裡開始?畢竟,機器識别的是數字,而不是我們語言中的字母。在機器學習中,這可能是一個棘手的問題。

那麼,我們如何操作和處理這些文本資料來構模組化型呢?答案就在自然語言處理(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()是以一個或多個空格作為分隔符。我們可以把分隔符換成任何東西。讓我們來看看。

單詞辨別化:

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()函數,并傳遞一個模式[.?!]。這意味着一旦遇到這些字元,句子就會被分割開來。

有興趣閱讀更多關于正規表達式的資訊嗎?下面的參考資料将幫助你開始學習NLP中的正規表達式:

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

要在其他作業系統上安裝它,可以通過下面連結檢視:

是以,讓我們看看如何利用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)。我鼓勵你收聽下面的DataHack Radio播客,以了解spaCy是如何建立的,以及你可以在哪裡使用它:

之外,下面是關于spaCy的一個更深入的教程:

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任務中的一個關鍵步驟。如果不先處理文本,我們就不能簡單地進入模型建構部分。

在本文中,對于給定的英文文本,我們使用了六種不同的辨別化方法(單詞和句子)。當然,還有其他的方法,但是這些方法已經足夠讓你開始進行辨別化了。

[1]: 有部分中文将其翻譯為分詞,但中文文本和英文文本在分詞上有所差别,且在本文中,不隻示範将英文文本段落分割成單詞,還示範将其分割成句子,是以在本文中将其翻譯為辨別化而不是分詞。