Skip to content

Commit

Permalink
Merge pull request #43 from ternaustralia/edmond/improve-labels
Browse files Browse the repository at this point in the history
Improve rendering instances with large number of predicates
  • Loading branch information
edmondchuc authored Jan 4, 2023
2 parents 6335fa4 + 66dad78 commit d264704
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 35 deletions.
10 changes: 8 additions & 2 deletions src/linkeddata_api/data/sparql.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import requests

from . import exceptions
from linkeddata_api.log_time import log_time


@log_time
def post(
query: str, sparql_endpoint: str, accept: str = "application/sparql-results+json"
) -> requests.Response:
Expand All @@ -21,7 +23,9 @@ def post(
"content-type": "application/sparql-query",
}

response = requests.post(url=sparql_endpoint, headers=headers, data=query, timeout=60)
response = requests.post(
url=sparql_endpoint, headers=headers, data=query, timeout=60
)

try:
response.raise_for_status()
Expand Down Expand Up @@ -51,7 +55,9 @@ def get(
}
params = {"query": query}

response = requests.get(url=sparql_endpoint, headers=headers, params=params, timeout=60)
response = requests.get(
url=sparql_endpoint, headers=headers, params=params, timeout=60
)

try:
response.raise_for_status()
Expand Down
2 changes: 2 additions & 0 deletions src/linkeddata_api/domain/curie.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
"https://w3id.org/tern/ontologies/tern/": "tern",
"http://www.w3.org/2002/07/owl#": "owl",
"http://www.w3.org/2001/XMLSchema#": "xsd",
"http://rdfs.org/ns/void#": "void",
"http://www.w3.org/ns/prov#": "prov",
}

# Don't find curies for these - speeds up request processing.
Expand Down
86 changes: 53 additions & 33 deletions src/linkeddata_api/domain/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
from jinja2 import Template

from linkeddata_api import data
from linkeddata_api.log_time import log_time


@log_time
def get(
uri: str,
sparql_endpoint: str,
Expand All @@ -24,20 +26,8 @@ def get(
SELECT DISTINCT ?label
WHERE {
{
BIND(<{{ uri }}> as ?uri)
VALUES (?labelProperty) {
(skos:prefLabel)
(rdfs:label)
(dcterms:title)
(schema:name)
(sdo:name)
(dcterms:identifier)
}
?uri ?labelProperty ?label .
}
{% if uri.startswith('http://linked.data.gov.au/def/tern-cv/') %}
UNION {
{
SERVICE <https://graphdb.tern.org.au/repositories/tern_vocabs_core> {
BIND(<{{ uri }}> as ?uri)
VALUES (?labelProperty) {
Expand All @@ -51,6 +41,19 @@ def get(
?uri ?labelProperty ?label .
}
}
{% else %}
{
BIND(<{{ uri }}> as ?uri)
VALUES (?labelProperty) {
(skos:prefLabel)
(rdfs:label)
(dcterms:title)
(schema:name)
(sdo:name)
(dcterms:identifier)
}
?uri ?labelProperty ?label .
}
{% endif %}
}
LIMIT 1
Expand All @@ -73,6 +76,15 @@ def get(
def _get_from_list_query(uris: list[str]) -> str:
# TODO: Currently, we try and fetch from TERN's controlled vocabularies.
# We may want to also fetch with a SERVICE query from other repositories in the future.
tern_cv_uris = [
uri for uri in uris if uri.startswith("http://linked.data.gov.au/def/tern-cv/")
]
uris = [
uri
for uri in uris
if not uri.startswith("http://linked.data.gov.au/def/tern-cv/")
]

template = Template(
"""
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
Expand All @@ -83,26 +95,16 @@ def _get_from_list_query(uris: list[str]) -> str:
SELECT DISTINCT ?uri ?label
WHERE {
{% for uri in uris %}
{
SELECT DISTINCT ?uri ?label
WHERE {
{
BIND(<{{ uri }}> as ?uri)
VALUES (?labelProperty) {
(skos:prefLabel)
(rdfs:label)
(dcterms:title)
(schema:name)
(sdo:name)
(dcterms:identifier)
VALUES (?uri) {
{% for uri in uris %}
(<{{ uri }}>)
{% endfor %}
}
?uri ?labelProperty ?label .
}
{% if uri.startswith('http://linked.data.gov.au/def/tern-cv/') %}
UNION {
SERVICE <https://graphdb.tern.org.au/repositories/tern_vocabs_core> {
BIND(<{{ uri }}> as ?uri)
{
VALUES (?labelProperty) {
(skos:prefLabel)
(rdfs:label)
Expand All @@ -114,19 +116,37 @@ def _get_from_list_query(uris: list[str]) -> str:
?uri ?labelProperty ?label .
}
}
{% endif %}
}
LIMIT 1
}
{% if not loop.last %}UNION{% endif %}
{% endfor %}
UNION {
SELECT DISTINCT ?uri ?label
WHERE {
VALUES (?uri) {
{% for uri in tern_cv_uris %}
(<{{ uri }}>)
{% endfor %}
}
SERVICE <https://graphdb.tern.org.au/repositories/tern_vocabs_core> {
VALUES (?labelProperty) {
(skos:prefLabel)
(rdfs:label)
(dcterms:title)
(schema:name)
(sdo:name)
(dcterms:identifier)
}
?uri ?labelProperty ?label .
}
}
}
}
"""
)
query = template.render(uris=uris)
query = template.render(uris=uris, tern_cv_uris=tern_cv_uris)
return query


@log_time
def get_from_list(
uris: list[str],
sparql_endpoint: str,
Expand Down

0 comments on commit d264704

Please sign in to comment.