我有一个 wordnet 中所有名词的列表,现在我只想留下作为车辆的单词并删除其余的单词。我该怎么做?下面是我想制作的伪代码,但我不知道如何使它工作
for word in wordlist:
if not "vehicle" in wn.synsets(word):
wordlist.remove(word)
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()]))
这将为您提供来自每个同义词集中的所有独特词,这些词是名词“车辆”(第一种感觉)的下义词。
def get_hyponyms(synset):
hyponyms = set()
for hyponym in synset.hyponyms():
hyponyms |= set(get_hyponyms(hyponym))
return hyponyms | set(synset.hyponyms())