閱讀1013 返回首頁    go iPhone_iPad_Mac_apple


秒懂!看機器學習如何淨化處理文本

更多深度文章,請關注:https://yq.aliyun.com/cloud

你不能直接把原始文本提交給機器學習或深層學習模型,而必須首先對文本進行淨化,也就是將文本分解成單詞,以及處理標點符號和大小寫。

事實上,你需要使用一整套的文本預處理方法,而且這個方法的選擇取決於你需要對自然語言做何種處理。

在本教程中,你將學到如何為機器學習建模而淨化和處理文本,包括:

  • 如何開發簡單的文本淨化工具。
  • 如何使用NLTK庫中更複雜的方法。
  • 在使用現代文字表示方法時如何處理文本。

讓我們開始吧。

機器學習文本淨化處理
照片出自changehali,保留部分權利。

教程概述

本教程包含六個部分,分別為:

  1. 弗蘭茨·卡夫卡的《變形記》
  2. 文本淨化是一件依賴於具體任務的工作
  3. 手動標記
  4. 使用NLTK進行標記和淨化
  5. 文本淨化注意事項

弗蘭茨·卡夫卡的《變形記》

首先選擇一個數據集。

本教程使用了弗蘭茨·卡夫卡《變形記》一書中的文字。選這本書中的文字並沒有什麼具體的原因,除了它比較短以外。我很喜歡這本書,希望你也會喜歡。我期望它是學生們必讀的經典之作之一。

《變形記》全文可以從Gutenberg項目免費獲得。

你也可以在這裏下載ASCII文本版:

下載該文件,並將其放在你當前的工作目錄中,文件名為“*metamorphosis.txt*“。

該文件包含了我們不感興趣的頁眉和頁腳,特別是版權和授權信息。請打開文件,刪除頁眉和頁腳,並將文件另存為“*metamorphosis_clean.txt*“。

這個幹淨文件的開頭應該是這樣的:

One morning, when Gregor Samsa woke from troubled dreams, he found himself transformed in his bed into a horrible vermin.

文件的結尾應該是這樣的:

And, as if in confirmation of their new dreams and good intentions, as soon as they reached their destination Grete was the first to get up and stretch out her young body.

文本淨化是一件依賴於具體任務的工作

在拿到了文本數據之後,清理文本數據的第一步是為了讓你對要實現的目標有一個清晰的概念。

仔細看看這篇文章。你能注意到哪些東西?

這是我發現的:

  • 它是純文本,所以沒有標記需要解析。
  • 原始德語的翻譯使用的是英國英語(例如“*travelling*“)。
  • 文本被人工斷行,每一行70個字符。
  • 沒有明顯的打印錯誤或拚寫錯誤。
  • 存在一些標點符號,如逗號、撇號、引號、問號等等。
  • 存在連字符,如“armour-like”。
  • 有很多地方都用破折號(“ - ”)來繼續句子(是否可以用逗號替代)?
  • 存在很多人名(例如“*Mr. Samsa*”)
  • 似乎沒有需要處理的數字(例如1999)
  • 存在段標記字符(例如“II”和“III”),之前已經刪除了第一個“I”。

我相信有很多雙訓練有素的眼睛能觀察到這些細節問題。

下文將展示最常見的文本淨化步驟。不過,請思考一下在處理這個文本文件時可能會遇到的一些小問題。

例如:

  • 如果對開發Kafkaesque語言模型感興趣,那麼可以保留所有的大小寫、引號和其他標點符號。
  • 如果對將文檔歸類為“*Kafka*”和“*非Kafka*”感興趣,也許可以去掉大小寫、標點符號。

請根據你要完成的任務來選擇如何處理文本數據。

手動標記

文字淨化很難,而本教程使用的文字已經很幹淨了。

我們可以編寫一些Python代碼來手動淨化它,這對於遇到的那些簡單問題來說是一個不錯的處理方法。而諸如正則表達式和分割字符串的工具則可能需要耗費你較多的時間。

