pythonsynonym

I need to find the synonyms for a given word from a sentence. For an example


I need to find the synonyms for a given word. For an example list1 = ['happy life']

In Python code, by using NLTK library, i got single word synonyms, but I need synonyms of two or more than two words together.


Solution

  • The easiest way to do this would be using a split method to split the two words into single words, then running that against the library that you're using. An example would be like the one below:

    from nltk.corpus import wordnet
    synonyms = []
    antonyms = []
    input = "happy life"
    
    input = input.split()
    
    dictSynonyms = {}
    
    
    for word in input:
        for syn in wordnet.synsets(word):
            for l in syn.lemmas():
                dictSynonyms[l.name()] = word