To obtain synonyms from WordNet using the NLTK (Natural Language Toolkit) library in Python, you can follow these steps:
Install NLTK if you haven't already. You can install it using pip:
pip install nltk
2. Import the necessary modules and download the WordNet corpus:
import nltk
from nltk.corpus import wordnet
nltk.download('wordnet')
3. Define a function that retrieves synonyms for a given word:
def get_synonyms(word):
synonyms = []
for syn in wordnet.synsets(word):
for lemma in syn.lemmas():
synonyms.append(lemma.name())
return synonyms
4.Call the get_synonyms function and provide the word for which you want to retrieve synonyms:
word = "happy"
synonyms = get_synonyms(word)
print(synonyms)
This will output a list of synonyms for the word "happy" as defined in WordNet.
By following these steps, you can leverage the NLTK library to access synonyms from WordNet in your Python program.