Python123
Python123

Reputation: 71

Get verb from noun Wordnet python

I'm trying to get the verb from a noun with Wordnet in python. Here is the code:

nouns = ["slip", "frustration"]
def nominalization(noun_word):
   set_of_related_verbs = set()

   for lemma in wn.lemmas(wn.morphy(noun_word, wn.NOUN), pos="n"):
      for related_form in lemma.derivationally_related_forms():
        set_of_related_verbs.add(related_form)
   return set_of_related_verbs

for word in nouns:
    set_of_words = nominalization(word)
    if len(set_of_words) >= 1:
        print word + "+++"
        print set_of_words

I need only verbs but I get the others pos too for example: slip+++ set([Lemma('slippery.a.01.slippy'), Lemma('err.v.01.slip')])

How can I change this?

If I use the line:

for synset in wn.synsets(related_form.name(), pos=wn.VERB):

I get only the verbs but some verbs can be not related to my noun for example: acquiring+++ set([Synset('get.v.01'), Synset('assume.v.03'), Synset('grow.v.08'), Synset('acquire.v.04'), Synset('acquire.v.05'), Synset('develop.v.03'), Synset('learn.v.01')])

Upvotes: 3

Views: 1451

Answers (1)

Sriram Sitharaman
Sriram Sitharaman

Reputation: 857

You can derive the lemmas and related word forms from which you can extract the verbs:

import nltk as nltk
from nltk.corpus import wordnet as wn

lem = wn.lemmas('frustration')
print lem

Result:

[Lemma('frustration.n.01.frustration'), Lemma('frustration.n.02.frustration'), Lemma('frustration.n.03.frustration')]

From which you can get the verbs like this:

related_forms = [lem[i].derivationally_related_forms() for i in range(len(lem))]
print related_forms

Result:

[[Lemma('thwart.v.01.frustrate')], [Lemma('thwart.v.01.frustrate')], [Lemma('thwart.v.01.frustrate'), Lemma('torment.v.02.frustrate')]]

Upvotes: 1

Related Questions