from K1_KnowledgeGraph import * from K2_Neo4jExecutor import * # 切割试题 def split_questions(file_path): with open(file_path, 'r', encoding='utf-8') as f: content = f.read() # 使用正则表达式匹配题目块(包含答案) pattern = r'(\d+\.\s+【.*?】.*?(?=\n\d+\.|\Z))' questions = re.findall(pattern, content, re.DOTALL) # 清洗每个题目块的空白字符 cleaned_questions = [q.strip() for q in questions] return cleaned_questions[:10] # 确保只返回前10题 if __name__ == '__main__': # 清库 init() # 准备执行 executor = K2_Neo4jExecutor( uri=NEO4J_URI, auth=NEO4J_AUTH ) # 新增数据库约束(确保节点必须带ID) init_script = """ CREATE CONSTRAINT IF NOT EXISTS FOR (kp:KnowledgePoint) REQUIRE kp.id IS UNIQUE; CREATE CONSTRAINT IF NOT EXISTS FOR (ab:AbilityPoint) REQUIRE ab.id IS UNIQUE; CREATE CONSTRAINT IF NOT EXISTS FOR (q:Question) REQUIRE q.id IS UNIQUE; """ executor.execute_cypher_text(init_script) # 使用示例 question_blocks = split_questions('ShiTi.md') # 验证分割结果 for i, block in enumerate(question_blocks, 1): print(f"第{i}题块:") print("-" * 50) try: kg = KnowledgeGraph(block) success, cypher, result = kg.run() # 替换一些特殊符号 cypher = cypher.replace('```neo4j', '').replace('```', '').replace('```cypher', '') print(cypher) res = executor.execute_cypher_text(cypher) print("恭喜,执行数据插入完成!") except Exception as e: print(f"程序初始化失败: {str(e)}")