gpt4 book ai didi

python - nltk:如何搜索某些单词之间的联系?

转载 作者:行者123 更新时间:2023-11-28 18:03:54 46 4
gpt4 key购买 nike

我正在使用 nltk 和 wordnet 来链接属于某些关系组的单词。例如'parking' 和'building' 应该有一些父链接。我使用上位词,但有些词没有联系。

x = wordnet.synset('parking.n.01')
y = wordnet.synset('building.n.01')

print(x._shortest_hypernym_paths(y))
print(y._shortest_hypernym_paths(x))

{Synset('parking.n.01'): 0, Synset('room.n.02'): 1, Synset('position.n.07'): 2, Synset('relation.n.01'): 3, Synset('abstraction.n.06'): 4, Synset('entity.n.01'): 5, Synset('ROOT'): 6} {Synset('building.n.01'): 0, Synset('structure.n.01'): 1, Synset('artifact.n.01'): 2, Synset('whole.n.02'): 3, Synset('object.n.01'): 4, Synset('physical_entity.n.01'): 5, Synset('entity.n.01'): 6, Synset('ROOT'): 7}

在这里,连接通过“entity.n.01”,老实说,它是几乎所有物理名词的词根。我怎样才能得到比这更好的东西?

我想要得到像'parking' -> 'structure' -> 'building'这样的东西;它可以更长,但“外来”词不应该出现在那里,例如“猴子”,它也可以压缩到实体。

最佳答案

找到一些有用的方法来查看可能性:

def getShortestHypernymPath(word1, word2, nulls=False):
syns1 = wordnet.synsets(word1)
syns2 = wordnet.synsets(word2)
for s1 in syns1:
for s2 in syns2:
lch = s2.lowest_common_hypernyms(s1)
if len(lch) > 0 or nulls:
print(s1, '<-->', s2, '===', lch)

nlpf.getShortestHypernymPath('parking', 'building', nulls=False)

返回:

Synset('parking.n.01') <--> Synset('building.n.01') === [Synset('entity.n.01')] Synset('parking.n.01') <--> Synset('construction.n.01') === [Synset('abstraction.n.06')] Synset('parking.n.01') <--> Synset('construction.n.07') === [Synset('abstraction.n.06')] Synset('parking.n.01') <--> Synset('building.n.04') === [Synset('abstraction.n.06')] Synset('parking.n.02') <--> Synset('building.n.01') === [Synset('entity.n.01')] Synset('parking.n.02') <--> Synset('construction.n.01') === [Synset('act.n.02')] Synset('parking.n.02') <--> Synset('construction.n.07') === [Synset('act.n.02')] Synset('parking.n.02') <--> Synset('building.n.04') === [Synset('abstraction.n.06')] Synset('park.v.02') <--> Synset('build.v.05') === [Synset('control.v.01')]

所以我至少可以对此进行调解。

关于python - nltk:如何搜索某些单词之间的联系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54636917/

46 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com