-
Notifications
You must be signed in to change notification settings - Fork 0
/
affiliations_ner.py
314 lines (265 loc) · 9.86 KB
/
affiliations_ner.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
import spacy
from nltk.metrics.distance import edit_distance
from string_sim import cos_sim_pair
from allennlp import pretrained
import re
import json
from pprint import pprint
import os
import argparse
# Ignore problematic files
IGNORE = {
'9724', # PDF not available on website
'9659', '9165', '8511', '9485', '9430', '9393', '9341', '9245',
'9166', '8858', '8687', '8574', '8518', # no whitespace
'9305', '9171', # Each page is an image
'8823', # NeurIPS template PDF (probably mistaken)
'8646', # Supplementary material (probably mistaken)
}
# For debugging: ignore everything except these files
KEEP = set()
UNICODE_CONVERSION = {
'\u0133': 'ij',
'\ufb00': 'ff',
'\ufb01': 'fi',
'\ufb02': 'fl',
'\ufb03': 'ffi',
'\ufb04': 'ffl',
}
REGEX_PATTERNS = {
'number': re.compile('[0-9][A-Za-z]'),
'all-caps': re.compile('[A-Z][A-Z]+'),
'camel-caps': re.compile('[A-Z]+[a-z]+[A-Z]+'),
'footer-affiliation': re.compile('^([^\x00-\x7F]|[0-9]|[0-9]:? )[A-Za-z]+'),
}
CONFERENCE_SIGN = '33rd Conference'
PER_EXCEPTIONS = ['Mila', 'Deepmind', 'Stanford']
LOC_EXCEPTIONS = ['Amazon']
def is_valid_line(line, index=None, invalid_indices=list()):
if len(line) < 1:
return False
elif len(invalid_indices) > 0 and index in invalid_indices:
return False
return True
def preprocess_header(lines):
new_lines = lines
# Separate the symbols
for i, line in enumerate(new_lines):
for char in set(line):
if UNICODE_CONVERSION.get(char) is not None:
new_lines[i] = new_lines[i].replace(char,
UNICODE_CONVERSION[char])
elif not (char.isalnum() or char.isascii()):
new_lines[i] = new_lines[i].replace(char, ' ' + char + ' ')
# Remove numbers at the start of words (footnotes)
for i, line in enumerate(new_lines):
removed = 0
for match in re.finditer(REGEX_PATTERNS['number'], line):
new_line = new_lines[i]
new_line = new_line[:match.start() - removed] + \
new_line[match.start() - removed + 1:]
removed += 1
new_lines[i] = new_line
# Strip lines in case only whitespace remains
new_lines = [line.strip() for line in new_lines]
return new_lines
def postprocess_entities(results, metadata):
entities = list()
def clean_entity(entity):
entity = ''.join(filter(lambda c: c.isalnum() or c.isascii(),
entity))
if entity[-1].isdigit():
entity = entity[:-1]
entity = entity.strip()
return entity
def maybe_add_entity(entity, tag=None):
if is_valid_entity(entity, metadata, tag=tag):
entity = clean_entity(entity)
entities.append(entity)
for result in results:
entity = str()
for word, tag in zip(result["words"], result["tags"]):
if tag == 'U-PER':
# Sometimes U-ORG is mistaken for U-PER e.g. DeepMind
maybe_add_entity(word, tag=tag)
elif tag == 'U-LOC':
maybe_add_entity(word, tag=tag)
elif tag == 'U-ORG':
maybe_add_entity(word)
elif tag == 'B-ORG':
entity += word
elif tag == 'I-ORG':
entity += ' ' + word
elif tag == 'L-ORG':
entity += ' ' + word
maybe_add_entity(entity)
entity = str()
return entities
def is_valid_entity(entity, metadata, threshold=0.25, tag=None):
clean_entity = ''.join(c for c in entity if not c.isdigit())
# Check if entity is an email address
if '@' in entity and '.' in entity:
return False
# Check if entity is actually a person (author from metadata)
for author in metadata['authors']:
dist = edit_distance(clean_entity, author)
if (dist / len(author)) < threshold:
# Close enough to be an author, therefore not affiliation
return False
# Ignore university schools and departments
# NOTE this is imperfect: there is 'college', 'laboratory', etc.
# if any([x in clean_entity.lower() for x in ['school', 'department']]):
# return False
if tag and tag == 'U-PER':
# Hard-coded exceptions
if any([ke in clean_entity for ke in PER_EXCEPTIONS]):
return True
# All-caps with at least 2 caps
elif re.match(REGEX_PATTERNS['all-caps'], clean_entity):
return True
# Camel-caps with at least 2 caps
elif re.match(REGEX_PATTERNS['camel-caps'], clean_entity):
return True
else:
return False
if tag and tag == 'U-LOC':
# Hard-coded exceptions
if any([ke in clean_entity for ke in LOC_EXCEPTIONS]):
return True
else:
return False
return True
def extract_affiliations(txt_path, metadata, predictor):
i = 0
lines = list()
header_lines = list()
post_abstract = False
sign_idx = -1
with open(txt_path, 'r') as f:
for i, line in enumerate(f.readlines()):
lines.append(line.strip())
if 'Abstract\n' in line:
post_abstract = True
elif CONFERENCE_SIGN in line:
sign_idx = i
break
elif not post_abstract:
line = line.strip()
if is_valid_line(line):
header_lines.append(line)
i += 1
footer_lines = list()
footer_affiliations_started = False
done = False
patience = 3
if sign_idx > 0:
for line in reversed(lines[:sign_idx]):
if not is_valid_line(line):
continue
match = re.match(REGEX_PATTERNS['footer-affiliation'], line)
if match and not footer_affiliations_started:
footer_affiliations_started = True
if footer_affiliations_started:
if match:
footer_lines.append(line)
else:
# Assume no line breaks between affiliations
# So we are done
done = True
if not (footer_affiliations_started or '@' in line):
patience -= 1
if done or patience <= 0: # not too far from page bottom
break
header_lines.extend(footer_lines)
# pprint(header_lines)
header_lines = preprocess_header(header_lines)
# print("")
# pprint(header_lines)
title_indices = find_title(header_lines, metadata['title'])
# print(title_indices)
results = []
for i, line in enumerate(header_lines):
if not is_valid_line(line, index=i, invalid_indices=title_indices):
continue
result = predictor.predict(sentence=line)
# for word, tag in zip(result["words"], result["tags"]):
# print(f"({tag}) {word}", end=" ")
# print("")
results.append(result)
affiliations = postprocess_entities(results, metadata)
pprint(affiliations)
return affiliations
def find_title(header_lines, title):
title_indices = list()
title_started = False
cum_sim = 0.0
cum_line = str()
title_ = title.strip().lower()
for i, line in enumerate(header_lines):
line_ = line.strip().lower()
sim = cos_sim_pair(title_, line_)
if not title_started and sim > 0.2:
title_started = True
cum_sim = sim
cum_line = line_
title_indices.append(i)
elif title_started:
combined = ' '.join([cum_line, line_])
combined_sim = cos_sim_pair(title_, combined)
if combined_sim > cum_sim:
# Similarity increased by appending this line
# Therefore, likely continuation of title
cum_sim = combined_sim
cum_line = combined
title_indices.append(i)
assert len(title_indices) != 0
return title_indices
def write_output(fname, output):
with open(fname, 'w') as f:
json.dump(output, f, indent=4)
def main(args):
fname = args.metadata
with open(fname, 'r') as f:
metadatas = json.load(f)
print('Building model...')
predictor = pretrained.named_entity_recognition_with_elmo_peters_2018()
output = list()
data_path = args.data
for i, metadata in enumerate(metadatas):
pdf_fname = os.path.split(metadata['pdf'])[1]
paper_id = pdf_fname.split('-')[0]
if len(KEEP) > 0 and paper_id not in KEEP:
continue
print(paper_id)
if paper_id in IGNORE:
print('Ignored')
continue
txt_fname = pdf_fname.replace('.pdf', '.txt')
txt_path = os.path.join(data_path, txt_fname)
affiliations = extract_affiliations(txt_path, metadata, predictor)
has_code = metadata['code']
data = {
'name': pdf_fname.replace('.pdf', ''),
'code': has_code,
'affiliations': affiliations
}
output.append(data)
if i % 100 == 0:
# Write periodically as a failsafe
write_output(args.output, output)
write_output(args.output, output)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('-m', '--metadata', type=str,
default='./out/neurips_2019/papers_metadata.json',
help='Path to metadata JSON file with PDF URLs')
parser.add_argument('-d', '--data', type=str,
default='./data/neurips_2019/txt',
help='Folder containing papers in text format')
parser.add_argument('-o', '--output', type=str,
default='./out/neurips_2019/affiliations.json',
help='File to write affiliation data')
return parser.parse_args()
if __name__ == '__main__':
args = parse_args()
main(args)