你不能直接从原始文本训练机器学习或深度学习模型,必须首先清理文本,这意味着将其拆分为单词,并处理标点符号和大小写。实际上,您可能需要使用一整套文本准备方法,方法的选择实际上取决于您的自然语言处理任务。在本教程中,您将了解如何清理和准备文本,以便通过机器学习进行建模。完成本教程后,您将了解:

如何开始使用非常简单的文本清理工具。

如何采取措施并使用NLTK库中更复杂的方法。

为自然语言处理模型准备文本时的注意事项。

2.1 教程概述

本教程分为以下几部分:

  1. 弗兰兹卡夫卡的Metamorphosis
  2. 文本清理是特定于任务的
  3. 手动标记
  4. 使用NLTK进行标记和清理
  5. 其他文字清理注意事项

2.2 弗兰兹卡夫卡的Metamorphosis

让我们从选择数据集开始。在本教程中,我们将使用Franz Kafka撰写的Metamorphosis一书的文本。没有具体的原因,除了它的简短,我喜欢它,你也可能喜欢它。我希望这是大多数学生在学校必读的经典之作。Metamorphosis的全文可从Gutenberg项目免费获得,您可以在此处下载文本的ASCII文本版本:

  1. Metamorphosis by Franz Kafka Plain Text UTF-8(可能需要加载页面两次)。

http://www.gutenberg.org/cache/epub/5200/pg5200.txt

下载文件并将其放在当前工作目录中,文件名为metamorphosis.txt。该文件包含我们不感兴趣的页眉和页脚信息,特别是版权和许可证信息。打开文件并删除页眉和页脚信息,并将文件保存为metamorphosis_clean.txt,clean文件的开头应如下所示:

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

该文件应以:

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

Poor Gregor...

2.3 文本清理是特定于任务的

在实际掌握了您的文本数据之后,清理文本数据的第一步是对您要实现的目标有一个强烈的了解,并在该上下文中查看您的文本,看看究竟可能有什么帮助。花点时间看看文字。你注意到什么?这是我看到的:

  • 它是纯文本,所以没有解析标记(耶!)。
  • 原始德语的翻译使用英国英语(例如旅行)。
  • 每行大约70个字符(meh)。
  • 没有明显的拼写错误或拼写错误。
  • 有标点符号,如逗号、撇号、引号、问号等。
  • 有像armour-like一样的连字符描述。
  • 使用破折号( - )连接连续的句子(可能用逗号替换?)。
  • 有名字(例如Samsa先生)
  • 似乎没有需要处理的数字(例如1999)
  • 分节标记(例如II和III)。

我确信还有很多人会接受训练有素的眼睛。我们将在本教程中查看一般文本清理步骤。

不过,请考虑我们在处理此文本文档时想要达到的一些目标。例如:

如果我们对开发Kafkaesque语言模型感兴趣,我们可能希望保留所有案例,引号和其他标点符号。

如果我们有兴趣将文档分类为Kafka和Not Kafka,那么我们可能会想要删除大小写,标点符号,甚至修饰单词。

根据你的任务,选择如何准备文本数据。

2.4 手动标记

虽然文本清理很难,但我们选择使用的文本已经非常干净了。我们可以编写一些Python代码来手动清理它,这对于遇到的那些简单问题来说是一个很好的练习。清洗过程中,正则表达式和拆分字符串这样的工具会是频繁的出现在你的代码中。

2.4.1 加载数据

让我们加载文本数据,以便我们可以使用它。文本很小,可以快速加载并轻松放入内存中。情况并非总是如此,您可能需要将代码写入内存映射文件。像NLTK这样的工具(将在下一节中介绍)将使得处理大文件变得更加容易。我们可以将整个metamorphosis_clean.txt加载到内存中,如下所示:

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

清单2.1:手动加载文件。

2.4.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])

代码清单2.2:用空格手动分割单词

运行该示例将文档拆分为一长串单词,并打印前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']

代码清单2.3:按空格分割单词的示例输出

2.4.3 选择单词

另一种方法可能是使用正则表达式模型(重新)并通过选择字母数字字符串(az,AZ,0-9和'')将文档拆分为单词。例如:

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

print(words[:100])

代码清单2.4:用regex手动选择单词。

再次,运行示例我们可以看到我们得到了单词列表。这一次,我们可以看到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']

代码清单2.5:用正则表达式选择单词的示例输出

2.4.4 按空格分割并删除标点符号

我们可能想要没有逗号和引号等标点符号的单词序列。我们也希望将连接词保持在一起。一种方法是通过空格将文档拆分为单词(如“按空白拆分”一节),然后使用字符串翻译将所有标点符号替换为空格(例如删除它)。Python提供了一个名为string.punctuation的常量,它提供了一个很好的标点字符列表。例如:

print(string.punctuation)

代码清单2.6:打印已知的标点字符。

结果是:

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

代码清单2.7:打印已知标点符号的示例输出。

