Skip to content

Commit

Permalink
fix: adds sameAs and graphs to endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
sennierer committed Feb 2, 2024
1 parent b4c9275 commit 769ee50
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 11 deletions.
20 changes: 18 additions & 2 deletions semantic_kraus_api/main_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
from fastapi import APIRouter, Depends, HTTPException
from fastapi_versioning import versioned_api_route

from .query_parameters import Search
from .models_v1 import PaginatedResponseSubjects
from .query_parameters import Search, GetEntity
from .models_v1 import PaginatedResponseSubjects, Person
from .utils import flatten_rdf_data, get_query_from_triplestore, toggle_urls_encoding


Expand All @@ -27,3 +27,19 @@ async def query_entities(search: Search = Depends()):
pages = math.ceil(int(res[0]["count"]) / search.limit) if len(res) > 0 else 0
count = int(res[0]["count"]) if len(res) > 0 else 0
return {"page": search.page, "count": count, "pages": pages, "results": res}


@router.get(
"/api/entities/person",
response_model=Person,
response_model_exclude_none=True,
tags=["Entities endpoints"],
description="Endpoint that allows to retrive a person by id.",
)
async def retrieve_person(query: GetEntity = Depends()):
query_dict = asdict(query)
res = get_query_from_triplestore_v2(query_dict, "get_person_v1.sparql")
# res = FakeList(**{"results": flatten_rdf_data(res)})
if len(res) == 0:
raise HTTPException(status_code=404, detail="Item not found")
return {"_results": flatten_rdf_data(res)}
62 changes: 59 additions & 3 deletions semantic_kraus_api/models_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
from fastapi_versioning import version, versioned_api_route
from fastapi import APIRouter, Depends, HTTPException

graph_mapping = {
"https://sk.acdh.oeaw.ac.at/project/dritte-walpurgisnacht": "Dritte Walpurgisnacht",
"https://sk.acdh.oeaw.ac.at/project/legal-kraus": "Legal Kraus Project",
"https://sk.acdh.oeaw.ac.at/project/fackel": "Die Fackel online",
}


def pp_source_professional_occupation(field, item, data) -> dict:
print("test")
return item


class SemanticKrausBackendBaseModel(RDFUtilsModelBaseClass):
errors: typing.List[str] | None = None
Expand All @@ -22,14 +33,40 @@ class InternationalizedLabel(SemanticKrausBackendBaseModel):
default: str
en: typing.Optional[str]
de: str | None = None
fi: str | None = None
si: str | None = None
du: str | None = None

def __init__(__pydantic_self__, **data: typing.Any) -> None:
super().__init__(**data)


class SameAs(SemanticKrausBackendBaseModel):
"""Used to provide source information"""

id: HttpUrl = Field(..., rdfconfig=FieldConfigurationRDF(path="sameAs", anchor=True))
object_label: str | None = Field(None, rdfconfig=FieldConfigurationRDF(path="objectLabel"))
graph: HttpUrl | None = Field(None, rdfconfig=FieldConfigurationRDF(path="graph"))
graph_label: str | None = Field(None, rdfconfig=FieldConfigurationRDF(path="graphLabel"))

def __init__(__pydantic_self__, **data: typing.Any) -> None:
if "graph" in data:
data["graphLabel"] = graph_mapping.get(data["graph"], data["graph"])
super().__init__(**data)


class ProfessionalOccupation(SemanticKrausBackendBaseModel):
"""Used to provide professional occupation information"""

id: HttpUrl = Field(..., rdfconfig=FieldConfigurationRDF(path="professionalOccupation", anchor=True))
label: InternationalizedLabel = Field(
..., rdfconfig=FieldConfigurationRDF(path="professionalOccupationLabel", default_dict_key="default")
)
source: str = Field(
...,
rdfconfig=FieldConfigurationRDF(
path="professionalOccupationSource", callback_function=pp_source_professional_occupation
),
)


class Entity(SemanticKrausBackendBaseModel):
id: str = Field(
...,
Expand All @@ -40,6 +77,25 @@ class Entity(SemanticKrausBackendBaseModel):
)
entity_class: HttpUrl = Field(..., rdfconfig=FieldConfigurationRDF(path="type"))
entity_class_label: str = Field(..., rdfconfig=FieldConfigurationRDF(path="typeLabel"))
graph: HttpUrl | None = Field(None, rdfconfig=FieldConfigurationRDF(path="graph_subject"))
graph_label: str | None = Field(None, rdfconfig=FieldConfigurationRDF(path="graph_subjectLabel"))
SameAs: typing.List[SameAs] | None

