ITADN

[BUG] serialize_to_turtle interpolates literals unescaped: a quote or newline in entity text emits invalid Turtle

#1098Openfabio-rovai 创建于 10 天前
buggood first issuehelp wanted
F
fabio-rovaicommented
### Summary `RDFSerializer.serialize_to_turtle()` (`semantica/export/rdf_exporter.py:369`) builds the literal with an f-string and no escaping: ```python lines.append(f' semantica:text "{text}" ;') ``` Any `"`, newline, carriage return or backslash in the entity text produces syntactically invalid Turtle. Entity text comes from extraction over free text, so quotes and newlines are ordinary, not edge cases. ### Version semantica 0.6.5, Python 3.13. ### Reproduction ```python from semantica.export.methods import export_rdf kg = {"entities": [{"id": "https://example.org/e3", "text": 'He said "hi"\nthen left', "type": "https://example.org/Person", "confidence": 1.0}], "relationships": []} export_rdf(kg, "out.ttl", format="turtle") ``` ### Actual ```turtle <https://example.org/e3> a <https://example.org/Person> ; semantica:text "He said "hi" then left" ; semantica:confidence 1.0 . ``` Both parsers reject it: - Oxigraph (strict RDF 1.1): `Parser error at line 6 between columns 30 and 32: hi is not a valid subject or graph name` - rdflib 7.x: `BadSyntax ... Bad syntax (objectList expected)` ### Expected Escaped per the Turtle grammar, either as an escaped short string (`\"`, `\n`, `\r`, `\\`) or a triple-quoted long string. ### Suggested fix A single `_escape_literal()` helper applied everywhere a value is interpolated into quotes, or delegate serialization to rdflib, which is already a hard dependency (`rdflib>=6.2.0` in `pyproject.toml`). The same unescaped pattern appears in the OWL-Time branch of `_owl_time_triples_for_rel()` for `time:inXSDDateTimeStamp` values. Related, previously fixed in a different module: #478 (OWLExporter generated invalid Turtle), #448 (BulkLoader), #450. The RDF exporter was not covered by those.
4 条评论