pythonsenti-wordnet

AttributeError: 'tuple' object has no attribute 'lower' sentiword


I try this code to calculate the sentiment using sentiword, I tried tokenization and pos tagging it works but when I tried this I get error message

print("Array..............\n\n")
tagged=np.array(df['tagged_texts'])
print(tagged)
pos=neg=obj=count=0
for word, tag in tagged:
    ss_set = None
    if 'NN' in tag and swn.senti_synsets(word):
        ss_set = list(swn.senti_synsets(word))[0]
    elif 'VB' in tag and swn.senti_synsets(word):
        ss_set = list(swn.senti_synsets(word))[0]
    elif 'JJ' in tag and swn.senti_synsets(word):
         ss_set = list(swn.senti_synsets(word))[0]
    elif 'RB' in tag and swn.senti_synsets(word):
         ss_set = list(swn.senti_synsets(word))[0]
    if ss_set:
        pos=pos+synset.pos_score()
        neg=neg+synset.neg_score()
        obj=obj+synset.obj_score()
        count+=1

I get error

    Array..............


[list([('no', 'DT'), ('coment', 'NN')])
 list([('fast', 'RB'), ('respon', 'NN')]) list([('giood', 'NN')]) ...
 list([('excelent', 'NN')]) list([('givemore', 'NN'), ('promo', 'NN')])
 list([('thankss', 'NN'), ('gojekkk', 'NN')])]
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-45-bb3df6752c85> in <module>
      5 for word, tag in tagged:
      6     ss_set = None
----> 7     if 'NN' in tag and swn.senti_synsets(word):
      8         ss_set = list(swn.senti_synsets(word))[0]
      9     elif 'VB' in tag and swn.senti_synsets(word):

~\Anaconda3\lib\site-packages\nltk\corpus\reader\sentiwordnet.py in senti_synsets(self, string, pos)
     94 
     95         sentis = []
---> 96         synset_list = wn.synsets(string, pos)
     97         for synset in synset_list:
     98             sentis.append(self.senti_synset(synset.name()))

~\Anaconda3\lib\site-packages\nltk\corpus\reader\wordnet.py in synsets(self, lemma, pos, lang, check_exceptions)
   1573         of that language will be returned.
   1574         """
-> 1575         lemma = lemma.lower()
   1576 
   1577         if lang == 'eng':

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

Can anyone help me to understand where this code got wrong? thank you


Solution

  • tagged begins as a list of pairs of tuples. So, word and tag are both tuples. e.g. the first values for word and tag will be

    word : ('no', 'DT') 
    tag : ('coment', 'NN')
    

    Perhaps, try:

    temp = []
        for x in tagged: 
            for y in x: 
                temp.append(y) 
    tagged = temp
    

    before running the loop - assuming you want to iterate over the tuples in your main loop.