forked from c01dkit/MedicalKnowledgeGraph_KBQA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
questionMapping.py
592 lines (501 loc) · 22.9 KB
/
questionMapping.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
"""
用于编写匹配规则
1. 首先定义关键词:既可以使用自定义词典的pos分词,也可以手动定义token分词
2. 然后在Question类内定义触发时所用的函数,拼接Sparql查询语句
3. 最后在rules列表内添加新的匹配规则,需要定义好权重、说明、匹配规则、调用函数等信息
"""
from refo import finditer, Predicate, Star, Any # 正则表达式库
import re
SPARQL_PREFIX = u"""
PREFIX : <http://www.medicalQA.zju/>
"""
SPARQL_SELECT_TEM = u"{prefix}\n" + \
u"SELECT DISTINCT {select} WHERE {{\n" + \
u"{expression}\n" + \
u"}}\n"
SPARQL_COUNT_TEM = u"{prefix}\n" + \
u"SELECT COUNT({select}) WHERE {{\n" + \
u"{expression}\n" + \
u"}}\n"
SPARQL_ASK_TEM = u"{prefix}\n" + \
u"ASK {{\n" + \
u"{expression}\n" + \
u"}}\n"
class W(Predicate):
def __init__(self, token=".*", pos=".*"):
self.token = re.compile(token + "$")
self.pos = re.compile(pos + "$")
super(W, self).__init__(self.match)
def match(self, word):
m1 = self.token.match(word.token)
m2 = self.pos.match(word.pos)
return m1 and m2
class Rule(object):
def __init__(self, condition_weight, description=None,
condition=None, action=None):
assert condition and action
self.condition_weight = condition_weight
self.description = description
self.condition = condition
self.action = action
def apply(self, word_objects):
matches = []
for m in finditer(self.condition, word_objects):
i, j = m.span()
matches.extend(word_objects[i:j])
return self.action(matches), self.condition_weight, self.description
illName = 'nhd' # 具体的疾病名
medicalName = 'nhn' # 医学相关的名词
illDepartment = 'nhc' # 疾病所属科目
illClass = 'nhl' # 疾病类别
drug = 'nhm' # 药品名称
illSymptom = 'nhs' # 疾病症状
illName_entity = (W(pos=illName))
illDepartment_entity = (W(pos=illDepartment))
illClass_entity = (W(pos=illClass))
medical_entity = (W(pos=medicalName))
drug_entity = (W(pos=drug))
illSymptom_pos = (W(pos=illSymptom))
is_ = (W('是'))
dis = (W('不')|W('不能'))
contains = (W('包括') | W('有') | W('涵盖') | W('包含'))
what = (W('什么') | W('哪些') | W('多少')|W('咋'))
should = (W('适合')| W('宜') |W('应该') |W('需要') |W('应当')|W('可以')|W('能')|W('了能'))
whether = (W('有没有')|W('是否')|W('是不是')|W('有无'))
why = (W('为什么')|W('为啥'))
cause = (W('导致') | W('诱因') | W('引起') |W('起因') |W('病因') |W('原因') |W('成因'))
clinic = (W('临床')|W('医学'))
cost = (W('收费')|W('花销')|W('开销')|W('钱'))
cure_ratio = (W('治愈率')| W('康复率') |W('成功率'))
department_of = (W('科室') | W('科'))
desc = (W('介绍')|W('描述'))
diagnosis = (W('诊断')| W('确诊') | W('判断'))
disease = (W('疾病')|W('病'))
drug = (W('药') | W('药物') |W('药品'))
food = (W('食物')|W('吃')|W('摄入'))
inspect = (W('检查')|W('诊断'))
insurance = (W('医保') | W('医疗保险'))
mobidity = (W('患病率') | W('发病率') | W('患病比例') |W('得病率'))
neopathy = (W('并发症'))
nursing = (W('护理'))
period = (W('治疗周期')| W('治疗时间') |W('时间')|W('多久'))
prevent = (W('预防')|W('避免'))
spread = (W('传染') | W('传染性') |W('传播') |W('传播性'))
suggestion = (W('建议')|W('怎么办'))
susceptible = (W('易感人群')|W('人群'))
symptom = (W('症状') | W('表现') |W('状况'))
treat = (W('治疗方式'))
treatment = (W('治')|W('治疗')|W('治愈'))
class Question:
def __init__(self):
pass
@staticmethod
def find_illness_treat(word_objects): # 查询疾病治疗方式
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :treat ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_suggestion(word_objects): # 查询疾病建议
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"{" + u"?a :ill_name '{illName}'." \
u"?a :suggestion ?x.".format(illName=w.token) + u"} UNION "\
u"{" + u"?a :ill_name '{illName}'." \
u"?a :nursing ?x.".format(illName=w.token) + u"}"
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_inspect(word_objects): # 查询疾病临床检查手段
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :inspect ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_cure_ratio(word_objects): # 查询疾病治愈率
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :cure_ratio ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_prevent(word_objects): # 查询疾病预防手段
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :prevent ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_nursing(word_objects): # 查询疾病护理方式
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :nursing ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_desc(word_objects): # 查询疾病简介
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :desc ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_badfood(word_objects): # 查询疾病忌食
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :badfood ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_goodfood(word_objects): # 查询疾病宜食
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :goodfood ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_diagnosis(word_objects): # 查询疾病诊断方式
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :diagnosis ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_susceptible(word_objects): # 查询疾病易感人群
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :susceptible ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_mobidity(word_objects): # 查询疾病发病率
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :mobidity ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_cost(word_objects): # 查询疾病开销
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :cost ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_cause(word_objects): # 查询疾病起因
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :cause ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_symptom(word_objects): # 查询疾病症状
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :symptom ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def judge_illness(word_objects): # 根据症状查询疾病
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illSymptom:
e = u"?a :ill_name ?x." \
u"?a :symptom ?b."\
u"FILTER regex(?b, '{illsymtom}')".format(illsymtom=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_neopathy(word_objects): # 查询疾病并发症
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :neopathy ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_cure_period(word_objects): # 查询疾病治疗周期
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :period ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_spread(word_objects): # 查询疾病传染性
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :spread ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_insurance(word_objects): # 查询疾病是否纳入医疗保险
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :insurance ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_department(word_objects): # 查询疾病所属科室
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :department ?b." \
u"?b :name ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_drugs(word_objects): # 查询疾病所用药品
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :drugs ?b." \
u"?b :name ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_illness_treatment(word_objects): # 查询疾病治疗手段
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illName:
e = u"?a :ill_name '{illName}'." \
u"?a :treatment ?b." \
u"?b :name ?x".format(illName=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
@staticmethod
def find_department_illnesses(word_objects): # 查询某个科室下的疾病
select = u"?x"
sparql = None
for w in word_objects:
if w.pos == illDepartment:
e = u"?a :department ?b." \
u"?b :name '{illDepartment}'." \
u"?a :ill_name ?x".format(illDepartment=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
elif w.pos == illClass:
e = u"?a :ill_name '{illClass}'." \
u"?a :department ?b." \
u"?c :department ?b." \
u"?c :ill_name ?x".format(illClass=w.token)
sparql = SPARQL_SELECT_TEM.format(prefix=SPARQL_PREFIX,
select=select,
expression=e)
break
return sparql
rules = [
Rule(condition_weight=2, description='查询疾病所属科室',
condition=(illName_entity + Star(Any(), greedy=False)+ department_of + Star(Any(), greedy=False)),
action=Question.find_illness_department),
Rule(condition_weight=4, description='查询科室下属疾病',
condition=((illDepartment_entity + contains + what + Star(disease,greedy=False)) |
(illClass_entity + Star(Any(), greedy=False) + contains + what + Star(disease,greedy=False))),
action=Question.find_department_illnesses),
Rule(condition_weight=2, description='查询疾病是否医保',
condition=(illName_entity + Star(Any(),greedy=False) + insurance),
action=Question.find_illness_insurance),
Rule(condition_weight=2, description='查询疾病传染性',
condition=(illName_entity + Star(Any(), greedy=False) + spread),
action=Question.find_illness_spread),
Rule(condition_weight=2, description='查询疾病并发症',
condition=(illName_entity + Star(Any(), greedy=False) + neopathy),
action=Question.find_illness_neopathy),
Rule(condition_weight=2, description='查询疾病治疗周期',
condition=(illName_entity + Star(Any(), greedy=False) + period),
action=Question.find_illness_cure_period),
Rule(condition_weight=2, description='查询疾病临床检查手段',
condition=((illName_entity + Star(Any() | clinic, greedy=False) + inspect)|
(whether+Star(Any(),greedy=False)+illName_entity)),
action=Question.find_illness_inspect),
Rule(condition_weight=2, description='查询疾病诊断方式',
condition=(illName_entity + Star(Any() | clinic, greedy=False) + diagnosis),
action=Question.find_illness_diagnosis),
Rule(condition_weight=3, description='查询疾病治疗费用',
condition=(illName_entity + Star(Any(), greedy=False) + cost),
action=Question.find_illness_cost),
Rule(condition_weight=2, description='查询疾病治愈率',
condition=(illName_entity + Star(Any(), greedy=False) + cure_ratio),
action=Question.find_illness_cure_ratio),
Rule(condition_weight=2, description='查询疾病预防手段',
condition=((illName_entity + Star(Any(), greedy=False) + prevent)|
(prevent + Star(Any(),greedy=False)+illName_entity)),
action=Question.find_illness_prevent),
Rule(condition_weight=2, description='查询疾病护理方式',
condition=(illName_entity + Star(Any(), greedy=False) + nursing),
action=Question.find_illness_nursing),
Rule(condition_weight=3, description='查询疾病建议',
condition=(illName_entity + Star(Any(), greedy=False) + suggestion),
action=Question.find_illness_suggestion),
Rule(condition_weight=3, description='查询疾病宜食',
condition=(illName_entity + Star(Any(), greedy=False) + Star(should,greedy=False) + food),
action=Question.find_illness_goodfood),
Rule(condition_weight=4, description='查询疾病忌食',
condition=(illName_entity + Star(Any(), greedy=False) + dis + Star(should,greedy=False) + food),
action=Question.find_illness_badfood),
Rule(condition_weight=1, description='查询疾病简介',
condition=((illName_entity + Star(Any(), greedy=False) + desc)|
(what+Star(Any(),greedy=False)+illName_entity)|
(illName_entity+Star(Any(),greedy=False)+what)|
(desc+Star(Any(),greedy=False)+illName_entity)),
action=Question.find_illness_desc),
Rule(condition_weight=2, description='查询疾病发病率',
condition=(illName_entity + Star(Any(), greedy=False) + mobidity),
action=Question.find_illness_mobidity),
Rule(condition_weight=2, description='查询疾病治疗手段',
condition=(illName_entity + Star(Any(), greedy=False) + treatment),
action=Question.find_illness_treatment),
Rule(condition_weight=2, description='查询疾病易感人群',
condition=(illName_entity + Star(Any(), greedy=False) + susceptible),
action=Question.find_illness_susceptible),
Rule(condition_weight=2, description='查询疾病起因',
condition=(illName_entity + Star(Any(), greedy=False) + cause),
action=Question.find_illness_cause),
Rule(condition_weight=2, description='查询疾病症状',
condition=(illName_entity + Star(Any(), greedy=False) + symptom),
action=Question.find_illness_symptom),
Rule(condition_weight=2, description='查询疾病治疗方式',
condition=(illName_entity + Star(Any(), greedy=False) + treat),
action=Question.find_illness_treat),
Rule(condition_weight=4, description='查询疾病所用药物',
condition=(illName_entity + Star(Any(),greedy=False) + drug),
action=Question.find_illness_drugs),
Rule(condition_weight=5, description='根据症状查询疾病',
condition=((illSymptom_pos + Star(Any(),greedy=False) + disease)|
(why + Star(Any(),greedy=False) + illSymptom_pos)|
(illSymptom_pos + Star(Any(),greedy=False) + what)),
action=Question.judge_illness)
]