-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemotionsPie.py
77 lines (61 loc) · 1.72 KB
/
emotionsPie.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
jsonFile = open("tweets12.json", "r")
data = json.load(jsonFile)
jsonFile.close()
# Update emotions (merge anticipation and anticip)
for tweet in data:
emotions_list = tweet['emotions']
for e in emotions_list:
if e == 'anticip':
emotions_list.remove(e)
emotions_list.append('anticipation')
# Update json file (merge anticipation and anticip)
def updateJsonFile():
for tweet in data:
emotions_list = tweet['emotions']
for e in emotions_list:
if e == 'anticip':
emotions_list.remove(e)
emotions_list.append('anticipation')
with open("tweets_emoSent2.json", "w+") as jsonFile:
jsonFile.write(json.dumps(data))
updateJsonFile()
jsonFile = open("tweets_emoSent2.json", "r")
data = json.load(jsonFile)
jsonFile.close()
def getEmotions():
all_emotions = []
for tweet in data:
emotions_list = tweet['emotions']
for e in emotions_list:
all_emotions.append(e)
return all_emotions
def getLabels():
labels = []
for tweet in data:
emotions_list = tweet['emotions']
for e in emotions_list:
if e not in labels:
labels.append(e)
return labels
def getValues(emotion):
value = 0
all_emotions = getEmotions()
for e in all_emotions:
if e == emotion:
value += 1
return value
def pieData():
labels = getLabels()
values = []
for emotion in labels:
val = getValues(emotion)
values.append(val)
data = {
'values': values,
'labels': labels,
'type': 'pie'
}
return data