Python - Stemming et Lemmatisation

Dans les domaines du traitement du langage naturel, nous rencontrons des situations où deux mots ou plus ont une racine commune. Par exemple, les trois mots - d'accord, d'accord et d'accord ont le même mot racine d'accord. Une recherche impliquant l'un de ces mots doit les traiter comme le même mot qui est le mot racine. Il devient donc essentiel de lier tous les mots à leur mot racine. La bibliothèque NLTK a des méthodes pour faire cette liaison et donner la sortie montrant le mot racine.

Le programme ci-dessous utilise l'algorithme de tige de Porter pour la tige.

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))

Lorsque nous exécutons le code ci-dessus, cela produit le résultat suivant.

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 lemmatisation est similaire à celle issue, mais elle apporte un contexte aux mots et va donc plus loin en reliant des mots ayant une signification similaire à un mot. Par exemple, si un paragraphe contient des mots tels que voitures, trains et automobile, il les liera tous à automobile. Dans le programme ci-dessous, nous utilisons la base de données lexicale WordNet pour la lemmatisation.

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))

Lorsque nous exécutons le code ci-dessus, cela produit le résultat suivant.

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