-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.py
193 lines (151 loc) · 6.9 KB
/
reader.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 19/06/2019
author: fenia
"""
from collections import OrderedDict
from recordtype import recordtype
import numpy as np
EntityInfo = recordtype('EntityInfo', 'id type mstart mend sentNo')
PairInfo = recordtype('PairInfo', 'type direction cross')
def chunks(l, n):
"""
Successive n-sized chunks from l.
"""
res = []
for i in range(0, len(l), n):
assert len(l[i:i + n]) == n
res += [l[i:i + n]]
return res
def overlap_chunk(chunk=1, lst=None):
if len(lst) <= chunk:
return [lst]
else:
return [lst[i:i + chunk] for i in range(0, len(lst)-chunk+1, 1)]
def read_subdocs(input_file, window, documents, entities, relations):
"""
Read documents as sub-documents of N consecutive sentences.
Args:
input_file: file with documents
"""
lost_pairs, total_pairs = 0, 0
lengths = []
sents = []
with open(input_file, 'r') as infile:
for line in infile:
line = line.rstrip().split('\t')
pmid = line[0]
text = line[1]
prs = chunks(line[2:], 17) # all the pairs in the document
sentences = text.split('|') # document sentences
all_sent_lengths = [len(s.split(' ')) for s in sentences] # document sentence lengths
sent_chunks = overlap_chunk(chunk=window, lst=sentences) # split document into sub-documents
unique_pairs = []
for num, sent in enumerate(sent_chunks):
sent_ids = list(np.arange(int(window)) + num)
sub_pmid = pmid+'__'+str(num)
if sub_pmid not in documents:
documents[sub_pmid] = [t.split(' ') for t in sent]
if sub_pmid not in entities:
entities[sub_pmid] = OrderedDict()
if sub_pmid not in relations:
relations[sub_pmid] = OrderedDict()
lengths += [max([len(d) for d in documents[sub_pmid]])]
sents += [len(sent)]
for p in prs:
# entities
for (ent, typ_, start, end, sn) in [(p[5], p[7], p[8], p[9], p[10]),
(p[11], p[13], p[14], p[15], p[16])]:
if ent not in entities[sub_pmid]:
s_ = list(map(int, sn.split(':'))) # doc-level ids
m_s_ = list(map(int, start.split(':')))
m_e_ = list(map(int, end.split(':')))
assert len(s_) == len(m_s_) == len(m_e_)
sent_no_new = []
mstart_new = []
mend_new = []
for n, (old_s, old_ms, old_me) in enumerate(zip(s_, m_s_, m_e_)):
if old_s in sent_ids:
sub_ = sum(all_sent_lengths[0:old_s])
assert sent[old_s-num] == sentences[old_s]
assert sent[old_s-num].split(' ')[(old_ms-sub_):(old_me-sub_)] == \
' '.join(sentences).split(' ')[old_ms:old_me]
sent_no_new += [old_s - num]
mstart_new += [old_ms - sub_]
mend_new += [old_me - sub_]
if sent_no_new and mstart_new and mend_new:
entities[sub_pmid][ent] = EntityInfo(ent, typ_,
':'.join(map(str, mstart_new)),
':'.join(map(str, mend_new)),
':'.join(map(str, sent_no_new)))
for p in prs:
# pairs
if (p[5] in entities[sub_pmid]) and (p[11] in entities[sub_pmid]):
if (p[5], p[11]) not in relations[sub_pmid]:
relations[sub_pmid][(p[5], p[11])] = PairInfo(p[0], p[1], p[2])
if (pmid, p[5], p[11]) not in unique_pairs:
unique_pairs += [(pmid, p[5], p[11])]
if len(prs) != len(unique_pairs):
for x in prs:
if (pmid, x[5], x[11]) not in unique_pairs:
if x[0] != '1:NR:2' and x[0] != 'not_include':
lost_pairs += 1
print('--> Lost pair {}, {}, {}: {} {}'.format(pmid, x[5], x[11], x[10], x[16]))
else:
if x[0] != '1:NR:2' and x[0] != 'not_include':
total_pairs += 1
todel = []
for pmid, d in relations.items():
if not relations[pmid]:
todel += [pmid]
for pmid in todel:
del documents[pmid]
del entities[pmid]
del relations[pmid]
print('LOST PAIRS: {}/{}'.format(lost_pairs, total_pairs))
assert len(entities) == len(documents) == len(relations)
return lengths, sents, documents, entities, relations
def read(input_file, documents, entities, relations):
"""
Read the full document at a time.
"""
lengths = []
sents = []
with open(input_file, 'r') as infile:
for line in infile:
line = line.rstrip().split('\t')
pmid = line[0]
text = line[1]
prs = chunks(line[2:], 17)
if pmid not in documents:
documents[pmid] = [t.split(' ') for t in text.split('|')]
if pmid not in entities:
entities[pmid] = OrderedDict()
if pmid not in relations:
relations[pmid] = OrderedDict()
# max intra-sentence length and max inter-sentence length
lengths += [max([len(s) for s in documents[pmid]] + [len(documents[pmid])])]
sents += [len(text.split('|'))]
allp = 0
for p in prs:
if (p[5], p[11]) not in relations[pmid]:
relations[pmid][(p[5], p[11])] = PairInfo(p[0], p[1], p[2])
allp += 1
else:
print('duplicates!')
# entities
if p[5] not in entities[pmid]:
entities[pmid][p[5]] = EntityInfo(p[5], p[7], p[8], p[9], p[10])
if p[11] not in entities[pmid]:
entities[pmid][p[11]] = EntityInfo(p[11], p[13], p[14], p[15], p[16])
assert len(relations[pmid]) == allp
todel = []
for pmid, d in relations.items():
if not relations[pmid]:
todel += [pmid]
for pmid in todel:
del documents[pmid]
del entities[pmid]
del relations[pmid]
return lengths, sents, documents, entities, relations