1. 加載數據

現在我們來加載文本數據吧。

這個文本文件很小,加載到內存的速度很快。但並不是所有的文本文件都會這麼小,你可能需要寫代碼將內存映射到文件上。像NLTK這樣的工具(下一節將介紹)能簡化對大型文件的處理。

將“*metamorphosis_clean.txt*”整個加載到內存中,如下所示:

# load text
filename = 'metamorphosis_clean.txt'
file = open(filename, 'rt')
text = file.read()
file.close()

運行該示例將整個文件加載到內存中。

2. 按空格分隔

幹淨的文本通常意味著可以在機器學習模型中使用的單詞或標記列表。因此,我們需要將原始文本轉換為單次列表,並保存下來。

最簡單的方法就是將文檔按空格進行分割,包括引號、新的一行、製表符等等。我們可以在Python中對加載的字符串使用split()函數。

# load text
filename = 'metamorphosis_clean.txt'
file = open(filename, 'rt')
text = file.read()
file.close()
# split into words by white space
words = text.split()
print(words[:100])

運行這個示例可以將文檔分割成一個很長的列表,然後打印前100個元素。

可以看到,標點符號被保留下來了(例如“*wasn’t*”和“*armour-like*”),這很好。還還可以看到,句尾的標點符號與最後一個單詞放在了一起,沒有分割開(例如“*thought*.”),這不太好。

['One', 'morning,', 'when', 'Gregor', 'Samsa', 'woke', 'from', 'troubled', 'dreams,', 'he', 'found', 'himself', 'transformed', 'in', 'his', 'bed', 'into', 'a', 'horrible', 'vermin.', 'He', 'lay', 'on', 'his', 'armour-like', 'back,', 'and', 'if', 'he', 'lifted', 'his', 'head', 'a', 'little', 'he', 'could', 'see', 'his', 'brown', 'belly,', 'slightly', 'domed', 'and', 'divided', 'by', 'arches', 'into', 'stiff', 'sections.', 'The', 'bedding', 'was', 'hardly', 'able', 'to', 'cover', 'it', 'and', 'seemed', 'ready', 'to', 'slide', 'off', 'any', 'moment.', 'His', 'many', 'legs,', 'pitifully', 'thin', 'compared', 'with', 'the', 'size', 'of', 'the', 'rest', 'of', 'him,', 'waved', 'about', 'helplessly', 'as', 'he', 'looked.', '"What\'s', 'happened', 'to', 'me?"', 'he', 'thought.', 'It', "wasn't", 'a', 'dream.', 'His', 'room,', 'a', 'proper', 'human']

3. 選擇單詞

另一種方法是使用正則表達式模型,並通過使用字母數字過濾字符串(a-z,A-Z,0-9和‘_’)將文檔分割成單詞。

例如:

# load text
filename = 'metamorphosis_clean.txt'
file = open(filename, 'rt')
text = file.read()
file.close()
# split based on words only
import re
words = re.split(r'\W+', text)
print(words[:100])

運行該示例,可以看到最終的單詞列表。 這一次,“*armour-like*”變成了兩個單詞“*armour*”和“*like*”(很好),但縮略詞,像“*What’s*”也變成了兩個單詞“*What*”和“*s*”(不是很好)。

