-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(utils): implement new type rdf importer
- Loading branch information
Showing
3 changed files
with
95 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
apis_core/apis_entities/triple_configs/E21_PersonFromDNB.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[[filters]] | ||
"rdf:type" = "gndo:DifferentiatedPerson" | ||
|
||
[attributes] | ||
forename = ["gndo:forename", "gndo:preferredNameEntityForThePerson/gndo:forename"] | ||
alternative_names = "gndo:variantNameForThePerson" | ||
surname = ["gndo:surname", "gndo:preferredNameEntityForThePerson/gndo:surname"] | ||
start_date_written = "gndo:dateOfBirth" | ||
end_date_written = "gndo:dateOfDeath" | ||
same_as = "owl:sameAs" | ||
profession = "gndo:professionOrOccupation" | ||
|
||
relations = ["gndo:placeOfDeath", "gndo:placeOfBirth"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# SPDX-FileCopyrightText: 2025 Birger Schacht | ||
# SPDX-License-Identifier: MIT | ||
|
||
import logging | ||
from collections import defaultdict | ||
from pathlib import Path | ||
|
||
import tomllib | ||
from AcdhArcheAssets.uri_norm_rules import get_normalized_uri | ||
from django.apps import apps | ||
from rdflib import RDF, BNode, Graph | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def find_matching_config(graph: Graph) -> dict | None: | ||
models_with_config = [ | ||
model for model in apps.get_models() if hasattr(model, "rdf_configs") | ||
] | ||
for model in models_with_config: | ||
for path in model.rdf_configs(): | ||
config = tomllib.loads(Path(path).read_text()) | ||
for _filter in config.get("filters", []): | ||
triples = [ | ||
( | ||
None, | ||
graph.namespace_manager.expand_curie(predicate), | ||
graph.namespace_manager.expand_curie(obj), | ||
) | ||
for predicate, obj in _filter.items() | ||
] | ||
triples = [triple in graph for triple in triples] | ||
if all(triples): | ||
logger.debug("Using %s for parsing graph", path) | ||
config["model"] = model | ||
return config | ||
return None | ||
|
||
|
||
def get_something_from_uri(uri: str) -> dict | None: | ||
uri = get_normalized_uri(uri) | ||
graph = Graph() | ||
graph.parse(uri) | ||
|
||
if config := find_matching_config(graph): | ||
result = defaultdict(list) | ||
result["model"] = config["model"] | ||
result["relations"] = defaultdict(list) | ||
|
||
for attribute, curies in config.get("attributes", {}).items(): | ||
if isinstance(curies, str): | ||
curies = [curies] | ||
for curie in curies: | ||
values = [] | ||
results = graph.query( | ||
"SELECT ?object WHERE { ?subject " + curie + " ?object }" | ||
) | ||
objects = [result.object for result in results] | ||
for obj in objects: | ||
if isinstance(obj, BNode): | ||
values.extend( | ||
[ | ||
value.toPython() | ||
for value in graph.objects(subject=obj) | ||
if value != RDF.Seq | ||
] | ||
) | ||
else: | ||
values.append(obj.toPython()) | ||
|
||
if attribute == "relations": | ||
result["relations"][curie].extend(values) | ||
else: | ||
result[attribute].extend(values) | ||
return dict(result) | ||
return None |