admin管理员组

文章数量:1584186

Python3自然语言处理(5)——预处理

注:转载请联系博主,或关注微信公众号"引文空间",后台提出转载申请,等待回复。否则将举报抄袭!

1.分词
当一个文档或者一个长字符串需要处理的时候,你首先要做的是将它拆分成一个个单词和标点符号,我们称这个过程为分词。接下来我们将了解NLTK中可用分词器的类型以及它们的用法。
创建一个名为tokenizer.py的文件并添加如下代码:


from nltk.tokenize import LineTokenizer,SpaceTokenizer,TweetTokenizer
from nltk import word_tokenize

我们将从LineTokernizer开始介绍。添加以下三行代码:

str1='My name is Maximus Decimus, commander of the Armies of the North, General of the Felix Legions and loyal servant to the true emperor, Marcus Aurelius. \nFather to a murdered son, husband to a murdered wife. \nAnd I will have my vengeance, in this life or the next.'
lTokenizer=LineTokenizer()
print('Line tokenizer output:',lTokenizer.tokenize(str1))

顾名思义,该分词器应该将输入的字符串拆分成行(非句子)。让我们看看分词器的输出效果:

Line tokenizer output: ['My name is Maximus Decimus, commander of the Armies of the North, General of the Felix Legions and loyal servant to the true emperor, Marcus Aurelius. ', 'Father to a murdered son, husband to a murdered wife. ', 'And I will have my vengeance, in this life or the next.']

如上所示,它返回了一个包含三个字符串的列表。这意味着给定的输入已经根据换行符的位置被拆分成了三行。LineTokernizer的作用是将输入的字符串拆分成行。
现在我们来看SpaceTokenizer。顾名思义,它是根据空格符来分词的。加入以下几行:

rawText='By 11 o\'clock on sunday, the doctor shall open the dispensary.'
sTokenizer=SpaceTokenizer()
print('Space Tokenizer output:',sTokenizer.tokenize(rawText))

sTokenizer是SpaceTokenize类的一个对象,调用tokenize()方法我们将看到如下输出:

Space Tokenizer output: ['By', '11', "o'clock", 'on', 'sunday,', 'the', 'doctor', 'shall', 'open', 'the', 'dispensary.']

正如期望的那样,输入的rawText被空格符""拆分。
接下来,调用word_tokenize()方法,示例如下:

print('Word Tokenizer output:',word_tokenize(rawText))

结果如下:

Word Tokenizer output: ['By', '11', "o'clock", 'on', 'sunday', ',', 'the', 'doctor', 'shall', 'open', 'the', 'dispensary', '.']

如上所示,SpaceTokenizer和word_tokenize()的区别是显而易见的。
最后我们介绍一下TweetTokernizer,处理特殊字符串的时候可以使用该分词器:

tTokenizer=TweetTokenizer()
print('Tweet Tokenizer output:',tTokenizer.tokenize("This is a coool #dummysmiley: :-) :-P <3"))

Tweets包含我们想要保持完整的特殊单词、特殊字符、标签、笑脸符号等。上述代码的运行结果如下:

Tweet Tokenizer output: ['This', 'is', 'a', 'coool', '#dummysmiley', ':', ':-)', ':-P', '<3']

正如我们看到的,Tokenizer保持特殊字符的完整性而没有进行拆分,笑脸符号也保持原封不动。这是一种特殊且比较少见的类,当需要的时候可以使用它。

2.词干提取
词干是没有任何后缀的词的基本组成部分,词干提取器的作用是去除后缀并输出词的词干。
创建一个名为stemmers.py的文件,并添加以下导入行:

from nltk import PorterStemmer,LancasterStemmer,word_tokenize

在进行词干提取之前,我们首先需要对输入文本进行分词,使用以下代码来完成这一步:

raw='My name is Maximus Decimus, commander of the Armies of the North, General of the Felix Legions and loyal servant to the true emperor, Marcus Aurelius. Father to a murdered son, husband to a murdered wife. And I will have my vengeance, in this life or the next.'
tokens=word_tokenize(raw)

