-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_orcid_works.py
205 lines (153 loc) · 8.36 KB
/
load_orcid_works.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
import os
import sys
import json
import tarfile
import gzip
import lib.xml_parse
import multiprocessing
SOURCE_FILES = "data/input/orcid_works_2022.lsv"
OUT_DIR_RAW = "data/input/ORCID_works_raw/"
OUT_DIR = "data/input/ORCID_works"
# the configuration for XML parsing using lib.xml_parse.XMLHandler
SEARCH_FOR = {
"/work:work/work:title/common:title": {"elementName": "title"},
"/work:work/work:title/common:subtitle": {"elementName": "subTitle"},
"/work:work/work:journal-title": {"elementName": "journal_title"},
"/work:work/work:short-description": {"elementName": "abstract"},
"/work:work/work:citation/work:citation-type": {"elementName": None}, # read value, but not return
"/work:work/work:citation/work:citation-value": {"elementName": "bibtex"},
"/work:work/work:type": {"elementName": "type"},
"publicationDate": {"elementName": "date"}, # aggregated publication year [, month, [,day]]
"/work:work/common:publication-date/common:year": {"elementName": None},
"/work:work/common:publication-date/common:month": {"elementName": None},
"/work:work/common:publication-date/common:day": {"elementName": None},
"/work:work/common:url": {"elementName": "url"},
"/work:work/common:external-ids/common:external-id/common:external-id-type": {"elementName": None},
"/work:work/common:external-ids/common:external-id/common:external-id-value": {"elementName": None},
"/work:work/common:external-ids/common:external-id/common:external-id-normalized": {"elementName": None},
"doi": {"elementName": "doi"}, # based on external-id
"issn": {"elementName": "issn"}, # based on external-id
"isbn": {"elementName": "isbn"}, # based on external-id
"/work:work/work:contributors/work:contributor/work:credit-name": {
"elementName": "otherNames",
"groupName": "authors",
"groupPath": "/work:work/work:contributors/work:contributor"
},
"/work:work/work:contributors/work:contributor/work:contributor-attributes/work:contributor-role": {
"elementName": None,
"groupName": "authors",
"groupPath": "/work:work/work:contributors/work:contributor"
},
"/work:work/work:contributors/work:contributor/common:contributor-orcid/common:path": {
"elementName": "id",
"groupName": "authors",
"groupPath": "/work:work/work:contributors/work:contributor"
}
}
class PublicationHandler(lib.xml_parse.XMLHandler):
"""This sub class extends the parsing process to handle specific data constellations"""
AUTHOR_ROLES = ["author", "http://credit.niso.org/contributor-roles/writing-original-draft/"]
def endElement(self, tag):
# take available parts of publication date
if "/work:work/common:publication-date" == self.curPath and \
"/work:work/common:publication-date/common:year" in self.data:
date = self.data["/work:work/common:publication-date/common:year"]
del self.data["/work:work/common:publication-date/common:year"]
if "/work:work/common:publication-date/common:month" in self.data:
date+= "-" + self.data["/work:work/common:publication-date/common:month"]
del self.data["/work:work/common:publication-date/common:month"]
if "/work:work/common:publication-date/common:day" in self.data:
date+= "-" + self.data["/work:work/common:publication-date/common:day"]
del self.data["/work:work/common:publication-date/common:day"]
self.data["publicationDate"] = date
# take only citaitons of type 'bibtex' or without type
if "/work:work/work:citation" == self.curPath and \
"/work:work/work:citation/work:citation-type" in self.data and \
"/work:work/work:citation/work:citation-value" in self.data:
if "bibtex" != self.data["/work:work/work:citation/work:citation-type"]:
del self.data["/work:work/work:citation/work:citation-value"]
del self.data["/work:work/work:citation/work:citation-type"]
# authors
if "/work:work/work:contributors/work:contributor" == self.curPath:
# filter author tags with missing name
if not "/work:work/work:contributors/work:contributor/work:credit-name" in self.data:
if "/work:work/work:contributors/work:contributor/common:contributor-orcid/common:path" in self.data:
del self.data["/work:work/work:contributors/work:contributor/common:contributor-orcid/common:path"]
if "/work:work/work:contributors/work:contributor/work:contributor-attributes/work:contributor-role" in self.data:
del self.data["/work:work/work:contributors/work:contributor/work:contributor-attributes/work:contributor-role"]
# take only contributors of role 'author' or without role
if "/work:work/work:contributors/work:contributor/work:contributor-attributes/work:contributor-role" in self.data:
if not self.data["/work:work/work:contributors/work:contributor/work:contributor-attributes/work:contributor-role"] in PublicationHandler.AUTHOR_ROLES:
del self.data["/work:work/work:contributors/work:contributor/work:credit-name"]
del self.data["/work:work/work:contributors/work:contributor/work:contributor-attributes/work:contributor-role"]
if "/work:work/work:contributors/work:contributor/common:contributor-orcid/common:path" in self.data:
del self.data["/work:work/work:contributors/work:contributor/common:contributor-orcid/common:path"]
# doi & issn & isbn
if "/work:work/common:external-ids/common:external-id" == self.curPath and \
"/work:work/common:external-ids/common:external-id/common:external-id-value" in self.data and \
"/work:work/common:external-ids/common:external-id/common:external-id-type" in self.data:
id_value = self.data["/work:work/common:external-ids/common:external-id/common:external-id-value"]
del self.data["/work:work/common:external-ids/common:external-id/common:external-id-value"]
if "/work:work/common:external-ids/common:external-id/common:external-id-normalized" in self.data:
id_value = self.data["/work:work/common:external-ids/common:external-id/common:external-id-normalized"]
del self.data["/work:work/common:external-ids/common:external-id/common:external-id-normalized"]
for id_type in ["doi", "issn", "isbn"]:
if id_type == self.data["/work:work/common:external-ids/common:external-id/common:external-id-type"]:
self.data[id_type] = id_value
del self.data["/work:work/common:external-ids/common:external-id/common:external-id-type"]
super().endElement(tag)
def process_source(url):
fileName_local = url.strip().split("/")[-1]
gzFileName = fileName_local + ".gz"
count = 0
print("\t process source: " + gzFileName)
parser = lib.xml_parse.Parser()
# download data
os.system("mkdir -p " + OUT_DIR)
os.system("mkdir -p " + OUT_DIR_RAW)
os.system("wget -O " + OUT_DIR_RAW + gzFileName + " " + url)
# extract and save data
with gzip.open(OUT_DIR + "/works_"+fileName_local+".jsonl.gz", 'wt', encoding='utf-8') as outWorks:
with gzip.open(OUT_DIR + "/authors_"+fileName_local+".jsonl.gz", 'wt', encoding='utf-8') as outAuthors:
with tarfile.open(OUT_DIR_RAW + gzFileName, 'r:gz') as tar:
for member in tar:
if "_works_" in member.name:
handler = PublicationHandler(SEARCH_FOR)
publ = parser.parse(handler, tar.extractfile(member))
fileName = member.name.split("/")[-1]
publ["orcid_publication_id"] = fileName.split(".")[0].split("_works_")[-1]
publ["orcid_id"] = fileName.split("_")[0]
if "authors" in publ:
authors = publ["authors"]
i = 0
for author in authors:
if not "id" in author:
author["id"] = publ["orcid_publication_id"] + "_" + str(i)
json.dump(author, outAuthors)
outAuthors.write('\n')
i += 1
publ["authors"] = [author["id"] for author in authors]
json.dump(publ, outWorks)
outWorks.write('\n')
count += 1
print("\t .. source " + gzFileName + " done: " + str(count) + " records")
# clean up temp file
os.system("rm " + OUT_DIR_RAW + gzFileName)
return count
def run():
print("start transforming publications ..")
# load source addresses
count = 0
queue = []
with open(SOURCE_FILES, 'r', newline='') as inFile:
for url in inFile:
queue.append(url)
# download and extract data in multiple threads
with multiprocessing.Pool(len(queue)) as pool:
results = pool.map(process_source, queue)
result_sum = 0
for num in results:
result_sum += num
print(str(result_sum) + " publications transformed")
if "__main__" == __name__:
run()