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.
45 lines
1.2 KiB
45 lines
1.2 KiB
3 weeks ago
|
# conda activate rag
|
||
|
# pip install py2neo pyvis
|
||
|
from py2neo import Graph
|
||
|
from pyvis.network import Network
|
||
|
from Config import Config
|
||
|
|
||
|
# 连接Neo4j
|
||
|
graph = Graph(Config.NEO4J_URI, auth=Config.NEO4J_AUTH)
|
||
|
|
||
|
# 查询所有知识点节点和关系
|
||
|
query = """
|
||
|
MATCH (n:KnowledgePoint)-[r]->(m)
|
||
|
RETURN n, r, m
|
||
|
"""
|
||
|
data = graph.run(query).data()
|
||
|
|
||
|
# 创建网络图
|
||
3 weeks ago
|
net = Network(height="750px", width="100%", notebook=True, cdn_resources='in_line')
|
||
3 weeks ago
|
|
||
|
# 添加节点和边
|
||
|
for item in data:
|
||
3 weeks ago
|
node_label = item['n'].get('name', '未命名')
|
||
3 weeks ago
|
net.add_node(item['n'].identity,
|
||
3 weeks ago
|
label=node_label,
|
||
|
name=node_label,
|
||
|
font={'size': 14, 'face': 'SimHei'})
|
||
|
|
||
|
node_label = item['m'].get('name', '未命名')
|
||
3 weeks ago
|
net.add_node(item['m'].identity,
|
||
3 weeks ago
|
label=node_label,
|
||
|
name=node_label,
|
||
|
font={'size': 14, 'face': 'SimHei'})
|
||
|
|
||
3 weeks ago
|
net.add_edge(item['n'].identity,
|
||
|
item['m'].identity,
|
||
|
title=type(item['r']).__name__)
|
||
|
|
||
|
# 生成HTML文件
|
||
3 weeks ago
|
with open("knowledge_graph.html", "w", encoding='utf-8') as f:
|
||
|
f.write(net.generate_html())
|
||
|
# 打开HTML文件
|
||
|
import webbrowser
|
||
|
webbrowser.open("knowledge_graph.html")
|
||
3 weeks ago
|
|