['One', 'morning', 'when', 'Gregor', 'Samsa', 'woke', 'from', 'troubled', 'dreams', 'he', 'found', 'himself', 'transformed', 'in', 'his', 'bed', 'into', 'a', 'horrible', 'vermin', 'He', 'lay', 'on', 'his', 'armour', 'like', 'back', 'and', 'if', 'he', 'lifted', 'his', 'head', 'a', 'little', 'he', 'could', 'see', 'his', 'brown', 'belly', 'slightly', 'domed', 'and', 'divided', 'by', 'arches', 'into', 'stiff', 'sections', 'The', 'bedding', 'was', 'hardly', 'able', 'to', 'cover', 'it', 'and', 'seemed', 'ready', 'to', 'slide', 'off', 'any', 'moment', 'His', 'many', 'legs', 'pitifully', 'thin', 'compared', 'with', 'the', 'size', 'of', 'the', 'rest', 'of', 'him', 'waved', 'about', 'helplessly', 'as', 'he', 'looked', 'What', 's', 'happened', 'to', 'me', 'he', 'thought', 'It', 'wasn', 't', 'a', 'dream', 'His', 'room']

3. 按空格分割並刪除標點符號

注意:本示例是用Python 3編寫的。

我們想要的是單詞,而不是標點符號,比如逗號或引號。我們也希望縮略詞不要被分割開。

一種方法是將文檔按空格進行分割(在“2. 按空格分隔”中提到的),然後使用字符串轉換來替換所有標點符號(例如刪除標點符號)。

Python提供了一個名為*string.punctuation*的常量,它是所有標點符號列表。例如:

print(string.punctuation)

結果是:

!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

Python提供了一個名為translate()的函數,它可以將一組字符映射到另一組。

可以使用函數maketrans()來創建一個映射表。這個函數的第三個參數用於列出在翻譯過程中要刪除的所有字符。例如:

table = str.maketrans('', '', string.punctuation)

我們可以將上麵這些代碼放在一起,加載文本文件,將其按空格分割成單詞,然後轉換每個單詞以刪除標點符號。

# load text
filename = 'metamorphosis_clean.txt'
file = open(filename, 'rt')
text = file.read()
file.close()
# split into words by white space
words = text.split()
# remove punctuation from each word
import string
table = str.maketrans('', '', string.punctuation)
stripped = [w.translate(table) for w in words]
print(stripped[:100])

在通常情況下,可以看到這已經達到了預期的效果。

諸如“*What’s*”這樣的縮略語已經變成了“*Whats*”,而“*armour-like*”已經變成了“*armourlike*”。

['One', 'morning', 'when', 'Gregor', 'Samsa', 'woke', 'from', 'troubled', 'dreams', 'he', 'found', 'himself', 'transformed', 'in', 'his', 'bed', 'into', 'a', 'horrible', 'vermin', 'He', 'lay', 'on', 'his', 'armourlike', 'back', 'and', 'if', 'he', 'lifted', 'his', 'head', 'a', 'little', 'he', 'could', 'see', 'his', 'brown', 'belly', 'slightly', 'domed', 'and', 'divided', 'by', 'arches', 'into', 'stiff', 'sections', 'The', 'bedding', 'was', 'hardly', 'able', 'to', 'cover', 'it', 'and', 'seemed', 'ready', 'to', 'slide', 'off', 'any', 'moment', 'His', 'many', 'legs', 'pitifully', 'thin', 'compared', 'with', 'the', 'size', 'of', 'the', 'rest', 'of', 'him', 'waved', 'about', 'helplessly', 'as', 'he', 'looked', 'Whats', 'happened', 'to', 'me', 'he', 'thought', 'It', 'wasnt', 'a', 'dream', 'His', 'room', 'a', 'proper', 'human']

4. 規範大小寫

將所有單詞轉換為統一的大小寫很常見。這能減少單詞量,但也會丟失某些差異(例如,“*Apple*”公司和“*apple*”水果是最常見的例子)。

可以對每個單詞調用lower()函數來將所有的單詞轉換為小寫。

例如:

filename = 'metamorphosis_clean.txt'
file = open(filename, 'rt')
text = file.read()
file.close()
# split into words by white space
words = text.split()
# convert to lower case
words = [word.lower() for word in words]
print(words[:100])

運行示例,可以看到所有的單詞現在都變成小寫了。

