forked from rsatrioadi/classviz
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconvert.py
More file actions
49 lines (43 loc) · 1.19 KB
/
convert.py
File metadata and controls
49 lines (43 loc) · 1.19 KB
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
#!/usr/bin/env python3
import json
import sys
def to_node(item):
return {
"data": {
"id": item['identity']['low'],
"labels": item['labels'],
"properties": item['properties']
}
}
def to_edge(item):
return {
"data": {
"id": item['identity']['low'],
"source": item['start']['low'],
"target": item['end']['low'],
"label": item['type'],
"properties": item['properties']
}
}
data = json.loads(sys.stdin.read())
nodes = list()
edges = list()
for item in sum(list(map(lambda x: x['_fields'], data)), []):
item_nodes = [item['start'], item['end']] + \
sum([[i['start'], i['end']] for i in item['segments']], [])
for item_node in item_nodes:
node = to_node(item_node)
if not any(n['data']['id'] == node['data']['id'] for n in nodes):
nodes.append(node)
item_edges = [i['relationship'] for i in item['segments']]
for item_edge in item_edges:
edge = to_edge(item_edge)
if not any(e['data']['id'] == edge['data']['id'] for e in edges):
edges.append(edge)
all = {
"elements": {
"nodes": nodes,
"edges": edges
}
}
print(json.dumps(all))