码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 在线问诊 Python、FastAPI、Neo4j — 构建问题分类器


    合集 - 在线问诊(10)
    1.在线问诊 Python、FastAPI、Neo4j — 创建药品节点09-122.在线问诊 Python、FastAPI、Neo4j — 创建症状节点09-143.在线问诊 Python、FastAPI、Neo4j — 创建 检查节点09-194.在线问诊 Python、FastAPI、Neo4j — 创建 疾病节点09-205.在线问诊 Python、FastAPI、Neo4j — 创建 饮食节点09-216.在线问诊 Python、FastAPI、Neo4j — 创建 节点关系09-22
    7.在线问诊 Python、FastAPI、Neo4j — 构建问题分类器09-27
    8.在线问诊 Python、FastAPI、Neo4j — 生成 Cypher 语句09-289.在线问诊 Python、FastAPI、Neo4j — 问题咨询10-1010.在线问诊 Python、FastAPI、Neo4j — 提供咨询接口服务10-11
    收起

    目录
    • 构建字典数据
    • 构建 Trie 字典树
    • 按实体组装字典
    • 问题分析

    将问题进行分析,和系统已有的分类进行关联

    构建字典数据

    将构建的知识图片字典化, 用于后面对问题的解析,下图为症状的字典,其它字典同理
    image

    构建 Trie 字典树

    将建字典数据,组装集合

    cur_dir = '/'.join(os.path.abspath(__file__).split('/')[:-1])
    #  特征词路径
    self.disease_path = os.path.join(cur_dir, '../dict/disease.txt')
    self.check_path = os.path.join(cur_dir, '../dict/examine.txt')
    self.drug_path = os.path.join(cur_dir, '../dict/drug.txt')
    self.food_path = os.path.join(cur_dir, '../dict/food.txt')
    self.symptom_path = os.path.join(cur_dir, '../dict/symptom.txt')
    self.deny_path = os.path.join(cur_dir, '../dict/deny.txt')
    # 加载数据
    self.disease_wds = [i.strip() for i in open(self.disease_path, encoding="utf-8") if i.strip()]   # ['干眼', '右膝髌上囊及关节腔少量积液']
    self.check_wds = [i.strip() for i in open(self.check_path, encoding="utf-8") if i.strip()]  # ['膝关节核磁', '视力', '砂眼', '辨色力', '角膜', '眼底']
    self.drug_wds = [i.strip() for i in open(self.drug_path, encoding="utf-8") if i.strip()]
    self.food_wds = [i.strip() for i in open(self.food_path, encoding="utf-8") if i.strip()]
    self.symptom_wds = [i.strip() for i in open(self.symptom_path, encoding="utf-8") if i.strip()] # ['畏光','干涩','看东西有时候清楚有时候不清楚']
    
    # 读出所有 dict 里面的字典数据,并拼接成一个大而全的 集合
    # ['干眼', '右膝髌上囊及关节腔少量积液','膝关节核磁', '视力', '砂眼', '辨色力', '角膜', '眼底','畏光','干涩','看东西有时候清楚有时候不清楚']
    self.region_words = set(self.disease_wds + self.check_wds + self.drug_wds + self.food_wds + self.symptom_wds)
    

    构建 Trie 字典树
    Trie字典树:https://www.cnblogs.com/vipsoft/p/17722820.html
    Aho-Corasick 算法 AC自动机实现:https://www.cnblogs.com/vipsoft/p/17722761.html

    # 目的是为了将来对用户提的问题,进行关键词快速提取
    def build_actree(self, word_list):
        """
        构造actree,加速过滤
        :param word_list:
        :return:
        """
        actree = ahocorasick.Automaton()
        for index, word in enumerate(word_list):
            actree.add_word(word, (index, word))  # 向trie树中添加单词
        actree.make_automaton()
        return actree
    

    按实体组装字典

    # 将 ['干眼', '右膝髌上囊及关节腔少量积液','膝关节核磁', '视力', '砂眼', '辨色力', '角膜', '眼底'],进行分类,组装成不同类型的字典
    def build_wdtype_dict(self):
        """
        构造词对应的类型
        :return:
        """
        wd_dict = dict()
        for wd in self.region_words:
            wd_dict[wd] = []
            if wd in self.disease_wds:
                wd_dict[wd].append('disease')
            if wd in self.check_wds:
                wd_dict[wd].append('check')
            if wd in self.drug_wds:
                wd_dict[wd].append('drug')
            if wd in self.food_wds:
                wd_dict[wd].append('food')
            if wd in self.symptom_wds:
                wd_dict[wd].append('symptom')
        return wd_dict
    

    问题分析

    通过AC算法,过滤关键词

    # "请问最近看东西有时候清楚有时候不清楚是怎么回事"
    def check_medical(self, question):
        """
        问句过滤
        :param question:
        :return:
        """
        region_wds = []
        for i in self.region_tree.iter(question):  # 从问题中,找出关键词
            wd = i[1][1]  # 看东西有时候清楚有时候不清楚
            region_wds.append(wd)
        stop_wds = []
        for wd1 in region_wds:
            for wd2 in region_wds:
                if wd1 in wd2 and wd1 != wd2:
                    stop_wds.append(wd1)
        final_wds = [i for i in region_wds if i not in stop_wds]  # '看东西有时候清楚有时候不清楚'
        medical_dict = {i: self.wdtype_dict.get(i) for i in final_wds}  # {'看东西有时候清楚有时候不清楚': ['symptom']}
        return medical_dict
    

    解析出问题的类型

    data['args'] = medical_dict
    # 若没有查到相关的外部查询信息,那么则将该疾病的描述信息返回
    if question_types == [] and 'symptom' in types:
       question_types = ['symptom_disease']
    # 将多个分类结果进行合并处理,组装成一个字典
    data['question_types'] = question_types
    

    输出字典

    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']}
    

    后面根据 question_types 生成 CQL语句

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

  • 相关阅读:
    Linux 关闭防火墙
    Java正则表达式之捕获组奥秘探索
    如何开通 Medium会员
    DBNet:具有可微分二值化的实时场景文本检测
    【网络安全】实操XSS订单系统漏洞(利用盲打)
    Android 9 固定以太网MAC地址(cmdline)
    Spring和SpringBoot简介
    【Python】使用Pandas和随机森林对鸢尾花数据集进行分类
    结构与联合
    代码随想录训练营二刷第二十三天 | 669. 修剪二叉搜索树 108.将有序数组转换为二叉搜索树 538.把二叉搜索树转换为累加树
  • 原文地址:https://www.cnblogs.com/vipsoft/p/17728136.html
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号