NLTK doesn't lemmatize uppercase words

Viewed 141

I'm trying to change plural words to singular in a string with a mix of upper case and lowercase words. e.g. CARDBOARD BOXES, DIMENSIONS: 19cm H x 10cm W x 30cm D

I used NLTK package to do so but it only accept lowercase strings and I don't want to lowercase all words in my string. any suggestion?

1 Answers

Hope it helps:

from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
lemma = []
for t in text.split():
    if t.isupper():
                t = lemmatizer.lemmatize(t.lower())
                lemma.append(t.upper())
            else:
                lemma.append(t)
        
text = ' '.join(lemma)
Related