-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph.py
322 lines (277 loc) · 9.86 KB
/
graph.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# coding: utf-8
import json
import os
import re
from collections import Counter
import matplotlib.pyplot as plt
import numpy as np
def involvedUsers(item):
topic = item['topic']
content = topic['content']
if 'involvedUsers' in topic.keys():
users = list(
map(lambda item: item['screenName'],
topic['involvedUsers']['users']))
else:
users = ''
string = []
if users:
string = [{'source': content, 'target': set(users)}]
return string
def merge_dict(source_set, lst):
res_lst = []
for source in source_set:
tmp = list(filter(lambda i: i['source'] == source, lst))
tmp_set = set()
for line in tmp:
tmp_set = tmp_set | line['target']
res_lst.append({'source': source, 'target': tmp_set})
return res_lst
def write_data(res_lst):
head = """
<!DOCTYPE html>
<meta charset="utf-8">
<style>
.link {
fill: none;
stroke: #666;
stroke-width: 1.5px;
}
#licensing {
fill: green;
}
.link.licensing {
stroke: green;
}
.link.resolved {
stroke-dasharray: 0, 2 1;
}
circle {
fill: #ccc;
stroke: #333;
stroke-width: 1.5px;
}
text {
font: 12px Microsoft YaHei;
pointer-events: none;
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
}
.linetext {
font-size: 12px Microsoft YaHei;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
var links = [
"""
tail = """
]
var nodes = {};
links.forEach(function (link) {
link.source = nodes[link.source] || (nodes[link.source] = { name: link.source });
link.target = nodes[link.target] || (nodes[link.target] = { name: link.target });
});
var width = 1920, height = 1080;
var force = d3.layout.force()
.nodes(d3.values(nodes))
.links(links)
.size([width, height])
.linkDistance(180)
.charge(-1500)
.on("tick", tick)
.start();
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var marker =
svg.append("marker")
.attr("id", "resolved")
.attr("markerUnits", "userSpaceOnUse")
.attr("viewBox", "0 -5 10 10")
.attr("refX", 32)
.attr("refY", -1)
.attr("markerWidth", 12)
.attr("markerHeight", 12)
.attr("orient", "auto")
.attr("stroke-width", 2)
.append("path")
.attr("d", "M0,-5L10,0L0,5")
.attr('fill', '#000000');
var edges_line = svg.selectAll(".edgepath")
.data(force.links())
.enter()
.append("path")
.attr({
'd': function (d) { return 'M ' + d.source.x + ' ' + d.source.y + ' L ' + d.target.x + ' ' + d.target.y },
'class': 'edgepath',
'id': function (d, i) { return 'edgepath' + i; }
})
.style("stroke", function (d) {
var lineColor;
lineColor = "#B43232";
return lineColor;
})
.style("pointer-events", "none")
.style("stroke-width", 0.5)
.attr("marker-end", "url(#resolved)");
var edges_text = svg.append("g").selectAll(".edgelabel")
.data(force.links())
.enter()
.append("text")
.style("pointer-events", "none")
.attr({
'class': 'edgelabel',
'id': function (d, i) { return 'edgepath' + i; },
'dx': 80,
'dy': 0
});
edges_text.append('textPath')
.attr('xlink:href', function (d, i) { return '#edgepath' + i })
.style("pointer-events", "none")
.text(function (d) { return d.rela; });
var circle = svg.append("g").selectAll("circle")
.data(force.nodes())
.enter().append("circle")
.style("fill", function (node) {
var color;
var link = links[node.index];
color = "#F9EBF9";
return color;
})
.style('stroke', function (node) {
var color;
var link = links[node.index];
color = "#A254A2";
return color;
})
.attr("r", 28)
.on("click", function (node) {
edges_line.style("stroke-width", function (line) {
console.log(line);
if (line.source.name == node.name || line.target.name == node.name) {
return 4;
} else {
return 0.5;
}
});
})
.call(force.drag);
var text = svg.append("g").selectAll("text")
.data(force.nodes())
.enter()
.append("text")
.attr("dy", ".35em")
.attr("text-anchor", "middle")
.style('fill', function (node) {
var color;
var link = links[node.index];
color = "#A254A2";
return color;
}).attr('x', function (d) {
var re_en = /[a-zA-Z]+/g;
if (d.name.match(re_en)) {
d3.select(this).append('tspan')
.attr('x', 0)
.attr('y', 2)
.text(function () { return d.name; });
}
else if (d.name.length <= 4) {
d3.select(this).append('tspan')
.attr('x', 0)
.attr('y', 2)
.text(function () { return d.name; });
} else {
var top = d.name.substring(0, 4);
var bot = d.name.substring(4, d.name.length);
d3.select(this).text(function () { return ''; });
d3.select(this).append('tspan')
.attr('x', 0)
.attr('y', -7)
.text(function () { return top; });
d3.select(this).append('tspan')
.attr('x', 0)
.attr('y', 10)
.text(function () { return bot; });
}
});
function tick() {
circle.attr("transform", transform1);
text.attr("transform", transform2);
edges_line.attr('d', function (d) {
var path = 'M ' + d.source.x + ' ' + d.source.y + ' L ' + d.target.x + ' ' + d.target.y;
return path;
});
edges_text.attr('transform', function (d, i) {
if (d.target.x < d.source.x) {
bbox = this.getBBox();
rx = bbox.x + bbox.width / 2;
ry = bbox.y + bbox.height / 2;
return 'rotate(180 ' + rx + ' ' + ry + ')';
}
else {
return 'rotate(0)';
}
});
}
function linkArc(d) {
return 'M ' + d.source.x + ' ' + d.source.y + ' L ' + d.target.x + ' ' + d.target.y
}
function transform1(d) {
return "translate(" + d.x + "," + d.y + ")";
}
function transform2(d) {
return "translate(" + (d.x) + "," + d.y + ")";
}
</script>
"""
with open('example/graph.html', 'w', encoding='utf-8') as f:
f.write(head)
for item in res_lst:
if len(item['target']) < 20:
for target in item['target']:
f.write('{ source: "' + item['source'] + '", target: "' +
target + '" },' + '\n')
f.write(tail)
# with open('example/graph.txt', 'w', encoding='utf-8') as f:
# for item in res_lst:
# if len(item['target']) < 20:
# for target in item['target']:
# f.write('{ source: "' + item['source'] + '", target: "' +
# target + '" },' + '\n')
# with open('example/graph.txt', 'r', encoding='utf-8') as f:
# lines = f.readlines()
# print(lines)
def count_user(res_data):
target_lst = list(map(lambda item: list(item['target']), res_lst))
target_lst = flat(target_lst)
num = 5
topic_dict = Counter(target_lst).items()
target_lst = list(filter(lambda item: item[1] >= num, topic_dict))
target_lst.sort(key=lambda item: item[1])
lst = []
for user in target_lst:
tmp = {'user': user, 'topics': []}
for item in res_lst:
if user[0] in item['target']:
tmp['topics'].append(item['source'])
lst += [tmp]
return lst
if __name__ == '__main__':
flat = lambda L: sum(map(flat, L), []) if isinstance(L, list) else [L]
res_data = []
for fpath in [
'2019-06-16', '2019-06-17', '2019-06-18', '2019-06-19',
'2019-06-21', '2019-06-22'
]:
for f in os.listdir(fpath):
if f.endswith('.json'):
with open(os.path.join(fpath, f), 'r', encoding='utf-8') as f:
data = json.load(f)['data']
tmp_data = list(map(involvedUsers, data))
res_data += flat(tmp_data)
res_data = flat(res_data)
source_set = set(map(lambda item: item['source'], res_data))
res_lst = merge_dict(source_set, res_data)
# user_lst = count_user(res_lst)
write_data(res_lst)