Stemming e lemmatizzazione

Sentiment Analysis con Python

Violeta Misheva

Data Scientist

Che cos'è lo stemming?

Lo stemming trasforma le parole nella loro radice, anche se lo stem non è una parola valida nella lingua.

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

Sentiment Analysis con Python

Che cos'è la lemmatizzazione?

La lemmatizzazione è simile allo stemming ma, a differenza di questo, riduce le parole a radici che sono parole valide nella lingua.

stay, stays, staying, stayed ----> stay
house, houses, housing ----> house
Sentiment Analysis con Python

Stemming vs. lemmatizzazione

Stemming

  • Produce radici delle parole
  • Veloce ed efficiente da calcolare

Lemmatizzazione

  • Produce parole reali
  • Più lenta dello stemming e può dipendere dalla parte del discorso
Sentiment Analysis con Python

Stemming di stringhe

from nltk.stem import PorterStemmer

porter = PorterStemmer()
porter.stem('wonderful')
'wonder'
Sentiment Analysis con Python

Stemmers per altre lingue

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'
Sentiment Analysis con Python

Come fare lo stemming di una frase

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', '!']
Sentiment Analysis con Python

Lemmatizzazione di una stringa

from nltk.stem import WordNetLemmatizer

WNlemmatizer = WordNetLemmatizer()
WNlemmatizer.lemmatize('wonderful', pos='a')
'wonderful'
Sentiment Analysis con Python

Esercitiamoci!

Sentiment Analysis con Python

Preparing Video For Download...