分词列表包含输入字符串raw产生的所有分词。
首先使用PorterStemmer,添加如下三行代码:

porter=PorterStemmer()
pStems=[porter.stem(t) for t in tokens]
print(pStems)

首先初始化词干提取器,然后对所有的输入文本应用该词干提取器,最后打印输出结果。通过观察输出结果,我们可以了解到更多信息:

['My', 'name', 'is', 'maximu', 'decimu', ',', 'command', 'of', 'the', 'armi', 'of', 'the', 'north', ',', 'gener', 'of', 'the', 'felix', 'legion', 'and', 'loyal', 'servant', 'to', 'the', 'true', 'emperor', ',', 'marcu', 'aureliu', '.', 'father', 'to', 'a', 'murder', 'son', ',', 'husband', 'to', 'a', 'murder', 'wife', '.', 'and', 'I', 'will', 'have', 'my', 'vengeanc', ',', 'in', 'thi', 'life', 'or', 'the', 'next', '.']

正如你在输出结果中看到的,所有的单词都去除了“s”“es”“e”“ed”“al”等后缀。
接下来使用LancasterStemmer,与porter相比较,它更容易出错,因为它包含更多要去除的尾缀:

lancaster=LancasterStemmer()
lStems=[lancaster.stem(t) for t in tokens]
print(lStems)

进行相似实验,用LancasterStemmer代替PorterStemmer。输出结果如下:

['my', 'nam', 'is', 'maxim', 'decim', ',', 'command', 'of', 'the', 'army', 'of', 'the', 'nor', ',', 'gen', 'of', 'the', 'felix', 'leg', 'and', 'loy', 'serv', 'to', 'the', 'tru', 'emp', ',', 'marc', 'aureli', '.', 'fath', 'to', 'a', 'murd', 'son', ',', 'husband', 'to', 'a', 'murd', 'wif', '.', 'and', 'i', 'wil', 'hav', 'my', 'veng', ',', 'in', 'thi', 'lif', 'or', 'the', 'next', '.']

我们将在输出部分讨论它们的差别,但是很容易就能看出该分词器对尾缀的处理优于Porter。尾缀如“us”“e”“th”“eral”“ered”等。
通过比较这两种词干提取器的输出,我们发现在去除尾缀方面lancaster做得更加彻底。它尽可能多地去除尾部字符,而porter相对来说尽可能少地去除尾部字符。

3.词形还原
一个词元是一个词的中心词,或者简单地说是一个词的基本组成。我们已经了解了什么是词干,但是与词干提取过程不同的是,词干是通过去除或替换尾缀获得的,而词元获取是一个字典匹配过程。由于词形还原是一个字典映射过程,因此词形还原相对于词干提取来说,是一个更复杂的过程。
创建一个名为lemmatizer.py的文件并添加如下代码:

from nltk import word_tokenize,WordNetLemmatizer

在进行任何词干提取之前,我们首先需要对输入文本进行分词,使用如下代码来完成:

raw='My name is Maximus Decimus, commander of the armies of the north, General of the Felix legions and loyal servant to the true emperor, Marcus Aurelius. Father to a murdered son, husband to a murdered wife. And I will have my vengeance, in this life or the next.'
tokens=word_tokenize(raw)

现在我们使用词形还原器lemmatizer,添加如下三行代码:

lemmatizer=WordNetLemmatizer()
lemmas=[lemmatizer.lemmatize(t) for t in tokens]
print(lemmas)

运行程序,上述三行代码的输出如下所示:

['My', 'name', 'is', 'Maximus', 'Decimus', ',', 'commander', 'of', 'the', 'army', 'of', 'the', 'north', ',', 'General', 'of', 'the', 'Felix', 'legion', 'and', 'loyal', 'servant', 'to', 'the', 'true', 'emperor', ',', 'Marcus', 'Aurelius', '.', 'Father', 'to', 'a', 'murdered', 'son', ',', 'husband', 'to', 'a', 'murdered', 'wife',<

本文标签: 自然语言