Python ProgrammingPython Programming

How do I do sentence or phrase Lemmatization using NLTK?

The process of lemmatization is very similar to stemming— where we remove word affixes by considering the vocabulary to get a base form of the word known as root word or lemma, which will always be present in the dictionary. For example, "good" "better" or "best" is lemmatized into good. The part of speech of a word is determined in lemmatization.


Lemmatization using NLTK

from nltk.stem import WordNetLemmatizer

wnl = WordNetLemmatizer()
text = ['She gripped the armrest as he passed two cars at a time.',
        'Her car was in full view.',
        'A number of cars carried out of state license plates.']

output = []
for sentence in text:
    output.append(" ".join([wnl.lemmatize(i) for i in sentence.split()]))

for item in output:
    print(item)

print("*" * 10)
print(wnl.lemmatize('jumps', 'n'))
print(wnl.lemmatize('jumping', 'v'))
print(wnl.lemmatize('jumped', 'v'))

print("*" * 10)
print(wnl.lemmatize('saddest', 'a'))
print(wnl.lemmatize('happiest', 'a'))
print(wnl.lemmatize('easiest', 'a'))


She gripped the armrest a he passed two car at a time.
Her car wa in full view.
A number of car carried out of state license plates.
**********
jump
jump
jump
**********
sad
happy
easy