Python - Stemming e Lemmatization

Nelle aree dell'elaborazione del linguaggio naturale ci imbattiamo in situazioni in cui due o più parole hanno una radice comune. Ad esempio, le tre parole - concordato, d'accordo e gradevole hanno la stessa radice di parola d'accordo. Una ricerca che coinvolge una qualsiasi di queste parole dovrebbe trattarle come la stessa parola che è la parola radice. Quindi diventa essenziale collegare tutte le parole nella loro parola radice. La libreria NLTK ha metodi per eseguire questo collegamento e fornire l'output che mostra la parola radice.

Il programma seguente utilizza l'algoritmo di stemming di Porter per lo stemming.

import nltk
from nltk.stem.porter import PorterStemmer
porter_stemmer = PorterStemmer()

word_data = "It originated from the idea that there are readers who prefer learning new skills from the comforts of their drawing rooms"
# First Word tokenization
nltk_tokens = nltk.word_tokenize(word_data)
#Next find the roots of the word
for w in nltk_tokens:
       print "Actual: %s  Stem: %s"  % (w,porter_stemmer.stem(w))

Quando eseguiamo il codice precedente, produce il seguente risultato.

Actual: It  Stem: It
Actual: originated  Stem: origin
Actual: from  Stem: from
Actual: the  Stem: the
Actual: idea  Stem: idea
Actual: that  Stem: that
Actual: there  Stem: there
Actual: are  Stem: are
Actual: readers  Stem: reader
Actual: who  Stem: who
Actual: prefer  Stem: prefer
Actual: learning  Stem: learn
Actual: new  Stem: new
Actual: skills  Stem: skill
Actual: from  Stem: from
Actual: the  Stem: the
Actual: comforts  Stem: comfort
Actual: of  Stem: of
Actual: their  Stem: their
Actual: drawing  Stem: draw
Actual: rooms  Stem: room

La lemmatizzazione è simile al ti stemming ma fornisce contesto alle parole, quindi fa un passo avanti collegando parole con significato simile a una parola. Ad esempio, se un paragrafo contiene parole come automobili, treni e automobile, le collegherà tutte ad automobile. Nel programma seguente utilizziamo il database lessicale di WordNet per la lemmatizzazione.

import nltk
from nltk.stem import WordNetLemmatizer
wordnet_lemmatizer = WordNetLemmatizer()

word_data = "It originated from the idea that there are readers who prefer learning new skills from the comforts of their drawing rooms"
nltk_tokens = nltk.word_tokenize(word_data)
for w in nltk_tokens:
       print "Actual: %s  Lemma: %s"  % (w,wordnet_lemmatizer.lemmatize(w))

Quando eseguiamo il codice precedente, produce il seguente risultato.

Actual: It  Lemma: It
Actual: originated  Lemma: originated
Actual: from  Lemma: from
Actual: the  Lemma: the
Actual: idea  Lemma: idea
Actual: that  Lemma: that
Actual: there  Lemma: there
Actual: are  Lemma: are
Actual: readers  Lemma: reader
Actual: who  Lemma: who
Actual: prefer  Lemma: prefer
Actual: learning  Lemma: learning
Actual: new  Lemma: new
Actual: skills  Lemma: skill
Actual: from  Lemma: from
Actual: the  Lemma: the
Actual: comforts  Lemma: comfort
Actual: of  Lemma: of
Actual: their  Lemma: their
Actual: drawing  Lemma: drawing
Actual: rooms  Lemma: room