Repository navigation
Expand file tree
/
Copy pathannotation.py
More file actions
376 lines (354 loc) · 14 KB
/
Copy pathannotation.py
File metadata and controls
376 lines (354 loc) · 14 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
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
from flask import Blueprint
from bs4 import BeautifulSoup as bs
import zipfile
from db import DB
import datetime
import os,json
from flask_login import current_user
from models import User
from flask import Flask, render_template, request, make_response, send_file, send_from_directory, jsonify
annotation_bp = Blueprint('annotation', __name__)
@annotation_bp.route('/<name>/<count>', methods=['POST', 'GET'])
def main(name, count):
files = os.listdir('static/源文件/'+name + '/')
firsts = os.listdir('static/一标/'+name + '/')
seconds = os.listdir('static/二标/' + name + '/')
three = os.listdir('static/三标/' + name + '/')
files = sorted(files)
firsts = sorted(firsts)
seconds = sorted(seconds)
three = sorted(three)
username = current_user.username
ens = []
if os.path.exists('static/labels/' + username + '_en.txt'):
with open('static/labels/' + username + '_en.txt', 'r', encoding='utf-8') as f:
for line in f.readlines():
if line.strip().replace('\n', '') != '':
ens.append(line.strip().replace('\n', ''))
else:
with open('static/labels/' + username + '_en.txt', 'a') as f:
pass
res = []
if os.path.exists('static/labels/' + username + '_re.json'):
with open('static/labels/' + username + '_re.json', 'r', encoding='utf-8') as f:
ex = json.load(f)
if ex:
for e in ex:
if e not in res:
res.append(e)
rel_types = []
for r in res:
rel_type = r['e1_label'] + '-' + r['e2_label']
if rel_type not in rel_types:
if rel_type == 'x-x':
rel_type = r['rel_type']
rel_types.append(rel_type)
res_f = {}
for rel in rel_types:
if len(rel.split('-')) == 1:
e1_label = 'x'
e2_label = 'x'
else:
e1_label = rel.split('-')[0]
e2_label = rel.split('-')[1]
res_f[rel] = {
'rel_type': [],
'e1_label': e1_label,
'e2_label': e2_label
}
if len(rel.split('-')) == 1:
res_f[rel]['rel_type'].append(r['rel_type'])
else:
for r in res:
if r['e1_label'] + '-' + r['e2_label'] == rel:
res_f[rel]['rel_type'].append(r['rel_type'])
else:
with open('static/labels/' + username + '_re.json', 'w', encoding='utf-8') as f:
ex = []
json.dump(ex, f, ensure_ascii=False)
return render_template('annotation.html', nam=name, count=count, files=files, firsts=firsts, seconds=seconds, three=three, ens=ens, res_f=res_f)
@annotation_bp.route('/get_labels', methods=['POST'])
def get_labels():
username = current_user.username
ens = []
if os.path.exists('static/labels/' + username + '_en.txt'):
with open('static/labels/' + username + '_en.txt', 'r', encoding='utf-8') as f:
for line in f.readlines():
if line != '':
ens.append(line.strip().replace('\n', ''))
else:
with open('static/labels/' + username + '_en.txt', 'a') as f:
pass
res = []
if os.path.exists('static/labels/' + username + '_re.json'):
with open('static/labels/' + username + '_re.json', 'r', encoding='utf-8') as f:
export = json.load(f)
if export:
for e in export:
if e not in res:
res.append(e)
else:
with open('static/labels/' + username + '_re.json', 'w', encoding='utf-8') as f:
export = []
json.dump(export, f, ensure_ascii=False)
return jsonify({'ens': ens, 'res': res})
@annotation_bp.route('/labels_config', methods=['POST'])
def labels_config():
username = current_user.username
ens = request.form.get('ens')
res = request.form.get('res')
es = []
rs = []
for en in ens.split('\n'):
if en != '':
es.append(en)
try:
with open('static/labels/' + username + '_en.txt', 'w', encoding='utf-8') as f:
for e in es:
f.write(e + '\n')
for re in res.split('\n'):
if re != '':
rs.append(re)
with open('static/labels/' + username + '_re.json', 'w', encoding='utf-8') as f:
export = []
for r in rs:
xs = r.split()
export.append({
'rel_type': xs[0],
'e1_label': xs[1],
'e2_label': xs[2]
})
json.dump(export, f, ensure_ascii=False)
return 'success'
except Exception:
return 'error'
@annotation_bp.route('/get_file_content', methods=['POST'])
def get_file_content():
name = request.form.get('name')
filena = request.form.get('filena')
save_path = request.form.get('save_path')
if save_path == 'yuanwenjian':
path = 'static/源文件/' + filena + '/' + name
elif save_path == 'yibiao':
path = 'static/一标/' + filena + '/' + name
elif save_path == 'erbiao':
path = 'static/二标/' + filena + '/' + name
else:
path = 'static/三标/' + filena + '/' + name
ty = name.split('.')[-1]
if ty != 'json':
with open(path, 'r', encoding='utf-8') as f:
content = f.read()
data1 = {'content': content, 'content_new': content}
return jsonify(data1)
else:
with open(path, 'r', encoding='utf-8') as f1:
export = json.load(f1)
result = export['result']
data = result[0]
content = data['content']
spans = data['spans']
spans = sorted(spans, key=lambda i: i['start_offset'], reverse=False)
if 'relation' in data:
relation = data['relation']
else:
relation = []
if relation:
relations = []
for re in relation:
r = {}
r['label'] = re['rel_type']
r['source'] = re['span_front']['start_offset']
if 'en_property' not in re['span_front']:
r['en1_prop'] = ''
else:
r['en1_prop'] = re['span_front']['en_property']
r['target'] = re['span_after']['start_offset']
if 'en_property' not in re['span_after']:
r['en2_prop'] = ''
else:
r['en2_prop'] = re['span_after']['en_property']
if 'rel_property' not in re:
r['rel_property'] = ''
else:
r['rel_property'] = re['rel_property']
relations.append(r)
re_label = []
re_labels = []
# for re in relation:
# r = {}
# r['label'] = re['rel_type']
# r['start'] = re['span_front']['label']
# r['end'] = re['span_after']['label']
# re_label.append(r)
# for re in re_label:
# if re not in re_labels:
# re_labels.append(re)
else:
re_labels = []
relations = []
if spans:
before_start = 0
content_new = ''
global labels
labels = []
labs = []
for span in spans:
labs.append(span['label'])
for lab in labs:
if lab not in labels:
labels.append(lab)
# print(labels)
# print(spans)
for span in spans:
if span['label'] != 'null':
start = span['start_offset']
end = span['end_offset']
span_name = span['span_name']
label = span['label']
before_span = content[before_start:start]
color = 'tag-txt'+get_entity_color(label, labels)
b = "<b class='tag-txt " + color + "' id=" + str(start) + " data-start=" + str(start) + " data-end=" \
+ str(end) + " data-char=" + span_name + " data-entity=" + label + ">" + span_name + "</b>"
content_new += before_span+b
before_start = end
content_new += content[spans[-1]['end_offset']:]
# print(content_new)
# print(relations)
data = {'content_new': content_new, 'relations': relations, 'content': content}
else:
data = {'content_new': content, 'relations': relations, 'content': content}
return jsonify(data)
def get_entity_color(label,las):
i = 0
for la in las:
if label == la:
return str(i)
i += 1
@annotation_bp.route('/to_json',methods=['POST'])
def to_json():
export = {}
result = []
data = {}
spans = []
data1 = request.form.get('html')
content = request.form.get('content')
relations = request.form.get('relations')
filena = request.form.get('filena')
name = request.form.get('name')
save_path = request.form.get('save_path')
save = request.form.get('save')
relation = json.loads(relations)
soup = bs(data1, 'lxml')
for b in soup.find_all('b'):
span = {}
span['span_name'] = b.get_text()
span['label'] = b.get('data-entity')
span['start_offset'] = int(b.get('data-start'))
span['end_offset'] = int(b.get('data-end'))
spans.append(span)
sps = []
for span in spans:
# print(span)
if span['start_offset'] > span['end_offset']:
t = span['start_offset']
span['start_offset'] = span['end_offset']
span['end_offset'] = t
sps.append(span)
else:
sps.append(span)
# print(relation)
for re in relation[::]:
#re['rel_type'] = '并发症'
if 'start_offset' in re['span_front'] and 'start_offset' in re['span_after']:
#print(re)
re['span_front']['start_offset'] = int(re['span_front']['start_offset'])
re['span_front']['end_offset'] = int(re['span_front']['end_offset'])
re['span_after']['start_offset'] = int(re['span_after']['start_offset'])
re['span_after']['end_offset'] = int(re['span_after']['end_offset'])
else:
# print(re)
relation.remove(re)
# print(relation)
# for re in relation[::]:
# if (re['span_front'] not in sps) or (re['span_after'] not in sps):
# relation.remove(re)
# print(relation)
data['data_id'] = 335623
data['content'] = content
data['spans'] = sps
data['relation'] = relation
result.append(data)
export['result'] = result
if save == 'no_save':
if save_path == 'yuanwenjian':
out = 'static/一标/' + filena + '/' + name + '.json'
elif save_path == 'yibiao':
out = 'static/二标/' + filena + '/' + name + '.json'
elif save_path == 'erbiao':
out = 'static/三标/' + filena + '/' + name + '.json'
else:
out = 'static/三标/' + filena + '/' + name + '.json'
else:
if save_path == 'yuanwenjian':
out = 'static/一标/' + filena + '/' + name + '.json'
elif save_path == 'yibiao':
out = 'static/一标/' + filena + '/' + name + '.json'
elif save_path == 'erbiao':
out = 'static/二标/' + filena + '/' + name + '.json'
elif save_path == 'sanbiao':
out = 'static/三标/' + filena + '/' + name + '.json'
with open(out, 'w', encoding='utf-8') as f1:
json.dump(export, f1, ensure_ascii=False)
return 'success'
@annotation_bp.route('/save_file_update/<filename>', methods=['POST','GET'])
def save_file_update(filename):
files = os.listdir('static/源文件/'+filename)
firsts = os.listdir('static/一标/'+filename)
seconds = os.listdir('static/二标/' + filename)
three = os.listdir('static/三标/' + filename)
if os.path.exists('static/zip/' + filename):
with zipfile.ZipFile('static/zip/' + filename + '/'+filename+'_一标.zip', 'w') as azip:
root = os.getcwd()
os.chdir('static/一标/'+ filename)
for x in firsts:
azip.write(x)
os.chdir(root)
with zipfile.ZipFile('static/zip/' + filename + '/'+filename+'_二标.zip', 'w') as azip1:
root = os.getcwd()
os.chdir('static/二标/' + filename)
for x in seconds:
azip1.write(x)
os.chdir(root)
with zipfile.ZipFile('static/zip/' + filename + '/'+filename+'_三标.zip', 'w') as azip2:
root = os.getcwd()
os.chdir('static/三标/' + filename)
for x in three:
azip2.write(x)
os.chdir(root)
else:
os.makedirs('static/zip/' + filename)
with zipfile.ZipFile('static/zip/' + filename + '/'+filename+'_一标.zip', 'w') as azip:
root = os.getcwd()
os.chdir('static/一标/'+ filename)
for x in firsts:
azip.write(x)
os.chdir(root)
with zipfile.ZipFile('static/zip/' + filename + '/'+filename+'_二标.zip', 'w') as azip1:
root = os.getcwd()
os.chdir('static/二标/' + filename)
for x in seconds:
azip1.write(x)
os.chdir(root)
with zipfile.ZipFile('static/zip/' + filename + '/'+filename+'_三标.zip', 'w') as azip2:
root = os.getcwd()
os.chdir('static/三标/' + filename)
for x in three:
azip2.write(x)
os.chdir(root)
files = sorted(files)
firsts = sorted(firsts)
seconds = sorted(seconds)
three = sorted(three)
data = {'files':files,'firsts':firsts,'seconds':seconds,'three':three}
return jsonify(data)