Originally posted at https://hub.psychoinformatics.de/msz/hochschulfaechersystematik
82 lines
3.1 KiB
Python
82 lines
3.1 KiB
Python
import json
|
|
from pathlib import Path
|
|
|
|
from rdflib import Graph, URIRef, Literal, Namespace, BNode
|
|
from rdflib.namespace import RDF, SKOS, XSD
|
|
|
|
|
|
# create an empty graph
|
|
g = Graph()
|
|
|
|
# our namespaces
|
|
dlthings = Namespace("https://concepts.datalad.org/s/things/v1/")
|
|
dlidentifiers = Namespace("https://concepts.datalad.org/s/identifiers/unreleased/")
|
|
trr379cps = Namespace("https://concepts.trr379.de/s/dfg-cp-survey/unreleased/")
|
|
|
|
# bind prefixes to namespaces
|
|
g.bind("dlthings", dlthings)
|
|
g.bind("dlidentifiers", dlidentifiers)
|
|
g.bind("trr379cps", trr379cps)
|
|
|
|
# load files
|
|
for f in Path("raw").glob("*jsonld"):
|
|
with f.open() as fp:
|
|
concept = json.load(fp)
|
|
|
|
subject = URIRef(concept["id"])
|
|
g.add((subject, RDF.type, trr379cps.Discipline))
|
|
g.add((subject, SKOS.prefLabel, Literal(concept["prefLabel"]["en"])))
|
|
g.add((subject, trr379cps.name, Literal(concept["prefLabel"]["en"])))
|
|
g.add((subject, dlthings.description, Literal(concept["prefLabel"]["de"])))
|
|
|
|
issued_id = BNode()
|
|
g.add((subject, dlidentifiers.identifier, issued_id))
|
|
g.add((issued_id, RDF.type, dlidentifiers.IssuedIdentifier))
|
|
g.add((issued_id, dlidentifiers.notation, Literal(concept["notation"][0])))
|
|
g.add((issued_id, dlidentifiers.schema_agency, Literal("Destatis")))
|
|
g.add((issued_id, dlidentifiers.creator, Literal("https://destatis.de/", datatype=XSD.anyURI)))
|
|
|
|
if broader := concept.get("broader", False):
|
|
g.add((subject, trr379cps.part_of, URIRef(broader["id"])))
|
|
|
|
if narrower := concept.get("narrower", False):
|
|
# add parts
|
|
for narrower_concept in narrower:
|
|
g.add((subject, trr379cps.parts, URIRef(narrower_concept["id"])))
|
|
# add a note
|
|
level = (
|
|
"area of study (subject group)"
|
|
if "broader" not in concept.keys()
|
|
else "teaching and research field"
|
|
)
|
|
msg = f"This is a {level}. Please try to select a more specific subject area."
|
|
g.add((subject, SKOS.note, Literal(msg)))
|
|
|
|
if concept.get("deprecated", False):
|
|
# for deprecated concepts, amend the display label
|
|
new_label = f"(DEPRECATED) {concept['prefLabel']['en']}"
|
|
g.set((subject, SKOS.prefLabel, Literal(new_label)))
|
|
|
|
# add a note about deprecation
|
|
# low-level disciplines don't have note, so we can use add, not set
|
|
if replacement := concept.get("isReplacedBy", False):
|
|
msg = f"DEPRECATED. Replaced by {replacement[0]['id']}"
|
|
g.add((subject, SKOS.note, Literal(msg)))
|
|
elif note := concept.get("note", False):
|
|
msg = f"DEPRECATED. {note.get('en')[0]}"
|
|
g.add((subject, SKOS.note, Literal(msg)))
|
|
|
|
g.serialize(Path("systematik.ttl"))
|
|
|
|
# write also individual objects
|
|
outdir = Path("out")
|
|
if not outdir.is_dir():
|
|
outdir.mkdir()
|
|
|
|
discipline_urirefs = g.subjects(predicate=RDF.type, object=trr379cps.Discipline, unique=True)
|
|
for s in discipline_urirefs:
|
|
subgraph = g.cbd(s) # Concise Bounded Description
|
|
subgraph.bind("dlthings", dlthings)
|
|
subgraph.bind("dlidentifiers", dlidentifiers)
|
|
subgraph.bind("trr379cps", trr379cps)
|
|
subgraph.serialize(outdir / (s.split("/")[-1] + ".ttl"))
|