我有 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())