# 查询症状 if intent == "query_symptom"and label == "Disease": sql = ["MATCH (d:Disease)-[:HAS_SYMPTOM]->(s) WHERE d.name='{0}' RETURN d.name,s.name".format(e) for e in entities] # 查询治疗方法 if intent == "query_cureway"and label == "Disease": sql = ["MATCH (d:Disease)-[:HAS_DRUG]->(n) WHERE d.name='{0}' return d.name,d.treatment," \ "n.name".format(e) for e in entities] # 查询治疗周期 if intent == "query_period"and label == "Disease": sql = ["MATCH (d:Disease) WHERE d.name='{0}' return d.name,d.period".format(e) for e in entities ### ... (下面类似)
执行cypher查询,返回结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14
defsearching(self, sqls): final_answers = []
for sql_ in sqls: intent = sql_['intent'] queries = sql_['sql'] answers = [] for query in queries: ress = graph.run(quary).data() answers += ress final_answer = self.answer_template(intent, answers) if final_answer: final_answers.append(final_answer) return final_answers
if intent == "query_symptom": disease_dic = {} for data in answers: d = data['d.name'] s = data['s.name'] if d notin disease_dic: disease_dic[d] = [s] else: disease_dic[d].append(s) i = 0 for k, v in disease_dic.items(): if i >= 10: break final_answer += "疾病 {0} 的症状有 {1} \n".format(k, ','.join(list(set(v)))) i += 1