【465】词干提取与词形还原

  词干(word stem)表示每个单词的主体部分。词干提取(stemming)就是提取词干的过程,通常是删除常见的后缀来实现。

  词形还原(lemmatization)考虑了单词在句子中的作用,单词的标准化形式为词元(lemma)。

  词干提取和词形还原这两种处理方法都是标准化(normalization)的形式之一,标准化是指尝试提取一个单词的某种标准形式。

  对比一种词干提取的方法(Poter词干提取器,从 nltk 包导入)与 spacy 包中实现词形还原。

import spacy
import nltk

# 加载 spacy 的英语模型,可以分词
en_nlp = spacy.load('en')
# 将 nltk 的 Porter 词干提取器实例化
stemmer = nltk.stem.PorterStemmer()

# 定义一个函数来对比区别
def compare_normalization(doc):
    # 在 spacy 中对文档进行分词
    doc_spacy = en_nlp(doc)
    # 打印出 spacy 找到的词元
    print("Lemmatization:")
    print([token.lemma_ for token in doc_spacy])
    # 打印出 Porter 词干提取器找到的词例
    print("Stemming:")
    print([stemmer.stem(token.norm_.lower()) for token in doc_spacy])

compare_normalization(u"Our meeting today was worse than yesterday, "
                       "I'm scared of meeting the clients tomorrow.")

output:
Lemmatization:
['-PRON-', 'meeting', 'today', 'be', 'bad', 'than', 'yesterday', ',', '-PRON-', 'be', 'scared', 'of', 'meet', 'the', 'client', 'tomorrow', '.']
Stemming:
['our', 'meet', 'today', 'wa', 'wors', 'than', 'yesterday', ',', 'i', 'am', 'scare', 'of', 'meet', 'the', 'client', 'tomorrow', '.']

  总结:词形还原效果更好。

猜你喜欢

转载自www.cnblogs.com/alex-bn-lee/p/12913945.html
今日推荐