['one', 'morning,', 'when', 'gregor', 'samsa', 'woke', 'from', 'troubled', 'dreams,', 'he', 'found', 'himself', 'transformed', 'in', 'his', 'bed', 'into', 'a', 'horrible', 'vermin.', 'he', 'lay', 'on', 'his', 'armour-like', 'back,', 'and', 'if', 'he', 'lifted', 'his', 'head', 'a', 'little', 'he', 'could', 'see', 'his', 'brown', 'belly,', 'slightly', 'domed', 'and', 'divided', 'by', 'arches', 'into', 'stiff', 'sections.', 'the', 'bedding', 'was', 'hardly', 'able', 'to', 'cover', 'it', 'and', 'seemed', 'ready', 'to', 'slide', 'off', 'any', 'moment.', 'his', 'many', 'legs,', 'pitifully', 'thin', 'compared', 'with', 'the', 'size', 'of', 'the', 'rest', 'of', 'him,', 'waved', 'about', 'helplessly', 'as', 'he', 'looked.', '"what\'s', 'happened', 'to', 'me?"', 'he', 'thought.', 'it', "wasn't", 'a', 'dream.', 'his', 'room,', 'a', 'proper', 'human']

注意

淨化文本真的很難,需要具體問題具體分析。記住,越簡單越好。文本數據越簡單,則模型越簡單,詞匯表更小。

下麵來看一下NLTK庫中的一些工具,可不僅僅是簡單的字符串拆分哦。

使用NLTK進行標記和淨化

Natural Language Toolkit(自然語言工具包)或簡稱NLTK是一個對文本進行處理和建模的Python庫。

它提供了不錯的加載和淨化文本工具,我們可以用這些工具來為機器學習和深度學習算法獲取數據。

1. 安裝 NLTK

你可以使用你最喜歡的軟件包管理器來安裝NLTK,例如pip:

sudo pip install -U nltk

安裝完成之後,你還需要安裝與庫一起配套使用的數據,其中包含了大量的文檔,你可以使用這些文檔來測試NLTK中的其他工具。

有多種方法來安裝數據和文檔,例如,用腳本:

import nltk
nltk.download()

或用命令行:

python -m nltk.downloader all

有關安裝和設置NLTK的更多幫助,請參閱:

2. 分割成句子

第一步是將文本分割成句子。

一些建模任務傾向於以段落或句子的形式輸入文本,例如word2vec。你可以先將文本分割成句子,再將每個句子分割成單詞,然後將每個句子保存到文件中,每行一個句子。

NLTK提供的*sent_tokenize()*函數可以將文本分割成句子。

下麵的示例將“*metamorphosis_clean.txt*”文件加載到內存中,將其分割成句子,並打印第一個句子。

# load data
filename = 'metamorphosis_clean.txt'
file = open(filename, 'rt')
text = file.read()
file.close()
# split into sentences
from nltk import sent_tokenize
sentences = sent_tokenize(text)
print(sentences[0])

運行這個示例,可以看到文檔被分割成了句子。

One morning, when Gregor Samsa woke from troubled dreams, he found
himself transformed in his bed into a horrible vermin.

3. 分割成單詞

NLTK提供了一個名為*word_tokenize()*的函數,可用於將字符串分割成標記(也就是單詞)。

它根據空格和標點符號進行分割。例如,逗號和句點被視為單獨的標記。而縮略語也會分割開(例如“*What’s*”變成“*What*”和“*’s*“)。引號會被保留。

例如:

# load data
filename = 'metamorphosis_clean.txt'
file = open(filename, 'rt')
text = file.read()
file.close()
# split into words
from nltk.tokenize import word_tokenize
tokens = word_tokenize(text)
print(tokens[:100])

運行這段代碼,可以看到標點符號現在已經成為了標記,後麵可以決定是否要過濾掉。

