菜哥学知识图谱(通过“基于医疗知识图谱的问答系统”)(六)(代码分析3)
程序员文章站
2022-03-04 13:06:45
...
上接菜哥学知识图谱(通过“基于医疗知识图谱的问答系统”)(五)(neo4j的cql语言)
本节继续进行代码分析。
7.answer_search.py。该类的classify()方法,将问题提取了关键字,并对问题进行了分类。下面对代码进行注释。
#!/usr/bin/env python3
# coding: utf-8
# File: question_classifier.py
# Author: lhy<[email protected],https://huangyong.github.io>
# Date: 18-10-4
import os
import ahocorasick #ahocorasick实现快速的关键字匹配
class QuestionClassifier:
def __init__(self):
cur_dir = '/'.join(os.path.abspath(__file__).split('/')[:-1])
# 特征词路径
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')
# 加载特征词
self.disease_wds= [i.strip() for i in open(self.disease_path, encoding='utf-8') if i.strip()] #把词取出,放入list #疾病名称
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)) #建立一个AC自动机,方法在后面。AC制动机的目的是加速匹配。
# 构建词典
self.wdtype_dict = self.build_wdtype_dict() #词的类型的字典,{'百日咳':'disease',....}
# 问句疑问词,把所有的问句形式穷举
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) #从问句中匹配出的,{'百日咳':'disease',....}
if not medical_dict:
return {}
data['args'] = medical_dict
#收集问句当中所涉及到的实体类型
types = []
for type_ in medical_dict.values():
types += type_ #所有的类型,如['disease','food',......]
question_type = 'others'
question_types = []
# 症状
if self.check_words(self.symptom_qwds, question) and ('disease' in types): #如果symptom_qwds(症状词) 在 question中,且有类型为'disease'的词,即知道疾病名称,则:
question_type = 'disease_symptom' #问句类型为:已知疾病询问症状
question_types.append(question_type)
if self.check_words(self.symptom_qwds, question) and ('symptom' in types): #如果症状词在question中,且有症状词
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() #创建一个空字典
for wd in self.region_words: #所有的关键字
wd_dict[wd] = []
if wd in self.disease_wds:
wd_dict[wd].append('disease') #如果该关键字属于 疾病,则wd_dict[wd] = ['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 #返回了一个字典,里面的内容是{'百日咳':'disease',....}
'''构造actree,加速过滤''' #输入list,输出一个AC树
def build_actree(self, wordlist):
actree = ahocorasick.Automaton()
for index, word in enumerate(wordlist):
actree.add_word(word, (index, word))
actree.make_automaton()
return actree
'''问句过滤'''
def check_medical(self, question):
region_wds = []
for i in self.region_tree.iter(question): #快速匹配,匹配的结果是一个双重元组序列,形如('百日咳',(3324,'百日咳')),就是前面的actree.add_word(word, (index, word))给加进来成tree的。这个index是在前面所有关键字加在一起之后的region_words中的***
wd = i[1][1] #把'百日咳'这个关键字给挑出来了
region_wds.append(wd) #把所有的关键字列入列表region_wds
stop_wds = [] #下面这几句做了这么一件事:
for wd1 in region_wds: #1.如果一个问句里面挑出了两个词
for wd2 in region_wds:
if wd1 in wd2 and wd1 != wd2: #2.如果其中一个词包含另一个词
stop_wds.append(wd1) #3.则把短的词写入stop_wds
final_wds = [i for i in region_wds if i not in stop_wds] #4.final_wds中是把region_wds去掉stop_wds。就是说如果问句中一个词包含另外一个词,则以长词为准。
final_dict = {i:self.wdtype_dict.get(i) for i in final_wds} #类似于{'百日咳':'disease',....},其中'百日咳'在问句中,且不在stop_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()
while 1:
question = input('input an question:')
data = handler.classify(question)
print(data)
上一篇: pytorch处理类别不平衡问题
下一篇: C#获取json文件