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
/
Copy pathner_plot.py
107 lines (93 loc) · 2.46 KB
/
ner_plot.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
import json
# Read data from JSON file
with open('updated_file_two_final.json', 'r') as f:
data = json.load(f)
# Convert data to vis.js format
nodes = []
edges = []
node_colors = {
"PERSON": "blue",
"ORG": "green",
"GPE":"red",
}
for node_id, node_data in data["nodes"].items():
node_id = int(node_id)
node = {
"id": node_id,
"label": node_data["label"],
"color": node_colors.get(node_data.get("ner", ""), "gray"),
"category": node_data["category"],
"ner": node_data.get("ner", "")
}
nodes.append(node)
for edge in data["edges"]:
edges.append({
"from": edge["from"],
"to": edge["to"],
"label": edge["label"],
"category": edge["category"],
"arrows": "to"
})
# Generate HTML and JavaScript for vis.js
html_template = """
<!DOCTYPE html>
<html>
<head>
<title>Network Graph</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/vis/4.21.0/vis-network.min.css" rel="stylesheet" type="text/css">
<style type="text/css">
#mynetwork {{
width: 100%;
height: 800px;
border: 1px solid lightgray;
}}
</style>
</head>
<body>
<div id="mynetwork"></div>
<script type="text/javascript">
var nodes = new vis.DataSet({nodes});
var edges = new vis.DataSet({edges});
var container = document.getElementById('mynetwork');
var data = {{
nodes: nodes,
edges: edges
}};
var options = {{
nodes: {{
shape: 'dot',
size: 20,
font: {{
size: 15,
color: '#000000'
}},
borderWidth: 2
}},
edges: {{
width: 2,
font: {{
size: 12,
align: 'middle'
}},
arrows: {{
to: {{enabled: true, scaleFactor: 1}}
}},
smooth: {{
enabled: true,
type: 'dynamic'
}}
}},
physics: {{
enabled: true
}}
}};
var network = new vis.Network(container, data, options);
</script>
</body>
</html>
"""
html_output = html_template.format(nodes=json.dumps(nodes), edges=json.dumps(edges))
with open('graph_color_put.html', 'w') as f:
f.write(html_output)
print("HTML file generated: graph.html")