How to get Sense Key in WordNet for NLTK Python?

4.3k views Asked by At

Hi Stackoverflow Community

I just started tinkering around with Python NLTK and have directed my attention to the Wordnet module.

I am attempting to get the Sense Ky for a given lemma and found the following:

s = wn.synset('skill.n.01')
s.lemmas # >>> [Lemma('skill.n.01.skill'), ... ]
s.lemmas[0].key # >>> 'skill%1:09:01::'

However, this implementation doesn't seem to be supported anymore.

Traceback (most recent call last):
File "C:/Users/Admin/PycharmProjects/momely/placementarchitect/testbench.py", line 59, in <module>
s.lemmas[0].key
TypeError: 'method' object is not subscriptable

I am wondering whether anyone would be able to point me in the right direction as to how I might be able to get the sense key given a lemma or synset?

Any advice would be highly appreciated!

3

There are 3 answers

0
alvas On BEST ANSWER

Take a look at https://stackoverflow.com/a/27518899/610569 for the difference between, Synset.lemmas()[0].key and Synset.lemmas()[0].key():

>>> from nltk.corpus import wordnet as wn
>>> wn.synset('dog.n.1')
Synset('dog.n.01')
>>> wn.synset('dog.n.1').lemmas()
[Lemma('dog.n.01.dog'), Lemma('dog.n.01.domestic_dog'), Lemma('dog.n.01.Canis_familiaris')]
>>> wn.synset('dog.n.1').lemmas()[0]
Lemma('dog.n.01.dog')
>>> wn.synset('dog.n.1').lemmas()[0].name()
u'dog'

# To retrieve Princeton WordNet style keys.
>>> wn.synset('dog.n.1').lemmas()[0].key()
u'dog%1:05:00::'

For Open Multilingual WordNet, using the offset + pos keys would be easier, e.g.:

>>> from nltk.corpus import wordnet as wn
>>> ss = wn.synset('dog.n.1')
>>> ss.offset()
2084071
>>> ss.pos()
u'n'
>>> '{}-{}'.format(str(ss.offset()).zfill(8), ss.pos())
'02084071-n'

Searching the offset + pos key (e.g. 02084071-n) on the OMW interface: http://compling.hss.ntu.edu.sg/omw/cgi-bin/wn-gridx.cgi?gridmode=grid will get you to a nice visualization page of the synset.

1
bouteillebleu On

If you look at an example of this module, e.g. in http://www.nltk.org/howto/wordnet.html, lemmas is a method - so you need to call it with () after it, for example:

s = wn.synset('skill.n.01')
s.lemmas() # >>> [Lemma('skill.n.01.skill'), ... ]

Then you can access the return value like so:

s.lemmas()[0].key # >>> 'skill%1:09:01::'

The TypeError: 'method' object is not subscriptable error message is telling you that you're trying to treat a method or function like a list - when you see an error like that, look for something you have [0] or similar with that might be a function.

0
Mike Nedelko On

So I figured it out. After a bit of digging in NLTK's Wordnet API, I noticed that the API appears to require the key to be called like this:

s = wn.synset('skill.n.01')
print(s.lemmas) # >>> <bound method Synset.lemmas of Synset('skill.n.01')>

print(str(s.lemmas()[0]._key)) #>>>skill%1:09:01::

As such the call ._key successfully returns the Sense Key.

Thank you @bouteillebleu for pointing me in the right direction to start with.

M