Beispiel #1
0
	def sentimentAnalysis(self, tweets, month="0"):
		if month == "0":
			enum = {"Jan":31, "Feb":28, "Mar":31, "Apr":30 , "May":31, "Jun":30, "Jul":31, "Aug":31, "Sep":30, "Oct":31, "Nov":30, "Dec":31}
			tweetMonth = {"Jan":[],"Feb":[],"Mar":[],"Apr":[],"May":[],"Jun":[],"Jul":[],"Aug":[],"Sep":[],"Oct":[],"Nov":[], "Dec":[]}
			pos = neg = neu = 0
			s = Sentianal()
			for tw in tweets:
				for t in tweets[tw]:
					count = 0
					for i in range(0, enum[tw]):
						date_temp = t['created_at'].split(" ")
						date = int(date_temp[2])
						if date == (i+1):
							sentimentScore = s.compute(t['text_clean'])     #compute sentiment score of a tweet
							if sentimentScore > 0.0:            #if positive
								tweets[tw][count]['sentiment'] = 'positive'
								pos = pos + 1                         #increment the number of positive tweets
							elif sentimentScore < 0.0:          #if negative
								tweets[tw][count]['sentiment'] = 'negative'
								neg = neg + 1                         #increment the number of negative tweets
							else:
								tweets[tw][count]['sentiment'] = 'neutral'
								neu = neu + 1
					count = count + 1			
			return (tweets, pos, neg, neu)
		else:	
			enum = {"Jan":31, "Feb":28, "Mar":31, "Apr":30 , "May":31, "Jun":30, "Jul":31, "Aug":31, "Sep":30, "Oct":31, "Nov":30, "Dec":31}
			tweetMonth = {"Jan":[],"Feb":[],"Mar":[],"Apr":[],"May":[],"Jun":[],"Jul":[],"Aug":[],"Sep":[],"Oct":[],"Nov":[], "Dec":[]}
			pos = neg = neu = 0
			s = Sentianal()
			for t in tweets[month]:
				count = 0
				for i in range(0, enum[month]):
					date_temp = t['created_at'].split(" ")
					date = int(date_temp[2])
					if date == (i+1):
						sentimentScore = s.compute(t['text_clean'])     #compute sentiment score of a tweet
						if sentimentScore > 0.0:            #if positive
							tweets[month][count]['sentiment'] = 'positive'
							pos = pos + 1                         #increment the number of positive tweets
						elif sentimentScore < 0.0:          #if negative
							tweets[month][count]['sentiment'] = 'negative'
							neg = neg + 1                         #increment the number of negative tweets
						else:
							tweets[month][count]['sentiment'] = 'neutral'
							neu = neu + 1
				count = count + 1			
			return (tweets, pos, neg, neu)
from sentianal import Sentianal
import numpy as numpy
from pandas import * 

import matplotlib.pyplot as plt

##################### you can modify this part ######################

corpusFile   = "jokowitweet.txt"
positiveFile = "jokowipositivetweet.txt"
negativeFile = "jokowinegativetweet.txt"

#####################################################################

s = Sentianal()

fin   = open(corpusFile, "r")
foutp = open(positiveFile, "w")
foutn = open(negativeFile, "w")

pos = 0
neg = 0

for line in fin:
   line = line.strip()
   sentimentScore = s.compute(line)     #compute sentiment score of a tweet
   if sentimentScore > 0.0:            #if positive
      pos += 1                         #increment the number of positive tweets
      foutp.write(line)                #write the positive tweet in a new file (separated)
      foutp.write("\n")
prab_data = DataFrame(tweet)

#rename the column
prab_data.columns = ['tweet']

#score the tweet
score = []

#create normalizer object
norm = Normalizer()

#create Stop word Removal object
st = StpRemoval()

#create sentiment analysis object
s = Sentianal()

for i in range(0, len(prab_data)):
    #normalize
    line = norm.normalize(prab_data['tweet'][i])

    #remove stopword
    line = st.removeStp(line)

    #score sentiment
    score.append(s.compute(line))

#join the dataframe
score_data = DataFrame(score)
prab_data = prab_data.join(score_data)
prab_data.columns = ['tweet_processed', 'score']
prab_data = DataFrame(tweet)

#rename the column
prab_data.columns = ['tweet']

#score the tweet
score = []

#create normalizer object
norm = Normalizer()

#create Stop word Removal object
st = StpRemoval()

#create sentiment analysis object
s = Sentianal()

for i in range(0,len(prab_data)):
	#normalize
	line = norm.normalize(prab_data['tweet'][i])
	
	#remove stopword
	line = st.removeStp(line)

	#score sentiment
	score.append(s.compute(line))

#join the dataframe
score_data = DataFrame(score)
prab_data = prab_data.join(score_data)
prab_data.columns = ['tweet_processed','score']
from sentianal import Sentianal
import numpy as numpy
from pandas import *

import matplotlib.pyplot as plt

##################### you can modify this part ######################

corpusFile = "jokowitweet.txt"
positiveFile = "jokowipositivetweet.txt"
negativeFile = "jokowinegativetweet.txt"

#####################################################################

s = Sentianal()

fin = open(corpusFile, "r")
foutp = open(positiveFile, "w")
foutn = open(negativeFile, "w")

pos = 0
neg = 0

for line in fin:
    line = line.strip()
    sentimentScore = s.compute(line)  #compute sentiment score of a tweet
    if sentimentScore > 0.0:  #if positive
        pos += 1  #increment the number of positive tweets
        foutp.write(line)  #write the positive tweet in a new file (separated)
        foutp.write("\n")