How to get first synset from list of Sentiwordnet?

1k Views Asked by At

I have a review text and I want to define if it is positive or negative. I 'm using sentiwordnet for getting the score of each word in the review. My problem is since each word has multiple synset I want only the first one: for example:

swn.senti_synsets('slow')
[SentiSynset('decelerate.v.01'), SentiSynset('slow.v.02'), \
SentiSynset('slow.v.03'), SentiSynset('slow.a.01'), SentiSynset('slow.a.02'), \
SentiSynset('slow.a.04'), SentiSynset('slowly.r.01'), SentiSynset('behind.r.03')]

I want the first one which is SentiSynset('decelerate.v.01') Here is my code:

Text  = "  I love the movie but hate the music"

word_tok = word_tokenize(Text)
for i in word_tok :
 g = nltk.tag.pos_tag([i])
 for word, tag in g:
   if tag.startswith('JJ'):
     new = 'a'
   elif  tag.startswith('V'):
     new = 'v'
   elif  tag.startswith('R'):
      new = 'r'
   else:
        new =''
   if new != '':
        synsets = list(swn.senti_synsets(word, new))
        b  = synsets[0]

First I tokenize the text, Then I get the tag of each word and change it to the tag that recognizes by Sentiwordnet. If the word is adjective/adverb/verb I want their first synset to get the pos/neg score. when I run this script I get the error

Traceback (most recent call last):
  File "C:\Python34\test2.py", line 39, in <module>
    b  = synsets[0]
IndexError: list index out of range

Can anyone see where I get wrong in my code? Thanks in advance

0

There are 0 best solutions below