def __init__(__pydantic_self__, **data: typing.Any) -> None:
if "graph_subject" in data:
data["graph_subjectLabel"] = graph_mapping.get(data["graph_subject"], None)
super().__init__(**data)


class Person(SemanticKrausBackendBaseModel):
id: str = Field(
...,
rdfconfig=FieldConfigurationRDF(path="person", anchor=True),
)
label: InternationalizedLabel | None = Field(
None, rdfconfig=FieldConfigurationRDF(path="label", default_dict_key="default")
)
professions: typing.List[ProfessionalOccupation] | None


class PaginatedResponseBase(SemanticKrausBackendBaseModel):
Expand Down
5 changes: 5 additions & 0 deletions semantic_kraus_api/query_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,8 @@ class Search(QueryBase):
],
description="Filter by entity type. Can be multiple",
)


@dataclasses.dataclass(kw_only=True)
class GetEntity:
id: str = Query(..., description="The id of the entity to retrieve")
23 changes: 17 additions & 6 deletions semantic_kraus_api/sparql/entities_search_v1.sparql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX frbroo: <https://cidoc-crm.org/frbroo/sites/default/files/FRBR2.4-draft.rdfs#>


SELECT ?subject ?type ?typeLabel ?count ?label
SELECT ?subject ?type ?typeLabel ?count ?label ?graph ?sameAs ?objectLabel ?graph_subject

WITH {
SELECT (COUNT(?subject) as ?count)
Expand All @@ -20,16 +20,27 @@ WITH {
SELECT ?subject ?type ?score
WHERE {
{% include 'search_entities_sub_v1.sparql' %}
}
}

{% if q %}ORDER BY DESC(?score) ?subject {% else %} ORDER BY ?subject {% endif %}
LIMIT {{limit}}
{% if _offset > 0 %}OFFSET {{_offset}}{% endif %}
} AS %query_set

WHERE {
INCLUDE %count_set
INCLUDE %query_set
?subject rdfs:label|skos:prefLabel ?label .
INCLUDE %query_set
GRAPH ?graph_subject {
?subject rdfs:label|skos:prefLabel ?label . }
OPTIONAL {?type rdfs:label ?typeLabel .
FILTER(LANG(?typeLabel) = "en")
}}
FILTER(LANG(?typeLabel) = "en")}
OPTIONAL {
GRAPH ?graph {
?subject ((owl:sameAs|^owl:sameAs)*) ?sameAs .
FILTER(?subject != ?sameAs)
OPTIONAL {?sameAs rdfs:label ?objectLabel .}

}
}

}
29 changes: 29 additions & 0 deletions semantic_kraus_api/sparql/get_person_v1.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
PREFIX frbroo: <https://cidoc-crm.org/frbroo/sites/default/files/FRBR2.4-draft.rdfs#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX crm: <http://www.cidoc-crm.org/cidoc-crm/>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?person ?occupation ?time ?employer ?name ?graph ?type ?typeLabel WHERE {
GRAPH ?graph {
BIND(<{{id}}> as ?person)
?person crm:P1_is_identified_by ?appellation.
?appellation rdf:type crm:E33_E41_Linguistic_Appellation;
rdf:value ?name.
OPTIONAL {
?appellation crm:P2_has_type ?type.

FILTER(?type NOT IN(<https://sk.acdh.oeaw.ac.at/types/person/persname/male>, <https://sk.acdh.oeaw.ac.at/types/person/persname/female>)
)
}

OPTIONAL{?person crm:P14i_performed ?occ.
?occ rdf:type frbroo:F51_Pursuit;
rdfs:label ?occupation.
OPTIONAL { ?occ crm:P10_falls_within ?employer. }
OPTIONAL { ?occ (crm:P4_has_time-span/rdfs:label) ?time. }
}
}
OPTIONAL{?type rdfs:label ?typeLabel}
}

0 comments on commit 769ee50

Please sign in to comment.