创新实训-中医药知识图谱的构建与应用05
这部分参考该github项目
根据不同的问题定义回答模板
'''根据对应的qustion_type,调用相应的回复模板'''
def answer_prettify(self, question_type, answers):
final_answer = []
if not answers:
return ''
if question_type == 'drug_taste':
desc = [i['n.name'] for i in answers]
subject = answers[0]['m.name']
final_answer = '{0}的性味是:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'drug_people':
desc = [i['n.name'] for i in answers]
subject = answers[0]['m.name']
final_answer = '{0}的禁忌人群为:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'disease_drug':
desc = [i['n.name'] for i in answers]
subject = answers[0]['m.name']
final_answer = '{0}可以尝试如下药物:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'drug_disease':
desc = [i['n.name'] for i in answers]
subject = answers[0]['m.name']
final_answer = '{0}能够用于治疗以下疾病:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
elif question_type == 'drug_ingredient':
desc = [i['n.name'] for i in answers]
subject = answers[0]['m.name']
final_answer = '{0}的成分有:{1}'.format(subject, ';'.join(list(set(desc))[:self.num_limit]))
return final_answer
定义sql语句
1.执行查询语句
2.根据问句类型调用回复模板生成最终答案
'''执行cypher查询,并返回相应结果'''
def search_main(self, sqls):
final_answers = []
for sql_ in sqls:
question_type = sql_['question_type']
queries = sql_['sql']
answers = []
for query in queries:
ress = self.g.run(query).data()
answers += ress
final_answer = self.answer_prettify(question_type, answers)
if final_answer:
final_answers.append(final_answer)
return final_answers