词干提取与词形还原

Python 中的情感分析

Violeta Misheva

Data Scientist

什么是词干提取?

词干提取将词转换为其词干,即使该词干不是语言中的有效词。

staying, stays, stayed ----> stay
house, houses, housing ----> hous

Python 中的情感分析

什么是词形还原?

词形还原与词干提取相似,但它将词还原为语言中的有效词。

stay, stays, staying, stayed ----> stay
house, houses, housing ----> house
Python 中的情感分析

词干提取 vs. 词形还原

词干提取

  • 生成词的词干
  • 计算快速高效

词形还原

  • 生成真实词形
  • 比词干提取慢,且可能依赖词性
Python 中的情感分析

字符串的词干提取

from nltk.stem import PorterStemmer

porter = PorterStemmer()
porter.stem('wonderful')
'wonder'
Python 中的情感分析

非英语的词干提取器

Snowball Stemmer:丹麦语、荷兰语、英语、芬兰语、法语、德语、匈牙利语、意大利语、挪威语、葡萄牙语、罗马尼亚语、俄语、西班牙语、瑞典语

from nltk.stem.snowball import SnowballStemmer

DutchStemmer = SnowballStemmer("dutch")
DutchStemmer.stem("beginnen")
'begin'
Python 中的情感分析

如何对句子做词干提取?

porter.stem('Today is a wonderful day!')
'today is a wonderful day!'
tokens = word_tokenize('Today is a wonderful day!')
stemmed_tokens = [porter.stem(token) for token in tokens]
stemmed_tokens
['today', 'is', 'a', 'wonder', 'day', '!']
Python 中的情感分析

字符串的词形还原

from nltk.stem import WordNetLemmatizer

WNlemmatizer = WordNetLemmatizer()
WNlemmatizer.lemmatize('wonderful', pos='a')
'wonderful'
Python 中的情感分析

Passons à la pratique !

Python 中的情感分析

Preparing Video For Download...