我们可以使用正则表达式来选择标点字符并使用sub()函数来把它们消除掉的功能。例如:

re_punc = re.compile('[%s]' % re.escape(string.punctuation))
# remove punctuation from each word
stripped = [re_punc.sub('', w) for w in words]

代码清单2.8:构建将删除标点符号的转换表的示例

我们将这些代码放在一起来完成加载文本文件、通过空格将其拆分为单词、转换每个单词和消除标点符号:

import string
import re
# load text
filename = 'metamorphosis_clean.txt'
file = open(filename, 'rt')
text = file.read()
file.close()
#split into words by white space
words = text.split()
#prepare regex for char filtering
re_punc = re.compile('[%s]' % re.escape(string.punctuation))
# remove punctuation from each word
stripped = [re_punc.sub('', w) for w in words]
print(stripped[:100])

代码清单2.9:手动删除标点符号

我们可以看到,这主要是产生了预期的效果。像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']

 

代码清单2.10:使用转换表删除标点符号的示例输出

有时文本数据可能包含不可打印的字符。我们可以使用类似的方法通过选string.printable常量的反转来过滤掉所有不可打印的字符。例如:

re_print = re.compile('[^%s]' % re.escape(string.printable))
result = [re_print.sub('', w) for w in words]

代码清单2.11:删除不可打印字符的示例

2.4.5 规范化案例

将所有单词转换为一种书写规范是很常见的。这意味着词汇量会缩小,也丢失一些区别(例如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])

代码清单2.12:手动规范化大小写

运行该示例,我们可以看到所有单词现在都是小写的。

 

 

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

 

代码清单2.13:删除标点符号的示例输出

2.4.6 关于清洗文字的说明

清理文本非常难,与具体的问题有关,并且要根据实际的需要决定使用什么样的套路。记住,越简单越好、简单的文本数据、简单的模型、小的词汇表。您可以随时根据模型处理问题的性能随时根据情况复杂化模型。接下来,我们将介绍NLTK库中的一些工具,它们提供的不仅仅是简单的字符串拆分。

2.5 使用NLTK进行标记和清理

Natural Language Toolkit,简称NLTK,是一个为处理和建模文本而编写的Python库。它提供了用于加载和清理文本的良好工具,我们可以使用这些工具为我们的数据做好准备,以便使用机器学习和深度学习算法。

2.5.1 安装NLTK

您可以使用自己喜欢的包管理器安装NLTK,例如pip。在POSIX兼容的机器上,可以使用如下命令:

sudo pip install -U nltk

代码清单2.14:安装NLTK库的命令

安装后,您将需要安装NLTK数据,包括一系列大量的文档集合,您可以在以后用它们来测试NLTK中的其他工具。有几种方法可以做到这一点,例如在脚本中:

import nltk

nltk.download()

代码清单2.15:用于下载所需文本数据的NLTK脚本

或者从命令行:

python -m nltk.downloader all

代码清单2.16:下载NLTK所需文本数据的命令

2.5.2 分句

一个很好的思路就是:第一步是将文本分成句子。一些建模任务更喜欢以段落或句子的形式输入数据,例如Word2Vec。您可以先将文本分成句子,将每个句子分成单词,然后将每个句子保存到文件中,每行一个句子。NLTK提供的sent_tokenize()函数将文本拆分成句子。下面的示例将metamorphosis_clean.txt文件加载到内存中,将其拆分为句子,然后打印第一个句子。

from nltk import sent_tokenize

# load data

filename = 'metamorphosis_clean.txt'

file = open(filename, 'rt')

text = file.read()

file.close()

sentences = sent_tokenize(text)

print(sentences[0])

代码清单2.17:将文本拆分成句子的NLTK脚本

运行这个例子,我们可以看到虽然文档被分成了句子,但每个句子仍然保留了原始文档中行的格式。

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

代码清单2.18:将文本拆分为句子的示例输出

2.5.3 分词

NLTK提供了一个名为word_tokenize()的函数,用于将字符串拆分为标记(单词)。它根据空格和标点符号分割标记。例如,逗号和句点被视为单独的标记。缩略词被分开(例如,What's变成What和's))。引用被保留,等等。例如:

from nltk.tokenize import word_tokenize

# load data

filename = 'metamorphosis_clean.txt'

file = open(filename, 'rt')

text = file.read()

file.close()

# split into words

tokens = word_tokenize(text)

print(tokens[:100])

代码清单2.19:用于将文本拆分为单词的NLTK脚本

运行代码,我们可以看到标点符号现在也被分成了词标记,下面我们可以来过滤掉标点符号。

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

 

代码2.20:将文本拆分为单词的示例输出

2.5.4 过滤掉标点符号

我们可以过滤掉我们不感兴趣的所有标记,例如所有独立标点符号。这可以通过迭代所有标记并仅保留那些全部为字母的标记来完成。Python具有可以使用的函数isalpha()。例如:

from nltk.tokenize import word_tokenize

