2

Sorry guys I'm new to NLP and I'm trying to apply NLTK Lemmatizer to the whole input text, however it seems not to work for even a simple sentence.

from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.tokenize import sent_tokenize
from nltk.stem import WordNetLemmatizer

def word_lemmatizer(text):
    lemmatizer = WordNetLemmatizer()
    lem_text = "".join([lemmatizer.lemmatize(w) for w in text])
    return lem_text

word_lemmatizer("bats flying balls")


[Output]:
'bats flying balls'
jsacharz
  • 21
  • 1

1 Answers1

1

While lemmatizing, you need to pass one word at time from given text; in your code, characters are getting passed into the lemmatizer; So try this;

def word_lemmatizer(text):
    lemmatizer = WordNetLemmatizer()
    lem_text = " ".join([lemmatizer.lemmatize(w) for w in text.split(" ")]) # edited code here
    return lem_text

word_lemmatizer("bats flying balls")

# Output;
'bat flying ball'
Sachin Kohli
  • 1,956
  • 1
  • 1
  • 6
  • Amazing, thanks heaps! it worked after splitting the text as suggested. – jsacharz Sep 30 '22 at 04:26
  • Glad to Help @jsacharz ... Drop a like & accept the best answer that works for your problem statement... To grow & motivate community... Happy Coding :) – Sachin Kohli Sep 30 '22 at 04:31