parent
b8d735a205
commit
c6a6c317fd
@ -0,0 +1,62 @@
|
||||
from py2neo import Graph
|
||||
import json
|
||||
from Config import *
|
||||
|
||||
def query_all_questions():
|
||||
# Neo4j连接配置
|
||||
neo4j_config = {
|
||||
"uri": NEO4J_URI, # 从Config导入
|
||||
"auth": NEO4J_AUTH # 从Config导入
|
||||
}
|
||||
|
||||
# 初始化图数据库连接
|
||||
graph = Graph(**neo4j_config)
|
||||
|
||||
try:
|
||||
# 获取所有试题ID
|
||||
question_ids = graph.run(
|
||||
"MATCH (q:Question) RETURN q.id AS question_id"
|
||||
).data()
|
||||
|
||||
results = []
|
||||
|
||||
# 遍历每个试题
|
||||
for record in question_ids:
|
||||
qid = record['question_id']
|
||||
# 执行查询
|
||||
data = graph.run("""
|
||||
MATCH (q:Question {id: $qid})
|
||||
OPTIONAL MATCH (q)-[:REQUIRES_KNOWLEDGE]->(kp:KnowledgePoint)
|
||||
OPTIONAL MATCH (q)-[:DEVELOPS_LITERACY]->(lp:LiteracyNode)
|
||||
RETURN
|
||||
q.content AS content,
|
||||
collect(DISTINCT kp.name) AS knowledge_points,
|
||||
collect(DISTINCT lp.title) AS literacy_points
|
||||
""", qid=qid).data()
|
||||
|
||||
if data:
|
||||
result = {
|
||||
"question_id": qid,
|
||||
"content": data[0]['content'],
|
||||
"knowledge_points": data[0]['knowledge_points'],
|
||||
"literacy_points": data[0]['literacy_points']
|
||||
}
|
||||
results.append(result)
|
||||
# 实时打印结果
|
||||
print(f"\n试题ID: {qid}")
|
||||
print(f"内容: {result['content'][:50]}...")
|
||||
print(f"知识点: {result['knowledge_points']}")
|
||||
print(f"素养点: {result['literacy_points']}")
|
||||
|
||||
# 保存结果到JSON文件
|
||||
with open('question_analysis.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(results, f, ensure_ascii=False, indent=2)
|
||||
|
||||
print("\n✅ 所有试题分析结果已保存到 question_analysis.json")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ 查询失败: {str(e)}")
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
query_all_questions()
|
Loading…
Reference in new issue