在线问诊 Python、FastAPI、Neo4j — 生成 Cypher 语句
  DGdJRDRdbXFV 2023年11月19日 30 0

目录

  • 构建节点字典
  • 构建Cypher CQL语句
  • Test


这边只是为了测试,演示效果和思路,实际应用中,可以通过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"]}]

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

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

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

作者:VipSoft


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
DGdJRDRdbXFV