Skip to content

Commit

Permalink
Add Nicer display of labels
Browse files Browse the repository at this point in the history
  • Loading branch information
epoz committed Oct 8, 2023
1 parent c2c4c02 commit ef6b09f
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
from typing import Optional
from urllib.parse import quote, parse_qs
import pyoxigraph as px
from .rdfer import prefixes, RDFer
from .rdfer import prefixes, RDFer, Nice
from rich.traceback import install
from .fts import init_fts, search
from .px_util import OxigraphSerialization, SynthQuerySolutions, results_to_triples
Expand Down Expand Up @@ -312,8 +312,9 @@ async def shmarql(
"https://docs.google.com/spreadsheets/d/1HVxe9DoKtJTfJHl0l_NQKH-3CVxv-LTHa6xMHoYBcFk/edit?usp=sharing"
)

uniq_uris = set()
nicer = Nice(None, [])
if e == "_local_":
QUERY_DEFAULT_LIMIT
buf = []

if s == "?s" and p == "?p" and o == "?o":
Expand All @@ -327,11 +328,13 @@ async def shmarql(
while len(buf) < QUERY_DEFAULT_LIMIT:
try:
ts, tp, to, _ = next(triples)
uniq_uris.update([ts, tp, to])
except StopIteration:
break
buf.append((ts, tp, to))

results = OxigraphSerialization(SynthQuerySolutions(buf)).json()
nicer = Nice(GRAPH, uniq_uris)
else:
if s or p or o:
q = (
Expand Down Expand Up @@ -413,6 +416,7 @@ async def shmarql(
"http://www.europeana.eu/schemas/edm/isShownBy",
],
"obj": obj,
"nicer": nicer,
},
)

Expand Down
26 changes: 26 additions & 0 deletions src/app/rdfer.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json, logging
from .config import PREFIXES
import pyoxigraph as px


def prefixes(value):
Expand All @@ -13,6 +14,31 @@ def prefixes(value):
return value


class Nice:
def __init__(self, graph: px.Store, uris: iter):
data = {}
if graph is None:
return
for uri in uris:
if not type(uri) == px.NamedNode:
continue
for s, p, o, _ in graph.quads_for_pattern(uri, None, None):
data.setdefault(s.value, {}).setdefault(p.value, []).append(o.value)
self.data = data

def s(self, uri):
D = self.data.get(uri, {})
for d in [
"http://www.w3.org/2000/01/rdf-schema#label",
"http://schema.org/name",
"http://www.w3.org/2004/02/skos/core#prefLabel",
]:
dd = D.get(d)
if dd:
return dd[0]
return prefixes(uri)


class RDFer:
def __init__(self, results):
self._data = {}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/browse.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
{% endif %}

{% if elem["type"] == "uri" %}
<a href="/shmarql?e={{e}}&{{param}}=&lt;{{elem['value']|urlencode}}&gt;">{{elem["value"]|prefixes}}</a>
<a href="/shmarql?e={{e}}&{{param}}=&lt;{{elem['value']|urlencode}}&gt;" title="{{elem["value"]|prefixes}}">{{nicer.s(elem["value"])}}</a>
{% elif elem["type"] == "bnode" %}
<a href="/shmarql?e={{e}}&{{param}}=&lt;_:{{elem['value']|urlencode}}&gt;">{{elem["value"]|prefixes}}</a>
{% else %}
Expand Down
2 changes: 1 addition & 1 deletion src/templates/detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@


{% if elem["type"] == "uri" %}
<a href="/shmarql?e={{e}}&{{param}}=&lt;{{elem['value']|urlencode}}&gt;">{{elem["value"]|prefixes}}</a>
<a href="/shmarql?e={{e}}&{{param}}=&lt;{{elem['value']|urlencode}}&gt;" title="{{elem["value"]|prefixes}}">{{nicer.s(elem["value"])}}</a>
{% elif elem["type"] == "bnode" %}
<a href="/shmarql?e={{e}}&{{param}}=&lt;_:{{elem['value']|urlencode}}&gt;">{{elem["value"]|prefixes}}</a>
{% else %}
Expand Down

0 comments on commit ef6b09f

Please sign in to comment.