diff --git a/dsRag/Neo4j/N0_ClearDb.py b/dsRag/Neo4j/N0_ClearDb.py index 07e57c10..f2ecd723 100644 --- a/dsRag/Neo4j/N0_ClearDb.py +++ b/dsRag/Neo4j/N0_ClearDb.py @@ -1,12 +1,6 @@ -from Config.Config import * -from Neo4j.Neo4jExecutor import * +from Neo4j.Neo4jExecutor import Neo4jExecutor -# 使用示例 if __name__ == '__main__': - executor = Neo4jExecutor( - uri=NEO4J_URI, - auth=NEO4J_AUTH - ) - # 清库 - clear(executor.graph) + executor = Neo4jExecutor.create_default() + executor.graph.run("MATCH (n) DETACH DELETE n") print("清库成功") \ No newline at end of file diff --git a/dsRag/Neo4j/N1_ReadZsd.py b/dsRag/Neo4j/N1_ReadZsd.py index 0ebb1edc..784be0ff 100644 --- a/dsRag/Neo4j/N1_ReadZsd.py +++ b/dsRag/Neo4j/N1_ReadZsd.py @@ -1,6 +1,7 @@ import json -from Config.Config import * -from Neo4j.Neo4jExecutor import * + +from Neo4j.Neo4jExecutor import Neo4jExecutor + def json_to_cypher(data): """将知识体系JSON转换为Cypher插入脚本""" @@ -47,12 +48,9 @@ MERGE (parent)-[:HAS_SUB_POINT]->(child);""") # 使用示例 if __name__ == '__main__': - executor = Neo4jExecutor( - uri=NEO4J_URI, - auth=NEO4J_AUTH - ) + executor = Neo4jExecutor.create_default() # 清库 - clear(executor.graph) + executor.graph.run("MATCH (n) DETACH DELETE n") # 这里替换成你的JSON数据变量 with open('小学数学知识点体系.json', 'r',encoding='utf-8') as f: diff --git a/dsRag/Neo4j/Neo4jExecutor.py b/dsRag/Neo4j/Neo4jExecutor.py index 93ac46c2..28f55cdb 100644 --- a/dsRag/Neo4j/Neo4jExecutor.py +++ b/dsRag/Neo4j/Neo4jExecutor.py @@ -1,8 +1,8 @@ import re from typing import List, Tuple -from Config.Config import * -from py2neo import Graph, Node, Relationship, Subgraph +from py2neo import Graph, Node, Relationship, Subgraph +from Config import Config def clear(db): # 清空数据 @@ -22,7 +22,6 @@ def clear(db): except Exception as e: print(f"删除操作失败: {e}") - def create_subgraph(db: Graph, nodes: List[Node], relations: List[Tuple[Node, str, Node]]) -> None: """统一创建子图""" subgraph = Subgraph( @@ -47,8 +46,19 @@ def tx_create(db: Graph, nodes: List[Node], relations: List[Tuple[Node, str, Nod raise class Neo4jExecutor: - def __init__(self, uri, auth): - self.graph = Graph(uri, auth=auth) + # 添加类变量存储连接配置 + NEO4J_URI = Config.NEO4J_URI + NEO4J_AUTH = Config.NEO4J_AUTH + + def __init__(self, uri=None, auth=None): + # 使用默认配置或传入参数 + self.graph = Graph(uri or self.NEO4J_URI, + auth=auth or self.NEO4J_AUTH) + + @classmethod + def create_default(cls): + """使用默认配置创建执行器""" + return cls(cls.NEO4J_URI, cls.NEO4J_AUTH) # 新增文本执行方法 def execute_cypher_text(self, cypher_text: str) -> dict: @@ -96,12 +106,4 @@ class Neo4jExecutor: except Exception as e: print(f"文件错误: {str(e)}") - return stats - -def init(): - executor = Neo4jExecutor( - uri=NEO4J_URI, - auth=NEO4J_AUTH - ) - # 清库 - clear(executor.graph) \ No newline at end of file + return stats \ No newline at end of file diff --git a/dsRag/Neo4j/__pycache__/Neo4jExecutor.cpython-310.pyc b/dsRag/Neo4j/__pycache__/Neo4jExecutor.cpython-310.pyc index d117f09e..ba52c73f 100644 Binary files a/dsRag/Neo4j/__pycache__/Neo4jExecutor.cpython-310.pyc and b/dsRag/Neo4j/__pycache__/Neo4jExecutor.cpython-310.pyc differ