-
Notifications
You must be signed in to change notification settings - Fork 1
/
addDaraMetadataToOntology.py
74 lines (63 loc) · 2.38 KB
/
addDaraMetadataToOntology.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
import json
import urllib2
import sys
def getJson(filename):
with open(filename, "r") as f:
return json.load(f, "utf-8")
def getEntities(data):
return data.get("entity")
def getLinks(data):
return data.get("entityLink")
def getDatasetIds(entities):
datasetIds = []
for entityId in entities.keys():
ids = entities.get(entityId).get("identifiers")
if (ids):
datasetIds.extend(ids)
return set(datasetIds)
def getMetadata(datasetIds, fields):
metadataMap = {}
for datasetId in datasetIds:
metadata = queryDara(datasetId)
metadataMap[datasetId] = getDataForFields(metadata, fields)
return metadataMap
def getDataForFields(metadata, fields):
relevantData = {}
completeData = metadata.get("response")
for field in fields.items():
content = removeEmpty(completeData.get("docs")[0].get(field[0]))
if content:
relevantData[field[1]] = content
return relevantData
def removeEmpty(list):
try:
return [entry for entry in list if entry != ""]
except TypeError:
return []
def queryDara(datasetId):
query = "http://www.da-ra.de/solr/dara/select/?q=doi:" + datasetId + "&wt=json"
sys.stderr.write(query+"\n")
return json.load(urllib2.urlopen(query))
def addMetadataToEntities(metadataMap, entities):
for entityId in entities:
entity = entities.get(entityId)
if entity.get("identifiers"):
for datasetId in entity.get("identifiers"):
metadata = metadataMap.get(datasetId)
if (metadata):
entity.update(metadata)
return entities
def outputEnrichedJson(entities, links):
enrichedData = {}
enrichedData["entityLink"] = links
enrichedData["entity"] = entities
return json.dumps(enrichedData, sort_keys=True, indent=4, ensure_ascii=False).encode('utf8')
if __name__=="__main__":
data = getJson(sys.argv[1])
links = getLinks(data)
entities = getEntities(data)
relevantFields = {"geographicCoverage":"spatial_en", "temporalCoverage":"numericInfo", "currentVersion":"versionInfo"}
enrichedEntities = addMetadataToEntities(getMetadata(getDatasetIds(entities), relevantFields), entities)
with open(sys.argv[2], "w") as f:
f.write(outputEnrichedJson(enrichedEntities, links))
print "wrote " + sys.argv[2]