user47467
user47467

Reputation: 1093

Python: Retrieving WordNet hypernyms from offset input

I know how to get hypernyms of words, like so :

word = 'girlfriend'
word_synsets = wn.synsets(word)[0]

hypernyms = word_synsets.hypernym_paths()[0]

for element in hypernyms:
    print element


Synset('entity.n.01')
Synset('physical_entity.n.01')
Synset('causal_agent.n.01')
Synset('person.n.01')
Synset('friend.n.01')
Synset('girlfriend.n.01')

My question is, if I wanted to search for the hypernym of an offset, how would I change this current code?

For example, given the offset 01234567-n its hypernyms are outputted. The hypernyms can either be outputted in synset form like my example, or (and preferably) as offset form. Thanks.

Upvotes: 1

Views: 235

Answers (1)

alvas
alvas

Reputation: 122012

Here's a cute function from pywsd that's originally from http://moin.delph-in.net/SemCor

def offset_to_synset(offset):
    """ 
    Look up a synset given offset-pos 
    (Thanks for @FBond, see http://moin.delph-in.net/SemCor)
    >>> synset = offset_to_synset('02614387-v')
    >>> print '%08d-%s' % (synset.offset, synset.pos)
    >>> print synset, synset.definition
    02614387-v
    Synset('live.v.02') lead a certain kind of life; live in a certain style
    """
    return wn._synset_from_pos_and_offset(str(offset[-1:]), int(offset[:8]))

Upvotes: 2

Related Questions