AttributeError: 'function' object has no attribute 'lower'

5k Views Asked by At

I am trying to use sentiwordnet text file "SentiWordNet_3.0.0_20130122.txt". When I am importing the sentiwordnet.py file and attempting to run it I am getting the error as follows:

The error occured as:

--------------------------------------------------------------------------         AttributeError
Traceback (most recent call last)
<ipython-input-13-6e4eb476b4b2> in <module>()
----> 1 happy = cv.senti_synsets('happy', 'a')

C:\Users\Desktop\fp\sentiwordnet.pyc in     senti_synsets(self, string, pos)
     65         synset_list = wn.synsets(string, pos)
     66         for synset in synset_list:
---> 67             sentis.append(self.senti_synset(synset.name))
     68         sentis = filter(lambda x : x, sentis)
     69         return sentis

C:\Users\Desktop\fp\sentiwordnet.pyc in senti_synset(self, *vals)
     52             return SentiSynset(pos_score, neg_score, synset)
     53         else:
---> 54             synset = wn.synset(vals[0])
     55             pos = synset.pos
     56             offset = synset.offset

C:\Users\Anaconda2\lib\site-packages\nltk\corpus\reader\wordnet.pyc in synset(self, name)
     1227     def synset(self, name):
     1228         # split name into lemma, part of speech and synset number
---> 1229         lemma, pos, synset_index_str = name.lower().rsplit('.', 2)
     1230         synset_index = int(synset_index_str) - 1
     1231 

AttributeError: 'function' object has no attribute 'lower'

My code:

import sentiwordnet as snw
SWN_FILENAME = "SentiWordNet_3.0.0_20130122.txt"
cv = snw.SentiWordNetCorpusReader(SWN_FILENAME)
happy = cv.senti_synsets('happy', 'a')
print happy
1

There are 1 best solutions below

2
On

Could you try out the following code:

import nltk.corpus.reader.sentiwordnet as snw
SWN_FILENAME = "SentiWordNet_3.0.0_20130122.txt"
cv = snw.SentiWordNetCorpusReader('', [SWN_FILENAME])
happy = cv.senti_synsets('happy', 'a')
print(happy)

I think the sentiwordnet.py file you're using does not match to the nltk package you have installed. I was able to reproduce your error if I used such a mismatched pair. The code above uses the SentiWordNetCorpusReader which comes with the nltk package (see NLTK sources); however, the constructor for the reader wants as first argument a 'root' (I do not know nltk and have no clue what this might be ;-)), as the second it wants a list of file-ids (I think they mean filenames, however the code seems to be flexible enough to just accept also a filename as string).

You could also use another package version of nltk which matches your sentiwordnet.py file, I guess.