├── Create_Graph.py ├── README.md ├── baseline_all_kg_triples_v3_small.txt └── 示例图.png /Create_Graph.py: -------------------------------------------------------------------------------- 1 | import json 2 | from py2neo import Graph, Node, Relationship, NodeMatcher, RelationshipMatcher 3 | 4 | # 连接数据库 5 | graph = Graph("http://localhost:7474", auth=("neo4j", "123456789"),name="neo4j") 6 | 7 | # graph.delete_all() 8 | matcher_node = NodeMatcher(graph) 9 | matcher_relation = RelationshipMatcher(graph) 10 | 11 | with open("baseline_all_kg_triples.txt", "r", encoding="utf-8") as file: 12 | # i = 0 13 | for line in file.readlines(): 14 | # i = i + 1 15 | # if i % 10000 == 0: 16 | # print(i) 17 | entity_1, entity_2, relation = line.split("\t") 18 | # print(entity_1,"",entity_2,"",relation) 19 | node_1 = matcher_node.match(name=entity_1).first() 20 | if node_1 is None: 21 | node_1 = Node(name=entity_1) 22 | graph.create(node_1) 23 | 24 | # relation为第二个节点的label 25 | node_2 = matcher_node.match(name=entity_2).first() 26 | if node_2 is None: 27 | node_2 = Node(relation, name=entity_2) 28 | graph.create(node_2) 29 | if not node_2.has_label(relation): 30 | # print(i) 31 | node_2.add_label(relation) 32 | graph.push(node_2) 33 | 34 | r = Relationship(node_1, relation, node_2) 35 | graph.create(r) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # 中医TCM-neo4j 知识图谱 2 | 3 | ## 1、配置 4 | 5 | Neo4j下载:https://neo4j.com/download-center/ 6 | 7 | 版本为社区版5.7.0(需要jdk17) 8 | 9 | 10 | 11 | ## 2、运行 12 | 13 | 修改Create_Graph.py文件中的数据库的账号和密码,并运行Create_Graph.py,浏览器中打开http://localhost:7474即可查看 14 | 15 | 16 | 17 | ## 3、查询 18 | 19 | 由于数据集的节点数过大,不能对知识图谱进行完全地展示 20 | 21 | 通过 22 | 23 | ``` 24 | MATCH (n)-[r]-(b) Where n.name='便秘' RETURN n,r,b 25 | ``` 26 | 27 | 对$n.name$进行修改可以查询与需要节点相关的节点 28 | 29 | 查询“便秘”症状节点的KG示例: 30 | 31 | ![image](示例图.png) 32 | -------------------------------------------------------------------------------- /baseline_all_kg_triples_v3_small.txt: -------------------------------------------------------------------------------- 1 | 喜热饮 胃中虚火证 证候 2 | 病在肛门外肿突而硬 初发期 证候 3 | 腹痛 心经积热证 证候 4 | 水红花子 SMIT13500 symmap_chemical 5 | 槲寄生 SMIT01519 symmap_chemical 6 | SMIT16807 Liver Tender chemical_MM 7 | 当归 SMIT05129 symmap_chemical 8 | SMIT10590 Cough Nonproductive chemical_MM 9 | 高热烦渴 热毒炽盛证 证候 10 | SMIT05979 Sharp Headache chemical_MM 11 | 艾叶 SMIT05580 symmap_chemical 12 | 檀香 SMIT15645 symmap_chemical 13 | 花椒 SMIT00086 symmap_chemical 14 | 拒按 气滞血瘀证 证候 15 | 辣椒 SMIT05174 symmap_chemical 16 | SMIT11035 Abdomen Discomfort chemical_MM 17 | SMIT12696 Common Cold chemical_MM 18 | 肠道热盛 肠道实热证 证候 19 | 反应迟钝 寒盛阳衰证 证候 20 | 疲乏 血瘀 证候 21 | 干姜 SMIT00507 symmap_chemical 22 | 骨碎补 SMIT10266 symmap_chemical 23 | 苔薄黄 阴虚火炎证 证候 24 | 阳痿 仙茅 中药 25 | 马兜铃 SMIT13155 symmap_chemical 26 | SMIT11873 Bronchial Asthma chemical_MM 27 | 瞿麦 SMIT02387 symmap_chemical 28 | 榧子 平 药性 29 | SMIT10923 Deafness chemical_MM 30 | SMIT08396 Headache Throbbing chemical_MM 31 | SMIT12511 Long Sleeper Syndrome chemical_MM 32 | 冬瓜皮 SMIT15121 symmap_chemical 33 | 处生疮疖疔痈 火毒流窜证 证候 34 | 半枝莲 SMIT13037 symmap_chemical 35 | 橘红 SMIT19275 symmap_chemical 36 | SMIT18017 Impetigo chemical_MM 37 | 鼻燥咽干 血虚津亏证 证候 38 | 干姜 SMIT00399 symmap_chemical 39 | 槐角 SMIT00738 symmap_chemical 40 | 咳嗽痰稠 硼砂 中药 41 | 断血流 SMIT16917 symmap_chemical 42 | 阴不制阳 阴虚阳浮证 证候 43 | SMIT05363 Polyarthralgia chemical_MM -------------------------------------------------------------------------------- /示例图.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ywjawmw/TCM_KG/f6d5401274c16806ec76800fff995cbd920c218f/示例图.png --------------------------------------------------------------------------------