main
HuangHai 3 weeks ago
parent 8b21f40ce6
commit 7f5c9e7974

@ -1,60 +0,0 @@
import json
from Util.Neo4jExecutor import Neo4jExecutor
def json_to_cypher(data):
"""将知识体系JSON转换为Cypher插入脚本"""
cypher = []
seen = set()
def process_node(node, parent_id=None):
nonlocal seen
node_id = node['value']
node_name = node['title'].replace("'", "''")
# 创建当前节点
if node_id not in seen:
cypher.append(f"MERGE (n:KnowledgePoint {{id: '{node_id}'}}) SET n.name = '{node_name}';")
seen.add(node_id)
# 创建父子关系
if parent_id:
cypher.append(f"""
MATCH (parent:KnowledgePoint {{id: '{parent_id}'}}),
(child:KnowledgePoint {{id: '{node_id}'}})
MERGE (parent)-[:HAS_SUB_POINT]->(child);""")
# 递归处理子节点
if 'children' in node and not node['isLeaf']:
for child in node['children']:
process_node(child, parent_id=node_id)
# 处理根节点
for root in data['data']['tree']:
process_node(root)
# 处理根节点的父级关系(如果有)
if root.get('parentValue'):
cypher.append(f"MERGE (parent:KnowledgePoint {{id: '{root['parentValue']}'}});")
cypher.append(f"""
MATCH (parent:KnowledgePoint {{id: '{root['parentValue']}'}}),
(child:KnowledgePoint {{id: '{root['value']}'}})
MERGE (parent)-[:HAS_SUB_POINT]->(child);""")
return '\n'.join(cypher)
# 使用示例
if __name__ == '__main__':
executor = Neo4jExecutor.create_default()
# 清库
executor.graph.run("MATCH (n) DETACH DELETE n")
# 这里替换成你的JSON数据变量
with open('小学数学知识点体系.json', 'r',encoding='utf-8') as f:
your_json_data = json.load(f)
cypherText=json_to_cypher(your_json_data)
executor.execute_cypher_text(cypherText)
print("数据插入成功!")

@ -37,7 +37,7 @@ def generate_cypher(knowledge_points):
cypher.append(
f"MERGE (n:KnowledgePoint {{id: '{node_id}'}}) "
f"SET n.title = '{title}', n.is_leaf = {is_leaf};"
f"SET n.name = '{title}', n.is_leaf = {is_leaf};"
)
# 创建父子关系

@ -19,12 +19,18 @@ net = Network(height="750px", width="100%", notebook=True, cdn_resources='in_lin
# 添加节点和边
for item in data:
node_label = item['n'].get('name', '未命名')
net.add_node(item['n'].identity,
label=item['n']['name'],
title=item['n'].get('description', ''))
label=node_label,
name=node_label,
font={'size': 14, 'face': 'SimHei'})
node_label = item['m'].get('name', '未命名')
net.add_node(item['m'].identity,
label=item['m']['name'],
title=item['m'].get('description', ''))
label=node_label,
name=node_label,
font={'size': 14, 'face': 'SimHei'})
net.add_edge(item['n'].identity,
item['m'].identity,
title=type(item['r']).__name__)

File diff suppressed because one or more lines are too long
Loading…
Cancel
Save