Search code examples
pythonnltkwordnet

Error while printing gloss definitions in python


I have following list of synsets in python:

string = ["Synset('bank.n.01')", "Synset('computer.n.01')", "Synset('work.v.02')", "Synset('super.a.01')"]

and I am trying to combine gloss definitions of individual in the following manner:

string1 = ""
for w in string:
     string1 = string1 + w.definition

But it is giving me following error:

Traceback (most recent call last):
   File "<stdin>", line 2, in <module>
AttributeError: 'str' object has no attribute 'definition'

But if I do

for w in wn.synsets("bank"):
    print w.definition

It runs successfully and gives correct result. Please tell me what should I do?


Solution

  • Question: Why do you have Synsets objects as strings?

    Native string objects in python doesn't have definition attribute, they only have these functions/attributes: https://docs.python.org/2/library/string.html

    What you need is a Synset object from NLTK, see http://www.nltk.org/_modules/nltk/corpus/reader/wordnet.html

    Going back to your code, what you need is the key to access the Synsets, e.g. bank.n.01:

    >>> from nltk.corpus import wordnet as wn
    >>> import re
    >>> list_of_synsets_in_str = ["Synset('bank.n.01')", "Synset('computer.n.01')", "Synset('work.v.02')", "Synset('super.a.01')"]
    >>> losis = list_of_synsets_in_str
    >>> [re.findall("'([^']*)'", i)[0] for i in losis]
    ['bank.n.01', 'computer.n.01', 'work.v.02', 'super.a.01']
    

    Then with the key cast it into a Synset object:

    >>> [wn.synset(re.findall("'([^']*)'", i)[0]) for i in losis]
    [Synset('bank.n.01'), Synset('computer.n.01'), Synset('work.v.02'), Synset('ace.s.01')]
    

    Then you can access the definition from the wn.synset(x).defintion():

    >>> list_of_synsets = [wn.synset(re.findall("'([^']*)'", i)[0]) for i in losis]
    >>> for i in list_of_synsets:
    ...     print i, i.definition()
    ... 
    Synset('bank.n.01') sloping land (especially the slope beside a body of water)
    Synset('computer.n.01') a machine for performing calculations automatically
    Synset('work.v.02') be employed
    Synset('ace.s.01') of the highest quality