Labels¶
Find duplicate names or descriptions¶
find_duplicate_labels() finds nodes with more than one distinct annotation literal. It accepts a Path, an RDF
string, or an RDFLib Graph. Paths can identify an RDF file or a directory of RDF files.
By default, it checks names using schema:name (https://schema.org/name), rdfs:label, dcterms:title, and
skos:prefLabel. Set names_or_descriptions="descriptions" to check schema:description
(https://schema.org/description), rdfs:comment, dcterms:description, and skos:definition instead.
The result is a dictionary mapping each affected node to a set of its distinct literals across the selected predicates. Nodes can be IRIs or blank nodes, including resources used as predicates or objects elsewhere in the graph. Identical literals on different predicates count once. Differences in spelling, datatype, or language count separately, including a language-tagged literal versus one without a language tag. Comparisons use the literals as stored by RDFLib, which may normalize typed literals when loading RDF.
from pathlib import Path
from rdflib import URIRef
from kurra.labels import find_duplicate_labels
source = Path("vocabulary.ttl")
# Find multiple distinct names using the default predicates.
names = find_duplicate_labels(source)
# Find multiple distinct descriptions instead.
descriptions = find_duplicate_labels(source, names_or_descriptions="descriptions")
# Extend the name predicates with additional annotation IRIs.
extra_names = find_duplicate_labels(
source,
additional_annotation_predicates=[
URIRef("https://example.com/displayName"),
URIRef("https://example.com/shortName"),
],
)
# Additional predicates can also extend the description predicates.
extra_descriptions = find_duplicate_labels(
source,
names_or_descriptions="descriptions",
additional_annotation_predicates=[URIRef("https://example.com/description")],
)
for node, values in names.items():
print(node.n3(), sorted(value.n3() for value in values))
Additional predicates supplement the defaults; they do not replace them. With no duplicates, the function returns an
empty dictionary. Values other than "names" or "descriptions" raise ValueError.
API reference¶
kurra.labels ¶
Functions to find RDF elements missing labels, and to acquire them from KurrawongAI's Semantic Background or another provided context.
find_duplicate_labels ¶
find_duplicate_labels(p: Path | str | Graph, names_or_descriptions: Literal['names', 'descriptions'] = 'names', additional_annotation_predicates: list[URIRef] | None = None) -> dict[IdentifiedNode, set[RDFLiteral]]
Find nodes with more than one distinct name or description literal.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
Path | str | Graph
|
An RDF file path, directory, RDF string, or Graph to scan. |
required |
names_or_descriptions
|
Literal['names', 'descriptions']
|
Use name predicates by default, or description predicates. |
'names'
|
additional_annotation_predicates
|
list[URIRef] | None
|
Extra annotation predicate IRIs to search. Defaults to no additional predicates. |
None
|
Returns:
| Type | Description |
|---|---|
dict[IdentifiedNode, set[Literal]]
|
A dict mapping each affected node to its distinct literals across the selected |
dict[IdentifiedNode, set[Literal]]
|
predicates. Literal spelling, datatype, and language distinguish values as |
dict[IdentifiedNode, set[Literal]]
|
stored in the graph. Identical values on different predicates count once. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If names_or_descriptions is neither "names" nor "descriptions". |
Source code in kurra/labels.py
find_missing_labels ¶
find_missing_labels(p: Path | str | Graph, local_context: Path | Graph | None = None) -> Iterable[URIRef]
Find all the IRIs in a graph missing labels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
p
|
Path | str | Graph
|
The RDF source to scan for IRIs missing labels as a file path, directory, RDF string, or Graph. |
required |
local_context
|
Path | Graph | None
|
An RDF file, directory, or Graph containing labels to check against, in addition to |
None
|
Returns:
| Type | Description |
|---|---|
Iterable[URIRef]
|
The IRIs missing a label. |
Source code in kurra/labels.py
get_labels ¶
get_labels(iris: list[URIRef], context: Graph | str | Path = 'https://fuseki.dev.kurrawong.ai/semback/sparql', return_type: Literal['graph', 'dict'] = 'graph', http_client: Client | None = None) -> Graph | dict[URIRef, str]
Get labels for the given IRIs from a given context, by default the KurrawongAI Semantic Background.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iris
|
list[URIRef]
|
The IRIs to fetch labels for. |
required |
context
|
Graph | str | Path
|
An RDF Graph, file, directory, or SPARQL endpoint containing labels to check against. Defaults to the KurrawongAI Semantic Background. |
'https://fuseki.dev.kurrawong.ai/semback/sparql'
|
return_type
|
Literal['graph', 'dict']
|
|
'graph'
|
http_client
|
Client | None
|
An optional HTTPX client to contain credentials if needed to access a SPARQL endpoint as context. A new one is created if not given. |
None
|
Returns:
| Type | Description |
|---|---|
Graph | dict[URIRef, str]
|
A Graph if |
Source code in kurra/labels.py
jsonld_context ¶
Create a JSON-LD context mapping resource labels to their IRIs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
Graph
|
The graph to generate a context for. |
required |
vocabulary
|
Graph | None
|
An additional graph to source labels and class/property types from. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
A dict mapping each generated JSON-LD term to its IRI. |