-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTopicsBarStatic.py
77 lines (58 loc) · 1.34 KB
/
TopicsBarStatic.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("words_per_topic.json", "r")
words_per_topic = json.load(jsonFile)
jsonFile.close()
COLORS = {
1: '#4885ed',
2: "#db3236",
3: "f4c20d",
4: "4885ed",
5: "3cba54"
}
# Sort lists in descending order
g = []
for k in words_per_topic.keys():
l = words_per_topic[k]
sorted_list = sorted(l, key=lambda x: x[1],reverse=True)
g.append(sorted_list)
# Get 5 most frequent words per topic
new_g = []
for item in g:
new_g.append(item[:5])
# Update dictionary with topics names
new_dic = {}
i = 0
for k in words_per_topic.keys():
new_dic.update({k:new_g[i]})
i += 1
def getValuesLabels(k):
labels = []
values = []
for pair in k:
labels.append(pair[0])
values.append(pair[1])
return values, labels
def wordsTopicData():
data = []
colors = []
for i in range(1,6):
colors.append(COLORS[i])
for k in words_per_topic.keys():
values, labels = getValuesLabels(new_dic[k])
values.reverse()
labels.reverse()
colors.reverse()
d = {
'type': 'bar',
'x': values,
'y': labels,
'orientation': 'h',
'marker': {
'color': colors
}
}
data.append([d])
return data
wordsTopicData()