医药问答系统(二)提取用户问题关键字以及识别问题类型

学习笔记,非原创代码


上一篇 医药问答系统(一)python将excel或json数据处理后存入数据库

思路

1.初始化

  1. 词典:疾病、科室、检查项目、药物、食物、具体品牌的药、症状、表否定意义的词以及一个拥有全部词语的词典region_word
  2. 把region_word中所有的词取出构造actree(为了加快后面的搜索速度):region_tree
  3. 新建一个词典wdtype_dict,存储region_word中对应词汇的类型(疾病、科室...)
  4. 构造同义词词典,便于理解用户意思,适应不同的表述方法

2.分析用户的问题

  1. 问句过滤(过滤出用户提到的领域内信息):通过region_tree查找出所有在词典region_word中出现的关键词,并且过滤掉更广泛的关键词,并且通过wdtype_dict给出关键词所属的词典。
  2. 问题分类(判断用户具体已知什么求什么):通过同义词表和wdtype_dict关键词词典判断出用户的具体问题

代码

代码中需要导入ahocorasick包,现在叫pahocorasick,安装此包比较麻烦,有以下两种方法:

  1. 用pycharm,需要visual studio环境
  2. 用Anaconda,不需要vs环境,比较容易

鉴于我vs2019安装不完全和vs2015安装闪退的问题,我采取了第二种方法。

(在此感谢某人的拯救,具体怎么弄我没试过,泪已流干o(╥﹏╥)o)

 

import os
import  ahocorasick

