【发布时间】:2013-02-26 03:58:25
【问题描述】:
我有一个 wordnet 中所有名词的列表,现在我想只留下作为车辆的单词并删除其余的单词。我该怎么做?下面是我想做的伪代码,但我不知道如何让它工作
for word in wordlist:
if not "vehicle" in wn.synsets(word):
wordlist.remove(word)
【问题讨论】:
我有一个 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()]))
这将为您提供来自每个同义词集中的所有唯一词,这些词是名词“车辆”(第一种意义)的 hyponym。
【讨论】:
Synset.closure(lambda s:s.hyponyms() 进入无限循环时,会有一个gotcha。试试wn.synset('restrain.v.01').closure(lambda s:s.hyponyms()
TypeError: 'method' object is not iterable。
lemma_names 是一种方法,因此应该添加括号。不知道这是否正确,但我将其留在这里供熟悉此模块的人查看。
def get_hyponyms(synset):
hyponyms = set()
for hyponym in synset.hyponyms():
hyponyms |= set(get_hyponyms(hyponym))
return hyponyms | set(synset.hyponyms())
【讨论】: