class AnswerSearching: def __init__(self): pass # 主要是根據不一樣的實體和意圖構造cypher查詢語句 def question_parser(self, data): """ 主要是根據不一樣的實體和意圖構造cypher查詢語句 :param data: {"Disease":[], "Alias":[], "Symptom":[], "Complication":[]} :return: """ pass # 將問題轉變爲cypher查詢語句 def transfor_to_sql(self, label, entities, intent): """ 將問題轉變爲cypher查詢語句 :param label:實體標籤 :param entities:實體列表 :param intent:查詢意圖 :return:cypher查詢語句 """ pass # 執行cypher查詢,返回結果 def searching(self, sqls): """ 執行cypher查詢,返回結果 :param sqls: :return:str """ pass # 根據不一樣意圖,返回不一樣模板的答案 def answer_template(self, intent, answers): """ 根據不一樣意圖,返回不一樣模板的答案 :param intent: 查詢意圖 :param answers: 知識圖譜查詢結果 :return: str """ pass 3、 代碼分模塊介紹 在Python中咱們使用py2neo進行查詢 首先安裝py2neo,pip install py2neo 鏈接上neo4j數據庫 from py2neo import Graph graph = Graph("http://localhost:7474", username="neo4j", password="neo4j") 根據不一樣的實體和意圖構造cypher查詢語句 def question_parser(data): """ 主要是根據不一樣的實體和意圖構造cypher查詢語句 :param data: {"Disease":[], "Alias":[], "Symptom":[], "Complication":[]} :return: """ sqls = [] if data: for intent in data["intentions"]: sql_ = {} sql_["intention"] = intent sql = [] if data.get("Disease"): sql = transfor_to_sql("Disease", data["Disease"], intent) elif data.get("Alias"): sql = transfor_to_sql("Alias", data["Alias"], intent) elif data.get("Symptom"): sql = transfor_to_sql("Symptom", data["Symptom"], intent) elif data.get("Complication"): sql = transfor_to_sql("Complication", data["Complication"], intent) if sql: sql_['sql'] = sql sqls.append(sql_) return sql 將問題轉變爲cypher查詢語句 def transfor_to_sql(label, entities, intent): """ 將問題轉變爲cypher查詢語句 :param label:實體標籤 :param entities:實體列表 :param intent:查詢意圖 :return:cypher查詢語句 """ if not entities: return [] sql = [] # 查詢症狀 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查詢,返回結果 def searching(sqls): """ 執行cypher查詢,返回結果 :param sqls: :return:str """ final_answers = [] for sql_ in sqls: intent = sql_['intention'] queries = sql_['sql'] answers = [] for query in queries: ress = graph.run(query).data() answers += ress final_answer = answer_template(intent, answers) if final_answer: final_answers.append(final_answer) return final_answers 根據不一樣意圖,返回不一樣模板的答案 def answer_template(intent, answers): """ 根據不一樣意圖,返回不一樣模板的答案 :param intent: 查詢意圖 :param answers: 知識圖譜查詢結果 :return: str """ final_answer = "" if not answers: return "" # 查詢症狀 if intent == "query_symptom": disease_dic = {} for data in answers: d = data['d.name'] s = data['s.name'] if d not in 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 ...