2018-10-04 23:28:23 +08:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# coding: utf-8
|
|
|
|
# File: question_parser.py
|
|
|
|
# Author: lhy<lhy_in_blcu@126.com,https://huangyong.github.io>
|
|
|
|
# Date: 18-10-4
|
|
|
|
|
|
|
|
class QuestionPaser:
|
|
|
|
|
2018-10-05 12:12:19 +08:00
|
|
|
'''构建实体节点'''
|
2018-10-04 23:28:23 +08:00
|
|
|
def build_entitydict(self, args):
|
|
|
|
entity_dict = {}
|
|
|
|
for arg, types in args.items():
|
|
|
|
for type in types:
|
|
|
|
if type not in entity_dict:
|
|
|
|
entity_dict[type] = [arg]
|
|
|
|
else:
|
|
|
|
entity_dict[type].append(arg)
|
|
|
|
|
|
|
|
return entity_dict
|
|
|
|
|
|
|
|
'''解析主函数'''
|
|
|
|
def parser_main(self, res_classify):
|
|
|
|
args = res_classify['args']
|
|
|
|
entity_dict = self.build_entitydict(args)
|
2018-10-05 12:12:19 +08:00
|
|
|
question_types = res_classify['question_types']
|
|
|
|
sqls = []
|
|
|
|
for question_type in question_types:
|
2018-10-05 17:20:22 +08:00
|
|
|
sql_ = {}
|
|
|
|
sql_['question_type'] = question_type
|
2018-10-05 12:12:19 +08:00
|
|
|
sql = []
|
|
|
|
if question_type == 'disease_symptom':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'symptom_disease':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('symptom'))
|
|
|
|
|
|
|
|
elif question_type == 'disease_cause':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'disease_acompany':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'disease_not_food':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'disease_do_food':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'food_not_disease':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('food'))
|
|
|
|
|
|
|
|
elif question_type == 'food_do_disease':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('food'))
|
|
|
|
|
|
|
|
elif question_type == 'disease_drug':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'drug_disease':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('drug'))
|
|
|
|
|
|
|
|
elif question_type == 'disease_check':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'check_disease':
|
2018-10-05 17:20:22 +08:00
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('check'))
|
2018-10-05 12:12:19 +08:00
|
|
|
|
|
|
|
elif question_type == 'disease_prevent':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'disease_lasttime':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'disease_cureway':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'disease_cureprob':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'disease_easyget':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
elif question_type == 'disease_desc':
|
|
|
|
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
|
|
|
|
|
|
|
|
if sql:
|
2018-10-05 17:20:22 +08:00
|
|
|
sql_['sql'] = sql
|
2018-10-05 12:12:19 +08:00
|
|
|
|
2018-10-05 17:20:22 +08:00
|
|
|
sqls.append(sql_)
|
2018-10-05 12:12:19 +08:00
|
|
|
|
2018-10-05 17:20:22 +08:00
|
|
|
return sqls
|
2018-10-05 12:12:19 +08:00
|
|
|
|
|
|
|
'''针对不同的问题,分开进行处理'''
|
|
|
|
def sql_transfer(self, question_type, entities):
|
|
|
|
if not entities:
|
|
|
|
return []
|
|
|
|
|
|
|
|
# 查询语句
|
|
|
|
sql = []
|
|
|
|
# 查询疾病的原因
|
|
|
|
if question_type == 'disease_cause':
|
|
|
|
sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.cause".format(i) for i in entities]
|
|
|
|
|
|
|
|
# 查询疾病的防御措施
|
2018-10-04 23:28:23 +08:00
|
|
|
elif question_type == 'disease_prevent':
|
2018-10-05 12:12:19 +08:00
|
|
|
sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.prevent".format(i) for i in entities]
|
|
|
|
|
|
|
|
# 查询疾病的持续时间
|
2018-10-04 23:28:23 +08:00
|
|
|
elif question_type == 'disease_lasttime':
|
2018-10-05 12:12:19 +08:00
|
|
|
sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.cure_lasttime".format(i) for i in entities]
|
|
|
|
|
|
|
|
# 查询疾病的治愈概率
|
2018-10-04 23:28:23 +08:00
|
|
|
elif question_type == 'disease_cureprob':
|
2018-10-05 12:12:19 +08:00
|
|
|
sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.cured_prob".format(i) for i in entities]
|
|
|
|
|
2018-10-05 17:20:22 +08:00
|
|
|
# 查询疾病的治疗方式
|
|
|
|
elif question_type == 'disease_cureway':
|
|
|
|
sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.cure_way".format(i) for i in entities]
|
|
|
|
|
2018-10-05 12:12:19 +08:00
|
|
|
# 查询疾病的易发人群
|
2018-10-04 23:28:23 +08:00
|
|
|
elif question_type == 'disease_easyget':
|
2018-10-05 12:12:19 +08:00
|
|
|
sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.easy_get".format(i) for i in entities]
|
|
|
|
|
|
|
|
# 查询疾病的相关介绍
|
|
|
|
elif question_type == 'disease_desc':
|
|
|
|
sql = ["MATCH (m:Disease) where m.name = '{0}' return m.name, m.desc".format(i) for i in entities]
|
|
|
|
|
|
|
|
# 查询疾病有哪些症状
|
|
|
|
elif question_type == 'disease_symptom':
|
|
|
|
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]
|
|
|
|
|
|
|
|
# 查询症状会导致哪些疾病
|
|
|
|
elif question_type == 'symptom_disease':
|
|
|
|
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]
|
|
|
|
|
|
|
|
# 查询疾病的并发症
|
|
|
|
elif question_type == 'disease_acompany':
|
|
|
|
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]
|
|
|
|
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]
|
|
|
|
sql = sql1 + sql2
|
|
|
|
# 查询疾病的忌口
|
|
|
|
elif question_type == 'disease_not_food':
|
|
|
|
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]
|
|
|
|
|
|
|
|
# 查询疾病建议吃的东西
|
|
|
|
elif question_type == 'disease_do_food':
|
|
|
|
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]
|
|
|
|
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]
|
|
|
|
sql = sql1 + sql2
|
|
|
|
|
|
|
|
# 已知忌口查疾病
|
|
|
|
elif question_type == 'food_not_disease':
|
|
|
|
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]
|
|
|
|
|
|
|
|
# 已知推荐查疾病
|
|
|
|
elif question_type == 'food_do_disease':
|
|
|
|
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]
|
|
|
|
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]
|
|
|
|
sql = sql1 + sql2
|
|
|
|
|
|
|
|
# 查询疾病常用药品-药品别名记得扩充
|
|
|
|
elif question_type == 'disease_drug':
|
|
|
|
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]
|
|
|
|
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]
|
|
|
|
sql = sql1 + sql2
|
|
|
|
|
|
|
|
# 已知药品查询能够治疗的疾病
|
2018-10-04 23:28:23 +08:00
|
|
|
elif question_type == 'drug_disease':
|
2018-10-05 12:12:19 +08:00
|
|
|
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]
|
|
|
|
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]
|
|
|
|
sql = sql1 + sql2
|
|
|
|
# 查询疾病应该进行的检查
|
|
|
|
elif question_type == 'disease_check':
|
|
|
|
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]
|
|
|
|
|
|
|
|
# 已知检查查询疾病
|
|
|
|
elif question_type == 'check_disease':
|
|
|
|
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]
|
|
|
|
|
|
|
|
return sql
|
|
|
|
|
2018-10-04 23:28:23 +08:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
handler = QuestionPaser()
|