main
黄海 5 months ago
parent 4b15e9d24d
commit 4afb412a7a

@ -135,10 +135,8 @@ class LLMClient:
# ================== 知识图谱模块 ================== # ================== 知识图谱模块 ==================
class KnowledgeManager: class KnowledgeManager:
"""知识图谱管理器"""
def __init__(self): def __init__(self):
self.graph = Graph(NEO4J_URI, auth=NEO4J_AUTH) self.graph = Graph(Config.NEO4J_URI, auth=Config.NEO4J_AUTH)
self._clean_data() self._clean_data()
self.knowledge_map = self._load_knowledge() self.knowledge_map = self._load_knowledge()
self.literacy_map = self._load_literacy() self.literacy_map = self._load_literacy()
@ -163,20 +161,25 @@ class KnowledgeManager:
def store_analysis(self, question_id: str, content: str, def store_analysis(self, question_id: str, content: str,
knowledge: List[str], literacy: List[str]): knowledge: List[str], literacy: List[str]):
"""存储分析结果""" """事务化存储方法"""
tx = self.graph.begin()
try: try:
# 创建题目节点 # 转义特殊字符
self.graph.run( safe_content = content.replace("'", "\\'")
f"MERGE (q:Question {{id: '{question_id}', content: '{content}'}})"
) # 创建/更新题目节点
tx.run(f"""
MERGE (q:Question {{id: '{question_id}'}})
SET q.content = '{safe_content}'
""")
# 关联知识点 # 关联知识点
for kp_name in knowledge: for kp_name in knowledge:
kp_id = next((k for k, v in self.knowledge_map.items() if v == kp_name), None) kp_id = next((k for k, v in self.knowledge_map.items() if v == kp_name), None)
if kp_id: if kp_id:
self.graph.run(f""" tx.run(f"""
MERGE (kp:KnowledgePoint {{id: '{kp_id}'}}) MERGE (kp:KnowledgePoint {{id: '{kp_id}'}})
WITH q, kp WITH kp
MATCH (q:Question {{id: '{question_id}'}}) MATCH (q:Question {{id: '{question_id}'}})
MERGE (q)-[:REQUIRES_KNOWLEDGE]->(kp) MERGE (q)-[:REQUIRES_KNOWLEDGE]->(kp)
""") """)
@ -185,16 +188,19 @@ class KnowledgeManager:
for lit_name in literacy: for lit_name in literacy:
lit_id = next((k for k, v in self.literacy_map.items() if v == lit_name), None) lit_id = next((k for k, v in self.literacy_map.items() if v == lit_name), None)
if lit_id: if lit_id:
self.graph.run(f""" tx.run(f"""
MERGE (lp:LiteracyNode {{value: '{lit_id}'}}) MERGE (lp:LiteracyNode {{value: '{lit_id}'}})
WITH q, lp WITH lp
MATCH (q:Question {{id: '{question_id}'}}) MATCH (q:Question {{id: '{question_id}'}})
MERGE (q)-[:DEVELOPS_LITERACY]->(lp) MERGE (q)-[:DEVELOPS_LITERACY]->(lp)
""") """)
tx.commit()
print("✅ 数据存储成功")
except Exception as e: except Exception as e:
tx.rollback()
print(f"❌ 存储失败: {str(e)}") print(f"❌ 存储失败: {str(e)}")
# ================== 核心逻辑模块 ================== # ================== 核心逻辑模块 ==================
class ProblemAnalyzer: class ProblemAnalyzer:
"""题目分析引擎""" """题目分析引擎"""

Loading…
Cancel
Save