I am getting this problem . my python code is

 for i,j in nltk.pos_tag(words):
        print i,j
        if 'JJ' in j:
            pj=(list(swn.senti_synsets(i,'a'))[0]).pos_score()
            print "pj:" ,pj
        elif 'RB' in j:
            pr =(list(swn.senti_synsets(i,'r'))[0]).pos_score()
            print "pr:" ,pr
        elif 'NN' in j:
            pn =(list(swn.senti_synsets(i,'n'))[0]).pos_score()
            print "pn:" ,pn
        elif 'VB' in j:
            pv =(list(swn.senti_synsets(i,'v'))[0]).pos_score()
            print "pv:" ,pv

where "words" is a list of words containing adjective , adverb , noun etc..
words=['good','only','excellent','really'.........] nearly 300 such words. when i run this code it works for first 2 words .that is positive score of good and only will b displayed. but when it goes to word "excellent " there is this error..
output:
good JJ
pj: 0.75
only RB
pr: 0.0
excellent JJ

    Traceback (most recent call last):
      File "C:\Python27\mat to file.py", line 39, in <module>
        pj+=(swn.senti_synsets(i,'a')[0]).pos_score()
    IndexError: list index out of range

please help !!!!

Dani AI

Generated

The IndexError comes from indexing the first element of an empty SentiWordNet result. As suggested, SentiWordNet lookups sometimes return no matches for a given lemma+POS, so blindly doing something like [0] will fail. Also note ’s point that the code actually run must be checked (using += requires the accumulator to be initialized first), so compare the exact running code with the snippet shown.

A practical debug sequence: print the token, its POS tag, and the contents of the SentiWordNet lookup (repr(list(...))) and the WordNet synsets (wn.synsets(...)) to see what exists. Normalize the token (lowercase + lemmatize) and map the POS tag to WordNet/SentiWordNet POS before lookup. If the senti lookup is empty, either try the WordNet synsets for that lemma and convert the first synset to a senti synset, or fall back to a neutral score (0.0). Always initialize any accumulators before using +=.

Example safe lookup pattern (illustrates the ideas above):

from nltk.corpus import wordnet as wn
from nltk.corpus import sentiwordnet as swn
from nltk.stem import WordNetLemmatizer

def tag_to_swn(tag):
    if tag.startswith('J'): return 'a'
    if tag.startswith('R'): return 'r'
    if tag.startswith('N'): return 'n'
    if tag.startswith('V'): return 'v'
    return None

lemmatizer = WordNetLemmatizer()
scores = {}

for token, tag in nltk.pos_tag(words):
    swn_pos = tag_to_swn(tag)
    lemma = token.lower()
    if swn_pos:
        lemma = lemmatizer.lemmatize(lemma, pos=swn_pos)
        syns = wn.synsets(lemma, pos=swn_pos)
        score = None
        for syn in syns:
            try:
                ss = swn.senti_synset(syn.name())
            except LookupError:
                continue
            score = ss.pos_score()
            break
        if score is None:
            score = 0.0
    else:
        score = 0.0
    scores[token] = score

Notes and cautions: avoid direct [0] access without checking length; accept 0.0 as a valid sentiment when no positive score is found; ensure NLTK corpora (sentiwordnet, wordnet) are installed; and confirm the actual running file matches the posted snippet (initialization and operator differences can hide bugs).

Recommended Answers

All 2 Replies

The list list(swn.senti_synsets(i,'a')) is probably the empty list [ ] You could check this by printing its repr() for example. Your program does not (yet) handle the case where this list is empty. You must add some code.

commented: thanku..it was tagged wrongly +0

Also the example code and the example error message you gave us are different, the error message has pj+=(swn.senti_synsets(i,'a')[0]).pos_score() and the example code has pj=(list(swn.senti_synsets(i,'a'))[0]).pos_score(). Not sure what you're trying to accomplish with this code, but I'm pretty sure you get your error from code that wasn't shown in the example.

commented: code has pj+=(list(swn.senti_synsets(i,'a')[0]).pos_score() . i forgot to include. thankyou. +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.