-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquestion_parser.py
179 lines (133 loc) · 8.13 KB
/
question_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
class QuestionPaser:
"""构建实体节点"""
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)
question_types = res_classify['question_types']
sqls = []
for question_type in question_types:
sql_ = {'question_type': question_type}
sql = []
if question_type == 'disease_clinicalManifestations':
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
elif question_type == 'clinicalManifestations_disease':
sql = self.sql_transfer(question_type, entity_dict.get('clinicalManifestations'))
elif question_type == 'disease_cause':
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
elif question_type == 'disease_relatedDiseases':
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
elif question_type == 'disease_site':
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
elif question_type == 'site_disease':
sql = self.sql_transfer(question_type, entity_dict.get('diseaseSite'))
elif question_type == 'disease_taboo':
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
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('treatment'))
elif question_type == 'disease_checks':
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
elif question_type == 'disease_department':
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
elif question_type == 'disease_diagnosis':
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
elif question_type == 'disease_prevention':
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
elif question_type == 'disease_treatment':
sql = self.sql_transfer(question_type, entity_dict.get('disease'))
elif question_type == 'disease_doctors':
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'))
elif question_type == 'clinicalManifestations_disease':
sql = self.sql_transfer(question_type, entity_dict.get('clinicalManifestations'))
if sql:
sql_['sql'] = sql
sqls.append(sql_)
return sqls
'''针对不同的问题,分开进行处理'''
def sql_transfer(self, question_type, entities):
if not entities:
return []
# 查询语句
sql = []
# 查询疾病有哪些症状
if question_type == 'disease_clinicalManifestations':
sql = ["MATCH (m:disease)-[r:r_clinicalManifestations]->(n:clinicalManifestations) where m.diseaseName =" \
" '{0}' return m.diseaseName, n.clinicalManifestationsName".format(i) for i in entities]
# 查询症状会导致哪些疾病
elif question_type == 'clinicalManifestations_disease':
sql = ["MATCH (m:disease)-[r:r_clinicalManifestations]->(n:clinicalManifestations) where " \
"n.clinicalManifestationsName = '{0}' return m.diseaseName, n.clinicalManifestationsName".format(i) for i in entities]
# 查询疾病的原因
elif question_type == 'disease_cause':
sql = ["match (m:disease)-[r:r_cause]->(n:cause) where m.diseaseName = '{0}' return m.diseaseName," \
" n.causeName".format(i) for i in entities]
# 查询疾病的相关疾病
elif question_type == 'disease_relatedDiseases':
sql = ["match (m:disease)-[r:r_relatedDiseases]->(n:relatedDiseases) where m.diseaseName = '{0}' " \
"return m.diseaseName, n.relatedDiseasesName".format(i) for i in entities]
# 查询疾病的发作部位
elif question_type == 'disease_site':
sql = ["match (m:disease)-[r:r_diseaseSite]->(n:diseaseSite) where m.diseaseName = '{0}' " \
"return m.diseaseName, n.diseaseSiteName".format(i) for i in entities]
# 查询发病部位对应的疾病
elif question_type == 'site_disease':
sql = ["match (m:disease)-[r:r_diseaseSite]->(n:diseaseSite) where n.diseaseSiteName = '{0}' " \
"return m.diseaseName, n.diseaseSiteName".format(i) for i in entities]
# 查询疾病的禁忌药物
elif question_type == 'disease_taboo':
sql = ["match (m:disease)-[r:r_taboos]->(n:taboos) where m.diseaseName = '{0}' " \
"return m.diseaseName, n.taboosName".format(i) for i in entities]
# 查询疾病的治疗药物
elif question_type == 'disease_drug':
sql = ["match (m:disease)-[r:r_treatment]->(n:treatment) where m.diseaseName = '{0}' and " \
"r.relation_property = '药物治疗' return m.diseaseName, n.treatmentName".format(i) for i in entities]
# 查询药物能治什么疾病
elif question_type == 'drug_disease':
sql = ["match (m:disease)-[r:r_treatment]->(n:treatment) where n.treatmentName = '{0}' and " \
"r.relation_property = '药物治疗' return m.diseaseName, n.treatmentName".format(i) for i in entities]
# 查询疾病的检查方式
elif question_type == 'disease_checks':
sql = ["match (m:disease)-[r:r_checks]->(n:checks) where m.diseaseName = '{0}' " \
"return m.diseaseName, n.checksName".format(i) for i in entities]
# 查询疾病属于哪个科室
elif question_type == 'disease_department':
sql = ["match (m:disease)-[r:r_department]->(n:department) where m.diseaseName = '{0}' " \
"return m.diseaseName, n.departmentName".format(i) for i in entities]
# 查询疾病的诊断方式
elif question_type == 'disease_diagnosis':
sql = ["match (m:disease)-[r:r_diagnosis]->(n:diagnosis) where m.diseaseName = '{0}' " \
"return m.diseaseName, n.diagnosisName".format(i) for i in entities]
# 查询疾病的预防方式
elif question_type == 'disease_prevention':
sql = ["match (m:disease)-[r:r_prevention]->(n:prevention) where m.diseaseName = '{0}' " \
"return m.diseaseName, n.preventionName".format(i) for i in entities]
# 查询疾病的治疗方式
elif question_type == 'disease_treatment':
sql = ["match (m:disease)-[r:r_treatment]->(n:treatment) where m.diseaseName = '{0}' " \
"return m.diseaseName, n.treatmentName".format(i) for i in entities]
# 查询疾病的相关医生
elif question_type == 'disease_doctors':
sql = ["match (m:disease)-[r:r_relatedDoctors]->(n:relatedDoctors) where m.diseaseName = '{0}' " \
"return m.diseaseName, n.relatedDoctorsName".format(i) for i in entities]
# 查询疾病的描述
elif question_type == 'disease_desc':
sql = ["match (m:disease) where m.diseaseName = '{0}' return m.diseaseName".format(i) for i in entities]
# 查询疾病的临床表现
elif question_type == 'clinicalManifestations_disease':
sql = ["match (m:disease) where m.diseaseName = '{0}' return m.diseaseName".format(i) for i in entities]
return sql
if __name__ == '__main__':
handler = QuestionPaser()