Random Synset Definition from WordNet

  • Share this:

Code introduction


This function takes a word as input and returns the definition of a random synset of that word from WordNet.


Technology Stack : nltk.corpus.wordnet, nltk.tokenize.word_tokenize

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from nltk.corpus import wordnet
from nltk.tokenize import word_tokenize

def random_wordnet_synset(word):
    synsets = wordnet.synsets(word)
    if synsets:
        return random.choice(synsets).definition()
    else:
        return "No definition found for the word."