自然语言处理3.6——规范化文本
在前面的例子中,在处理文本词汇前经常要将文本转化成小写,即(w.lower() for w in words).通过lower()将文本规范化为小写,这样一来,"The"和"the"的区别被忽略了。
我们常常进行更多的尝试,例如去掉文本中的所有词缀已经提取词干的任务等。下一步是确保结果形式是字典中确定的词,即词形归并任务。首先定义一下本节使用的数据。
>>>raw="""DENNIS: Listen, strange women lying in ponds distributing swords is no basis for a system of government. Supreme executive power derives from a mandate from the masses, not from some farcical aquatic ceremony.""" >>>tokens=nltk.word_tokenize(raw)
1.词干提取器
NLTK中包括了一个现成的词干提取器,如果需要使用词干提取器,应该优先使用它们中的一个,而不是使用正则表达式制作自己的词干提取器,因为NLTK中的词干提取器能处理的不规则情况很广泛。Porter和Lancaster词干提取器按照他们的规则剥离词缀。下面的例子表明Porter词干提取器正确处理了lying(将他映射为lie),而Lancaster词干提取器并没有处理。
>>>import nltk >>>porter=nltk.PorterStemmer() >>>lancaster=nltk.LancasterStemmer() >>>print([porter.stem(t) for t in tokens]) ['DENNI', ':', 'Listen', ',', 'strang', 'women', 'lie', 'in', 'pond', 'distribut', 'sword', 'is', 'no', 'basi', 'for', 'a', 'system', 'of', 'govern', '.', 'Suprem', 'execut', 'power', 'deriv', 'from', 'a', 'mandat', 'from', 'the', 'mass', ',', 'not', 'from', 'some', 'farcic', 'aquat', 'ceremoni', '.'] >>>print([lancaster.stem(t) for t in tokens]) ['den', ':', 'list', ',', 'strange', 'wom', 'lying', 'in', 'pond', 'distribut', 'sword', 'is', 'no', 'bas', 'for', 'a', 'system', 'of', 'govern', '.', 'suprem', 'execut', 'pow', 'der', 'from', 'a', 'mand', 'from', 'the', 'mass', ',', 'not', 'from', 'som', 'farc', 'aqu', 'ceremony', '.']
词干提取器过程没有明确定义,通常选择合适应用的词干提取器。如果要索引文本或者使搜索支持不同词汇形式的话,Porter词干提取器是一个很好的选择。
class IndexedText(object): def __init__(self, stemmer, text): self._text = text self._stemmer = stemmer self._index = nltk.Index((self._stem(word), i) for (i, word) in enumerate(text)) def concordance(self, word, width=40): key = self._stem(word) wc = int(width/4) # words of context for i in self._index[key]: lcontext = ' '.join(self._text[i-wc:i]) rcontext = ' '.join(self._text[i:i+wc]) ldisplay = '{:>{width}}'.format(lcontext[-width:], width=width) rdisplay = '{:{width}}'.format(rcontext[:width], width=width) print(ldisplay, rdisplay) def _stem(self, word): return self._stemmer.stem(word).lower()
>>> porter = nltk.PorterStemmer() >>> grail = nltk.corpus.webtext.words('grail.txt') >>> text = IndexedText(porter, grail) >>> text.concordance('lie') r king ! DENNIS : Listen , strange women lying in ponds distributing swords is no beat a very brave retreat . ROBIN : All lies ! MINSTREL : [ singing ] Bravest of Nay . Nay . Come . Come . You may lie here . Oh , but you are wounded ! doctors immediately ! No , no , please ! Lie down . [ clap clap ] PIGLET : Well ere is much danger , for beyond the cave lies the Gorge of Eternal Peril , which you . Oh ... TIM : To the north there lies a cave -- the cave of Caerbannog -- h it and lived ! Bones of full fifty men lie strewn about its lair . So , brave k not stop our fight ' til each one of you lies dead , and the Holy Grail returns t
2.词形归并
WordNet词形归并器删除词缀产生的词,都是它的字典中的词。这个额外的检查过程会使得速度变慢:
>>> wnl = nltk.WordNetLemmatizer() >>> [wnl.lemmatize(t) for t in tokens] ['DENNIS', ':', 'Listen', ',', 'strange', 'woman', 'lying', 'in', 'pond', 'distributing', 'sword', 'is', 'no', 'basis', 'for', 'a', 'system', 'of', 'government', '.', 'Supreme', 'executive', 'power', 'derives', 'from', 'a', 'mandate', 'from', 'the', 'mass', ',', 'not', 'from', 'some', 'farcical', 'aquatic', 'ceremony', '.']
如果想要编辑一些文本词汇,或者想要一个有效词条(中心词)列表,WordNet词形归并器是一个不错的选择、