当前位置:   article > 正文

Python 医学知识图谱问答系统(二),建立医学知识图谱,基于neo4j知识图谱的医学问答体系_基于知识图谱的医药问答系统答案检索方法

基于知识图谱的医药问答系统答案检索方法

接上期:

知识图谱已经构建完毕,下面来建立问答体系:

在刘老师的代码中,是这样构造问答体系的,主要分为三部分:问题分类,问题解析,问题答案查询。

(1)在问题分类中,主要将咱们提出的问题进行分类处理,不同的问题对应不同的字典,而不同的问句的表达方式也需要穷举一下,如下图所示,主要咨询的为有疾病的症状、疾病的发生原因、疾病的并发症、疾病的用药、疾病的预防以及疾病的治疗周期等问题,针对这些问题,我们需要对这些问句的写法进行穷举,其次针对每一类问题,我们都会建立相应的关键词词典:

 代码如下:

  1. import os
  2. import ahocorasick
  3. class QuestionClassifier:
  4. def __init__(self):
  5. cur_dir = '/'.join(os.path.abspath(__file__).split('/')[:-1])
  6. # 特征词路径
  7. self.disease_path = os.path.join(cur_dir, 'dict/disease.txt')
  8. self.department_path = os.path.join(cur_dir, 'dict/department.txt')
  9. self.check_path = os.path.join(cur_dir, 'dict/check.txt')
  10. self.drug_path = os.path.join(cur_dir, 'dict/drug.txt')
  11. self.food_path = os.path.join(cur_dir, 'dict/food.txt')
  12. self.producer_path = os.path.join(cur_dir, 'dict/producer.txt')
  13. self.symptom_path = os.path.join(cur_dir, 'dict/symptom.txt')
  14. self.deny_path = os.path.join(cur_dir, 'dict/deny.txt')
  15. # 加载特征词
  16. self.disease_wds= [i.strip() for i in open(self.disease_path) if i.strip()]
  17. self.department_wds= [i.strip() for i in open(self.department_path) if i.strip()]
  18. self.check_wds= [i.strip() for i in open(self.check_path) if i.strip()]
  19. self.drug_wds= [i.strip() for i in open(self.drug_path) if i.strip()]
  20. self.food_wds= [i.strip() for i in open(self.food_path) if i.strip()]
  21. self.producer_wds= [i.strip() for i in open(self.producer_path) if i.strip()]
  22. self.symptom_wds= [i.strip() for i in open(self.symptom_path) if i.strip()]
  23. 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)
  24. self.deny_words = [i.strip() for i in open(self.deny_path) if i.strip()]
  25. # 构造领域actree
  26. self.region_tree = self.build_actree(list(self.region_words))
  27. # 构建词典
  28. self.wdtype_dict = self.build_wdtype_dict()
  29. # 问句疑问词
  30. self.symptom_qwds = ['症状', '表征', '现象', '症候', '表现']
  31. self.cause_qwds = ['原因','成因', '为什么', '怎么会', '怎样才', '咋样才', '怎样会', '如何会', '为啥', '为何', '如何才会', '怎么才会', '会导致', '会造成']
  32. self.acompany_qwds = ['并发症', '并发', '一起发生', '一并发生', '一起出现', '一并出现', '一同发生', '一同出现', '伴随发生', '伴随', '共现']
  33. self.food_qwds = ['饮食', '饮用', '吃', '食', '伙食', '膳食', '喝', '菜' ,'忌口', '补品', '保健品', '食谱', '菜谱', '食用', '食物','补品']
  34. self.drug_qwds = ['药', '药品', '用药', '胶囊', '口服液', '炎片']
  35. self.prevent_qwds = ['预防', '防范', '抵制', '抵御', '防止','躲避','逃避','避开','免得','逃开','避开','避掉','躲开','躲掉','绕开',
  36. '怎样才能不', '怎么才能不', '咋样才能不','咋才能不', '如何才能不',
  37. '怎样才不', '怎么才不', '咋样才不','咋才不', '如何才不',
  38. '怎样才可以不', '怎么才可以不', '咋样才可以不', '咋才可以不', '如何可以不',
  39. '怎样才可不', '怎么才可不', '咋样才可不', '咋才可不', '如何可不']
  40. self.lasttime_qwds = ['周期', '多久', '多长时间', '多少时间', '几天', '几年', '多少天', '多少小时', '几个小时', '多少年']
  41. self.cureway_qwds = ['怎么治疗', '如何医治', '怎么医治', '怎么治', '怎么医', '如何治', '医治方式', '疗法', '咋治', '怎么办', '咋办', '咋治']
  42. self.cureprob_qwds = ['多大概率能治好', '多大几率能治好', '治好希望大么', '几率', '几成', '比例', '可能性', '能治', '可治', '可以治', '可以医']
  43. self.easyget_qwds = ['易感人群', '容易感染', '易发人群', '什么人', '哪些人', '感染', '染上', '得上']
  44. self.check_qwds = ['检查', '检查项目', '查出', '检查', '测出', '试出']
  45. self.belong_qwds = ['属于什么科', '属于', '什么科', '科室']
  46. self.cure_qwds = ['治疗什么', '治啥', '治疗啥', '医治啥', '治愈啥', '主治啥', '主治什么', '有什么用', '有何用', '用处', '用途',
  47. '有什么好处', '有什么益处', '有何益处', '用来', '用来做啥', '用来作甚', '需要', '要']
  48. print('model init finished ......')
  49. return
  50. '''分类主函数'''
  51. def classify(self, question):
  52. data = {}
  53. medical_dict = self.check_medical(question)
  54. if not medical_dict:
  55. return {}
  56. data['args'] = medical_dict
  57. #收集问句当中所涉及到的实体类型
  58. types = []
  59. for type_ in medical_dict.values():
  60. types += type_
  61. question_type = 'others'
  62. question_types = []
  63. # 症状
  64. if self.check_words(self.symptom_qwds, question) and ('disease' in types):
  65. question_type = 'disease_symptom'
  66. question_types.append(question_type)
  67. if self.check_words(self.symptom_qwds, question) and ('symptom' in types):
  68. question_type = 'symptom_disease'
  69. question_types.append(question_type)
  70. # 原因
  71. if self.check_words(self.cause_qwds, question) and ('disease' in types):
  72. question_type = 'disease_cause'
  73. question_types.append(question_type)
  74. # 并发症
  75. if self.check_words(self.acompany_qwds, question) and ('disease' in types):
  76. question_type = 'disease_acompany'
  77. question_types.append(question_type)
  78. # 推荐食品
  79. if self.check_words(self.food_qwds, question) and 'disease' in types:
  80. deny_status = self.check_words(self.deny_words, question)
  81. if deny_status:
  82. question_type = 'disease_not_food'
  83. else:
  84. question_type = 'disease_do_food'
  85. question_types.append(question_type)
  86. #已知食物找疾病
  87. if self.check_words(self.food_qwds+self.cure_qwds, question) and 'food' in types:
  88. deny_status = self.check_words(self.deny_words, question)
  89. if deny_status:
  90. question_type = 'food_not_disease'
  91. else:
  92. question_type = 'food_do_disease'
  93. question_types.append(question_type)
  94. # 推荐药品
  95. if self.check_words(self.drug_qwds, question) and 'disease' in types:
  96. question_type = 'disease_drug'
  97. question_types.append(question_type)
  98. # 药品治啥病
  99. if self.check_words(self.cure_qwds, question) and 'drug' in types:
  100. question_type = 'drug_disease'
  101. question_types.append(question_type)
  102. # 疾病接受检查项目
  103. if self.check_words(self.check_qwds, question) and 'disease' in types:
  104. question_type = 'disease_check'
  105. question_types.append(question_type)
  106. # 已知检查项目查相应疾病
  107. if self.check_words(self.check_qwds+self.cure_qwds, question) and 'check' in types:
  108. question_type = 'check_disease'
  109. question_types.append(question_type)
  110. # 症状防御
  111. if self.check_words(self.prevent_qwds, question) and 'disease' in types:
  112. question_type = 'disease_prevent'
  113. question_types.append(question_type)
  114. # 疾病医疗周期
  115. if self.check_words(self.lasttime_qwds, question) and 'disease' in types:
  116. question_type = 'disease_lasttime'
  117. question_types.append(question_type)
  118. # 疾病治疗方式
  119. if self.check_words(self.cureway_qwds, question) and 'disease' in types:
  120. question_type = 'disease_cureway'
  121. question_types.append(question_type)
  122. # 疾病治愈可能性
  123. if self.check_words(self.cureprob_qwds, question) and 'disease' in types:
  124. question_type = 'disease_cureprob'
  125. question_types.append(question_type)
  126. # 疾病易感染人群
  127. if self.check_words(self.easyget_qwds, question) and 'disease' in types :
  128. question_type = 'disease_easyget'
  129. question_types.append(question_type)
  130. # 若没有查到相关的外部查询信息,那么则将该疾病的描述信息返回
  131. if question_types == [] and 'disease' in types:
  132. question_types = ['disease_desc']
  133. # 若没有查到相关的外部查询信息,那么则将该疾病的描述信息返回
  134. if question_types == [] and 'symptom' in types:
  135. question_types = ['symptom_disease']
  136. # 将多个分类结果进行合并处理,组装成一个字典
  137. data['question_types'] = question_types
  138. return data
  139. '''构造词对应的类型'''
  140. def build_wdtype_dict(self):
  141. wd_dict = dict()
  142. for wd in self.region_words:
  143. wd_dict[wd] = []
  144. if wd in self.disease_wds:
  145. wd_dict[wd].append('disease')
  146. if wd in self.department_wds:
  147. wd_dict[wd].append('department')
  148. if wd in self.check_wds:
  149. wd_dict[wd].append('check')
  150. if wd in self.drug_wds:
  151. wd_dict[wd].append('drug')
  152. if wd in self.food_wds:
  153. wd_dict[wd].append('food')
  154. if wd in self.symptom_wds:
  155. wd_dict[wd].append('symptom')
  156. if wd in self.producer_wds:
  157. wd_dict[wd].append('producer')
  158. return wd_dict
  159. '''构造actree,加速过滤'''
  160. def build_actree(self, wordlist):
  161. actree = ahocorasick.Automaton()
  162. for index, word in enumerate(wordlist):
  163. actree.add_word(word, (index, word))
  164. actree.make_automaton()
  165. return actree
  166. '''问句过滤'''
  167. def check_medical(self, question):
  168. region_wds = []
  169. for i in self.region_tree.iter(question):
  170. wd = i[1][1]
  171. region_wds.append(wd)
  172. stop_wds = []
  173. for wd1 in region_wds:
  174. for wd2 in region_wds:
  175. if wd1 in wd2 and wd1 != wd2:
  176. stop_wds.append(wd1)
  177. final_wds = [i for i in region_wds if i not in stop_wds]
  178. final_dict = {i:self.wdtype_dict.get(i) for i in final_wds}
  179. return final_dict
  180. '''基于特征词进行分类'''
  181. def check_words(self, wds, sent):
  182. for wd in wds:
  183. if wd in sent:
  184. return True
  185. return False
  186. if __name__ == '__main__':
  187. handler = QuestionClassifier()
  188. while 1:
  189. question = input('input an question:')
  190. data = handler.classify(question)
  191. print(data)