['One', 'morning', ',', 'when', 'Gregor', 'Samsa', 'woke', 'from', 'troubled', 'dreams', ',', 'he', 'found', 'himself', 'transformed', 'in', 'his', 'bed', 'into', 'a', 'horrible', 'vermin', '.', 'He', 'lay', 'on', 'his', 'armour-like', 'back', ',', 'and', 'if', 'he', 'lifted', 'his', 'head', 'a', 'little', 'he', 'could', 'see', 'his', 'brown', 'belly', ',', 'slightly', 'domed', 'and', 'divided', 'by', 'arches', 'into', 'stiff', 'sections', '.', 'The', 'bedding', 'was', 'hardly', 'able', 'to', 'cover', 'it', 'and', 'seemed', 'ready', 'to', 'slide', 'off', 'any', 'moment', '.', 'His', 'many', 'legs', ',', 'pitifully', 'thin', 'compared', 'with', 'the', 'size', 'of', 'the', 'rest', 'of', 'him', ',', 'waved', 'about', 'helplessly', 'as', 'he', 'looked', '.', '``', 'What', "'s", 'happened', 'to']

4. 過濾標點符號

我們可以過濾掉我們不感興趣的標記,例如所有獨立的標點符號。

通過遍曆所有標記並僅保留所有字母的標記可以實現這個目的。 在Python中,isalpha()這個函數很有用。

例如:

# load data
filename = 'metamorphosis_clean.txt'
file = open(filename, 'rt')
text = file.read()
file.close()
# split into words
from nltk.tokenize import word_tokenize
tokens = word_tokenize(text)
# remove all tokens that are not alphabetic
words = [word for word in tokens if word.isalpha()]
print(tokens[:100])

運行這個示例,你可以看到不僅標點符號,而且“*armour-like*”和“*‘s*”也被過濾掉了。

['One', 'morning', 'when', 'Gregor', 'Samsa', 'woke', 'from', 'troubled', 'dreams', 'he', 'found', 'himself', 'transformed', 'in', 'his', 'bed', 'into', 'a', 'horrible', 'vermin', 'He', 'lay', 'on', 'his', 'back', 'and', 'if', 'he', 'lifted', 'his', 'head', 'a', 'little', 'he', 'could', 'see', 'his', 'brown', 'belly', 'slightly', 'domed', 'and', 'divided', 'by', 'arches', 'into', 'stiff', 'sections', 'The', 'bedding', 'was', 'hardly', 'able', 'to', 'cover', 'it', 'and', 'seemed', 'ready', 'to', 'slide', 'off', 'any', 'moment', 'His', 'many', 'legs', 'pitifully', 'thin', 'compared', 'with', 'the', 'size', 'of', 'the', 'rest', 'of', 'him', 'waved', 'about', 'helplessly', 'as', 'he', 'looked', 'What', 'happened', 'to', 'me', 'he', 'thought', 'It', 'was', 'a', 'dream', 'His', 'room', 'a', 'proper', 'human', 'room']

5. 過濾掉停止詞

停止詞是指那些對這個詞語的深層含義沒有貢獻的詞。

這些是最常見的停止詞:“*the*”,“*a*”和“*is*”。

對於某些應用(如文檔分類)來說,刪除停止詞非常必要。

NLTK提供了各種語言(如英語)最常用的停止詞列表,可以像如下代碼那樣加載:

from nltk.corpus import stopwords
stop_words = stopwords.words('english')
print(stop_words)

你可以看到完整的列表:

