如何在python nltk和wordnet中获取单词/同义词集的所有下位词?

问题描述 投票:0回答:2

我有 WordNet 中所有名词的列表。我想删除所有不是车辆的名词。我该怎么做?下面是我想要制作的伪代码,但我不知道如何使其工作:

for word in wordlist:
  if not "vehicle" in wn.synsets(word):
    wordlist.remove(word)
python nltk wordnet
2个回答
14
投票
from nltk.corpus import wordnet as wn
vehicle = wn.synset('vehicle.n.01')
typesOfVehicles = list(set([w for s in vehicle.closure(lambda s:s.hyponyms()) for w in s.lemma_names()]))

这将为您提供每个同义词集中的所有独特单词,即名词“车辆”的下义词(第一义)。


9
投票
def get_hyponyms(synset):
    hyponyms = set()
    for hyponym in synset.hyponyms():
        hyponyms |= set(get_hyponyms(hyponym))
    return hyponyms | set(synset.hyponyms())
© www.soinside.com 2019 - 2024. All rights reserved.