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.

54 lines
1.7 KiB

5 months ago
from K1_KnowledgeGraph import *
from K2_Neo4jExecutor import *
5 months ago
# 切割试题
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题
5 months ago
if __name__ == '__main__':
5 months ago
# 清库
init()
5 months ago
# 准备执行
executor = K2_Neo4jExecutor(
uri=NEO4J_URI,
auth=NEO4J_AUTH
)
5 months ago
# 新增数据库约束确保节点必须带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)
5 months ago
# 使用示例
question_blocks = split_questions('ShiTi.md')
5 months ago
5 months ago
# 验证分割结果
for i, block in enumerate(question_blocks, 1):
print(f"{i}题块:")
print("-" * 50)
try:
kg = KnowledgeGraph(block)
5 months ago
success, cypher, result = kg.run()
5 months ago
# 替换一些特殊符号
cypher = cypher.replace('```neo4j', '').replace('```', '').replace('```cypher', '')
5 months ago
print(cypher)
res = executor.execute_cypher_text(cypher)
print("恭喜,执行数据插入完成!")
except Exception as e:
print(f"程序初始化失败: {str(e)}")