RandomDev
RandomDev

Reputation: 87

Create Dictionary of Words and Synonyms

I'm a beginner in Python, and I have a dictionary of words as below:

thisdict ={  "Animal",  "Metal",  "Car"}

And I am getting their synsets as below:

syns = {w : [] for w in thisdict}
for k, v in syns.items():
    for synset in wordnet.synsets(k):
        for lemma in synset.lemmas():
            v.append(lemma.name())
            print(syns)

At the moment, the output of syns for Animal is:

 {'Animal': ['animal']}
 {'Animal': ['animal', 'animate_being']}
 {'Animal': ['animal', 'animate_being', 'beast']}
 {'Animal': ['animal', 'animate_being', 'beast', 'brute']}
 {'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature']}
 {'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly', 'sensual']}

My question is, is there a way to create a dictionary where each row contains a word and it's synonym, for example:

Animal: 'animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly', 'sensual' 
Cat: ...

EDIT Thanks to DannyMoshe, I've added if not key.lower() == lemma.name(): before the append and now have the following output:

['animate_being']
['animate_being', 'beast']
['animate_being', 'beast', 'brute']
['animate_being', 'beast', 'brute', 'creature']
['animate_being', 'beast', 'brute', 'creature', 'fauna']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly', 'sensual']

Is there a way to select the last line, ['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly', 'sensual'], and match it to Animal in thisdict?

Upvotes: 1

Views: 1833

Answers (2)

Aviroxi
Aviroxi

Reputation: 115

What's happening in your code is you are printing the sysn after you add each element because your print statement is inside all the for loops which leads to the printing sysn each time you add the element.

To get the output as u need the print statement should be outside of all the loops so that print statement will print after completing of adding the elements

thisdict = {"Animal","Metal","Car"}
syns = {w : [] for w in thisdict}
for k, v in syns.items():
   for synset in wordnet.synsets(k):
       for lemma in synset.lemmas():
           v.append(lemma.name())
print(syns)

Upvotes: 2

DannyMoshe
DannyMoshe

Reputation: 6255

I think this is the complete answer you're looking for:

syns = {w : [] for w in thisdict}
for k, v in syns.items():
    for synset in wordnet.synsets(k):
        for lemma in synset.lemmas():
            if not k.lower() == lemma.name(): 
                syns[k].append(lemma.name())
print(syns['Animal'])

or if you just want the synonyms as a String:

print ' '.join(syns['Animal'])

Upvotes: 2

Related Questions