• 在线问诊 Python、FastAPI、Neo4j — 生成 Cypher 语句



    这边只是为了测试,演示效果和思路,实际应用中,可以通过NLP构建CQL
    接上一篇的问题分类

    question = "请问最近看东西有时候清楚有时候不清楚是怎么回事"
    # 最终输出
    data = {'args': {'看东西有时候清楚有时候不清楚': ['symptom']}, 'question_types': ['symptom_disease']}
    
    question = "干眼常用药有哪些"
    # 最终输出
    data = {'args': {'干眼': ['disease']}, 'question_types': ['disease_drug']}
    
    question = "干眼哪些不能吃"
    data = {'args': {'干眼': ['disease']}, 'question_types': ['disease_not_food']}
    

    构建节点字典

    目的,为了拼CQL,查出符合条件的节点详情

    def build_nodedict(self, args):
        """
        构建节点字典
        :param args: {'看东西有时候清楚有时候不清楚': ['symptom']}
        :return: 组装成 => {'symptom': '看东西有时候清楚有时候不清楚'}
        """
        node_dict = {}
        for arg, types in args.items():
            for type in types:
                if type not in node_dict:
                    node_dict[type] = [arg]
                else:
                    node_dict[type].append(arg)
        return node_dict
    
    # 输入:
    {'看东西有时候清楚有时候不清楚': ['symptom']}
    # 输出:
    {'symptom': ['看东西有时候清楚有时候不清楚']}
    

    构建Cypher CQL语句

    # 查询症状会导致哪些疾病
    if question_type == 'symptom_disease':
        sql = ["MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
    
    # 查询症状会导致哪些疾病
    if question_type == 'symptom_disease':
        sql = ["MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
    
    # 查询疾病常用药品-药品别名记得扩充
    if question_type == 'disease_drug':
        sql = ["MATCH (m:Disease)-[r:used_drugs]->(n:Drug) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
    
    
    # 查询疾病的忌口
    if question_type == 'disease_not_food':
        sql = ["MATCH (m:Disease)-[r:noteat_foods]->(n:Foods) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
    
    

    node_dict.get('symptom')

    Test

    if __name__ == '__main__':
        handler = QuestionPaser()
        question_class = {'args': {'看东西有时候清楚有时候不清楚': ['symptom']}, 'question_types': ['symptom_disease']}
        cql = handler.parser_main(question_class)
        print(cql)
    

    输出:

    # 输入
    question_class = {'args': {'看东西有时候清楚有时候不清楚': ['symptom']}, 'question_types': ['symptom_disease']}
    # 输出
    [{'question_type': 'symptom_disease', 'sql': ["MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where n.name = '看东西有时候清楚有时候不清楚' return m.name, r.name, n.name"]}]
    
    # 输入:
    question_class = {'args': {'干眼': ['disease']}, 'question_types': ['disease_drug']}
    # 输出: 
    [{'question_type': 'disease_drug', 'sql': ["MATCH (m:Disease)-[r:used_drugs]->(n:Drug) where m.name = '干眼' return m.name, r.name, n.name"]}]
    
    # 输入:
    question_class = {'args': {'干眼': ['disease']}, 'question_types': ['disease_not_food']}
    # 输出:
    [{'question_type': 'disease_not_food', 'sql': ["MATCH (m:Disease)-[r:noteat_foods]->(n:Foods) where m.name = '干眼' return m.name, r.name, n.name"]}]
    

    image

    后面根据 生成的 CQL语句,查询出知识图谱中对应的数据,

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

  • 相关阅读:
    DC -2 靶机复盘
    自定义实现乘风破浪的小船
    金仓数据库KingbaseES物理备份恢复命令选项(stanza-create命令)
    移动跨平台技术方案浅析
    LVS+keepalived高可用
    windows上的IOCP如何使用,并用C++实现多客户端服务器
    AI视频教程下载-ChatGPT速成课程:工作中的ChatGPT入门
    人工智能与数据挖掘的关系
    ComText让机器人有了情节记忆
    【CSDN】5周年创作纪念日,不忘初心,砥砺前行。
  • 原文地址:https://www.cnblogs.com/vipsoft/p/17729384.html