Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added SentiWordNet Approach.
  • Loading branch information
adl13006 committed Apr 16, 2016
1 parent 31fad1a commit b4c94c3
Show file tree
Hide file tree
Showing 2 changed files with 342 additions and 249 deletions.
44 changes: 44 additions & 0 deletions SentiWordLex.py
@@ -0,0 +1,44 @@
from __future__ import division
import sys
import time

import nltk
from nltk.corpus import movie_reviews
from nltk.corpus import sentiwordnet as swn
from nltk.corpus import wordnet as wn

start_time = time.time()
count = 0.00
correct = 0.00
ids = sorted(movie_reviews.fileids())

for reviews in ids: #For every review
score = 0.0
positive = 0.0
negative = 0.0
tokens = nltk.pos_tag(nltk.word_tokenize(movie_reviews.raw(fileids=[reviews]))) #Tokenize all words with POS
for token in tokens:
if (token[1]== "JJ" or token[1] == "JJR" or token[1] == "JJS"): # If adjective, check value
if len(wn.synsets(token[0], pos=wn.ADJ)) != 0 and swn.senti_synset(wn.synsets(token[0], pos=wn.ADJ)[0].name()) :
word = wn.synsets(token[0], pos=wn.ADJ)[0].name()
print word
print swn.senti_synset(word)
positive = positive + swn.senti_synset(word).pos_score()
negative = negative + swn.senti_synset(word).neg_score()
print "%s, %d, %d" %(word,positive,negative)
score = positive - negative
if (score < 0):
print "Negative at %f" % (score)
sentiment = 'neg'
else:
sentiment = 'pos'
print "Positive at %d" % (score)
if (sentiment == movie_reviews.categories(fileids=[reviews])[0]):
print "Correct"
correct = correct + 1.00
count = count + 1.00

print correct/count
print "Seconds: %d" %(time.time() - start_time)
print "correct:", correct/len(ids)
print "positive:", positive/len(ids)

0 comments on commit b4c94c3

Please sign in to comment.