Random Synonym Finder

  • Share this:

Code introduction


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