(2)在问题解析中,主要对问题进行构建知识图谱的查询语句,根据问题分类模块的问题分类结果,从而构建不同的查询语句。需要注意的是,在neo4j中采用的是Cypher 查询语言,这个和sql语句还是有差别的。

本模块代码如下:

  1. class QuestionPaser:
  2. '''构建实体节点'''
  3. def build_entitydict(self, args):
  4. entity_dict = {}
  5. for arg, types in args.items():
  6. for type in types:
  7. if type not in entity_dict:
  8. entity_dict[type] = [arg]
  9. else:
  10. entity_dict[type].append(arg)
  11. return entity_dict
  12. '''解析主函数'''
  13. def parser_main(self, res_classify):
  14. args = res_classify['args']
  15. entity_dict = self.build_entitydict(args)
  16. question_types = res_classify['question_types']
  17. sqls = []
  18. for question_type in question_types:
  19. sql_ = {}
  20. sql_['question_type'] = question_type
  21. sql = []
  22. if question_type == 'disease_symptom':
  23. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  24. elif question_type == 'symptom_disease':
  25. sql = self.sql_transfer(question_type, entity_dict.get('symptom'))
  26. elif question_type == 'disease_cause':
  27. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  28. elif question_type == 'disease_acompany':
  29. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  30. elif question_type == 'disease_not_food':
  31. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  32. elif question_type == 'disease_do_food':
  33. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  34. elif question_type == 'food_not_disease':
  35. sql = self.sql_transfer(question_type, entity_dict.get('food'))
  36. elif question_type == 'food_do_disease':
  37. sql = self.sql_transfer(question_type, entity_dict.get('food'))
  38. elif question_type == 'disease_drug':
  39. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  40. elif question_type == 'drug_disease':
  41. sql = self.sql_transfer(question_type, entity_dict.get('drug'))
  42. elif question_type == 'disease_check':
  43. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  44. elif question_type == 'check_disease':
  45. sql = self.sql_transfer(question_type, entity_dict.get('check'))
  46. elif question_type == 'disease_prevent':
  47. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  48. elif question_type == 'disease_lasttime':
  49. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  50. elif question_type == 'disease_cureway':
  51. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  52. elif question_type == 'disease_cureprob':
  53. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  54. elif question_type == 'disease_easyget':
  55. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  56. elif question_type == 'disease_desc':
  57. sql = self.sql_transfer(question_type, entity_dict.get('disease'))
  58. if sql:
  59. sql_['sql'] = sql
  60. sqls.append(sql_)
  61. return sqls
  62. '''针对不同的问题,分开进行处理'''
  63. def sql_transfer(self, question_type, entities):
  64. if not entities:
  65. return []
  66. # 查询语句
  67. sql = []
  68. # 查询疾病的原因
  69. if question_type == 'disease_cause':
  70. sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.cause".format(i) for i in entities]
  71. # 查询疾病的防御措施
  72. elif question_type == 'disease_prevent':
  73. sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.prevent".format(i) for i in entities]
  74. # 查询疾病的持续时间
  75. elif question_type == 'disease_lasttime':
  76. sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.cure_lasttime".format(i) for i in entities]
  77. # 查询疾病的治愈概率
  78. elif question_type == 'disease_cureprob':
  79. sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.cured_prob".format(i) for i in entities]
  80. # 查询疾病的治疗方式
  81. elif question_type == 'disease_cureway':
  82. sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.cure_way".format(i) for i in entities]
  83. # 查询疾病的易发人群
  84. elif question_type == 'disease_easyget':
  85. sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.easy_get".format(i) for i in entities]
  86. # 查询疾病的相关介绍
  87. elif question_type == 'disease_desc':
  88. sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.desc".format(i) for i in entities]
  89. # 查询疾病有哪些症状
  90. elif question_type == 'disease_symptom':
  91. sql = ["MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  92. # 查询症状会导致哪些疾病
  93. elif question_type == 'symptom_disease':
  94. 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]
  95. # 查询疾病的并发症
  96. elif question_type == 'disease_acompany':
  97. sql1 = ["MATCH (m:Disease)-[r:acompany_with]->(n:Disease) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  98. sql2 = ["MATCH (m:Disease)-[r:acompany_with]->(n:Disease) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  99. sql = sql1 + sql2
  100. # 查询疾病的忌口
  101. elif question_type == 'disease_not_food':
  102. sql = ["MATCH (m:Disease)-[r:no_eat]->(n:Food) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  103. # 查询疾病建议吃的东西
  104. elif question_type == 'disease_do_food':
  105. sql1 = ["MATCH (m:Disease)-[r:do_eat]->(n:Food) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  106. sql2 = ["MATCH (m:Disease)-[r:recommand_eat]->(n:Food) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  107. sql = sql1 + sql2
  108. # 已知忌口查疾病
  109. elif question_type == 'food_not_disease':
  110. sql = ["MATCH (m:Disease)-[r:no_eat]->(n:Food) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  111. # 已知推荐查疾病
  112. elif question_type == 'food_do_disease':
  113. sql1 = ["MATCH (m:Disease)-[r:do_eat]->(n:Food) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  114. sql2 = ["MATCH (m:Disease)-[r:recommand_eat]->(n:Food) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  115. sql = sql1 + sql2
  116. # 查询疾病常用药品-药品别名记得扩充
  117. elif question_type == 'disease_drug':
  118. sql1 = ["MATCH (m:Disease)-[r:common_drug]->(n:Drug) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  119. sql2 = ["MATCH (m:Disease)-[r:recommand_drug]->(n:Drug) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  120. sql = sql1 + sql2
  121. # 已知药品查询能够治疗的疾病
  122. elif question_type == 'drug_disease':
  123. sql1 = ["MATCH (m:Disease)-[r:common_drug]->(n:Drug) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  124. sql2 = ["MATCH (m:Disease)-[r:recommand_drug]->(n:Drug) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  125. sql = sql1 + sql2
  126. # 查询疾病应该进行的检查
  127. elif question_type == 'disease_check':
  128. sql = ["MATCH (m:Disease)-[r:need_check]->(n:Check) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  129. # 已知检查查询疾病
  130. elif question_type == 'check_disease':
  131. sql = ["MATCH (m:Disease)-[r:need_check]->(n:Check) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
  132. return sql
  133. if __name__ == '__main__':
  134. handler = QuestionPaser()

(3)在问题答案查询模块中,主要执行问题解析模块中构造的Cypher查询语句,从neo4j的知识图谱中获取查询结果,并对查询结果进行语言加工,构造人工提问的返回答案:

代码:

  1. from py2neo import Graph
  2. class AnswerSearcher:
  3. def __init__(self):
  4. self.g = Graph(
  5. host="127.0.0.1",
  6. http_port=7474,
  7. user="lhy",
  8. password="lhy123")
  9. self.num_limit = 20
  10. '''执行cypher查询,并返回相应结果'''
  11. def search_main(self, sqls):
  12. final_answers = []
  13. for sql_ in sqls:
  14. question_type = sql_['question_type']
  15. queries = sql_['sql']
  16. answers = []
  17. for query in queries:
  18. ress = self.g.run(query).data()
  19. answers += ress
  20. final_answer = self.answer_prettify(question_type, answers)
  21. if final_answer:
  22. final_answers.append(final_answer)
  23. return final_answers
  24. '''根据对应的qustion_type,调用相应的回复模板'''
  25. def answer_prettify(self, question_type, answers):
  26. final_answer = []
  27. if not answers:
  28. return ''
  29. if question_type == 'disease_symptom':
  30. desc = [i['n.name'] for i in answers]
  31. subject = answers[0]['m.name']
  32. final_answer = '{0}的症状包括:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  33. elif question_type == 'symptom_disease':
  34. desc = [i['m.name'] for i in answers]
  35. subject = answers[0]['n.name']
  36. final_answer = '症状{0}可能染上的疾病有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  37. elif question_type == 'disease_cause':
  38. desc = [i['m.cause'] for i in answers]
  39. subject = answers[0]['m.name']
  40. final_answer = '{0}可能的成因有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  41. elif question_type == 'disease_prevent':
  42. desc = [i['m.prevent'] for i in answers]
  43. subject = answers[0]['m.name']
  44. final_answer = '{0}的预防措施包括:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  45. elif question_type == 'disease_lasttime':
  46. desc = [i['m.cure_lasttime'] for i in answers]
  47. subject = answers[0]['m.name']
  48. final_answer = '{0}治疗可能持续的周期为:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  49. elif question_type == 'disease_cureway':
  50. desc = [';'.join(i['m.cure_way']) for i in answers]
  51. subject = answers[0]['m.name']
  52. final_answer = '{0}可以尝试如下治疗:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  53. elif question_type == 'disease_cureprob':
  54. desc = [i['m.cured_prob'] for i in answers]
  55. subject = answers[0]['m.name']
  56. final_answer = '{0}治愈的概率为(仅供参考):{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  57. elif question_type == 'disease_easyget':
  58. desc = [i['m.easy_get'] for i in answers]
  59. subject = answers[0]['m.name']
  60. final_answer = '{0}的易感人群包括:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  61. elif question_type == 'disease_desc':
  62. desc = [i['m.desc'] for i in answers]
  63. subject = answers[0]['m.name']
  64. final_answer = '{0},熟悉一下:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  65. elif question_type == 'disease_acompany':
  66. desc1 = [i['n.name'] for i in answers]
  67. desc2 = [i['m.name'] for i in answers]
  68. subject = answers[0]['m.name']
  69. desc = [i for i in desc1 + desc2 if i != subject]
  70. final_answer = '{0}的症状包括:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  71. elif question_type == 'disease_not_food':
  72. desc = [i['n.name'] for i in answers]
  73. subject = answers[0]['m.name']
  74. final_answer = '{0}忌食的食物包括有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  75. elif question_type == 'disease_do_food':
  76. do_desc = [i['n.name'] for i in answers if i['r.name'] == '宜吃']
  77. recommand_desc = [i['n.name'] for i in answers if i['r.name'] == '推荐食谱']
  78. subject = answers[0]['m.name']
  79. final_answer = '{0}宜食的食物包括有:{1}\n推荐食谱包括有:{2}'.format(subject, ';'.join(list(set(do_desc))[:self.num_limit]), ';'.join(list(set(recommand_desc))[:self.num_limit]))
  80. elif question_type == 'food_not_disease':
  81. desc = [i['m.name'] for i in answers]
  82. subject = answers[0]['n.name']
  83. final_answer = '患有{0}的人最好不要吃{1}'.format(';'.join(list(set(desc))[:self.num_limit]), subject)
  84. elif question_type == 'food_do_disease':
  85. desc = [i['m.name'] for i in answers]
  86. subject = answers[0]['n.name']
  87. final_answer = '患有{0}的人建议多试试{1}'.format(';'.join(list(set(desc))[:self.num_limit]), subject)
  88. elif question_type == 'disease_drug':
  89. desc = [i['n.name'] for i in answers]
  90. subject = answers[0]['m.name']
  91. final_answer = '{0}通常的使用的药品包括:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  92. elif question_type == 'drug_disease':
  93. desc = [i['m.name'] for i in answers]
  94. subject = answers[0]['n.name']
  95. final_answer = '{0}主治的疾病有{1},可以试试'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  96. elif question_type == 'disease_check':
  97. desc = [i['n.name'] for i in answers]
  98. subject = answers[0]['m.name']
  99. final_answer = '{0}通常可以通过以下方式检查出来:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  100. elif question_type == 'check_disease':
  101. desc = [i['m.name'] for i in answers]
  102. subject = answers[0]['n.name']
  103. final_answer = '通常可以通过{0}检查出来的疾病有{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
  104. return final_answer
  105. if __name__ == '__main__':
  106. searcher = AnswerSearcher()

(4)以上是知识图谱问答体系的主题框架部分,下面为调用模块,就是问答系统的输入和输出模块:

代码:

  1. from question_classifier import *
  2. from question_parser import *
  3. from answer_search import *
  4. '''问答类'''
  5. class ChatBotGraph:
  6. def __init__(self):
  7. self.classifier = QuestionClassifier()
  8. self.parser = QuestionPaser()
  9. self.searcher = AnswerSearcher()
  10. def chat_main(self, sent):
  11. answer = '暂无答案,爱莫能助呦'
  12. res_classify = self.classifier.classify(sent)
  13. if not res_classify:
  14. return answer
  15. res_sql = self.parser.parser_main(res_classify)
  16. final_answers = self.searcher.search_main(res_sql)
  17. if not final_answers:
  18. return answer
  19. else:
  20. return '\n'.join(final_answers)
  21. if __name__ == '__main__':
  22. handler = ChatBotGraph()
  23. while 1:
  24. question = input('用户:')
  25. answer = handler.chat_main(question)
  26. print('小秘书:', answer)

以上为问答体系的全部代码,主要框架如下:

 代码参考刘焕勇老师的项目,连接如下,Author: lhy<lhy_in_blcu@126.com,https://huangyong.github.io>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/459789
推荐阅读
相关标签
  

闽ICP备14008679号