目录
relationship_data.csv
症状,检查,疾病,药品,宜吃,忌吃
"上下楼梯疼,不能久站,感觉有点肿","膝关节核磁","右膝髌上囊及关节腔少量积液","扶他林","西红柿,香蕉","辣椒,大蒜"
"眼睛胀痛,干涩,畏光,眼胀,眼痛,看东西有时候清楚有时候不清楚","视力,眼底","干眼","施图伦","胡萝卜,核桃仁,菠菜","海鲜,芥末"
关系:症状-检查
def generate_examine() -> list:
"""
关系:疾病-检查
"""
rels_check = []
df = pd.read_csv('relationship_data.csv')
for idx, row in df.iterrows():
for symptom in row['检查'].split(','):
for exam in row['症状'].split(','):
rels_check.append([exam, symptom])
rels_check = deduplicate(rels_check)
return rels_check
def relationship_rels_check():
"""
# 创建关系
match(p:Symptom),(q:Examine) where p.name='上下楼梯疼' and q.name='膝关节核磁' create (p)-[rel:need_check{name:'症状检查'}]->(q)
# 删除关系
MATCH(p: Symptom)-[r: need_check]-(q:Examine)
WHERE p.name = '上下楼梯疼' and q.name = '膝关节核磁'
DELETE r
"""
cql = "MATCH(p:Symptom)-[r:need_check]-(q:Examine) DELETE r"
neo4j.execute_write(cql)
print("删除成功 => need_check")
# 症状 需要 做哪些检查
rels_check = generate_examine()
print(rels_check)
cql_list = generate_cql('Symptom', 'Examine', rels_check, 'need_check', '症状检查')
for cql in cql_list:
neo4j.execute_write(cql)
print(cql)
关系:疾病-症状
def generate_symptom() -> list:
"""
关系:疾病-症状 (疾病有哪些症状)
"""
rels_check = []
df = pd.read_csv('relationship_data.csv')
for idx, row in df.iterrows():
for symptom in row['症状'].split(','):
for exam in row['疾病'].split(','):
rels_check.append([exam, symptom])
rels_check = deduplicate(rels_check)
return rels_check
代码重构
包括疾病用药,食物能吃,食物不能吃的关系。
详细代码如下
import logging
from utils.neo4j_provider import neo4j
import pandas as pd
logging.root.setLevel(logging.INFO)
# 关系去重函数
def deduplicate(relation_old) -> list:
relation_new = []
for each in relation_old:
if each not in relation_new:
relation_new.append(each)
return relation_new
def generate_cql(start_node, end_node, edges, rel_type, rel_name) -> str:
"""
生成 CQL
"""
cql = []
for edge in edges:
p = edge[0]
q = edge[1]
# 创建关系的 Cypher 语句
cql.append(
"MATCH(p:%s),(q:%s) WHERE p.name='%s' and q.name='%s' CREATE (p)-[rel:%s{name:'%s'}]->(q)" % (start_node, end_node, p, q, rel_type, rel_name))
print('创建关系 {}-{}->{}'.format(p, rel_type, q))
return cql
def generate_relation(l_name, r_name) -> list:
relation_list = []
df = pd.read_csv('relationship_data.csv')
for idx, row in df.iterrows():
for l_node in row[l_name].split(','):
for r_node in row[r_name].split(','):
relation_list.append([l_node, r_node])
return deduplicate(relation_list)
def create_relationship(l_node, r_node, relationship, l_data_name, r_data_name, relation_name):
"""
创建关系
:param l_node: 左节点 name
:param r_node: 右节点 name
:param relationship: 关系
:param l_data_name: 左数据列名
:param r_data_name: 右数据列名
:param relation_name: 关系 name
:return:
"""
neo4j.delete_relationship(l_node, r_node, relationship)
relation_list = generate_relation(l_data_name, r_data_name)
print(relation_list)
cql_list = generate_cql(l_node, r_node, relation_list, relationship, relation_name)
for cql in cql_list:
neo4j.execute_write(cql)
print(cql)
def relationship_relation_check():
l_node = "Symptom"
r_node = "Examine"
relationship = "need_check"
l_data_name = '症状'
r_data_name = '检查'
rel_name = '症状检查'
create_relationship(l_node, r_node, relationship, l_data_name, r_data_name, rel_name)
def relationship_has_symptom():
l_node = "Disease"
r_node = "Symptom"
relationship = "has_symptom"
l_data_name = '疾病'
r_data_name = '症状'
rel_name = '症状'
create_relationship(l_node, r_node, relationship, l_data_name, r_data_name, rel_name)
def relationship_used_drugs():
l_node = "Disease"
r_node = "Drug"
relationship = "used_drugs"
l_data_name = '疾病'
r_data_name = '药品'
rel_name = '常用药品'
create_relationship(l_node, r_node, relationship, l_data_name, r_data_name, rel_name)
def relationship_doeat_foods():
l_node = "Disease"
r_node = "Foods"
relationship = "doeat_foods"
l_data_name = '疾病'
r_data_name = '宜吃'
rel_name = '推荐食物'
create_relationship(l_node, r_node, relationship, l_data_name, r_data_name, rel_name)
def relationship_noteat_foods():
l_node = "Disease"
r_node = "Foods"
relationship = "noteat_foods"
l_data_name = '疾病'
r_data_name = '忌吃'
rel_name = '忌吃食物'
create_relationship(l_node, r_node, relationship, l_data_name, r_data_name, rel_name)
if __name__ == "__main__":
# 有症状需要做哪些检查
relationship_relation_check()
# 疾病有哪些症状
relationship_has_symptom()
# 疾病常用药物
relationship_used_drugs()
# 推荐饮食
relationship_doeat_foods()
# 不宜饮食
relationship_noteat_foods()
源代码地址:https://gitee.com/VipSoft/VipQA
标签:node,name,relationship,Python,FastAPI,relation,Neo4j,data,check From: https://www.cnblogs.com/vipsoft/p/17714771.html