-
Notifications
You must be signed in to change notification settings - Fork 2
/
global_country_wise_stats.py
executable file
·87 lines (75 loc) · 2.65 KB
/
global_country_wise_stats.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# -*- coding: utf-8 -*-
import json
from pymongo import MongoClient
client = MongoClient('localhost', 27017)
'''
database : main_db
'''
db = client.main_db
topic = ["politic_sentiment", "tourism_sentiment", "economy_sentiment", \
"religion_sentiment"]
#set according to above topic
i = 3
#Sentiment values of corrected tweets
def sentiment_stats():
print "::", topic[0] , "::"
res = db.religion_sentiment.find({}, {'_id' : False}) #CHNG_COLLEC.
cnt_pos, cnt_neg, cnt_ntrl = 0, 0, 0
for x in res:
if(x["polarity"] >= 0.20):
cnt_pos += 1;
elif(x["polarity"] <= -0.1):
cnt_neg += 1;
else:
cnt_ntrl += 1;
print "Positive :: %d" % (cnt_pos)
print "Negative :: %d" % (cnt_neg)
print "Neutral :: %d" % (cnt_ntrl)
#Single json data consists of all coutries stats (pos, neg, ntrl)
def segregate_country():
print "::", topic[i] , "::"
result = db.religion_sentiment.find({}, {"_id" : False}) #CHNG_COLLEC.
country_data = {}
cnt = 0
for twt in result:
cnt += 1
if("place" in twt):
if("country" in twt["place"]):
place = twt["place"]["country"]
#add key if not exist
if(place not in country_data):
country_data[place] = {}
country_data[place]["positive"] = 0
country_data[place]["negative"] = 0
country_data[place]["neutral"] = 0
#check value of tweet
if(twt["polarity"] >= 0.20):
country_data[place]["positive"] += 1
elif(twt["polarity"] <= -0.10):
country_data[place]["negative"] += 1
else:
country_data[place]["neutral"] += 1
else:
pass
else:
pass
#Writing to file
with open("rel_country.json", "w") as fw:
json.dump(country_data, fw)
#Save the Stats in Collection::
db.religion_country_stats.insert(country_data) #CHNG_COLLEC.
#print "Inserted Segregated Country Data!!"
# Count number of countries
def count_country():
result = db.economy_country_stats.find({}, {"_id" : False}) #CHNG_COLLEC.
print "::", topic[i], "::"
for r in result:
print "Total Countries found : %d" % (len(r))
if __name__ == "__main__":
#Separate All countries counts of POS, NEG, NTRL Tweets
segregate_country()
# Count total Countries participate in this analysis
count_country()
# General Distribution of sentiment in different label (POS, NEG, NTRL)
sentiment_stats()
print "Stats Complete!!"