詞幹提取與詞形還原

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. 詞形還原

詞幹提取(Stemming)

  • 產生單字的詞幹
  • 計算快速且有效率

詞形還原(Lemmatization)

  • 輸出是真實單字
  • 較慢,且可能依賴詞性
Python 情感分析

字串的詞幹提取

from nltk.stem import PorterStemmer

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

非英語的詞幹器

Snowball Stemmer:Danish、Dutch、English、Finnish、French、German、Hungarian、Italian、Norwegian、Portuguese、Romanian、Russian、Spanish、Swedish

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 情感分析

一起來練習吧!

Python 情感分析

Preparing Video For Download...