You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# 导入pipmaster模块用于检查并安装依赖包
import pipmaster as pm
# 检查并安装pyvis库( 用于可视化网络图)
if not pm . is_installed ( " pyvis " ) :
pm . install ( " pyvis " )
# 检查并安装networkx库( 用于处理图结构数据)
if not pm . is_installed ( " networkx " ) :
pm . install ( " networkx " )
# 导入必要的库
import networkx as nx # 用于创建和操作复杂的网络结构
from pyvis . network import Network # 用于交互式网络可视化
import random # 用于生成随机颜色
# 从GraphML文件读取知识图谱数据
# 文件路径: ./dickens/graph_chunk_entity_relation.graphml
G = nx . read_graphml ( " ../Topic/DongHua/graph_chunk_entity_relation.graphml " )
# 创建pyvis网络可视化对象
# 参数说明:
# height="100vh" - 设置可视化高度为100%视口高度
# notebook=True - 设置为在notebook环境中使用
net = Network ( height = " 100vh " , notebook = True )
# 将networkx图转换为pyvis网络图
net . from_nx ( G )
# 为每个节点设置随机颜色和提示信息
for node in net . nodes :
# 生成随机十六进制颜色代码
node [ " color " ] = " # {:06x} " . format ( random . randint ( 0 , 0xFFFFFF ) )
# 如果节点有description属性, 设置为鼠标悬停提示
if " description " in node :
node [ " title " ] = node [ " description " ]
# 为每条边设置提示信息
for edge in net . edges :
# 如果边有description属性, 设置为鼠标悬停提示
if " description " in edge :
edge [ " title " ] = edge [ " description " ]
# 生成并显示HTML格式的可视化结果
# 输出文件: knowledge_graph.html
net . show ( " ./Html/knowledge_graph.html " )