How to get all the hyponyms of a word/synset in python nltk and wordnet?

隐身守侯 提交于 2019-11-26 16:45:45

问题


I have a list of all the nouns in wordnet now i want to leave only words which are vehicles and remove the rest. How do i do it? Below is the pseudo-code i want to make but i do not know how to make it work

for word in wordlist:
  if not "vehicle" in wn.synsets(word):
    wordlist.remove(word)

回答1:


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()]))

This will give you all the unique words from every synset that is a hyponym of the noun "vehicle" (1st sense).




回答2:


def get_hyponyms(synset):
    hyponyms = set()
    for hyponym in synset.hyponyms():
        hyponyms |= set(get_hyponyms(hyponym))
    return hyponyms | set(synset.hyponyms())


来源:https://stackoverflow.com/questions/15330725/how-to-get-all-the-hyponyms-of-a-word-synset-in-python-nltk-and-wordnet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!