-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_dataset.py
476 lines (388 loc) · 21.5 KB
/
create_dataset.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
import pandas as pd
import csv
import requests
import tqdm
import json
import os
from elsapy.elsclient import ElsClient
from elsapy.elsprofile import ElsAuthor, ElsAffil
from elsapy.elsdoc import FullDoc, AbsDoc
from elsapy.elssearch import ElsSearch
import pycountry
import re
from urllib.parse import urlparse, parse_qs
def getElsevierAPIKey():
with open("apiKey.json") as f:
data = json.load(f)
return data["apiKey"]
def load_data_csv(file_path):
# Create a new Dataframe
data = []
with open(file_path, newline="") as csvfile:
reader = csv.reader(csvfile)
for row in reader:
# Parse JSON strings
json_data_1 = json.loads(row[0])
json_data_2 = json.loads(row[1])
json_data_3 = json.loads(row[2])
# Append the data to the list
data.append([json_data_1, json_data_2, json_data_3])
# Filter the data in which json_data_3 == 1
data = [d for d in data if d[2] == 1]
return data
def convert_to_dataframe(data):
# Create a new Dataframe
df = pd.DataFrame(data, columns=["Published", "Preprint"])
return df
def fetch_crossref_data(doi):
url = f"https://api.crossref.org/works/{doi}"
response = requests.get(url)
if response.status_code == 200:
return response.json()
else:
print(response)
print(f"Error fetching data for DOI: {doi}")
return None
def basic_dataset_creation(df):
data_frame = pd.DataFrame(columns=["SubmissionID", "SubmissionYear", "SubmissionTitle", "SubmissionAbstract", "firstName", "middleName", "lastName", "isFirstAuthor", "city", "country", "countryCode", "institution", "doi", "authorScopusId", "authorPublicationHistory"])
for index, row in tqdm.tqdm(df.iterrows(), total=df.shape[0]):
preprint = row["Preprint"]
published = row["Published"]
submissionID = preprint.get("DOI", None)
date = preprint.get("issued", None)
if date != None:
submissionYear = date["date-parts"][0][0]
else:
submissionYear = None
submissionTitle = preprint.get("title", None)
submissionTitle = submissionTitle[0]
submissionAbstract = None
city = None
country = None
countryCode = None
institution = None
doi = published.get("DOI", None)
authors = preprint.get("author", [])
for author in authors:
firstName = author.get("given", None)
middleName = None
lastName = author.get("family", None)
isFirstAuthor = None
authorScopusId = None
authorPublicationHistory = None
if firstName == None or lastName == None:
break
new_row = pd.DataFrame([[submissionID, submissionYear, submissionTitle, submissionAbstract, firstName, middleName, lastName, isFirstAuthor, city, country, countryCode, institution, doi, authorScopusId, authorPublicationHistory]], columns=["SubmissionID", "SubmissionYear", "SubmissionTitle", "SubmissionAbstract", "firstName", "middleName", "lastName", "isFirstAuthor", "city", "country", "countryCode", "institution", "doi", "authorScopusId", "authorPublicationHistory"])
data_frame = pd.concat([data_frame, new_row], ignore_index=True)
return data_frame
def clean_abstract(abstract):
# Remove <jats:title> and </jats:title> tags and any content between them
abstract = re.sub(r'<jats:title>[^>]+</jats:title>', '', abstract)
# Remove <jats:p> and </jats:p> tags
abstract = re.sub(r'<jats:p>|</jats:p>', '', abstract)
# Remove any other <jats:*> tags
abstract = re.sub(r'<jats:[^>]+>', '', abstract)
# Remove </jats:sec> tags
abstract = re.sub(r'</jats:sec>', '', abstract)
# Remove all other HTML tags
abstract = re.sub(r'<[^>]+>', '', abstract)
return abstract
def fetch_crossref_data(doi):
url = f"https://api.crossref.org/works/{doi}"
#print(url)
response = requests.get(url)
if response.status_code == 200:
data = response.json()
return data
else:
print(f"Error fetching data for DOI: {doi}")
return None
def extend_dataset_with_crossref(submissionIds):
listOfIdAbstract = {}
print(submissionIds[:10])
print(len(submissionIds))
i = 0
for id in tqdm.tqdm(submissionIds):
data = fetch_crossref_data(id)
if data:
abstract = data.get("message", {}).get("abstract", None)
if abstract:
abstract = clean_abstract(abstract)
listOfIdAbstract[id] = abstract
else:
listOfIdAbstract[id] = None
return listOfIdAbstract
def fetch_openalex_data(doi):
url = f"https://api.openalex.org/works/https://doi.org/{doi}"
response = requests.get(url)
if response.status_code == 200:
#print(response.json())
return response.json()
else:
print(response)
print(f"Error fetching data for DOI: {doi}")
return None
def get_country_name(country_code):
try:
country = pycountry.countries.get(alpha_2=country_code)
return country.name
except:
return None
def extend_dataset_with_openAlex(df):
openalex_data = []
missedOut = 0
inside = 0
out = 0
for index, row in tqdm.tqdm(df.iterrows(), total=df.shape[0]):
doi = row["PreprintID"]
pubdoi = row["PublicationID"]
match = row["Match"]
temp = doi
doi = pubdoi
pubdoi = temp
data = fetch_openalex_data(doi)
if data:
language = data.get("language", None)
if language == "en":
break_outer_loop = False
inside_data = []
submissionID = data.get("doi", None)
submissionID = submissionID[15:]
submissionYear = data.get("publication_year")
submissionTitle = data.get("title", None)
authors = data.get("authorships", [])
if (len(authors) != 0):
for author in authors:
notFound = True
fullName = author.get("author", {}).get("display_name", None)
if (len(fullName.split()) == 2):
firstName, lastName = fullName.split()
middleName = None
elif (len(fullName.split()) == 3):
firstName, middleName, lastName = fullName.split()
else:
firstName = fullName
middleName = None
lastName = None
isFirstAuthor = author.get("author_position", None)
if isFirstAuthor == "first":
isFirstAuthor = 1
else:
isFirstAuthor = 0
if len(author.get("institutions", [])) != 0:
countryCode = author.get("institutions", [])[0].get("country_code", None)
country = get_country_name(countryCode) if countryCode else None
institution = author.get("institutions", [])[0].get("display_name", None)
else:
#print("No institutions")
break_outer_loop = True
break
doi = pubdoi
if None in [submissionID, submissionYear, submissionTitle, firstName, lastName, isFirstAuthor, country, countryCode, institution, doi]:
#print(f"Breaking at index {index} due to missing values")
#print(f"submissionID: {submissionID}, submissionYear: {submissionYear}, submissionTitle: {submissionTitle}, firstName: {firstName}, lastName: {lastName}, isFirstAuthor: {isFirstAuthor}, country: {country}, countryCode: {countryCode}, institution: {institution}, doi: {doi}")
break_outer_loop = True
break
authorId = author.get("author", {}).get("id", None)
# Extract id from https://openalex.org/A5100341522
if authorId:
authorId = authorId[21:]
url = f"https://api.openalex.org/authors/{authorId}"
response = requests.get(url)
if response.status_code == 200:
authorData = response.json()
authorScopusId = authorData.get("ids", {}).get("scopus", None)
authorOrchidId = authorData.get("ids", {}).get("orcid", None)
if authorScopusId != None or authorOrchidId != None:
# Extract id from http://www.scopus.com/inward/authorDetails.url?authorID=36455008000&partnerID=MN8TOARS
if authorScopusId != None:
url = authorScopusId
parsed_url = urlparse(url)
# Extract the query parameters
query_params = parse_qs(parsed_url.query)
# Get the authorID
authorIDReal = query_params.get("authorID", [None])[0]
authorIDReal = re.findall(r'\d+', url)
authorIDReal = authorIDReal[0]
scopusURL = "https://api.elsevier.com/content/search/scopus?query=AU-ID(" + authorIDReal + ")&view=COMPLETE"
headers = {
"X-ELS-APIKey": getElsevierAPIKey(),
"Accept": "application/json"
}
response = requests.get(scopusURL, headers=headers)
if response.status_code == 200:
response = response.json()
authorPublicationHistory = []
log = response.get("search-results", {}).get("entry", None)
if log == None:
authorPublicationHistory = None
out += 1
break
for paper in log:
pTitle = paper.get("dc:title", None)
pAbstract = paper.get("dc:description", None)
pDOI = paper.get("prism:doi", None)
pCitationCount = paper.get("citedby-count", None)
pScopusID = paper.get("dc:identifier", None)
pAuthors = paper.get("author", None)
pA = []
if pAuthors:
for author in pAuthors:
f = author.get("given-name", None)
l = author.get("surname", None)
if f != None and l != None:
a = f + " " + l
pA.append(a)
authorPublicationHistory.append({"title": pTitle, "abstract": pAbstract, "doi": pDOI, "citationCount": pCitationCount, "scopusID": pScopusID, "authors": pA})
else:
print("Error fetching data for DOI: ", doi)
break
else:
# url = https://orcid.org/0000-0002-7864-2578'
authorIDReal = authorOrchidId[18:]
scopusURL = "https://api.elsevier.com/content/search/scopus?query=orcid(" + authorIDReal + ")&view=COMPLETE"
headers = {
"X-ELS-APIKey": getElsevierAPIKey(),
"Accept": "application/json"
}
response = requests.get(scopusURL, headers=headers)
if response.status_code == 200:
response = response.json()
authorPublicationHistory = []
log = response.get("search-results", {}).get("entry", None)
if log == None:
authorPublicationHistory = None
out += 1
break
for paper in log:
pTitle = paper.get("dc:title", None)
pAbstract = paper.get("dc:description", None)
pDOI = paper.get("prism:doi", None)
pCitationCount = paper.get("citedby-count", None)
pScopusID = paper.get("dc:identifier", None)
pAuthors = paper.get("author", None)
pA = []
if pAuthors:
for author in pAuthors:
f = author.get("given-name", None)
l = author.get("surname", None)
if f != None and l != None:
a = f + " " + l
pA.append(a)
authorPublicationHistory.append({"title": pTitle, "abstract": pAbstract, "doi": pDOI, "citationCount": pCitationCount, "scopusID": pScopusID, "authors": pA})
else:
print("Error fetching data for DOI: ", doi)
break
else:
#print("No Scopus ID")
#print(authorOrchidId)
#print(authorData.get("ids", {}))
authorScopusId = None
break
else:
notFound = False
break
if notFound:
new_row = {
"SubmissionID": submissionID,
"SubmissionYear": submissionYear,
"SubmissionTitle": submissionTitle,
"SubmissionAbstract": None,
"firstName": firstName,
"middleName": middleName,
"lastName": lastName,
"isFirstAuthor": isFirstAuthor,
"city": None,
"country": country,
"countryCode": countryCode,
"institution": institution,
"doi": doi,
"authorID": authorIDReal,
"authorPublicationHistory": authorPublicationHistory,
"match": match
}
inside_data.append(new_row)
if break_outer_loop == False:
for row in inside_data:
openalex_data.append(row)
else:
print("Current Size:", len(openalex_data))
missedOut += 1
print("Missed Ones: ", missedOut)
openalex_df = pd.DataFrame(openalex_data)
return openalex_df
def fetch_elsevier_data(DOI):
API_KEY = getElsevierAPIKey()
client = ElsClient(API_KEY)
print(API_KEY)
my_auth = ElsAuthor(
uri = f"https://api.elsevier.com/content/author/orcid/{DOI}")
# Read author data, then write to disk
if my_auth.read(client):
print ("my_auth.full_name: ", my_auth)
else:
print ("Read author failed.")
def extend_dataset_with_elsevier(df):
print("Entering Here")
for index, row in tqdm.tqdm(df.iterrows(), total=df.shape[0]):
doi = row["SubmissionID"]
firstName = row["firstName"]
lastName = row["lastName"]
data = fetch_elsevier_data(doi)
def get_id_of_preprint_publication(data):
data_frame = pd.DataFrame(columns=["PreprintID", "PublicationID", "Match"])
data = pd.DataFrame(data, columns=["Preprint", "Published", "Match"])
for index, row in tqdm.tqdm(data.iterrows(), total=data.shape[0]):
preprintID = row["Preprint"]
publicationID = row["Published"]
match = row["Match"]
preID = preprintID.get("DOI", None)
pubID = publicationID.get("DOI", None)
new_row = pd.DataFrame([[preID, pubID, match]], columns=["PreprintID", "PublicationID", "Match"])
data_frame = pd.concat([data_frame, new_row], ignore_index=True)
return data_frame
def main():
if not os.path.exists("data_with_ids.csv"):
data = load_data_csv("data-training.csv")
df = get_id_of_preprint_publication(data)
# Save the df to a csv file
df.to_csv("data_with_ids.csv", index=False)
else:
df = pd.read_csv("data_with_ids.csv")
if not os.path.exists("crossref_data.csv"):
extended_dataset = extend_dataset_with_openAlex(df)
# Save the df to a csv file
extended_dataset.to_csv("openalex_data.csv", index=False)
else:
extended_dataset = pd.read_csv("openalex_data.csv")
# Ensure the SubmissionAbstract column is of type object
extended_dataset["SubmissionAbstract"] = extended_dataset["SubmissionAbstract"].astype(object)
print(len(extended_dataset))
if not os.path.exists("extended_dataset_v2.csv"):
# Get all the submissionId from the extended dataset
submissionIds = extended_dataset["SubmissionID"].tolist()
# Remove duplicates
print(len(submissionIds))
submissionIds = list(set(submissionIds))
print(len(submissionIds))
print(len(extended_dataset))
listOfIdAbstract = extend_dataset_with_crossref(submissionIds)
for id, abstract in tqdm.tqdm(listOfIdAbstract.items()):
print(type(abstract))
print(type(extended_dataset.loc[extended_dataset["SubmissionID"] == id, "SubmissionAbstract"]))
if abstract is not None:
extended_dataset.loc[extended_dataset["SubmissionID"] == id, "SubmissionAbstract"] = abstract
else:
extended_dataset.drop(extended_dataset[extended_dataset["SubmissionID"] == id].index, inplace=True)
extended_dataset.to_csv("extended_dataset_v2.csv", index=False)
else:
extended_dataset = pd.read_csv("extended_dataset_v2.csv")
# Clean all the titles in the dataset
for index, row in tqdm.tqdm(extended_dataset.iterrows(), total=extended_dataset.shape[0]):
title = row["SubmissionTitle"]
cleanedTitle = clean_abstract(title)
extended_dataset.loc[index, "SubmissionTitle"] = cleanedTitle
extended_dataset.to_csv("extended_dataset_v3.csv", index=False)
# Printt the first row all the columns
print(extended_dataset.iloc[0])
main()