Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
BigData/SentiWordLex.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
44 lines (40 sloc)
1.55 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |