Last active
December 27, 2022 10:29
-
-
Save kshirsagarsiddharth/c38213433921b2ac4c079fc0c1fd215c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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