-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpostprocess.py
More file actions
126 lines (106 loc) · 4.21 KB
/
postprocess.py
File metadata and controls
126 lines (106 loc) · 4.21 KB
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
import configure
import os
def get_map():
drug_types = "T116,T195,T123,T122,T103,T120,T104,T200,T196,T126,T131,T125,T129,T130,T197,T114,T109,T121,T192,T127"
procedure_types = "T060,T065,T058,T059,T063,T062,T061"
device_types = "T203,T074,T075"
activity_types = "T052,T053,T056,T051,T064,T055,T066,T057,T054"
sem_map = {}
for item in drug_types.split(','):
sem_map[item] = 'drug'
for item in procedure_types.split(','):
sem_map[item] = 'procedure'
for item in device_types.split(','):
sem_map[item] = 'device'
for item in activity_types.split(','):
sem_map[item] = 'activity'
return sem_map
def get_semtype_id(file):
sem_id = {}
with open(file, 'r') as f:
for line in f:
tokens = line.split('|')
sem_id[tokens[3]] = tokens[2]
return sem_id
def convert2standard(snippets):
sem_id = get_semtype_id(os.path.join(configure.RESOURCE_PATH, 'semGroups.txt'))
sem_map = get_map()
result = []
for snippet in snippets:
standard = {'file_id': snippet['file_id'], 'start': snippet['start_pos'], 'end': snippet['end_pos'],
'text': snippet['raw_text']}
drug = []
procedure = []
device = []
activity = []
components = []
for entity in snippet['entities']:
components.append(entity['cui'])
temp_entity = {'text': entity['ngram'], 'maps_to': entity['cui'] + ':' + entity['term']}
sem_type = sem_map[sem_id[entity['semtypes'][0]]]
del entity['ngram']
del entity['term']
del entity['cui']
del entity['similarity']
del entity['semtypes']
del entity['preferred']
del entity['id']
# deal with the negation
if 'has_negation' not in entity.keys() or len(entity['has_negation']) == 0:
entity['has_negation'] = 'affirmed'
else:
entity['has_negation'] = 'negated'
for key, value in entity.items():
temp_entity[key] = value
if sem_type == 'drug':
drug.append(temp_entity)
if sem_type == 'procedure':
procedure.append(temp_entity)
if sem_type == 'device':
device.append(temp_entity)
if sem_type == 'activity':
activity.append(temp_entity)
if len(drug) > 0:
standard['has_drug'] = drug
if len(procedure) > 0:
standard['has_procedure'] = procedure
if len(device) > 0:
standard['has_device'] = device
if len(activity) > 0:
standard['has_activity'] = activity
# add the relation
if snippet['relation'] == 'N/A':
relation = 'N/A'
elif snippet['relation'] == 'combined_with':
comp = '<->'.join(components)
relation = 'combined_with (' + comp + ')'
elif snippet['relation'] == 'before':
comp = '->'.join(components)
relation = 'before (' + comp + ')'
elif snippet['relation'] == 'after':
comp = '->'.join(reversed(components))
relation = 'before (' + comp + ')'
elif snippet['relation'] == 'contain':
comp = '->'.join(components)
relation = 'contain (' + comp + ')'
elif snippet['relation'] == 'in':
comp = '->'.join(reversed(components))
relation = 'contain (' + comp + ')'
elif snippet['relation'] == 'substituted_for':
comp = '->'.join(components)
relation = 'substituted_for (' + comp + ')'
elif snippet['relation'] == 'switched_to':
comp = '->'.join(reversed(components))
relation = 'substituted_for (' + comp + ')'
elif snippet['relation'] == 'without':
comp = '->'.join(components)
relation = 'without (' + comp + ')'
elif snippet['relation'] == 'or':
comp = '<->'.join(components)
relation = 'or (' + comp + ')'
standard['has_relation'] = relation
result.append(standard)
return result
def run(snippets):
snippets = convert2standard(snippets)
return snippets