Python:从偏移输入中检索WordNet上位词

时间:2016-02-22 19:54:18

标签: python nltk wordnet

我知道如何获得单词的上位词,如下:

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')

我的问题是,如果我想搜索hypernym的{​​{1}},我该如何更改此当前代码?

例如,给定偏移量offset,输出其上位词。上位词可以像我的例子一样以01234567-n形式输出,或者(并且最好)以synset形式输出。感谢。

1 个答案:

答案 0 :(得分:2)

这是来自pywsd的可爱功能,最初来自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]))