class QuestionClassifier:
    def __init__(self):
        cur_dir = 'D:\\BaiduNetdiskDownload\\QAMedicalKG/'
        # 特征词路径
        self.disease_path = os.path.join(cur_dir, 'dict/disease.txt')
        self.department_path = os.path.join(cur_dir, 'dict/department.txt')
        self.check_path = os.path.join(cur_dir, 'dict/check.txt')
        self.drug_path = os.path.join(cur_dir, 'dict/drug.txt')
        self.food_path = os.path.join(cur_dir, 'dict/food.txt')
        self.producer_path = os.path.join(cur_dir, 'dict/producer.txt')
        self.symptom_path = os.path.join(cur_dir, 'dict/symptom.txt')
        self.deny_path = os.path.join(cur_dir, 'dict/deny.txt')
        # 加载特征词:打开文件用i遍历,用strip方法去除i的首尾空格,因为文件本身的写入格式,i取到的就是一个个本身的词汇而不是一个字一个字
        self.disease_wds= [i.strip() for i in open(self.disease_path,encoding="utf-8") if i.strip()]#encoding="utf-8"
        self.department_wds= [i.strip() for i in open(self.department_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.producer_wds= [i.strip() for i in open(self.producer_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()]
        self.region_words = set(self.department_wds + self.disease_wds + self.check_wds + self.drug_wds + self.food_wds + self.producer_wds + self.symptom_wds)
        self.deny_words = [i.strip() for i in open(self.deny_path,encoding="utf-8") if i.strip()]
        # 构造领域actree
        self.region_tree = self.build_actree(list(self.region_words))
        # 构建词典
        self.wdtype_dict = self.build_wdtype_dict()
        # 问句疑问词
        self.symptom_qwds = ['症状', '表征', '现象', '症候', '表现']
        self.cause_qwds = ['原因','成因', '为什么', '怎么会', '怎样才', '咋样才', '怎样会', '如何会', '为啥', '为何', '如何才会', '怎么才会', '会导致', '会造成']
        self.acompany_qwds = ['并发症', '并发', '一起发生', '一并发生', '一起出现', '一并出现', '一同发生', '一同出现', '伴随发生', '伴随', '共现']
        self.food_qwds = ['饮食', '饮用', '吃', '食', '伙食', '膳食', '喝', '菜' ,'忌口', '补品', '保健品', '食谱', '菜谱', '食用', '食物','补品']
        self.drug_qwds = ['药', '药品', '用药', '胶囊', '口服液', '炎片']
        self.prevent_qwds = ['预防', '防范', '抵制', '抵御', '防止','躲避','逃避','避开','免得','逃开','避开','避掉','躲开','躲掉','绕开',
                             '怎样才能不', '怎么才能不', '咋样才能不','咋才能不', '如何才能不',
                             '怎样才不', '怎么才不', '咋样才不','咋才不', '如何才不',
                             '怎样才可以不', '怎么才可以不', '咋样才可以不', '咋才可以不', '如何可以不',
                             '怎样才可不', '怎么才可不', '咋样才可不', '咋才可不', '如何可不']
        self.lasttime_qwds = ['周期', '多久', '多长时间', '多少时间', '几天', '几年', '多少天', '多少小时', '几个小时', '多少年']
        self.cureway_qwds = ['怎么治疗', '如何医治', '怎么医治', '怎么治', '怎么医', '如何治', '医治方式', '疗法', '咋治', '怎么办', '咋办', '咋治']
        self.cureprob_qwds = ['多大概率能治好', '多大几率能治好', '治好希望大么', '几率', '几成', '比例', '可能性', '能治', '可治', '可以治', '可以医']
        self.easyget_qwds = ['易感人群', '容易感染', '易发人群', '什么人', '哪些人', '感染', '染上', '得上']
        self.check_qwds = ['检查', '检查项目', '查出', '检查', '测出', '试出']
        self.belong_qwds = ['属于什么科', '属于', '什么科', '科室']
        self.cure_qwds = ['治疗什么', '治啥', '治疗啥', '医治啥', '治愈啥', '主治啥', '主治什么', '有什么用', '有何用', '用处', '用途',
                          '有什么好处', '有什么益处', '有何益处', '用来', '用来做啥', '用来作甚', '需要', '要']

        print('model init finished ......')

        return

    '''分类主函数'''
    def classify(self, question):
        data = {}
        #过滤出所有有用的行业内关键词
        medical_dict = self.check_medical(question)
        if not medical_dict:
            return {}
        data['args'] = medical_dict
        #收集问句当中所涉及到的实体类型
        types = []
        #type_右边的下划线是为了和系统变量区分开,取到的是上一步关键词的类型
        for type_ in medical_dict.values():
            types += type_
        question_type = 'others'

        question_types = []
        
        #理解用户语义:遍历同义词库,如果该类型词和问题关键词匹配上,说明用户有问到这方面问题
        # 症状  已知疾病,查症状
        if self.check_words(self.symptom_qwds, question) and ('disease' in types):
            question_type = 'disease_symptom'
            question_types.append(question_type)
        #已知症状查症状?
        if self.check_words(self.symptom_qwds, question) and ('symptom' in types):
            question_type = 'symptom_disease'
            question_types.append(question_type)

        # 原因 已知疾病查原因
        if self.check_words(self.cause_qwds, question) and ('disease' in types):
            question_type = 'disease_cause'
            question_types.append(question_type)
        # 并发症 已知疾病查并发症
        if self.check_words(self.acompany_qwds, question) and ('disease' in types):
            question_type = 'disease_acompany'
            question_types.append(question_type)

        # 推荐食品  已知疾病查食物
        if self.check_words(self.food_qwds, question) and 'disease' in types:
            deny_status = self.check_words(self.deny_words, question)
            if deny_status:
                question_type = 'disease_not_food'
            else:
                question_type = 'disease_do_food'
            question_types.append(question_type)

        #已知食物找对疾病的作用
        if self.check_words(self.food_qwds+self.cure_qwds, question) and 'food' in types:
            deny_status = self.check_words(self.deny_words, question)
            if deny_status:
                question_type = 'food_not_disease'
            else:
                question_type = 'food_do_disease'
            question_types.append(question_type)

        # 推荐药品  已知疾病查药物
        if self.check_words(self.drug_qwds, question) and 'disease' in types:
            question_type = 'disease_drug'
            question_types.append(question_type)

        # 药品治啥病  已知药物查对疾病的作用
        if self.check_words(self.cure_qwds, question) and 'drug' in types:
            question_type = 'drug_disease'
            question_types.append(question_type)

        # 已知疾病接受检查项目
        if self.check_words(self.check_qwds, question) and 'disease' in types:
            question_type = 'disease_check'
            question_types.append(question_type)

        # 已知检查项目查对相应疾病的作用
        if self.check_words(self.check_qwds+self.cure_qwds, question) and 'check' in types:
            question_type = 'check_disease'
            question_types.append(question_type)

        # 已知疾病查如何预防
        if self.check_words(self.prevent_qwds, question) and 'disease' in types:
            question_type = 'disease_prevent'
            question_types.append(question_type)

        # 已知疾病查医疗周期
        if self.check_words(self.lasttime_qwds, question) and 'disease' in types:
            question_type = 'disease_lasttime'
            question_types.append(question_type)

        # 已知疾病查治疗方式
        if self.check_words(self.cureway_qwds, question) and 'disease' in types:
            question_type = 'disease_cureway'
            question_types.append(question_type)

        # 已知疾病查治愈可能性
        if self.check_words(self.cureprob_qwds, question) and 'disease' in types:
            question_type = 'disease_cureprob'
            question_types.append(question_type)

        # 已知疾病查易感染人群
        if self.check_words(self.easyget_qwds, question) and 'disease' in types :
            question_type = 'disease_easyget'
            question_types.append(question_type)

        # 若没有查到相关的外部查询信息,那么则将该疾病的描述信息返回
        if question_types == [] and 'disease' in types:
            question_types = ['disease_desc']

        # 若没有查到相关的外部查询信息,那么则将该疾病的描述信息返回
        if question_types == [] and 'symptom' in types:
            question_types = ['symptom_disease']

        # 将多个分类结果进行合并处理,组装成一个字典
        data['question_types'] = question_types

        return data

    '''构造词对应的类型'''
    def build_wdtype_dict(self):
        wd_dict = 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.department_wds:
                wd_dict[wd].append('department')
            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')
            if wd in self.producer_wds:
                wd_dict[wd].append('producer')
        return wd_dict

    '''构造actree,加速过滤即多模匹配,给定很多个单词,查找有多少个单词在这个wordlist词典中出现过'''
    def build_actree(self, wordlist):
        actree = ahocorasick.Automaton()#创建一棵空的actree
        for index, word in enumerate(wordlist):#enumerate()将wordlist组合为一个索引序列,同时列出数据和数据下标
            actree.add_word(word, (index, word))
        actree.make_automaton()
        return actree

    '''问句过滤'''
    def check_medical(self, question):
        region_wds = []
        #i的格式(2, (2067, '肺气肿')),这一步会取出领域内关键词
        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:
                #比如“内科”in“消化内科”,并且!=
                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]
        final_dict = {i:self.wdtype_dict.get(i) for i in final_wds}

        return final_dict

    '''基于特征词进行分类'''
    def check_words(self, wds, sent):
        for wd in wds:
            if wd in sent:
                return True
        return False


if __name__ == '__main__':
    #实例化
    handler = QuestionClassifier()
    question = input('input an question:')
    data = handler.classify(question)
    print(data)

医药问答系统完整项目下载地址:
https://download.csdn.net/download/floracuu/15927225?spm=1001.2014.3001.5501


下一篇 医药问答系统(三)将用户问题转化为neo4j的查询语句

  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值