首页 > 编程语言 >在线问诊 Python、FastAPI、Neo4j — 创建 节点关系

在线问诊 Python、FastAPI、Neo4j — 创建 节点关系

时间:2023-09-22 11:44:08浏览次数:47  
标签:node name relationship Python FastAPI relation Neo4j data check

目录

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)

image

关系:疾病-症状

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

image

代码重构

包括疾病用药,食物能吃,食物不能吃的关系。
详细代码如下

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()

image

源代码地址:https://gitee.com/VipSoft/VipQA

标签:node,name,relationship,Python,FastAPI,relation,Neo4j,data,check
From: https://www.cnblogs.com/vipsoft/p/17714771.html

相关文章

  • 【Python深度学习】深度学习中框架和模型的区别
        深度学习是人工智能领域的一股强大力量,它的快速发展离不开深度学习框架和模型的进步。本文将介绍深度学习框架和模型的基本概念、它们之间的联系与区别,以及如何根据项目需求选择合适的框架和模型。一、深度学习框架        深度学习框架是进行深度学习研......
  • 已解决The following specifications were found to be incompatible with the existi
    已解决Thefollowingspecificationswerefoundtobeincompatiblewiththeexistingpythoninstallation文章目录报错问题解决方法PS报错问题之前在工作中遇到过这个坑,记录一下问题以及解决方法,不一定针对所有情况都能用,但是可以供大家参考。问题描述如下:UnsatisfiableErr......
  • 已解决tensorflow.python.framework.errors_impl.InvalidArgumentError: slice index
    已解决tensorflow.python.framework.errors_impl.InvalidArgumentError:sliceindex1ofdimension0outofbounds.文章目录报错问题解决方法声明报错问题之前在工作中遇到过这个坑,记录一下问题以及解决方法,不一定针对所有情况都能用,但是可以供大家参考。问题描述如下:tensor......
  • python 打印功能测试程序 2
    #python打印功能测试程序fromCDHTMLTableimportHTMLTablefromPySide2.QtCoreimportQRect,QPoint,QSize,QtfromPySide2.QtGuiimportQImage,QIcon,QPixmap,QPainter,QTextDocumentfromPySide2.QtWidgetsimportQApplication,QMainWindow,QLabel,QSizeP......
  • Python Pywavelet 小波阈值
    https://blog.csdn.net/Dax1n/article/details/70304316https://buildmedia.readthedocs.org/media/pdf/pywavelets/stable/pywavelets.pdf 5.2.14节......
  • multiprocessing:Python像线程一样管理进程
    前言multiprocessing库是基于threadingAPI,它可以把工作划分为多个进程。有些情况下,multiprocessing可以作为临时替换取代threading来利用多个CPU内核,相应地避免Python全局解释器锁所带来的计算瓶颈。下面,我们来看看multiprocessing库创建进程与threading库有多像。创建一个进......
  • python爬虫简易demo
    以下是一个简单的Python爬虫示例,用于从指定的网页中提取标题和链接:importrequestsfrombs4importBeautifulSoupdefcrawl(url):#发送HTTPGET请求获取网页内容response=requests.get(url)#使用BeautifulSoup解析网页内容soup=BeautifulSoup(r......
  • Python集合
    #集合是一个无序的不重复元素序列numset={1,2,2,3}#如果存在重复元素保留一个print(numset)sqared={x**2forxin[1,2,3]}#也支持集合推导式print(sqared)set1=set([1,2,3])#从列表和元组通过set()都可转集合print(set1)set1=set(range(1,5))print(set1)s......
  • python实现消消乐游戏界面
    功能需求创建一个简单的10x10的游戏区域,其中包含了6种颜色的方块。实现思路1.首先导入pygame和random库。2.设置屏幕大小为800x600像素。3.定义一个颜色列表,包含了6种不同的颜色。 定义方块的大小为40像素。4.创建一个10x10的游戏区域,每个格子的颜色从颜色列表中随机选择。5.......
  • python: Bubble Sort
     #encoding:utf-8#版权所有2023涂聚文有限公司#许可信息查看:#描述:#Author:geovindu,GeovinDu涂聚文.#IDE:PyCharm2023.1python311#Datetime:2023/9/2121:55#User:geovindu#Product:PyCharm#Project:EssentialAlgor......