-
Notifications
You must be signed in to change notification settings - Fork 1
/
analyse.py
67 lines (54 loc) · 1.79 KB
/
analyse.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from __future__ import division
__author__ = 'agrimasthana'
import mysql.connector
import csv
def setup():
global dbconfig
dbconfig = config['config']
def gettotal():
csr = cnx.cursor()
csr.execute('Select COUNT(*) from Tweets.Tweets')
totaltweet = csr.fetchone()
csr.close()
return totaltweet[0]
def getpositive():
csr = cnx.cursor()
csr.execute("Select COUNT(*) from Tweets.Tweets where sentiment='pos'")
postweet = csr.fetchone()
csr.close()
return postweet[0]
def getnegative():
csr = cnx.cursor()
csr.execute("Select COUNT(*) from Tweets.Tweets where sentiment='neg'")
negtweet = csr.fetchone()
csr.close()
return negtweet[0]
def cleandb():
csr = cnx.cursor()
csr.execute("delete from Tweets.`Tweets`")
cnx.commit()
return -1
if __name__ == '__main__':
config = {}
execfile('settings.conf', config)
setup()
try:
cnx = mysql.connector.connect(**dbconfig)
except Exception as E:
print E
tweets = gettotal()
positive = getpositive()
negative = getnegative()
print "Optimism is :", positive / tweets * 100, "%"
print "Pessimism is :", negative / tweets * 100, "%"
print "undecided are :", 100 - (positive / tweets * 100 + negative / tweets * 100), "%"
with open('data.csv', 'w') as csvfile:
fieldnames = ['age', 'population']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerow({'age': 'Positive', 'population': positive / tweets * 100})
writer.writerow({'age': 'Negative', 'population': negative / tweets * 100})
# writer.writerow({'age': 'Neutral', 'population': 100 - (positive / tweets * 100 + negative / tweets * 100)})
res = cleandb()
if res == -1:
print("Done :)")