目录
这边只是为了测试,演示效果和思路,实际应用中,可以通过NLP构建CQL
接上一篇的问题分类
question = "请问最近看东西有时候清楚有时候不清楚是怎么回事" # 最终输出 data = {'args': {'看东西有时候清楚有时候不清楚': ['symptom']}, 'question_types': ['symptom_disease']} question = "干眼常用药有哪些" # 最终输出 data = {'args': {'干眼': ['disease']}, 'question_types': ['disease_drug']} question = "干眼哪些不能吃" data = {'args': {'干眼': ['disease']}, 'question_types': ['disease_not_food']}
构建节点字典
目的,为了拼CQL,查出符合条件的节点详情
def build_nodedict(self, args): """ 构建节点字典 :param args: {'看东西有时候清楚有时候不清楚': ['symptom']} :return: 组装成 => {'symptom': '看东西有时候清楚有时候不清楚'} """ node_dict = {} for arg, types in args.items(): for type in types: if type not in node_dict: node_dict[type] = [arg] else: node_dict[type].append(arg) return node_dict
# 输入: {'看东西有时候清楚有时候不清楚': ['symptom']} # 输出: {'symptom': ['看东西有时候清楚有时候不清楚']}
构建Cypher CQL语句
# 查询症状会导致哪些疾病 if 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] # 查询症状会导致哪些疾病 if 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] # 查询疾病常用药品-药品别名记得扩充 if question_type == 'disease_drug': sql = ["MATCH (m:Disease)-[r:used_drugs]->(n:Drug) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities] # 查询疾病的忌口 if question_type == 'disease_not_food': sql = ["MATCH (m:Disease)-[r:noteat_foods]->(n:Foods) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]
node_dict.get('symptom')
Test
if __name__ == '__main__': handler = QuestionPaser() question_class = {'args': {'看东西有时候清楚有时候不清楚': ['symptom']}, 'question_types': ['symptom_disease']} cql = handler.parser_main(question_class) print(cql)
输出:
# 输入 question_class = {'args': {'看东西有时候清楚有时候不清楚': ['symptom']}, 'question_types': ['symptom_disease']} # 输出 [{'question_type': 'symptom_disease', 'sql': ["MATCH (m:Disease)-[r:has_symptom]->(n:Symptom) where n.name = '看东西有时候清楚有时候不清楚' return m.name, r.name, n.name"]}] # 输入: question_class = {'args': {'干眼': ['disease']}, 'question_types': ['disease_drug']} # 输出: [{'question_type': 'disease_drug', 'sql': ["MATCH (m:Disease)-[r:used_drugs]->(n:Drug) where m.name = '干眼' return m.name, r.name, n.name"]}] # 输入: question_class = {'args': {'干眼': ['disease']}, 'question_types': ['disease_not_food']} # 输出: [{'question_type': 'disease_not_food', 'sql': ["MATCH (m:Disease)-[r:noteat_foods]->(n:Foods) where m.name = '干眼' return m.name, r.name, n.name"]}]
后面根据 生成的 CQL语句,查询出知识图谱中对应的数据,