2025-01-27 09:33:35 +01:00
|
|
|
import pipmaster as pm
|
2025-01-27 23:21:34 +08:00
|
|
|
|
2025-01-27 09:33:35 +01:00
|
|
|
if not pm.is_installed("pyvis"):
|
|
|
|
pm.install("pyvis")
|
2025-02-19 13:47:07 +01:00
|
|
|
if not pm.is_installed("networkx"):
|
|
|
|
pm.install("networkx")
|
2025-01-27 09:33:35 +01:00
|
|
|
|
2025-02-19 13:47:07 +01:00
|
|
|
import networkx as nx
|
2024-10-20 09:55:52 +08:00
|
|
|
from pyvis.network import Network
|
2024-10-20 21:17:09 +08:00
|
|
|
import random
|
2024-10-20 09:55:52 +08:00
|
|
|
|
|
|
|
# Load the GraphML file
|
2024-10-25 13:32:25 +05:30
|
|
|
G = nx.read_graphml("./dickens/graph_chunk_entity_relation.graphml")
|
2024-10-20 09:55:52 +08:00
|
|
|
|
|
|
|
# Create a Pyvis network
|
2024-10-26 14:04:11 +08:00
|
|
|
net = Network(height="100vh", notebook=True)
|
2024-10-20 09:55:52 +08:00
|
|
|
|
|
|
|
# Convert NetworkX graph to Pyvis network
|
|
|
|
net.from_nx(G)
|
|
|
|
|
2024-12-09 17:30:40 +08:00
|
|
|
|
2024-12-08 21:46:33 +05:30
|
|
|
# Add colors and title to nodes
|
2024-10-20 21:17:09 +08:00
|
|
|
for node in net.nodes:
|
2024-10-25 13:32:25 +05:30
|
|
|
node["color"] = "#{:06x}".format(random.randint(0, 0xFFFFFF))
|
2024-12-08 21:46:33 +05:30
|
|
|
if "description" in node:
|
|
|
|
node["title"] = node["description"]
|
|
|
|
|
|
|
|
# Add title to edges
|
|
|
|
for edge in net.edges:
|
|
|
|
if "description" in edge:
|
|
|
|
edge["title"] = edge["description"]
|
2024-10-20 21:17:09 +08:00
|
|
|
|
2024-10-20 09:55:52 +08:00
|
|
|
# Save and display the network
|
2024-10-25 13:32:25 +05:30
|
|
|
net.show("knowledge_graph.html")
|