You can download this code by clicking the button below.
This code is now available for download.
This function takes a word as an argument and returns a random synonym for that word. If the word has no synonyms, it returns the word itself.
Technology Stack : NLTK (Natural Language Toolkit)
Code Type : Function
Code Difficulty : Intermediate
import random
import nltk
from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize
def find_synonyms(word):
synonyms = wordnet.synsets(word)
if synonyms:
return random.choice(synonyms).lemmas()[0].name()
else:
return word