# load data

filename = 'metamorphosis_clean.txt'

file = open(filename, 'rt')

text = file.read()

file.close()

# split into words

tokens = word_tokenize(text)

# remove all tokens that are not alphabetic

words = [word for word in tokens if word.isalpha()]

print(words[:100])

代码清单2.21:删除标点符号的NLTK脚本

运行该示例,您不仅可以看到标点符号,还可以看到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']

 

代码清单2.22:删除标点符号的示例输出

2.5.5 过滤掉停用词(和管道)

停止词语是那些对词语的深层含义没有贡献的词语。它们是最常用的词语,例如:a,a和is。对于某些应用程序(如文档分类),删除停用词可能是有意义的。NLTK提供了各种语言(例如英语)的停用词列表。它们可以按如下方式加载:

from nltk.corpus import stopwords

stop_words = stopwords.words('english')

print(stop_words)

代码清单2.23:NLTK脚本打印停用词

您可以看到完整列表,如下所示:

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

 

代码清单2.24:打印停用词的输出示例

您可以看到它们都是小写并删除了标点符号。您可以将您的tokens与停用词进行比较并过滤掉它们,但您必须确保以相同的方式准备文本。让我们通过一小段文本准备来演示这一点,包括:

  1. 加载原始文本。
  2. 分成tokens。
  3. 转换为小写。
  4. 从每个标记中删除标点符号。
  5. 从剩余tokens中过滤掉非字母。
  6. 从剩余tokens中过滤掉停用词。

import string

import re

from nltk.tokenize import word_tokenize

from nltk.corpus import stopwords

# load data

filename = 'metamorphosis_clean.txt'

file = open(filename, 'rt')

text = file.read()

file.close()

# split into words

tokens = word_tokenize(text)

# convert to lower case

tokens = [w.lower() for w in tokens]

# prepare regex for char filtering

re_punc = re.compile('[%s]' % re.escape(string.punctuation))

# remove punctuation from each word

stripped = [re_punc.sub('', w) for w in tokens]

#remove remaining tokens that are not alphabetic words = [word for word in stripped if word.isalpha()]

#filter out stop words

stop_words = set(stopwords.words('english')) words = [w for w in words if not w in stop_words] print(words[:100])

 

 

 

代码清单2.25:NLTK脚本过滤掉停用词。

      运行这个例子,我们可以看到除了所有其他变换之外,象 a和to之类的停止词已经被清除。我注意到我们仍然留下像nt这样的token。兔子洞很深,我们需要做的还有很多。

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

2.5.6 词干

词干是指将每个单词缩减为其根或基的过程。例如捕shing, shed, sher都减少到sh。一些应用程序,如文档分类,可以从词干中受益,以便既减少词汇量又专注于文档的感觉或情感,而不是更深层的含义。尽管有许多词干提取算法,但目前行业中流行的方法还是Porter Stemming算法,可以通过NLTK类库中的PorterStemmer类使用这个算法。例如:

from nltk.tokenize import word_tokenize

from nltk.stem.porter import PorterStemmer

# load data

filename = 'metamorphosis_clean.txt'

file = open(filename, 'rt')

text = file.read()

file.close()

# split into words

tokens = word_tokenize(text)

# stemming of words

porter=PorterStemmer()

stemmed = [porter.stem(word) for word in tokens] print(stemmed[:100])

代码清单2.27:NLTK脚本词干。

运行这个例子,你可以看到单词已经减少到它们的词干,比如trouble已经成为troubl。您还可以看到词干实现还将token全部变成小写。

['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'

代码2.28:词干词的示例输出

在NLTK中有一套很好的词干和词形还原算法可供选择,如果将词语缩减到它们的根目录就是你的项目需要的东西。

2.6 其他文字清理注意事项

我们才入手,因为本教程的源文本开始时相当干净,所以我们跳过了许多您可能需要在自己的项目中处理的文本清理问题。以下是清理文本时的其他注意事项的简短列表:

  1. 处理不适合内存的大型文档和大量文本文档。
  2. 从标记中提取文本,如HTML,PDF或其他结构化文档格式。
  3. 从其他语言到英语的音译。
  4. 将Unicode字符解码为规范化形式,例如UTF8。
  5. 处理特定领域的单词,短语和首字母缩略词。
  6. 处理或删除数字,例如日期和金额。
  7. 找到并纠正常见的拼写错误和拼写错误。

以及更多...

这份列表还可列出很多。希望您能够看到获得真正干净的文本是不可能的,我们能够做到的就是根据我们时间、资源和知识处理处适合的文本数据。清洗的想法实际上是由项目的特定任务或关注点决定的。

比较中肯的意见是在每次转换后不断检查您的token。我试图在本教程中表明,我希望你能理解这一点。理想情况下,您可以在每次转换后把中间处理结果保存到新的文件,以免中途出现意外你就不用在重新开始所有的工作。


0 条 查看最新 评论

没有评论
暂时无法发表评论