Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

fix(export): normalize IRIs across RDF serializers

mikemikimike52ba7b6890c5
diff --git a/semantica/export/rdf_exporter.py b/semantica/export/rdf_exporter.py
index 276ed799..f68bb120 100644
--- a/semantica/export/rdf_exporter.py
+++ b/semantica/export/rdf_exporter.py
@@ -31,6 +31,7 @@ License: MIT
from pathlib import Path
from decimal import Decimal, InvalidOperation
+from html import escape as xml_escape
from typing import Any, Dict, List, Optional, Set, Union
from urllib.parse import quote, urlsplit
@@ -696,6 +697,8 @@ class RDFSerializer:
lines.append(' xmlns:semantica="https://semantica.dev/ns#">')
lines.append("")
+ namespaces = self.namespace_manager.extract_namespaces(rdf_data)
+
# Convert entities to RDF/XML
entities = rdf_data.get("entities", [])
for entity in entities:
@@ -705,13 +708,19 @@ class RDFSerializer:
entity_text = entity.get("text", "")
entity_id = mint_entity_iri(entity_text)
- entity_type = entity.get("type", DEFAULT_ENTITY_TYPE)
+ entity_type = entity.get("type") or DEFAULT_ENTITY_TYPE
text = entity.get("text") or entity.get("label", "")
confidence = normalize_confidence(entity.get("confidence", 1.0))
# RDF/XML syntax: rdf:Description with rdf:about
- lines.append(f' <rdf:Description rdf:about="{entity_id}">')
- lines.append(f' <rdf:type rdf:resource="{entity_type}"/>')
+ entity_iri = xml_escape(
+ self._as_turtle_iri(entity_id, namespaces), quote=True
+ )
+ entity_type_iri = xml_escape(
+ self._as_turtle_iri(entity_type, namespaces), quote=True
+ )
+ lines.append(f' <rdf:Description rdf:about="{entity_iri}">')
+ lines.append(f' <rdf:type rdf:resource="{entity_type_iri}"/>')
lines.append(f" <semantica:text>{text}</semantica:text>")
if confidence is None:
self.logger.warning(
@@ -731,11 +740,19 @@ class RDFSerializer:
for rel in relationships:
source_id = rel.get("source_id") or rel.get("source")
target_id = rel.get("target_id") or rel.get("target")
- rel_type = rel.get("type", "semantica:related_to")
+ # RDF/XML predicates are emitted as QNames, unlike resource
+ # attributes which use the shared absolute-IRI normalizer.
+ rel_type = rel.get("type") or "semantica:related_to"
# Relationship as property on source entity
- lines.append(f' <rdf:Description rdf:about="{source_id}">')
- lines.append(f' <{rel_type} rdf:resource="{target_id}"/>')
+ source_iri = xml_escape(
+ self._as_turtle_iri(source_id, namespaces), quote=True
+ )
+ target_iri = xml_escape(
+ self._as_turtle_iri(target_id, namespaces), quote=True
+ )
+ lines.append(f' <rdf:Description rdf:about="{source_iri}">')
+ lines.append(f' <{rel_type} rdf:resource="{target_iri}"/>')
lines.append(" </rdf:Description>")
lines.append("")
@@ -855,20 +872,12 @@ class RDFSerializer:
"""
lines = []
+ namespaces = self.namespace_manager.extract_namespaces(rdf_data)
+
def expand_uri(uri: str) -> str:
if not uri:
return ""
- if uri.startswith("http"):
- return f"<{uri}>"
- if uri.startswith("semantica:"):
- return f"<https://semantica.dev/ns#{uri.split(':', 1)[1]}>"
- if uri.startswith("rdf:"):
- return f"<http://www.w3.org/1999/02/22-rdf-syntax-ns#{uri.split(':', 1)[1]}>"
- if uri.startswith("rdfs:"):
- return f"<http://www.w3.org/2000/01/rdf-schema#{uri.split(':', 1)[1]}>"
- if ":" in uri:
- return f"<{uri}>"
- return f"<https://semantica.dev/ns#{uri}>"
+ return f"<{self._as_turtle_iri(uri, namespaces)}>"
# Convert entities
entities = rdf_data.get("entities", [])
@@ -882,7 +891,7 @@ class RDFSerializer:
subject = expand_uri(entity_id)
# Type triple
- entity_type = entity.get("type", "semantica:Entity")
+ entity_type = entity.get("type") or DEFAULT_ENTITY_TYPE
lines.append(
f"{subject} <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> {expand_uri(entity_type)} ."
)
@@ -916,7 +925,7 @@ class RDFSerializer:
for rel in relationships:
source_id = rel.get("source_id") or rel.get("source")
target_id = rel.get("target_id") or rel.get("target")
- rel_type = rel.get("type", "semantica:related_to")
+ rel_type = rel.get("type") or DEFAULT_RELATION_TYPE
if source_id and target_id:
lines.append(
diff --git a/tests/export/test_rdf_exporter_turtle_iris.py b/tests/export/test_rdf_exporter_turtle_iris.py
index 9d6e4ab1..33473fb5 100644
--- a/tests/export/test_rdf_exporter_turtle_iris.py
+++ b/tests/export/test_rdf_exporter_turtle_iris.py
@@ -146,3 +146,39 @@ def test_turtle_expands_context_prefixes_and_mints_relative_values():
RDF.type,
URIRef("https://example.org/Person"),
) in parsed
+
+
+def test_rdfxml_normalizes_resource_iris():
+ """RDF/XML resource attributes use the same safe absolute IRIs."""
+ data = {
+ "entities": [
+ {"id": "Acme Corp", "type": "Person"},
+ {"id": "mailto:foo", "type": "isbn:0451450523"},
+ ],
+ "relationships": [
+ {"source": "Jane Doe", "target": "Acme Corp", "type": "knows"}
+ ],
+ }
+ rdfxml = RDFExporter().export_to_rdf(data, format="rdfxml")
+ parsed = Graph().parse(data=rdfxml, format="xml")
+
+ assert URIRef("https://semantica.dev/ns#Acme%20Corp") in parsed.all_nodes()
+ assert URIRef("mailto:foo") in parsed.all_nodes()
+
+
+def test_ntriples_normalizes_resource_iris():
+ """N-Triples resource IRIs reject neither spaces nor opaque schemes."""
+ data = {
+ "entities": [
+ {"id": "Acme Corp", "type": "Person"},
+ {"id": "mailto:foo", "type": "isbn:0451450523"},
+ ],
+ "relationships": [
+ {"source": "Jane Doe", "target": "Acme Corp", "type": "knows"}
+ ],
+ }
+ ntriples = RDFExporter().export_to_rdf(data, format="ntriples")
+ parsed = Graph().parse(data=ntriples, format="nt")
+
+ assert URIRef("https://semantica.dev/ns#Acme%20Corp") in parsed.all_nodes()
+ assert URIRef("mailto:foo") in parsed.all_nodes()