Skip to content

Commit

Permalink
refactor: replace yaml with toml for rdf parser
Browse files Browse the repository at this point in the history
  • Loading branch information
b1rger committed Oct 16, 2023
1 parent 0b8b639 commit 4f96182
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 3 deletions.
244 changes: 244 additions & 0 deletions apis_core/default_settings/RDF_default_settings.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
#########################################
# Create an entity `apis_ontology.Place`
# from a geonames RDF endpoint
##########################################
[PlaceFromGeonames]
model = "apis_ontology.Place"
filter_sparql = """
PREFIX gn: <http://www.geonames.org/ontology#>
ASK {
?subject gn:featureClass <https://www.geonames.org/ontology#P> .
?subject gn:featureCode <https://www.geonames.org/ontology#P.PPL> .
}
"""
[[PlaceFromGeonames.attributes]]
# name
sparql = """
PREFIX gn: <http://www.geonames.org/ontology#>
SELECT ?name
WHERE
{
?subject gn:name|gn:officialName|gn:alternateName ?prefName
FILTER (LANGMATCHES(LANG(?prefName), "de") || LANGMATCHES(LANG(?prefName), "en") || LANG(?prefName) = "")
BIND(?prefName AS ?name)
}
ORDER BY ?lang
"""
[[PlaceFromGeonames.attributes]]
# alternative_label
sparql = """
PREFIX gn: <http://www.geonames.org/ontology#>
SELECT ?altName (LANG(?altName) AS ?lang)
WHERE {
?subject gn:alternateName ?altName
FILTER (LANGMATCHES(LANG(?altName), "de") || LANGMATCHES(LANG(?altName), "en") || LANG(?prefName) = "")
}
"""
[[PlaceFromGeonames.attributes]]
# kind
sparql = """
PREFIX gn: <http://www.geonames.org/ontology#>
SELECT ?kind
WHERE {
?subject gn:featureCode ?kind
}
"""
[[PlaceFromGeonames.attributes]]
# lat
sparql = """
PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
SELECT ?lat
WHERE {
?subject wgs84_pos:lat ?lat.
?subject wgs84_pos:long ?long
}
"""
[[PlaceFromGeonames.attributes]]
# long
sparql = """
PREFIX wgs84_pos: <http://www.w3.org/2003/01/geo/wgs84_pos#>
SELECT ?long
WHERE {
?subject wgs84_pos:lat ?lat.
?subject wgs84_pos:long ?long
}
"""
[[PlaceFromGeonames.attributes]]
# parent
sparql = """
PREFIX gn: <http://www.geonames.org/ontology#>
SELECT ?parent
WHERE {
?subject gn:parentCountry ?parent
}
"""

#########################################
# Create an entity `apis_ontology.Place`
# from a d-nb.info RDF endpoint
#########################################
[PlaceFromDNB]
model = "apis_ontology.Place"
filter_sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
ASK {
?subject gndo:preferredNameForThePlaceOrGeographicName ?object .
}
"""
[[PlaceFromDNB.attributes]]
# name
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?name
WHERE {
?subject gndo:preferredNameForThePlaceOrGeographicName ?prefName
BIND(?prefName AS ?name)
}
"""
[[PlaceFromDNB.attributes]]
# lon
sparql = """
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
SELECT ?lon
WHERE {
?subject geo:hasGeometry ?geo1 .
?geo1 geo:asWKT ?point .
BIND(REPLACE(str(?point), "Point . \\+(\\d+\\.\\d+) .+$", "$1") as ?lon)
}
"""
[[PlaceFromDNB.attributes]]
# lat
sparql = """
PREFIX geo: <http://www.opengis.net/ont/geosparql#>
SELECT ?lat
WHERE {
?subject geo:hasGeometry ?geo1 .
?geo1 geo:asWKT ?point .
BIND(REPLACE(str(?point), "Point . \\+(\\d+\\.\\d+) \\+(\\d+\\.\\d+) .$", "$2") as ?lat)
}
"""

#########################################
# Create an entity `apis_ontology.Person`
# from a d-nb.info RDF endpoint
#########################################
[PersonFromDNB]
model = "apis_ontology.Person"
filter_sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
ASK {
?subject gndo:preferredNameForThePerson ?object .
}
"""
[[PersonFromDNB.attributes]]
# name
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?name
WHERE {
?subject gndo:preferredNameForThePerson ?name .
OPTIONAL {
?subject gndo:preferredNameEntityForThePerson ?med .
?med gndo:forename ?first_name.
?med gndo:surname ?name2 .
BIND(?name2 as ?name)
}
BIND(CONCAT(?name, ",", ?first_name) AS ?name)
}
"""
[[PersonFromDNB.attributes]]
# profession
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?profession
WHERE {
?subject gndo:professionOrOccupation ?profession
}
"""
[[PersonFromDNB.attributes]]
# date_of_birth
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?date_of_birth
WHERE {
?subject gndo:dateOfBirth ?start_date_written
BIND(?start_date_written AS ?date_of_birth)
}
"""
[[PersonFromDNB.attributes]]
# date_of_death
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?date_of_death
WHERE {
?subject gndo:dateOfDeath ?end_date_written
BIND(?end_date_written AS ?date_of_death)
}
"""
[[PersonFromDNB.attributes]]
# place_of_birth
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?place_of_birth
WHERE {
?subject gndo:placeOfBirth ?place_of_birth
}
"""

##############################################
# Create an entity `apis_ontology.Institution`
# from a d-nb.info RDF endpoint
##############################################
[InstitutionFromDNB]
model = "apis_ontology.Institution"
filter_sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
ASK {
?subject gndo:preferredNameForTheCorporateBody ?object .
}
"""
[[InstitutionFromDNB.attributes]]
# name
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?name
WHERE {
?subject gndo:preferredNameForTheCorporateBody ?name
}
"""
[[InstitutionFromDNB.attributes]]
# altName
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?altName
WHERE {
?subject gndo:variantNameForTheCorporateBody ?altName
}
"""
[[InstitutionFromDNB.attributes]]
# place
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?place
WHERE {
?subject gndo:placeOfBusiness ?place
}
"""
[[InstitutionFromDNB.attributes]]
# start_date_written
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?start_date_written
WHERE {
?subject gndo:dateOfEstablishment ?start_date_written
}
"""
[[InstitutionFromDNB.attributes]]
# end_date_written
sparql = """
PREFIX gndo: <https://d-nb.info/standards/elementset/gnd#>
SELECT ?end_date_written
WHERE {
?subject gndo:dateOfTermination ?end_date_written
}
"""
4 changes: 2 additions & 2 deletions apis_core/utils/rdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

import pathlib
import logging
import tomllib

from yaml import safe_load
from rdflib import Graph
from typing import Tuple

Expand All @@ -24,7 +24,7 @@ def get_modelname_and_dict_from_uri(
graph.parse(uri)

settings_file = settings_file or rdf_object_mapping_file()
settings = safe_load(settings_file.read_text())
settings = tomllib.load(settings_file.read_text())
matching_definition = None
for key, definition in settings.items():
if set(definition_must_have_keys) <= set(definition.keys()):
Expand Down
2 changes: 1 addition & 1 deletion apis_core/utils/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def clean_uri_mapping_file() -> Path:


def rdf_object_mapping_file() -> Path:
default = default_settings() / "RDF_default_settings.yml"
default = default_settings() / "RDF_default_settings.toml"
mapping_file = getattr(settings, "APIS_RDF_YAML_SETTINGS", default)
return Path(mapping_file)

Expand Down

0 comments on commit 4f96182

Please sign in to comment.