This repository has been archived by the owner on Nov 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
graph_show.py
200 lines (176 loc) · 6.18 KB
/
graph_show.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
class GraphShow():
"""Create demo page"""
def __init__(self):
self.base = '''
<html>
<head>
<script type="text/javascript" src="VIS/dist/vis.js"></script>
<link href="VIS/dist/vis.css" rel="stylesheet" type="text/css">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="VIS_draw"></div>
<script type="text/javascript">
var nodes = data_nodes;
var edges = data_edges;
var container = document.getElementById("VIS_draw");
var data = {
nodes: nodes,
edges: edges
};
console.log(data);
var options = {
nodes: {
shape: 'circle',
font: {
size: 15
}
},
edges: {
font: {
size: 10,
align: 'center'
},
arrows: {
to: { enabled: true, scaleFactor: 1.2 }
},
smooth: { enabled: true }
},
physics: {
enabled: true
}
};
data.nodes.forEach(function (node) {
if (node.category === 'frequency') {
node.color = 'lightblue';
} else if (node.category === 'keyword') {
node.color = 'lightgreen';
}
else if (node.category === 'Organization') {
node.color = 'red';
}else if (node.category === 'Location') {
node.color = 'black';
}else if (node.category === 'related') {
node.color = 'lightcoral';
}
// Set a common size for all nodes
});
data.nodes.forEach(function (node) {
// Adjust node color based on value from result_dic
if (node.value) {
// Assuming result_dic values are in the range [0, 1]
var hue = Math.floor((1 - node.value) * 120); // Adjust hue based on value
node.color = 'lightyellow';
}
});
data.edges.forEach(function (edge) {
if (edge.category === 'frequency') {
edge.color = { color: 'blue' };
} else if (edge.category === 'keyword') {
edge.color = { color: 'green' };
} else if (edge.category === 'related') {
edge.color = { color: 'red' };
} else {
edge.color = { color: 'black' };
}
});
var network = new vis.Network(container, data, options);
</script>
</body>
</html>
'''
def create_html(self, data_nodes, data_edges):
"""Generate html file"""
f = open('graph_show.html', 'w+')
html = self.base.replace('data_nodes', str(data_nodes)).replace('data_edges', str(data_edges))
f.write(html)
f.close()
def return_edge(self, events, result_dic):
"""Read data and values"""
nodes = []
# for event in events:
# nodes.append(event[0])
# nodes.append(event[1])
# print(f"Node: {event[0]}, Index: {index * 2}")
# print(f"Node: {event[1]}, Index: {index * 2 + 1}")
for index, event in enumerate(events):
nodes.append(event[0])
nodes.append(event[1])
# print(f"Node: {event[0]}, Index: {index * 2}")
# print(f"Node: {event[1]}, Index: {index * 2 + 1}")
# print(nodes)
node_dict = {node: index for index, node in enumerate(nodes)}
# print(node_dict)
data_nodes = []
data_edges = []
for node, id in node_dict.items():
data = {
"id": id,
"label": node,
"category": 'frequency' if 'frequency' in node else 'keyword' if 'keyword' in node else 'related' if 'related' in node else 'Organization' if 'Organization' in node else 'Location' if 'Location' in node else 'other',
"ner": "PERSON" if "PERSON" in node else "org",
"value": result_dic.get(node, 0) # Get value from result_dic, default to 0 if not present
}
data_nodes.append(data)
# print(events)
for edge in events:
data = {
'from': node_dict.get(edge[0]),
'label': '',
'to': node_dict.get(edge[1]),
'category': 'frequency' if 'frequency' in edge else 'keyword' if 'keyword' in edge else 'related' if 'related' else 'Organization' if 'Organization' in node in edge else 'Location' if 'Location' in node else 'other'
}
data_edges.append(data)
# print(data_nodes,data_edges)
self.create_html(data_nodes, data_edges)
return data_edges, data_nodes
def create_page(self, events, result_dic):
"""Read data and values, assign edge and node colors based on categories and values"""
nodes = {}
for event in events:
nodes[event[0]] = event[1]
node_dict = nodes
data_nodes = []
data_edges = []
for node, id in node_dict.items():
category = 'frequency' if 'frequency' in id else 'keyword' if 'keyword' in id else 'related' if 'related' in id else 'Organization' if 'Organization' in id else 'Location' if 'Location' in node else 'other'
data = {
"id": id,
"label": node,
"color": {'background': self.get_node_color(category), 'border': 'black'},
"value": result_dic.get(node, 0) # Get value from result_dic, default to 0 if not present
}
data_nodes.append(data)
for edge in events:
category = 'frequency' if 'frequency' in edge else 'keyword' if 'keyword' in edge else 'related' if 'related' in edge else 'other'
data = {
'from': node_dict.get(edge[0]),
'label': '',
'to': node_dict.get(edge[1]),
'color': {'color': self.get_edge_color(category)}
}
data_edges.append(data)
self.create_html(data_nodes, data_edges)
return
def get_node_color(self, category):
if category == 'frequency':
return 'lightblue'
elif category == 'keyword':
return 'lightgreen'
elif category == 'related':
return 'lightcoral'
elif category=='Organization':
return 'red'
elif category=='Location':
return 'black'
else:
return 'blue'
def get_edge_color(self, category):
if category == 'frequency':
return 'blue'
elif category == 'keyword':
return 'green'
elif category == 'related':
return 'red'
else:
return 'gray'