File size: 1,542 Bytes
110ce02
 
 
 
 
 
 
 
 
8e81f52
110ce02
 
 
 
 
d5e6064
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import networkx as nx
import warnings
import os

from pyvis.network import Network

warnings.filterwarnings("ignore")

# Load the GraphML file
file_path = "./sample/graph_chunk_entity_relation.graphml"

assert os.path.exists(file_path), f"File {file_path} does not exist."
G = nx.read_graphml(file_path)

# Convert the graph to node-link data format
dict_graph = nx.node_link_data(G)
print("Number of nodes:", len(dict_graph['nodes']))
print("Number of edges:", len(dict_graph['links']))

# Create a Pyvis network
network = Network(width="100%",
                  height="100vh",
                  notebook=True,
                  bgcolor="#f8fafc",
                  font_color="#1e293b")

# Convert NetworkX graph to Pyvis network
network.from_nx(G)

# Add colors and title to nodes
for node in network.nodes:
    if "description" in node:
        node["title"] = node["description"]

    node['color'] = {'background': '#e0e7ff', 'border': '#6366f1', 'highlight': {'background': '#c7d2fe', 'border': '#4f46e5'}}
    node['font'] = {'size': 14, 'color': '#1e293b'}
    node['shape'] = 'dot'
    node['size'] = 20

# Add title to edges
for edge in network.edges:
    if "description" in edge:
        edge["title"] = edge["description"]

    edge['width'] = 4
    edge['color'] = {'color': '#6366f1', 'highlight': '#4f46e5'}
    edge['font'] = {'size': 12, 'color': '#4b5563', 'face': 'Arial'}

# Save and display the network
filename_out = "knowledge_graph.html"
network.show(filename_out)
print(f"Knowledge graph saved to {filename_out}")