ontocast.tool.facts_validation.literal_repair¶
LLM-free parse-time repairs on rendered facts graphs.
Every rewrite here either retypes a literal the schema grounds, resolves an unambiguous near-miss or code, or collapses a degenerate encoding — no repair invents a value.
Attributes¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
Functions:¶
dedupe_literal_variants(graph, fact_namespaces=None)
¶
Collapse duplicate literals differing only in language tag or datatype.
The renderer emits the same value inconsistently across chunks —
"X"@en in one unit, "X"^^xsd:string in another, a plain "X"
in a third — and after aggregation one (subject, predicate) carries
all three as distinct RDF terms. One survives per lexical form: the
language-tagged form (each distinct language kept — those are distinct
assertions), else the plain form, else the xsd:string form. Reified
provenance follows the survivor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
Aggregated facts graph, mutated in place. |
required |
fact_namespaces
|
Sequence[str] | None
|
When set, only subjects under these namespaces are touched. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
One |
list[GraphRepairRecord]
|
|
Source code in ontocast/tool/facts_validation/literal_repair.py
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 | |
literal_ranges(ontology_graph, catalog_roles=None)
¶
Each predicate with one declared retypable datatype range, with it.
Read from the unit's ontology_graph, the whole catalog
(catalog_roles, memoised there with the well-known vocabularies'
ranges) and the well-known vocabularies, so a property the unit's snapshot
did not retrieve is still known. A predicate declared with two different
datatypes anywhere is left out: no single value fits both. Ranges
inherited through rdfs:subPropertyOf are not followed.
Source code in ontocast/tool/facts_validation/literal_repair.py
materialize_entailed_types(graph, ontology_graph)
¶
Assert the types the ontology context entails on the graph's nodes.
For every triple x p y: when the context declares p rdfs:domain D
with D a named class, x a D is added; when it declares
p rdfs:range R with R a named class and y an IRI, y a R
is added. owl:Thing and the other vacuous classes are never asserted,
nor is a type on a node the context itself types (a catalog individual).
A type is added only where it is consistent with what the node already
asserts: when the node has no type, or when the entailed class
specialises one of its asserted types. Nothing is added when the entailed
class is already in the asserted types' superclass closure (redundant),
or when every asserted type is unrelated to it -- that contradiction is
the DOMAIN_VIOLATION finding's to report, and materialising the
domain there would hide it.
An IRI still untyped after that takes the class a link lists through
schema:domainIncludes / schema:rangeIncludes, when the property
lists exactly one class and declares no rdfs counterpart, and all of
the node's links agree on it. A listed class never joins an asserted type
and never types a blank node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
Facts graph, repaired in place. |
required |
ontology_graph
|
RDFGraph | None
|
Ontology context declaring domains and ranges. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, list[GraphRepairRecord]]
|
Tuple of (number of added triples, applied-repair records). |
Source code in ontocast/tool/facts_validation/literal_repair.py
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 | |
normalize_literals_against_schema(graph, ontology_graph, catalog_roles=None)
¶
Retype literals whose predicate declares a compatible datatype range.
Applies :func:schema_retypings to graph in place.
Returns:
| Type | Description |
|---|---|
int
|
Number of retyped literals. |
Source code in ontocast/tool/facts_validation/literal_repair.py
partition_literals_outside_temporal_ranges(graph, ontology_graph, catalog_roles=None)
¶
Split off :func:temporal_range_mismatches, quarantined for the critic.
Returns:
| Type | Description |
|---|---|
tuple[RDFGraph, list[RejectedLiteralTriple]]
|
Tuple of (clean graph, quarantined triples). |
Source code in ontocast/tool/facts_validation/literal_repair.py
promote_degenerate_bounds(graph, *, numeric_value_property, lower_bound_property, upper_bound_property, inclusive_flag_properties=())
¶
Rewrite equal lower/upper bounds into a single scalar value, in place.
A node whose lower and upper bounds carry the same canonical numeric value encodes an exact scalar as a fake range. The rewrite fires only when the encoding is unambiguous: exactly one literal per bound property, equal canonical values, no existing scalar on the node, and no exclusive-bound flag (an exclusive equal bound denotes an empty interval — malformed, and left for findings). Property IRIs are injected by the caller from configuration; nothing is hardcoded.
Returns:
| Type | Description |
|---|---|
int
|
Number of nodes rewritten. |
Source code in ontocast/tool/facts_validation/literal_repair.py
promote_degenerate_bounds_from_vocabulary(graph, ontology_graph, vocabulary)
¶
Run :func:promote_degenerate_bounds with properties from configuration.
Active only when the quantity vocabulary names all three roles —
numeric_value, lower_bound, upper_bound (roles containing
inclusive supply the optional bound flags). The default vocabulary
carries no bound roles, so this is off unless a deployment configures its
range encoding.
Source code in ontocast/tool/facts_validation/literal_repair.py
repair_compact_iri_literals(graph, ontology_context_graph, known_terms)
¶
Coerce plain-string compact IRIs on IRI-valued positions into IRIs.
A JSON-LD bare string ("qudt:unit": "unit:NanoM") parses as a literal,
so the node loses its link and reads as missing the property. A literal is
rewritten only when every condition holds: it is plain (no language tag,
no datatype or xsd:string); the predicate is not rdf:type (that is
:func:repair_literal_type_objects); the lexical form is a compact IRI
whose prefix the graph or the known-prefix context binds; and either the
schema says the predicate takes an IRI or the expanded IRI is a known term.
Prose such as "time: 10 minutes" never matches the compact-IRI form.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
Facts graph, repaired in place. |
required |
ontology_context_graph
|
RDFGraph
|
Schema that says which predicates take IRIs. |
required |
known_terms
|
set[str]
|
IRIs the catalog and fallback vocabulary declare; covers predicates whose range the vendored vocabulary leaves undeclared. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, list[GraphRepairRecord]]
|
Tuple of (number of rewritten triples, applied-repair records). |
Source code in ontocast/tool/facts_validation/literal_repair.py
repair_literal_type_objects(graph)
¶
Coerce literal rdf:type objects into IRIs.
The renderer sometimes emits a "prefix:Class"^^xsd:string instead of
a prefix:Class (JSON-LD bare-string type values parse the same way).
A literal-typed node is invisible to SPARQL class queries, reasoning, and
the aggregator's URI minting/entity matching, all of which guard on
isinstance(obj, URIRef). Absolute IRIs and compact IRIs bound in the
graph are rewritten deterministically; unresolvable forms become MANDATORY
findings.
Returns:
| Type | Description |
|---|---|
int
|
Tuple of (number of rewritten triples, unresolved findings, |
list[FactsUnitFinding]
|
applied-repair records). |
Source code in ontocast/tool/facts_validation/literal_repair.py
repair_paired_object_properties(graph)
¶
Move a node off a datatype property onto the object property paired with it.
A well-known vocabulary can state one relation twice, once for a literal
and once for a node (sosa:hasSimpleResult / sosa:hasResult,
:func:~ontocast.onto.well_known.well_known_object_pairs). A node written
on the datatype one is rewritten to the object one. A reifier follows its
statement.
Returns:
| Type | Description |
|---|---|
tuple[int, list[GraphRepairRecord]]
|
Tuple of (number of triples moved, applied-repair records). |
Source code in ontocast/tool/facts_validation/literal_repair.py
repair_property_aliases(graph, ontology_graph, *, min_ratio=0.95, exempt_terms=None, full_catalog_terms=None, catalog_roles=None)
¶
Rewrite near-miss predicates in catalog namespaces; report ambiguity.
A predicate that is not a catalog term and not a real term of a
well-known vocabulary (:func:~ontocast.onto.well_known.is_well_known_term)
exists nowhere. When exactly one catalog property (catalog_roles, the
whole catalog, else the snapshot) in another namespace has its very local
name, the prefix slipped and that property is the target; this holds for
a predicate in a catalog namespace or a well-known one. A predicate in a
namespace the catalog declares terms in is otherwise a near-miss
(ex:lowerBound for ex:hasLowerBound), and the rewrite fires only when
exactly one candidate qualifies: its name tokens contain the alias's,
the alias's contain its, or the two spell the same once case and
separators are folded. Several qualifying candidates are a tie that the
SequenceMatcher ratio may break -- the best-scoring one wins when it
clears min_ratio and nothing ties it. Similarity alone never licenses
a rewrite: a high ratio also joins names that differ by exactly one token,
and rewriting across that gap substitutes one property for another.
Anything else becomes a mandatory finding carrying the top suggestions.
A class written as an rdf:type object follows the same exact-name
rule: one that exists nowhere is rewritten to the catalog class, in
another namespace, with exactly its local name -- the only one, or among
several the one that is a subclass of every other (class_alias).
Only namespaces the catalog declares terms in are eligible for the
near-miss rewrite (see :func:collect_declared_namespaces); exempt_terms (expanded fallback
vocabulary) are never treated as near-misses, and neither is a predicate
in full_catalog_terms: a term the whole catalog declares is real even
when this unit's snapshot did not retrieve it, and the snapshot's
look-alike is then a different property, not the intended one. Such a
predicate is left untouched here; whether it is reported is the term
checks' business.
Returns:
| Type | Description |
|---|---|
int
|
Tuple of (number of rewritten triples, unresolved findings, |
list[FactsUnitFinding]
|
applied-repair records). |
Source code in ontocast/tool/facts_validation/literal_repair.py
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 | |
repair_reversed_triples(graph, ontology_graph)
¶
Turn around a triple the context shows was written backwards.
x p y becomes y p x when p has a domain and a class range,
both ends are typed IRIs, x is outside the domain and y outside the
range as written, and swapped both fit. One end fitting, or a type the
context cannot place, leaves the triple as it is: the subject may be
mistyped, or the property wrong, and neither is a direction. The sides are
read from rdfs and from listed (schema:*Includes) classes alike:
a listed class contradicts nothing on its own, but evidence at all four
positions is a direction. A symmetric property is never turned. A reifier
follows its statement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
Facts graph, repaired in place. |
required |
ontology_graph
|
RDFGraph | None
|
Ontology context declaring domains and ranges. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, list[GraphRepairRecord]]
|
Tuple of (number of triples turned, applied-repair records). |
Source code in ontocast/tool/facts_validation/literal_repair.py
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 | |
resolve_code_literals(graph, ontology_graph, code_predicates=())
¶
Link nodes to the catalog individual whose code they already carry.
A renderer that reads 4-15 days often annotates the value node with the
code it saw — qudt:ucumCode "d" — instead of the object property that
points at the individual — qudt:unit unit:DAY. The graph is well-formed,
so no range check fires, but every query reading the object property gets
an unbound result. The code came from the text and the individual is in the
catalog, so the link is recoverable without asking the model again.
Fully schema-driven, no vocabulary compiled in: the connecting property is whichever object property the ontology context declares with a range the resolved individual is typed as, and a domain the subject satisfies. If the schema offers several such properties, or none, nothing is added.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
graph
|
RDFGraph
|
Rendered facts graph, repaired in place. |
required |
ontology_graph
|
RDFGraph | None
|
Merged ontology context, read-only. |
required |
code_predicates
|
Sequence[str]
|
Predicates carrying machine-resolvable codes. |
()
|
Returns:
| Type | Description |
|---|---|
tuple[int, list[GraphRepairRecord]]
|
Tuple of (number of added triples, applied-repair records). |
Source code in ontocast/tool/facts_validation/literal_repair.py
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 | |
schema_retypings(graph, ontology_graph, catalog_roles=None)
¶
Literals whose predicate declares a compatible datatype range, retyped.
Fixes the qudt:numericValue 230 vs "230"^^xsd:decimal drift at parse
time, and the same drift for the date, time and duration datatypes: when
the range (:func:literal_ranges) is in :data:_RETYPABLE_RANGE_DATATYPES
and the lexical form is a value of it, the literal is rewritten with it. A
calendar text that names exactly one value of the range ("29 June
2021" on an xsd:date range) is rewritten to that value.
A literal is only retyped from an untyped, xsd:string, or numeric source
-- a string range must never be able to clobber a correctly typed value --
and language-tagged literals are left alone, since they are
rdf:langString and retyping would discard the tag.
Reads graph only.
Returns:
| Type | Description |
|---|---|
list[tuple[Triple, Triple]]
|
Each retyped triple with its replacement. |
Source code in ontocast/tool/facts_validation/literal_repair.py
temporal_range_mismatches(graph, ontology_graph, catalog_roles=None)
¶
Text on a date, time or duration range that is not a value of it.
Run after :func:normalize_literals_against_schema, so a text that is such
a value has already been retyped: what is left untyped, xsd:string or
language-tagged on a predicate whose range (:func:literal_ranges) is a
temporal datatype is not one -- a recurrence or a phrase where a date
belongs. Numeric ranges are left to the numeric checks.
Returns:
| Type | Description |
|---|---|
list[tuple[tuple[Node, Node, Node], RejectedLiteralTriple]]
|
Each such triple with its quarantine record ( |
list[tuple[tuple[Node, Node, Node], RejectedLiteralTriple]]
|
names the range). |
Source code in ontocast/tool/facts_validation/literal_repair.py
typed_literal(lexical, datatype)
¶
lexical as a well-formed literal of datatype, or None.
The one constructor the retypes share. A number, date, time or duration is
judged by its XSD lexical space (see :func:_typed_value); another
datatype by whether rdflib reads a value from the form.
Source code in ontocast/tool/facts_validation/literal_repair.py
unresolved_quarantine(graph, items)
¶
The quarantined triples graph has not since made good.
One is resolved when it was written back, when its subject now states its predicate with a new value of the kind it was set aside for lacking (an IRI for an object property, a valid literal of the expected or declared datatype), or when the subject states its text under another predicate.
Source code in ontocast/tool/facts_validation/literal_repair.py
write_back_temporal_quarantine(graph, items)
¶
Put back, as rdfs:comment, date text the unit still holds aside.
A text set aside for not being a value of a date, time or duration
datatype -- written plain on such a range, or typed with such a datatype
it is not a value of -- and not since resolved (:func:unresolved_quarantine)
is kept verbatim on its subject, so nothing is lost and no false date is
stated. A subject the graph no longer names, in either position, gets
nothing: a comment would make a node of a comment alone. Quarantined strings on object properties
and bad numbers are not written back.
Returns:
| Type | Description |
|---|---|
list[GraphRepairRecord]
|
One |
list[RejectedLiteralTriple]
|
|