Using nltk Sentiwordnet with python

I am doing twitter sentiment analysis using python NLTK. I need a dictionary that contains + ve and -ve polarity words. I read so much about sentiwordnet, but when I use it for my project, it does not give effective and quick results. I think that I am not using it correctly. Can someone tell me the correct way to use it? Here are the steps I have taken so far:

  • tokenization of tweets
  • Token POS Marking
  • passing each tag to sentinet

I use the nltk package for tokenization and tagging. See part of my code below:

import nltk
from nltk.stem import *
from nltk.corpus import sentiwordnet as swn

tokens=nltk.word_tokenize(row) #for tokenization, row is line of a file in which tweets are saved.
tagged=nltk.pos_tag(tokens) #for POSTagging

for i in range(0,len(tagged)):
     if 'NN' in tagged[i][1] and len(swn.senti_synsets(tagged[i][0],'n'))>0:
            pscore+=(list(swn.senti_synsets(tagged[i][0],'n'))[0]).pos_score() #positive score of a word
            nscore+=(list(swn.senti_synsets(tagged[i][0],'n'))[0]).neg_score()  #negative score of a word
    elif 'VB' in tagged[i][1] and len(swn.senti_synsets(tagged[i][0],'v'))>0:
           pscore+=(list(swn.senti_synsets(tagged[i][0],'v'))[0]).pos_score()
           nscore+=(list(swn.senti_synsets(tagged[i][0],'v'))[0]).neg_score()
    elif 'JJ' in tagged[i][1] and len(swn.senti_synsets(tagged[i][0],'a'))>0:
           pscore+=(list(swn.senti_synsets(tagged[i][0],'a'))[0]).pos_score()
           nscore+=(list(swn.senti_synsets(tagged[i][0],'a'))[0]).neg_score()
    elif 'RB' in tagged[i][1] and len(swn.senti_synsets(tagged[i][0],'r'))>0:
           pscore+=(list(swn.senti_synsets(tagged[i][0],'r'))[0]).pos_score()
           nscore+=(list(swn.senti_synsets(tagged[i][0],'r'))[0]).neg_score()

In the end, I will calculate how many tweets are positive and how many tweets are negative. Where am I mistaken? How to use it? And is there any other similar dictionary that is easy to use?

+4
1

, , . : http://sentiment.christopherpotts.net/lexicons.html#resources , Bing Liu Opinion Lexicon .

, - - .

+3

Source: https://habr.com/ru/post/1617751/


All Articles