Random Synset Selection from WordNet

  • Share this:

Code introduction


This function uses the WordNetLemmatizer from the Lingua library to get a random synset of a word. First, it converts the word into a lemma, and then it retrieves all the synsets of the lemma and randomly selects one.


Technology Stack : Lingua library, WordNetLemmatizer

Code Type : Function

Code Difficulty : Intermediate


                
                    
import random
from lingua import WordNetLemmatizer

def random_wordnet_synset(word):
    lemmatizer = WordNetLemmatizer()
    lemma = lemmatizer.lemmatize(word, pos='n')
    synsets = list(lemma.sense_keys())
    return random.choice(synsets)

def get_synset_of_word(word):
    if not word:
        return "No word provided"
    synset = random_wordnet_synset(word)
    return synset