3

When I try to print the hypernym, I just want the word rather the all the information about the word.

pp = wn.synset('grow.v.01')
pp1= pp.hypernyms()
print pp1

My output is [Synset('change.v.02')]. I just want "change". What change do i need to do? Sorry I am new to wordnet.

code ninja
  • 31
  • 2
  • See https://stackoverflow.com/questions/27517924/extract-word-from-synset-using-wordnet-in-nltk-3-0 – alvas Jun 16 '17 at 08:31

1 Answers1

1

You can use the lemma_names function of the Synset object. Bear in mind it returns list of names, you can pick the one you are happy with (in this case its only 1 result 'change').

>> print(pp1[0].lemma_names())
['change']

Also calling hypernyms() also returns you a list, thus I used pp1[0]. For example querying for 'dog' returns [dog, frump, cad...] etc.. If you want to get all lemma_names for all hypernyms, you can use a list comprehension.

>> [s.lemma_names() for s in wn.synsets('dog')]
[['dog', 'domestic_dog', 'Canis_familiaris'],
 ['frump', 'dog'],
 ['dog'],
 ...
 ['chase', 'chase_after', 'trail', 'tail', 'tag', 'give_chase', 'dog', 'go_after',  'track']]
umutto
  • 7,460
  • 4
  • 43
  • 53
  • thanks, with print(pp1[0].lemma_names()), I get the output [u'change'], what does 'u' mean and why it differs from yours? It that a wordnet version issue or something else? – code ninja Jun 16 '17 at 03:16
  • @codeninja `u` keyword means it is a `unicode` string. I wrote my response on python 3, where literal strings are encoded as `unicode` by *default*, thus the difference. In anyways its just how it is represented on console, shouldn't make any difference for you, you are handling `change`, `u` just shows you its encoding. (If you print a member of the list ie: `print(pp1[0].lemma_names()[0])` I believe it should print out without the unicode keyword in python 2 too) – umutto Jun 16 '17 at 03:22