Skip to content

Instantly share code, notes, and snippets.

@kshirsagarsiddharth
Last active December 27, 2022 10:29
Show Gist options
  • Save kshirsagarsiddharth/c38213433921b2ac4c079fc0c1fd215c to your computer and use it in GitHub Desktop.
Save kshirsagarsiddharth/c38213433921b2ac4c079fc0c1fd215c to your computer and use it in GitHub Desktop.
import nltk
from nltk.stem import WordNetLemmatizer
# Initialize the lemmatizer
lemmatizer = WordNetLemmatizer()
def lemmatize_text(text):
# Tokenize the text
tokens = nltk.word_tokenize(text)
# Lemmatize the tokens
lemmas = [lemmatizer.lemmatize(token) for token in tokens]
# Join the lemmas into a single string
lemmatized_text = ' '.join(lemmas)
return lemmatized_text
# Test the function
text = "I am running to the store to buy milk"
lemmatize_text(text)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment