我知道如何获得单词的上位词,如下:
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
形式输出。感谢。
答案 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]))