forked from leonard-thong/SciAnnotate
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
116 lines (107 loc) · 4.5 KB
/
test.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
def prehandle_data(**kwargs):
collection = kwargs['collection']
document = kwargs['document']
directory = collection
real_dir = real_directory(directory)
document = path_join(real_dir, document)
txt_file_path = document + '.txt'
ann_file_path = txt_file_path[:-4] + '.ann'
function_ann_file_path = txt_file_path[:-4] + '_func.ann'
out = []
with open(txt_file_path, 'r') as txt_file:
for line in txt_file.readlines():
sentence = dict()
sentence['sentence'] = line
sentence['annotation'] = []
out.append(sentence)
return _prehandle_data(out, txt_file_path, ann_file_path,function_ann_file_path)
def _prehandle_data(out, txt_file_path, ann_file_path, function_ann_file_path):
res = dict()
with open(ann_file_path, 'r') as ann_file:
for line in ann_file.readlines():
line_num = -1
sentence = dict()
sentence['sentence'] = ''
sentence['annotation'] = []
data = []
line = line.replace('\t', ' ')
info = line.split(' ')
source_name = info[1].split('_')[0]
temp = info[1].split('_')[1:]
label = ''
for i in range(len(temp)):
label += temp[i]
data.append(source_name)
data.append(label)
start = int(info[2])
end = int(info[3])
line_dict = judge_line(txt_file_path)
line_start_index = [key for key in line_dict]
line_start_index = sorted(line_start_index)
for i in range(len(line_start_index)):
if start > int(line_start_index[i]) and end < (int(line_start_index[i]) + len(line_dict[line_start_index[i]])):
sentence['sentence']=line_dict[line_start_index[i]]
start -= int(line_start_index[i])
end -= int(line_start_index[i])
line_num = i
break
'''
elif start > line_start_index[i] and end > (line_start_index[i] + len(line_dict[line_start_index[i]])):
'''
data.append(start)
data.append(end)
out[line_num]['annotation'].append(data)
with open(function_ann_file_path, 'r') as function_ann_file:
for line in function_ann_file.readlines():
line_num = -1
sentence = dict()
sentence['sentence'] = ''
sentence['annotation'] = []
data = []
line = line.replace('\t', ' ')
info = line.split(' ')
source_name = info[1].split('_')[0]
temp = info[1].split('_')[1:]
label = ''
for i in range(len(temp)):
label += temp[i]
data.append(source_name)
data.append(label)
start = int(info[2])
end = int(info[3])
line_dict = judge_line(txt_file_path)
line_start_index = [key for key in line_dict]
line_start_index = sorted(line_start_index)
for i in range(len(line_start_index)):
if start > int(line_start_index[i]) and end < (int(line_start_index[i]) + len(line_dict[line_start_index[i]])):
sentence['sentence']=line_dict[line_start_index[i]]
start -= int(line_start_index[i])
end -= int(line_start_index[i])
line_num = i
break
'''
elif start > line_start_index[i] and end > (line_start_index[i] + len(line_dict[line_start_index[i]])):
'''
data.append(start)
data.append(end)
out[line_num]['annotation'].append(data)
res['processedData'] = out
return res
def judge_line(txt_file_path):
count = 0
line_dict = dict()
with open(txt_file_path, 'r') as txt_file:
for line in txt_file.readlines():
line_dict[count] = line
count += len(line)
return line_dict
txt_file_path = '/Users/robin/research/brat/data/Local/test.txt'
out = []
with open(txt_file_path, 'r') as txt_file:
for line in txt_file.readlines():
sentence = dict()
sentence['sentence'] = line
sentence['annotation'] = []
out.append(sentence)
res = _prehandle_data(out, '/Users/robin/research/brat/data/Local/test.txt', '/Users/robin/research/brat/data/Local/test.ann','/Users/robin/research/brat/data/Local/test_func.ann')
print(res)