[英]How to get all the hyponyms of a word/synset in python nltk and wordnet?
我有一個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()]))
這將為您提供每個同義詞集中的所有獨特單詞,這是名詞“vehicle”(第一感)的下位詞。
def get_hyponyms(synset):
hyponyms = set()
for hyponym in synset.hyponyms():
hyponyms |= set(get_hyponyms(hyponym))
return hyponyms | set(synset.hyponyms())
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.