['i', 'me', 'my', 'myself', 'we', 'our', 'ours', 'ourselves', 'you', 'your', 'yours', 'yourself', 'yourselves', 'he', 'him', 'his', 'himself', 'she', 'her', 'hers', 'herself', 'it', 'its', 'itself', 'they', 'them', 'their', 'theirs', 'themselves', 'what', 'which', 'who', 'whom', 'this', 'that', 'these', 'those', 'am', 'is', 'are', 'was', 'were', 'be', 'been', 'being', 'have', 'has', 'had', 'having', 'do', 'does', 'did', 'doing', 'a', 'an', 'the', 'and', 'but', 'if', 'or', 'because', 'as', 'until', 'while', 'of', 'at', 'by', 'for', 'with', 'about', 'against', 'between', 'into', 'through', 'during', 'before', 'after', 'above', 'below', 'to', 'from', 'up', 'down', 'in', 'out', 'on', 'off', 'over', 'under', 'again', 'further', 'then', 'once', 'here', 'there', 'when', 'where', 'why', 'how', 'all', 'any', 'both', 'each', 'few', 'more', 'most', 'other', 'some', 'such', 'no', 'nor', 'not', 'only', 'own', 'same', 'so', 'than', 'too', 'very', 's', 't', 'can', 'will', 'just', 'don', 'should', 'now', 'd', 'll', 'm', 'o', 're', 've', 'y', 'ain', 'aren', 'couldn', 'didn', 'doesn', 'hadn', 'hasn', 'haven', 'isn', 'ma', 'mightn', 'mustn', 'needn', 'shan', 'shouldn', 'wasn', 'weren', 'won', 'wouldn']

可以看到,它們都是小寫字母,並且標點符號已被刪除。你可以將你的標記與停止詞進行比較,並將其過濾掉。

下麵來演示一下這個過程:

  1. 加載原始文本。
  2. 分成多個標記。
  3. 轉換為小寫。
  4. 從每個標記中刪除標點符號。
  5. 濾除不是字母的標記。
  6. 過濾掉停止詞。
# load data
filename = 'metamorphosis_clean.txt'
file = open(filename, 'rt')
text = file.read()
file.close()
# split into words
from nltk.tokenize import word_tokenize
tokens = word_tokenize(text)
# convert to lower case
tokens = [w.lower() for w in tokens]
# remove punctuation from each word
import string
table = str.maketrans('', '', string.punctuation)
stripped = [w.translate(table) for w in tokens]
# remove remaining tokens that are not alphabetic
words = [word for word in stripped if word.isalpha()]
# filter out stop words
from nltk.corpus import stopwords
stop_words = set(stopwords.words('english'))
words = [w for w in words if not w in stop_words]
print(words[:100])

運行這個例子,可以看到除了其他的轉換之外,像“*a*”和“*to*”這樣的停止詞已被刪除。但是還留下了像“*nt*”這樣的標記。 革命尚未成功,同誌仍須努力。

['one', 'morning', 'gregor', 'samsa', 'woke', 'troubled', 'dreams', 'found', 'transformed', 'bed', 'horrible', 'vermin', 'lay', 'armourlike', 'back', 'lifted', 'head', 'little', 'could', 'see', 'brown', 'belly', 'slightly', 'domed', 'divided', 'arches', 'stiff', 'sections', 'bedding', 'hardly', 'able', 'cover', 'seemed', 'ready', 'slide', 'moment', 'many', 'legs', 'pitifully', 'thin', 'compared', 'size', 'rest', 'waved', 'helplessly', 'looked', 'happened', 'thought', 'nt', 'dream', 'room', 'proper', 'human', 'room', 'although', 'little', 'small', 'lay', 'peacefully', 'four', 'familiar', 'walls', 'collection', 'textile', 'samples', 'lay', 'spread', 'table', 'samsa', 'travelling', 'salesman', 'hung', 'picture', 'recently', 'cut', 'illustrated', 'magazine', 'housed', 'nice', 'gilded', 'frame', 'showed', 'lady', 'fitted', 'fur', 'hat', 'fur', 'boa', 'sat', 'upright', 'raising', 'heavy', 'fur', 'muff', 'covered', 'whole', 'lower', 'arm', 'towards', 'viewer']

6. 詞幹單詞

詞幹提取是指抽取每個單詞的詞幹或詞根的過程。例如,“*fishing*,”、“*fished*,”、“*fisher*”都可以縮減為“*fish*”。

目前有很多的詞幹抽取算法,但最流行的是Porter Stemming算法。 該方法可以通過PorterStemmer類在NLTK中使用。

例如:

# load data
filename = 'metamorphosis_clean.txt'
file = open(filename, 'rt')
text = file.read()
file.close()
# split into words
from nltk.tokenize import word_tokenize
tokens = word_tokenize(text)
# stemming of words
from nltk.stem.porter import PorterStemmer
porter = PorterStemmer()
stemmed = [porter.stem(word) for word in tokens]
print(stemmed[:100])

運行這個例子,可以看到很多單詞都已經被抽取了詞幹,比如,“*trouble*”已經變成“*troubl*”。而且,詞幹提取還使標記變為小寫。

['one', 'morn', ',', 'when', 'gregor', 'samsa', 'woke', 'from', 'troubl', 'dream', ',', 'he', 'found', 'himself', 'transform', 'in', 'hi', 'bed', 'into', 'a', 'horribl', 'vermin', '.', 'He', 'lay', 'on', 'hi', 'armour-lik', 'back', ',', 'and', 'if', 'he', 'lift', 'hi', 'head', 'a', 'littl', 'he', 'could', 'see', 'hi', 'brown', 'belli', ',', 'slightli', 'dome', 'and', 'divid', 'by', 'arch', 'into', 'stiff', 'section', '.', 'the', 'bed', 'wa', 'hardli', 'abl', 'to', 'cover', 'it', 'and', 'seem', 'readi', 'to', 'slide', 'off', 'ani', 'moment', '.', 'hi', 'mani', 'leg', ',', 'piti', 'thin', 'compar', 'with', 'the', 'size', 'of', 'the', 'rest', 'of', 'him', ',', 'wave', 'about', 'helplessli', 'as', 'he', 'look', '.', '``', 'what', "'s", 'happen', 'to'

在NLTK中有一整套很棒的詞幹和詞匯算法可供選擇。

文本清理注意事項

由於本教程所使用的原始文本非常幹淨,因此我們可能忽略了你本需要在自己的項目中要做的文本淨化工作。

以下是淨化文本的一些注意事項:

  • 處理那種不適合全部加載到內存裏的大型文件和大批量的文本文檔。
  • 從HTML、PDF或其他結構化文檔格式的標記中提取文本。
  • 從其他語言翻譯為英文。
  • 將Unicode字符解碼為標準的形式,比如UTF8。
  • 處理特定領域的單詞、短語和縮略詞。
  • 處理或刪除數字,比如日期和數量。
  • 查找和糾正常見的打印錯誤和拚寫錯誤。
  • ……

真正幹淨的文本是不可能存在的,我們需要根據自己所擁有的時間、資源和知識來做到最好。“幹淨”這個概念是由項目的具體要求來決定的。

在每次轉換文本之後都要保存為新的文件,以便後期可以把所有的數據放在一起進行比較查看。

進一步閱讀

如果你想閱讀更深入的內容,本章節提供了相關的內容。

總結

在本教程中,你學會了如何用Python為機器學習淨化文本。

具體一點說,你學到了:

  • 如何開發簡單的文本淨化工具。
  • 如何使用NLTK庫中更複雜的方法。
  • 在使用現代文字表示方法時如何準備文本。

文章原標題《How to Clean Text for Machine Learning with Python》,作者:Jason Brownlee,譯者:夏天,審校:主題曲。

文章為簡譯,更為詳細的內容,請查看原文

本文由北郵@愛可可-愛生活老師推薦,阿裏雲雲棲社區組織翻譯。

最後更新:2017-10-20 11:04:12

  上一篇:go  你的Wi-Fi 還安全嗎?全球重大漏洞WPA2 KRACK 詳細分析報告
  下一篇:go  利用雲效度量功能進行質量運營和效率驅動提升