Python:从offset inpu中检索WordNet超文本词

2024-04-20 10:37:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我知道如何得到单词的缩写,比如:

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

我的问题是,如果我想搜索offsethypernym,我将如何更改当前的代码?在

例如,给定偏移量01234567-n,它的上一个词被输出。可以像我的例子一样以synset形式输出,或者(最好)以offset形式输出。谢谢。在


Tags: inforelement单词形式wordoffsetentity
1条回答
网友
1楼 · 发布于 2024-04-20 10:37:20

这是一个来自^{}的可爱函数,它最初来自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]))

相关问题 更多 >