Re: [nltk-users] [synset.lemma_names for synset in synset_list] leads to AttributeError: 'list' object has no attribute 'lower'

1,174 views
Skip to first unread message

Michael Deeringer

unread,
Sep 8, 2012, 2:55:05 PM9/8/12
to nltk-...@googlegroups.com
What type are the elements of lemma_list? What type should lemma be?


On Sat, Sep 8, 2012 at 1:16 PM, typetoken <type...@gmail.com> wrote:
On page 77 of the book natural language processing with Python, we have such an exercise: The polysemy of a word is the number of senses it has. Using WordNet, we can determine that the noun doghas seven senses with len(wn.synsets('dog', 'n')).Compute the average polysemy of nouns, verbs, adjectives, and adverbs according

I wrote the following function to solve it. However, it pops up "AttributeError: 'list' object has no attribute 'lower'". Quite confused, I supposed lemma_list=[synset.lemma_names for synset in synset_list] has made all the lemmas into a new list, hasn't it?

>>> def average_polysemy(pos):
synset_list = list(wn.all_synsets(pos))
lemma_list = [synset.lemma_names for synset in synset_list]
sense_number = 0
for lemma in lemma_list:
sense_number_new = len(wn.synsets(lemma, pos))
sense_number = sense_number + sense_number_new
return sense_number/len(synset_list)

>>> average_polysemy('n')

Traceback (most recent call last):
  File "<pyshell#54>", line 1, in <module>
    average_polysemy('n')
  File "<pyshell#53>", line 6, in average_polysemy
    sense_number_new = len(wn.synsets(lemma, pos))
  File "C:\Python27\lib\site-packages\nltk\corpus\reader\wordnet.py", line 1191, in synsets
    lemma = lemma.lower()
AttributeError: 'list' object has no attribute 'lower'

Thanks for your tips

--
 
 

Steven Bird

unread,
Sep 8, 2012, 2:55:31 PM9/8/12
to nltk-...@googlegroups.com
The error message shows that you are attempting to apply a string
method to a list. If you print out the value of "lemma" inside the
loop, you will see that it is a list (of lemma_names). You cannot look
up the synset for a list, and this is why you're seeing an error.
Perhaps you need a nested loop, to go over each lemma_name?

John H. Li

unread,
Sep 9, 2012, 12:52:25 AM9/9/12
to nltk-...@googlegroups.com
Thanks very much for all of your tips. Take noun as an example. First, I need find all the lemma_names in all the synsets whose pos is 'n'. Second, for each lemma_name, I will check all their sense number. 

1)  Surely,we can know the number of synset whose pos is noun by 
>>> len([synset for synset in wn.all_synsets('n')])
82115

However, confusingly it is unsuccessful to get a list of lemma names of these synsets by 
>>> lemma_list = [synset.lemma_names for synset in wn.all_synsets('n')]
>>> lemma_list[:20]
[['entity'], ['physical_entity'], ['abstraction', 'abstract_entity'], ['thing'], ['object', 'physical_object'], ['whole', 'unit'], ['congener'], ['living_thing', 'animate_thing'], ['organism', 'being'], ['benthos'], ['dwarf'], ['heterotroph'], ['parent'], ['life'], ['biont'], ['cell'], ['causal_agent', 'cause', 'causal_agency'], ['person', 'individual', 'someone', 'somebody', 'mortal', 'soul'], ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna'], ['plant', 'flora', 'plant_life']]
>>> type(lemma_list)
<type 'list'>

Though the lemma_list is a list in the above codes, it contains so many unnecessary [ and ]. How come it is like this? But what we desire and expect is a list without this brackets. Confused, I am really curious to know why.

2)  Then I have to use a loop and extend to get all the lemma_names from synset:
>>> synset_list = list(wn.all_synsets('n'))
>>> lemma_list = []
>>> for synset in synset_list:
lemma_list.extend(synset.lemma_names)
>>> lemma_list[:20]
['entity', 'physical_entity', 'abstraction', 'abstract_entity', 'thing', 'object', 'physical_object', 'whole', 'unit', 'congener', 'living_thing', 'animate_thing', 'organism', 'being', 'benthos', 'dwarf', 'heterotroph', 'parent', 'life', 'biont']

3) In this case, I have to use loop to get all the lemma_names instead of [synset.lemma_names for synset in wn.all_synsets('n')]. The following is a working solution:

>>> def average_polysemy(pos):
 synset_list = list(wn.all_synsets(pos))
 sense_number = 0
 lemma_list = []
 for synset in synset_list:
  lemma_list.extend(synset.lemma_names)
 for lemma in lemma_list:
  sense_number_new = len(wn.synsets(lemma, pos))
  sense_number = sense_number + sense_number_new
 return sense_number/len(synset_list)

>>> average_polysemy('n')
3

Thanks again.


--



Reply all
Reply to author
Forward
0 new messages