-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsoftware-mentions-extractor.py
executable file
·284 lines (233 loc) · 9.05 KB
/
software-mentions-extractor.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
#!/usr/bin/env python3
'''Extracts mentions from papers
Usage:
./software-mentions-extractor [options] DIR DIR DIR...
Output:
A list of files with then names corresponding to the directories
on the command line, with / changed to _ and a prefix f_. Each file
is the result of extraction of all files in the current directory.
Each file is a tab separated list with the following fields:
- Paper license (comm or non_comm);
- Paper location in the directory;
- Paper pmcid or empty;
- Paper pmid or empty;
- Paper doi or empty;
- Paper publication date or empty;
- Mention source: paper_title, paper_abstract, fig_caption,
tab_caption, section name;
- Mention source number (paragraph number for body text, caption
number for captions) or empty;
- Source text (sentence).
- Software name;
- Software version or empty.
Author:
Boris Veytsman
'''
import pubmed_parser as pp
import argparse
import sys
import numpy as np
import torch
from transformers import BertForTokenClassification, BertTokenizerFast
import re
from os.path import exists
import glob
import os
tag_values = ['I-version', 'O', 'I-software', 'B-version', 'B-software', 'PAD']
argparser = argparse.ArgumentParser(
description=
'Extract software mentions from papers in NXML format.'
)
argparser.add_argument('directories', metavar='DIR',
nargs='+', help='list ' +
'of directories with NXML files, ' +
'one per line')
argparser.add_argument('-d', '--debug', action='store_true',
help='Debug mode on')
argparser.add_argument("-l", "--license", default="",
help="License, either comm or non comm. " +
"If empty (the default), determine from the " +
"directory names")
argparser.add_argument("-m", "--model",
default=
"../../software-mention-extraction/models/scibert_software_sent",
help="The location of the trained model, " +
"by default %(default)s")
argparser.add_argument("-o", "--outputdir", default="test/output",
help="Output directory, by default " +
"%(default)s.")
def process_directory (directory, args):
'''Process all files in one directory from the arguments.
Arguments:
directory -- directory to process
args -- list of command line arguments
Returns:
None.
We open output file and write down the results
'''
directory = directory.rstrip()
output_file_base = "f_" + re.sub("/", "_", directory)
if (exists(args.outputdir + "/" + output_file_base + ".tsv") or
exists(args.outputdir + "/" + output_file_base + ".tsv.tmp")):
return
output = open(args.outputdir + "/" + output_file_base + ".tsv.tmp",
"w")
headers = ('license', 'location', 'pmcid', 'pmid', 'doi',
'pubdate', 'source', 'number', 'text', 'software',
'version')
print('\t'.join(headers), file=output)
license = args.license
if (len(license)==0):
if(re.search("/non_comm/", directory, flags=re.IGNORECASE)):
license='non_comm'
else:
license='comm'
for file in glob.glob(directory + "/*.nxml"):
process_file(file, license, output)
output.close()
os.rename(args.outputdir + "/" + output_file_base + ".tsv.tmp",
args.outputdir + "/" + output_file_base + ".tsv")
def process_file (file, license, output):
'''Process one file and dump the results (if any) to output handle.
Arguments:
file -- file location (XML)
license -- the current license
output -- a handle
'''
if args.debug:
print("Processing " + file, file=sys.stderr)
try:
tree = pp.utils.read_xml(file)
except:
return
try:
metadata = pp.parse_pubmed_xml(tree=tree)
except:
return
pmcid = metadata.get('pmc', '')
pmid = metadata.get('pmid', '')
doi = metadata.get('doi', '')
pubdate = metadata.get('publication_year', '')
title = metadata.get('full_title', '')
abstract = metadata.get('abstract', '')
pmcid = re.sub("[ \t\n\r]+", " ", pmcid)
pmid = re.sub("[ \t\n\r]+", " ", pmid)
doi = re.sub("[ \t\n\r]+", " ", doi)
pubdate = re.sub("[ \t\n\r]+", " ", pubdate)
title = re.sub("[ \t\n\r]+", " ", title)
abstract = re.sub("[ \t\n\r]+", " ", abstract)
id = [license, file, pmcid, pmid, doi, pubdate]
process_object(id, 'paper_title', 0, title, output)
process_object(id, 'paper_abstract', 0, abstract, output)
try:
figs = pp.parse_pubmed_caption(tree=tree)
except:
figs = None
if figs != None:
for i, fig in enumerate(figs):
process_object(id, 'fig_caption', i,
fig.get('fig_caption', ''), output)
try:
tables = pp.parse_pubmed_table(tree=tree,
return_xml=False)
except:
tables = None
if tables != None:
for i, table in enumerate(tables):
process_object(id, 'tab_caption', i,
table.get('caption', ''),
output)
try:
paras = pp.parse_pubmed_paragraph(tree=tree,
all_paragraph=True)
except:
paras = None
if paras != None:
for i, para in enumerate(paras):
section = para.get('section', '')
section = re.sub("[ \t\n\r]+", " ", section)
text = para.get('text', '')
process_object(id, section, i,
text, output)
def process_object(id, source, number, text, output):
'''Extract software mentions from paragraph, title, abstract or caption.
Arguments:
id -- a list of ids for the paper
source -- a string with the source (abstract, paragraph...)
number -- number of the source in the sequence of sources
text -- the text of the source
output -- handle to dump the result
'''
text = re.sub("[ \t\n\r]+", " ", text)
sentences = text.split(". ")
for sentence in sentences:
sentence = sentence[:512]
ner_result = get_soft_ver_labels(sentence)
for soft, version in collapse(ner_result):
print('\t'.join(id +
[source, str(number), sentence,
soft, version]),
file=output)
def get_soft_ver_labels(sentence):
'''Convert a sentence into a list of tuples (token, tag).
Arguments:
sentence -- a sentence to tag
Returns:
A list of tokens and tags
'''
tokenized_sentence = tokenizer.encode(sentence)
input_ids = torch.tensor([tokenized_sentence])#.cuda()
with torch.no_grad():
output = model(input_ids)
label_indices = np.argmax(output[0].to('cpu').numpy(), axis=2)
tokens = tokenizer.convert_ids_to_tokens(input_ids.to('cpu').numpy()[0])
new_tokens, new_labels = [], []
for token, label_idx in zip(tokens, label_indices[0]):
if token.startswith("##"):
new_tokens[-1] = new_tokens[-1] + token[2:]
else:
new_labels.append(tag_values[label_idx])
new_tokens.append(token)
return list(zip(new_tokens[1:-1],
new_labels[1:-1]))
def collapse (ner_result):
'''Convert tagged sequence of tokens into list [(software, version),...]
Arguments:
ner_result -- a list of tuples (token, tag)
Returns:
a list of tuples (soft, ver)
'''
collapsed_list = []
current_soft = ""
current_version = ""
for token, tag in ner_result:
if tag == "O":
if current_soft != "":
collapsed_list.append((current_soft, current_version))
current_soft=""
current_version=""
if tag == "B-software" or tag == "I-software":
if current_version != "":
collapsed_list.append((current_soft, current_version))
current_soft = str(token)
curent_version=""
else:
if current_soft == "":
current_soft = str(token)
else:
current_soft = current_soft + " " + str(token)
if tag == "I-version" or tag == "B-version":
if current_version == "":
current_version = str(token)
else:
current_version = current_version + " " + str(token)
if current_soft != "":
collapsed_list.append((current_soft, current_version))
return collapsed_list
if __name__ == "__main__":
args = argparser.parse_args()
trained_model = args.model
tokenizer = BertTokenizerFast.from_pretrained(trained_model, do_lower_case=False)
model = BertForTokenClassification.from_pretrained(trained_model)
for directory in args.directories:
process_directory(directory, args)