diff --git a/AI/Neo4j/K1_KnowledgeGraph.py b/AI/Neo4j/Backup/K1_KnowledgeGraph.py similarity index 100% rename from AI/Neo4j/K1_KnowledgeGraph.py rename to AI/Neo4j/Backup/K1_KnowledgeGraph.py diff --git a/AI/Neo4j/K2_Neo4jExecutor.py b/AI/Neo4j/Backup/K2_Neo4jExecutor.py similarity index 100% rename from AI/Neo4j/K2_Neo4jExecutor.py rename to AI/Neo4j/Backup/K2_Neo4jExecutor.py diff --git a/AI/Neo4j/K3_Start.py b/AI/Neo4j/Backup/K3_Start.py similarity index 94% rename from AI/Neo4j/K3_Start.py rename to AI/Neo4j/Backup/K3_Start.py index 7c771f88..c9f254ea 100644 --- a/AI/Neo4j/K3_Start.py +++ b/AI/Neo4j/Backup/K3_Start.py @@ -36,7 +36,7 @@ if __name__ == '__main__': executor.execute_cypher_text(init_script) # 分段读入题目 - question_blocks = split_questions('ShiTi.md') + question_blocks = split_questions('../ShiTi.md') # 验证分割结果 for i, block in enumerate(question_blocks, 1): diff --git a/AI/Neo4j/__pycache__/K2_Neo4jExecutor.cpython-310.pyc b/AI/Neo4j/Backup/__pycache__/K2_Neo4jExecutor.cpython-310.pyc similarity index 86% rename from AI/Neo4j/__pycache__/K2_Neo4jExecutor.cpython-310.pyc rename to AI/Neo4j/Backup/__pycache__/K2_Neo4jExecutor.cpython-310.pyc index d0ca6eee..5fb5a2ca 100644 Binary files a/AI/Neo4j/__pycache__/K2_Neo4jExecutor.cpython-310.pyc and b/AI/Neo4j/Backup/__pycache__/K2_Neo4jExecutor.cpython-310.pyc differ diff --git a/AI/Neo4j/import.cypher b/AI/Neo4j/Backup/import.cypher similarity index 100% rename from AI/Neo4j/import.cypher rename to AI/Neo4j/Backup/import.cypher diff --git a/AI/Neo4j/优化.cypher b/AI/Neo4j/Backup/优化.cypher similarity index 100% rename from AI/Neo4j/优化.cypher rename to AI/Neo4j/Backup/优化.cypher diff --git a/AI/Neo4j/小学数学一至三年级.cypher b/AI/Neo4j/Backup/小学数学一至三年级.cypher similarity index 100% rename from AI/Neo4j/小学数学一至三年级.cypher rename to AI/Neo4j/Backup/小学数学一至三年级.cypher diff --git a/AI/Neo4j/小学数学四至六年级.cypher b/AI/Neo4j/Backup/小学数学四至六年级.cypher similarity index 100% rename from AI/Neo4j/小学数学四至六年级.cypher rename to AI/Neo4j/Backup/小学数学四至六年级.cypher diff --git a/AI/Neo4j/InputShiTi.py b/AI/Neo4j/InputShiTi.py new file mode 100644 index 00000000..689e4857 --- /dev/null +++ b/AI/Neo4j/InputShiTi.py @@ -0,0 +1,148 @@ +# -*- coding: utf-8 -*- +import re +import hashlib +from py2neo import Graph +from openai import OpenAI +from Config import * + + +class KnowledgeGraph: + def __init__(self, content: str): + self.content = content + self.question_id = hashlib.md5(content.encode()).hexdigest()[:8] + self.graph = Graph(NEO4J_URI, auth=NEO4J_AUTH) + self.knowledge_points = self._get_knowledge_points() + self.client = OpenAI(api_key=MODEL_API_KEY, base_url=MODEL_API_URL) + + def _get_knowledge_points(self) -> dict: + """修复字段名称错误""" + try: + # 确保返回字段与节点属性名称匹配 + return {row['n.id'].lower(): row['n.name'] # 修改为n.id + for row in self.graph.run("MATCH (n:KnowledgePoint) RETURN n.id, n.name")} + except Exception as e: + print(f"获取知识点失败:", str(e)) + return {} + + def _make_prompt(self) -> str: + """生成知识点识别专用提示词""" + example_ids = list(self.knowledge_points.keys())[:5] + example_names = [self.knowledge_points[k] for k in example_ids] + + return f"""你是一个数学专家,请分析题目考查的知识点,严格: +1. 只使用以下存在的知识点(格式:ID:名称): +{", ".join([f"{k}:{v}" for k, v in zip(example_ids, example_names)])}... +共{len(self.knowledge_points)}个可用知识点 + +2. 按此格式生成Cypher: +MERGE (q:Question {{id: "{self.question_id}"}}) +SET q.content = "题目内容" +WITH q +MATCH (kp:KnowledgePoint {{id: "知识点ID"}}) +MERGE (q)-[:TESTS_KNOWLEDGE]->(kp)""" + + def _clean_cypher(self, code: str) -> str: + """修复WITH语句顺序问题""" + safe = [] + cypher_block = re.findall(r"```(?:cypher)?\n(.*?)```", code, re.DOTALL) + if not cypher_block: + return "" + + # 强制先创建Question节点 + has_question = False + for line in cypher_block[0].split('\n'): + line = line.split('//')[0].strip() + if not line: + continue + + # 确保Question节点最先创建 + if 'MERGE (q:Question' in line: + has_question = True + safe.insert(0, line) # 确保这行在最前面 + continue + + # 安全过滤 + if 'CREATE' in line.upper(): + continue + + # 自动补全WITH语句(仅在Question创建之后) + if has_question and 'MERGE (q)-[:TESTS_KNOWLEDGE]' in line and not any('WITH q' in l for l in safe): + safe.append("WITH q") + + # ID存在性验证 + if 'MATCH (kp:KnowledgePoint' in line: + kp_id = re.findall(r"id: ['\"](.*?)['\"]", line) + if kp_id and kp_id[0] not in self.knowledge_points: + continue + + safe.append(line) + + # 补充必要WITH语句 + if has_question and not any(line.startswith('WITH q') for line in safe): + safe.insert(1, "WITH q") # 在创建Question之后立即添加 + + return '\n'.join([line for line in safe if line]) + + def run(self) -> str: + """执行知识点关联流程""" + try: + response = self.client.chat.completions.create( + model=MODEL_NAME, + messages=[ + { + "role": "system", + "content": self._make_prompt() + }, + { + "role": "user", + "content": f"题目内容:{self.content}\n请分析考查的知识点,只返回Cypher代码" + } + ] + ) + + raw_cypher = response.choices[0].message.content + cleaned_cypher = self._clean_cypher(raw_cypher) + + if cleaned_cypher: + print("验证通过的Cypher:\n", cleaned_cypher) + return cleaned_cypher + return "" + + except Exception as e: + print("知识点分析失败:", str(e)) + return "" + +def query_related_knowledge(self): + """查询题目关联的知识点""" + cypher = f""" + MATCH (q:Question {{id: "{self.question_id}"}})-[:TESTS_KNOWLEDGE]->(kp) + RETURN kp.id AS knowledge_id, kp.name AS knowledge_name + """ + try: + result = self.graph.run(cypher).data() + if result: + print(f"题目关联的知识点({self.question_id}):") + for row in result: + print(f"- {row['knowledge_name']} (ID: {row['knowledge_id']})") + else: + print("该题目尚未关联知识点") + return result + except Exception as e: + print("查询失败:", str(e)) + return [] + +# 测试用例 +if __name__ == '__main__': + test_case = """【时间问题】甲乙两车从相距240公里的两地同时出发相向而行,甲车时速60公里,乙车时速40公里,几小时后相遇?""" + + test_case = """【时间问题】甲乙两车从相距240公里的两地同时出发相向而行...""" + + kg = KnowledgeGraph(test_case) + cypher = kg.run() + + if cypher: + kg.graph.run(cypher) + print("执行成功!关联知识点:") + kg.query_related_knowledge() # 新增查询 + else: + print("未生成有效Cypher") \ No newline at end of file diff --git a/AI/Neo4j/ReadXiaoXueMath.py b/AI/Neo4j/ReadXiaoXueMath.py index 28be5b33..93e7ebee 100644 --- a/AI/Neo4j/ReadXiaoXueMath.py +++ b/AI/Neo4j/ReadXiaoXueMath.py @@ -1,6 +1,8 @@ import json -from K2_Neo4jExecutor import * +from Neo4j.Backup.K2_Neo4jExecutor import * +from Neo4j.Util import clear + def json_to_cypher(data): """将知识体系JSON转换为Cypher插入脚本""" diff --git a/AI/Neo4j/XiaoXueMath2.json b/AI/Neo4j/XiaoXueMath2.json deleted file mode 100644 index 63167f2d..00000000 --- a/AI/Neo4j/XiaoXueMath2.json +++ /dev/null @@ -1,363 +0,0 @@ -{ - "status": 200, - "data": { - "tree": [ - { - "children": [ - { - "children": [ - { - "isLeaf": true, - "title": "整数的认识", - "value": "297B32CE384244A9A4B1560F7B788D6A", - "key": "297B32CE384244A9A4B1560F7B788D6A", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "奇数与偶数的初步认识", - "value": "E00DED49C41A4A9A8892026BD713E7D6", - "key": "E00DED49C41A4A9A8892026BD713E7D6", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "奇偶性问题", - "value": "FD14220D3B1941B8A1F9B5E70731593E", - "key": "FD14220D3B1941B8A1F9B5E70731593E", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "十进制计数法", - "value": "00F3E0C387C6493A9215547B4C9824EE", - "key": "00F3E0C387C6493A9215547B4C9824EE", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "倒数的认识", - "value": "18ABC778EE82483486EE501882CF4945", - "key": "18ABC778EE82483486EE501882CF4945", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "整数的读法和写法", - "value": "1C8DE94C3B194D14A2D195781E471640", - "key": "1C8DE94C3B194D14A2D195781E471640", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "整数的改写和近似数", - "value": "ED4C27B6E4164D1186D47A1388D42C13", - "key": "ED4C27B6E4164D1186D47A1388D42C13", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "整数大小的比较", - "value": "38CA0DBDC6AB4D67B760461F4DA4F508", - "key": "38CA0DBDC6AB4D67B760461F4DA4F508", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "分数的意义、读写及分类", - "value": "6E372C5C0AFF4FA19BBA83919A592095", - "key": "6E372C5C0AFF4FA19BBA83919A592095", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "分数与除法的关系", - "value": "2F0C4B6D532043999CEFC4E19C083604", - "key": "2F0C4B6D532043999CEFC4E19C083604", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "整数、假分数和带分数的互化", - "value": "3EFF5101123C49E28144794D4EF85C48", - "key": "3EFF5101123C49E28144794D4EF85C48", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "分数的基本性质", - "value": "437A127587584F9D8838BCCDFEEAEDF3", - "key": "437A127587584F9D8838BCCDFEEAEDF3", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "位置与方向", - "value": "60137BC830F94F99B40573C39195A89D", - "key": "60137BC830F94F99B40573C39195A89D", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "最简分数", - "value": "D0CB0C0946D94053A6A43EA65809845E", - "key": "D0CB0C0946D94053A6A43EA65809845E", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "分数大小的比较", - "value": "80A9C0D3CC014FD594B017EBAE134CB5", - "key": "80A9C0D3CC014FD594B017EBAE134CB5", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "约分和通分", - "value": "4F965D21C8A849EABE4DFA3064046242", - "key": "4F965D21C8A849EABE4DFA3064046242", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "小数的读写、意义及分类", - "value": "51631F790F2D480E80BF0E86357F62E1", - "key": "51631F790F2D480E80BF0E86357F62E1", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "循环小数", - "value": "14A51ED381AC4AE29700A30C35914540", - "key": "14A51ED381AC4AE29700A30C35914540", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "小数的性质及改写", - "value": "D621BB0E0C30402E8943BC9337E85C6F", - "key": "D621BB0E0C30402E8943BC9337E85C6F", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "小数点位置的移动与小数大小的变化规律", - "value": "C5F4CDE80EC54ED2844984380F9807E3", - "key": "C5F4CDE80EC54ED2844984380F9807E3", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "近似数及其求法", - "value": "62B012D771784C9DAA41A34D1E620D6A", - "key": "62B012D771784C9DAA41A34D1E620D6A", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "小数大小的比较", - "value": "0F8B5271C1FB4820A286B0B885D91804", - "key": "0F8B5271C1FB4820A286B0B885D91804", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "小数与分数的互化", - "value": "998F3D9211F84899A11952222612AE87", - "key": "998F3D9211F84899A11952222612AE87", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "小数点的移动引起小数的大小变化", - "value": "E1A6AD53EBCB4830BD0B48A7E28BA227", - "key": "E1A6AD53EBCB4830BD0B48A7E28BA227", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "百分数的意义、读写及应用", - "value": "21F7CDCBCD7741ADBE53EA72DD3A794F", - "key": "21F7CDCBCD7741ADBE53EA72DD3A794F", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "小数、分数和百分数之间的关系及其转化", - "value": "42CAD90DD7D64896A2D2853D0A27223F", - "key": "42CAD90DD7D64896A2D2853D0A27223F", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "百分数的实际应用", - "value": "59AE63A406B449BD913744F2D8E2361B", - "key": "59AE63A406B449BD913744F2D8E2361B", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "百分数的加减乘除运算", - "value": "11D6E99334CF41EABB7F10C970986C95", - "key": "11D6E99334CF41EABB7F10C970986C95", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "分数、百分数复合应用题", - "value": "022412DB556E44E49F2502115A999F74", - "key": "022412DB556E44E49F2502115A999F74", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "百分率应用题", - "value": "922EE944A9C4471E9BD63BE4AD29B1A1", - "key": "922EE944A9C4471E9BD63BE4AD29B1A1", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "存款利息与纳税相关问题", - "value": "7E740D512DE44692BBB8A5655D320684", - "key": "7E740D512DE44692BBB8A5655D320684", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "数轴的认识", - "value": "B1F3F6FA537A429B8CAAE623594ABCC5", - "key": "B1F3F6FA537A429B8CAAE623594ABCC5", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "负数的意义及其应用", - "value": "44ECB44412314180B8F57DE6021870E3", - "key": "44ECB44412314180B8F57DE6021870E3", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "正、负数大小的比较", - "value": "A43407EC57A147A8B716D71D49DAD5FF", - "key": "A43407EC57A147A8B716D71D49DAD5FF", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "正、负数的运算", - "value": "FF582BB3A509481BB7A699467556284A", - "key": "FF582BB3A509481BB7A699467556284A", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "自然数的认识", - "value": "F4F9E8DA69AD4283B82DF21B2193C6E2", - "key": "F4F9E8DA69AD4283B82DF21B2193C6E2", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "因数和倍数的意义", - "value": "FCFDF26E4A404E01B12C76DC35AB53E5", - "key": "FCFDF26E4A404E01B12C76DC35AB53E5", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "因数与倍数", - "value": "0DD626CFECF7472BB5F9DE1D359C85FE", - "key": "0DD626CFECF7472BB5F9DE1D359C85FE", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "2、3、5的倍数特征", - "value": "D6D65681F11B4E41BF48FAB570C320AC", - "key": "D6D65681F11B4E41BF48FAB570C320AC", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "找一个数的因数的方法", - "value": "5F2FBDDD2BF74E75BB0EF0D243549E62", - "key": "5F2FBDDD2BF74E75BB0EF0D243549E62", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "找一个数的倍数的方法", - "value": "03B4D200BE4D404895F427C24725E017", - "key": "03B4D200BE4D404895F427C24725E017", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "公倍数和最小公倍数", - "value": "32F716B70DC9454C9CD694492BF8A781", - "key": "32F716B70DC9454C9CD694492BF8A781", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "公因数和公倍数应用题", - "value": "AB91622EC3FD4CE2A7D6CD1E38CB64E6", - "key": "AB91622EC3FD4CE2A7D6CD1E38CB64E6", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "因数、公因数和最大公因数", - "value": "E9E771D409C440B6A929AE6054740F8C", - "key": "E9E771D409C440B6A929AE6054740F8C", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "求几个数的最大公因数的方法", - "value": "BF7282E1B51640D589375E457AF3EBDD", - "key": "BF7282E1B51640D589375E457AF3EBDD", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "求几个数的最小公倍数的方法", - "value": "16F8EC2F86094C9D85EF0F5BD6B8E3AF", - "key": "16F8EC2F86094C9D85EF0F5BD6B8E3AF", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "合数与质数", - "value": "05916BBA6A4C4561A3EE6D56F457B2B0", - "key": "05916BBA6A4C4561A3EE6D56F457B2B0", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "合数分解质因数", - "value": "9177BA828C3A4671BE84B553A5E4E4E2", - "key": "9177BA828C3A4671BE84B553A5E4E4E2", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - }, - { - "isLeaf": true, - "title": "真分数和假分数", - "value": "72D9E0ED3B4D461697A79D17ED4DCD60", - "key": "72D9E0ED3B4D461697A79D17ED4DCD60", - "parentValue": "B978FCBCEE0347B5AA8DB8E96A7B12F3" - } - ], - "isLeaf": false, - "title": "数的认识", - "value": "B978FCBCEE0347B5AA8DB8E96A7B12F3", - "key": "B978FCBCEE0347B5AA8DB8E96A7B12F3", - "parentValue": "0D821754BAB14DBA9164F0F060418B17" - } - ] - } - ] - } -} \ No newline at end of file diff --git a/AI/Neo4j/__pycache__/K1_KnowledgeGraph.cpython-310.pyc b/AI/Neo4j/__pycache__/K1_KnowledgeGraph.cpython-310.pyc deleted file mode 100644 index f4fccbef..00000000 Binary files a/AI/Neo4j/__pycache__/K1_KnowledgeGraph.cpython-310.pyc and /dev/null differ