Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

Merge branch 'main' into metadata-passthrough

KaifAhmad1fb02c868f8c3
diff --cc semantica/export/rdf_exporter.py
index ce64a3a2,32a09895..bcd89140
--- a/semantica/export/rdf_exporter.py
+++ b/semantica/export/rdf_exporter.py
@@@ -720,31 -534,21 +787,32 @@@ class RDFSerializer
text = entity.get("text") or entity.get("label", "")
confidence = normalize_confidence(entity.get("confidence", 1.0))
- lines.append(
- f"<{self._as_turtle_iri(entity_id, merged_namespaces)}> a "
- f"<{self._as_turtle_iri(entity_type, merged_namespaces)}> ;"
- )
+ clauses = [
- f"a <{entity_type}>",
++ f"a <{self._as_turtle_iri(entity_type, merged_namespaces)}>",
+ f'semantica:text "{text}"',
+ ]
if confidence is None:
self.logger.warning(
f"Entity {entity_id} has a confidence that is not a number "
f"({entity.get('confidence')!r}), so no confidence is written"
)
- lines.append(f' semantica:text "{text}" .')
else:
- lines.append(f' semantica:text "{text}" ;')
- lines.append(
- f' semantica:confidence "{confidence}"^^<{CONFIDENCE_DATATYPE}> .'
+ clauses.append(
+ f'semantica:confidence "{confidence}"^^<{CONFIDENCE_DATATYPE}>'
+ )
+ clauses.extend(
+ _turtle_metadata_clauses(
+ _metadata_statements(
+ entity.get("metadata"), metadata_terms, self.logger
+ )
)
+ )
+
- lines.append(f"<{entity_id}> {clauses[0]} ;")
++ entity_iri = self._as_turtle_iri(entity_id, merged_namespaces)
++ lines.append(f"<{entity_iri}> {clauses[0]} ;")
+ for clause in clauses[1:-1]:
+ lines.append(f" {clause} ;")
+ lines.append(f" {clauses[-1]} .")
lines.append("")
# Convert relationships to RDF triplets
@@@ -772,30 -582,6 +846,31 @@@
)
lines.extend(owl_lines)
+ # Graph-level metadata needs a subject, and this serializer has never
+ # minted a document node. Rather than invent one here, it is written
+ # only when the caller names the graph; issue #1147 is where the
+ # default subject comes from once that lands.
+ graph_clauses = (
+ _turtle_metadata_clauses(
+ _metadata_statements(
+ rdf_data.get("metadata"), metadata_terms, self.logger
+ )
+ )
+ if graph_uri
+ else []
+ )
+ if graph_clauses:
++ graph_iri = self._as_turtle_iri(graph_uri, merged_namespaces)
+ lines.append("")
+ lines.append(
- f"<{graph_uri}> {graph_clauses[0]} "
++ f"<{graph_iri}> {graph_clauses[0]} "
+ + (";" if len(graph_clauses) > 1 else ".")
+ )
+ for clause in graph_clauses[1:-1]:
+ lines.append(f" {clause} ;")
+ if len(graph_clauses) > 1:
+ lines.append(f" {graph_clauses[-1]} .")
+
return "\n".join(lines)
def _reified_relationship_triples(
@@@ -951,15 -752,14 +1044,17 @@@
confidence = normalize_confidence(entity.get("confidence", 1.0))
# RDF/XML syntax: rdf:Description with rdf:about
+ # Attribute values are delimited by quotes, and both of these
+ # are caller input. Element text is left alone deliberately: that
+ # is #1098, and it is being fixed on its own path.
- lines.append(
- f' <rdf:Description rdf:about="{_escape_xml(str(entity_id))}">'
+ entity_iri = xml_escape(
+ self._as_turtle_iri(entity_id, namespaces), quote=True
)
- lines.append(
- f' <rdf:type rdf:resource="{_escape_xml(str(entity_type))}"/>'
+ 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(
@@@ -1000,23 -795,6 +1099,26 @@@
lines.append(" </rdf:Description>")
lines.append("")
+ graph_lines = (
+ _rdfxml_metadata_lines(
+ _metadata_statements(
+ rdf_data.get("metadata"), metadata_terms, self.logger
+ ),
+ " ",
+ self.logger,
+ )
+ if graph_uri
+ else []
+ )
+ if graph_lines:
- lines.append(f' <rdf:Description rdf:about="{_escape_xml(graph_uri)}">')
++ graph_iri = xml_escape(
++ self._as_turtle_iri(graph_uri, namespaces), quote=True
++ )
++ lines.append(f' <rdf:Description rdf:about="{graph_iri}">')
+ lines.extend(graph_lines)
+ lines.append(" </rdf:Description>")
+ lines.append("")
+
lines.append("</rdf:RDF>")
return "\n".join(lines)
@@@ -1153,11 -909,10 +1255,13 @@@
Returns:
String containing N-Triples serialization
"""
+ metadata_terms = _resolve_metadata_terms(options.pop("metadata_terms", None))
+ graph_uri: Optional[str] = options.pop("graph_uri", None)
+
lines = []
+ namespaces = self.namespace_manager.extract_namespaces(rdf_data)
+
def expand_uri(uri: str) -> str:
if not uri:
return ""