goodmami / wn

A modern, interlingual wordnet interface for Python
https://wn.readthedocs.io/
MIT License
197 stars 19 forks source link

antonyms in languages other than German and English #160

Closed chlor closed 2 years ago

chlor commented 2 years ago

How to get antonyms from language sources other than German or English language. Here is my way to get antonyms:

if synset.get_related('antonym'): antonyms.update(list(itertools.chain(*[ant.lemmas() for ant in synset.get_related('antonym')]))) for sense in synset.senses(): if sense.get_related('antonym'): ant = sense.get_related('antonym') for a in ant: antonyms.update(a.word().lemma())

chlor commented 2 years ago

I 've tested it with Italian, French an Spanish and found no examples.

fcbond commented 2 years ago

The wordnets from omw 1.* do not have their own relations but piggyback on PWN. So lemmas in synsets in any language that have antonyms in PWN should be antonyms, or at least have opposite meanings. You can get a bigger list if you also expand through similar-to, ...

On Tue, 8 Mar 2022, 20:19 Christina Lohr, @.***> wrote:

I 've tested it with Italian, French an Spanish and found no examples.

— Reply to this email directly, view it on GitHub https://github.com/goodmami/wn/issues/160#issuecomment-1061719120, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAIPZRRQBMOXXZJO4K54UJTU65AVJANCNFSM5QGFTTSA . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you are subscribed to this thread.Message ID: @.***>

chlor commented 2 years ago

Thanks for the quick reply, how can I read antonyms from OWN networks? The code fragments I mentioned are not sufficient.

fcbond commented 2 years ago

Hi,

Not optimized at all, but this should work.  It gets the antonym relations from English and then looks them up in Japanese.

import wn
en = wn.Wordnet('omw-en:1.4')
ja = wn.Wordnet('omw-ja:1.4')

### a set of tuples of ili ids of antonyms,
### sorted internally (so we only get one way: id1 < id2)
antonyms = set()

### store ilis for antonym pairs
for synset in en.synsets():
    ### check synsets (there aren't any)
    if syn_anto := synset.get_related('antonym'):
        print(synset, syn_anto)
    ### check senses
    for sense in synset.senses():
        if sen_anto := sense.get_related('antonym'):
            for anto in sen_anto:
                antonyms.add(tuple(sorted([sense.synset().ili.id,
                                           anto.synset().ili.id])))
### print for Japanese
for (a1, a2) in antonyms:
    s1 = ja.synsets(ili=a1)
    s2 = ja.synsets(ili=a2)
    print(s1,s2)
chlor commented 2 years ago

Thanks, it works! Here is my extension to get antoynms:


import wn
en = wn.Wordnet('omw-en:1.4')
ja = wn.Wordnet('omw-ja:1.4')

### a set of tuples of ili ids of antonyms,
### sorted internally (so we only get one way: id1 < id2)
antonyms = set()
antonyms_txt = set()

### store ilis for antonym pairs
for synset in en.synsets():
    if syn_anto := synset.get_related('antonym'):
        print(synset, syn_anto)
    for sense in synset.senses():
        if sen_anto := sense.get_related('antonym'):
            for anto in sen_anto:
                antonyms.add(tuple(sorted([sense.synset().ili.id, anto.synset().ili.id])))

dict_ant = {}

for (a1, a2) in antonyms:
    s1 = ja.synsets(ili=a1)
    s2 = ja.synsets(ili=a2)
    for s in s1:
        for lem in s.lemmas():
            got_ants = [s.lemmas() for s in s2]
            if got_ants:
                dict_ant[lem] = got_ants

for d in dict_ant:
    print(d, *itertools.chain.from_iterable(dict_ant[d]))

print(len(dict_ant))