lehr-und-forschungsbereiche/tojson.py
Michał Szczepanik 70d7abd8d7 Add display notes about selecting narrower subject areas.
The same way we did it last year - as `display_note` (`SKOS:note`).

The DFG does not ask for that specific input, but with all the
structured we are spoiled for choice - and whenever someone selects a
subject area, broader categories follow unambiguously via `part_of`.
2026-07-28 19:24:20 +02:00

70 lines
2.5 KiB
Python

import json
from pathlib import Path
import tomllib
def get_label_and_desc(record: dict):
"""Get label and description from a record
We stored prefLabel.de (always) and prefLabel.en (usually). The
schema does not support internationalization, so we prefer using
english label as the prefLabel and put the German one into
description.
"""
prefLabel = record["prefLabel"]
try:
label = prefLabel["en"]
except KeyError:
label = prefLabel["de"]
description = prefLabel["de"]
return label, description
def create_discipline(id, label, desc, part_of_id = None, parts_ids = None):
ns = "https://trr379.de/ns/dfg-cp-survey/discipline/"
record = {
"pid": f"{ns}{id}",
"display_label": label,
"name": label,
"description": desc,
"identifiers": [{"creator": "https://ror.org/01kratx56", "notation": id}],
}
if part_of_id is not None:
record["part_of"] = f"{ns}{part_of_id}"
if parts_ids is not None:
record["parts"] = [f"{ns}{part_id}" for part_id in parts_ids]
return record
with Path("derived/personal-stellenstatistik.toml").open("rb") as fp:
data = tomllib.load(fp)
records = []
for group_id, group in data.items():
group_label, group_desc = get_label_and_desc(group)
field_ids = [x for x in group.keys() if x != "prefLabel"]
record = create_discipline(group_id, group_label, group_desc, part_of_id = None, parts_ids = field_ids)
record["display_note"] = "This is an area of study (subject group). Please try to select a more specific subject area."
records.append(record)
for field_id, field in group.items():
if field_id == "prefLabel":
continue
field_label, field_desc = get_label_and_desc(field)
area_ids = [x for x in field.keys() if x != "prefLabel"]
record = create_discipline(field_id, field_label, field_desc, part_of_id = group_id, parts_ids = area_ids)
record["display_note"] = "This is a teaching and research field. Please try to select a more specific subject area."
records.append(record)
for area_id, area in field.items():
if area_id == "prefLabel":
continue
area_label, area_desc = get_label_and_desc(area)
record = create_discipline(area_id, area_label, area_desc, part_of_id = field_id, parts_ids = None)
records.append(record)
with Path("derived/all_records.jsonl").open("w") as fp:
for record in records:
fp.write(json.dumps(record) + "\n")