我现在有一个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()]))
这将为您提供名词“车辆”(第1个意义)的每个下位词的所有唯一单词。
def get_hyponyms(synset):
hyponyms = set()
for hyponym in synset.hyponyms():
hyponyms |= set(get_hyponyms(hyponym))
return hyponyms | set(synset.hyponyms())
Synset.closure(lambda s:s.hyponyms()
进入无限循环时,有一个gotcha
。尝试wn.synset('restrain.v.01').closure(lambda s:s.hyponyms()
。 - alvaslemma_names
是一个方法,因此应该添加括号。不知道是否正确,但我将其留在这里供熟悉此模块的人审查。 - Tom Zych