93 lines
3.7 KiB
Python
93 lines
3.7 KiB
Python
import json
|
|
import os
|
|
from pyspark.sql import SparkSession
|
|
from affro_cluster import *
|
|
import sys
|
|
|
|
folder_path = sys.argv[1]
|
|
hdfs_output_path = sys.argv[2]
|
|
|
|
# Initialize Spark session
|
|
spark = SparkSession.builder.appName("AffRo").getOrCreate()
|
|
|
|
def remove_duplicates(list_of_dicts):
|
|
# Use a set to store tuples of dictionary items to filter out duplicates
|
|
seen = set()
|
|
unique_list_of_dicts = []
|
|
|
|
for d in list_of_dicts:
|
|
# Convert the dictionary to a tuple of items
|
|
items = tuple(d.items())
|
|
if items not in seen:
|
|
seen.add(items)
|
|
unique_list_of_dicts.append(d)
|
|
|
|
return unique_list_of_dicts
|
|
|
|
def update_record(record):
|
|
id = record['id']
|
|
authors = []
|
|
try:
|
|
for author in record['authors']:
|
|
author_object = {}
|
|
if 'orcid.org/0' in author['fullName']:
|
|
author_object['Name'] = {'Full':author['fullName'].split(',')[1], 'First' : None, 'Last' : None}
|
|
author_object['ORCID'] = author['fullName'].split(',')[0][:36]
|
|
else:
|
|
author_object['Name'] = {'Full':author['fullName'], 'First' : None, 'Last' : None}
|
|
author_object['ORCID'] = None
|
|
author_object['Raw_affiliations'] = [affiliation['raw_affiliation_string'] for affiliation in author['affiliations']]
|
|
all_affs_with_ror = []
|
|
have_ror = False
|
|
for affiliation in author['affiliations']:
|
|
# author_object['Raw_affiliations'] = [x for x in affiliation['raw_affiliation_string']]
|
|
if 'ORCID: 0' in affiliation['raw_affiliation_string']:
|
|
x = affiliation['raw_affiliation_string']
|
|
author_object['ORCID'] = 'https://orcid.org/'+x.split('ORCID: ')[1]
|
|
elif 'ORCID 0' in affiliation['raw_affiliation_string']:
|
|
x = affiliation['raw_affiliation_string']
|
|
author_object['ORCID'] = 'https://orcid.org/'+x.split('ORCID ')[1]
|
|
if 'ror.org' in affiliation['raw_affiliation_string']:
|
|
have_ror = True
|
|
all_affs_with_ror.append({
|
|
'Origin': 'data',
|
|
'RORid': affiliation['raw_affiliation_string'][0:25],
|
|
'Confidence': None
|
|
})
|
|
|
|
|
|
else:
|
|
if len(affro(affiliation['raw_affiliation_string']))>0:
|
|
author_object['Organization_PIDs'] = affro(affiliation['raw_affiliation_string'])
|
|
author_object['Organization_PIDs'] = remove_duplicates([json.loads(x) for x in author_object['Organization_PIDs']])
|
|
|
|
else:
|
|
author_object['Organization_PIDs'] = []
|
|
|
|
if have_ror == True:
|
|
author_object['Organization_PIDs'] = all_affs_with_ror
|
|
order = ["Name", "Raw_affiliations", "Organization_PIDs", "ORCID"]
|
|
|
|
reordered_data = {k: author_object[k] for k in order}
|
|
|
|
authors.append(reordered_data)
|
|
|
|
|
|
organizations = remove_duplicates([x for author in authors for x in author['Organization_PIDs']])
|
|
|
|
updt = {'ID' : id, 'Authors' : authors, 'Organizations' : organizations}
|
|
return updt
|
|
except Exception as e:
|
|
print(f"Error processing record with id {record.get('id')}: {str(e)}")
|
|
return None
|
|
|
|
df = spark.read.json(folder_path)
|
|
|
|
# Apply the update_record function
|
|
updated_rdd = df.rdd.map(lambda row: update_record(row.asDict()))
|
|
|
|
# Convert updated RDD to JSON strings
|
|
json_rdd = updated_rdd.map(lambda record: json.dumps(record))
|
|
|
|
json_rdd.saveAsTextFile(hdfs_output_path)
|