Spaces:
Sleeping
Sleeping
RDF Validation Deployment
commited on
Commit
Β·
d344469
1
Parent(s):
3804b9b
Fix RDF property extraction: use OWL property types instead of invalid RDFS.Property - 2025-10-04 15:27:47
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ import gradio as gr
|
|
| 16 |
import json
|
| 17 |
import requests
|
| 18 |
from typing import Dict, List, Optional, Any
|
| 19 |
-
from rdflib import Graph, URIRef, Literal, Namespace, RDFS, RDF
|
| 20 |
from functools import lru_cache
|
| 21 |
import re
|
| 22 |
|
|
@@ -93,27 +93,33 @@ class BIBFRAMEKnowledgeBase:
|
|
| 93 |
|
| 94 |
print(f"π Found {len(bf_subjects)} BIBFRAME subjects total")
|
| 95 |
|
| 96 |
-
# Extract properties
|
| 97 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
|
| 99 |
-
|
| 100 |
-
for prop_type in property_types:
|
| 101 |
prop_count = 0
|
| 102 |
for prop in self.ontology_graph.subjects(RDF.type, prop_type):
|
| 103 |
if str(prop).startswith(str(BF)):
|
| 104 |
local_name = str(prop).replace(str(BF), "")
|
| 105 |
-
|
| 106 |
-
"
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
|
|
|
| 115 |
|
| 116 |
-
#
|
| 117 |
print("π Looking for properties with domain/range...")
|
| 118 |
domain_props = set()
|
| 119 |
for prop in self.ontology_graph.subjects(RDFS.domain, None):
|
|
@@ -139,40 +145,24 @@ class BIBFRAMEKnowledgeBase:
|
|
| 139 |
"subPropertyOf": self._get_super_properties(prop)
|
| 140 |
}
|
| 141 |
|
| 142 |
-
#
|
| 143 |
-
from rdflib import OWL
|
| 144 |
-
owl_property_types = [OWL.ObjectProperty, OWL.DatatypeProperty, OWL.AnnotationProperty]
|
| 145 |
-
for prop_type in owl_property_types:
|
| 146 |
-
try:
|
| 147 |
-
for prop in self.ontology_graph.subjects(RDF.type, prop_type):
|
| 148 |
-
if str(prop).startswith(str(BF)):
|
| 149 |
-
local_name = str(prop).replace(str(BF), "")
|
| 150 |
-
if f"bf:{local_name}" not in self.properties:
|
| 151 |
-
self.properties[f"bf:{local_name}"] = {
|
| 152 |
-
"uri": str(prop),
|
| 153 |
-
"label": self._get_label(prop),
|
| 154 |
-
"definition": self._get_comment(prop),
|
| 155 |
-
"domain": self._get_domains(prop),
|
| 156 |
-
"range": self._get_ranges(prop),
|
| 157 |
-
"subPropertyOf": self._get_super_properties(prop)
|
| 158 |
-
}
|
| 159 |
-
except:
|
| 160 |
-
pass # OWL might not be available
|
| 161 |
-
|
| 162 |
-
# Extract classes
|
| 163 |
print("π Looking for classes...")
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
"
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
|
| 177 |
self._loaded = True
|
| 178 |
total_props = len(self.properties)
|
|
|
|
| 16 |
import json
|
| 17 |
import requests
|
| 18 |
from typing import Dict, List, Optional, Any
|
| 19 |
+
from rdflib import Graph, URIRef, Literal, Namespace, RDFS, RDF, OWL
|
| 20 |
from functools import lru_cache
|
| 21 |
import re
|
| 22 |
|
|
|
|
| 93 |
|
| 94 |
print(f"π Found {len(bf_subjects)} BIBFRAME subjects total")
|
| 95 |
|
| 96 |
+
# Extract properties using OWL property types (BIBFRAME uses OWL, not RDFS)
|
| 97 |
+
print("π Extracting OWL properties...")
|
| 98 |
+
owl_property_types = [
|
| 99 |
+
(OWL.ObjectProperty, "ObjectProperty"),
|
| 100 |
+
(OWL.DatatypeProperty, "DatatypeProperty"),
|
| 101 |
+
(OWL.AnnotationProperty, "AnnotationProperty"),
|
| 102 |
+
(RDF.Property, "RDF.Property")
|
| 103 |
+
]
|
| 104 |
|
| 105 |
+
for prop_type, type_name in owl_property_types:
|
|
|
|
| 106 |
prop_count = 0
|
| 107 |
for prop in self.ontology_graph.subjects(RDF.type, prop_type):
|
| 108 |
if str(prop).startswith(str(BF)):
|
| 109 |
local_name = str(prop).replace(str(BF), "")
|
| 110 |
+
if f"bf:{local_name}" not in self.properties:
|
| 111 |
+
self.properties[f"bf:{local_name}"] = {
|
| 112 |
+
"uri": str(prop),
|
| 113 |
+
"label": self._get_label(prop),
|
| 114 |
+
"definition": self._get_comment(prop),
|
| 115 |
+
"domain": self._get_domains(prop),
|
| 116 |
+
"range": self._get_ranges(prop),
|
| 117 |
+
"subPropertyOf": self._get_super_properties(prop)
|
| 118 |
+
}
|
| 119 |
+
prop_count += 1
|
| 120 |
+
print(f"π Found {prop_count} properties of type {type_name}")
|
| 121 |
|
| 122 |
+
# Also extract properties that have domain/range but might not have explicit type
|
| 123 |
print("π Looking for properties with domain/range...")
|
| 124 |
domain_props = set()
|
| 125 |
for prop in self.ontology_graph.subjects(RDFS.domain, None):
|
|
|
|
| 145 |
"subPropertyOf": self._get_super_properties(prop)
|
| 146 |
}
|
| 147 |
|
| 148 |
+
# Extract classes (try both OWL.Class and RDFS.Class)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
print("π Looking for classes...")
|
| 150 |
+
class_types = [(OWL.Class, "OWL.Class"), (RDFS.Class, "RDFS.Class")]
|
| 151 |
+
|
| 152 |
+
for class_type, type_name in class_types:
|
| 153 |
+
class_count = 0
|
| 154 |
+
for cls in self.ontology_graph.subjects(RDF.type, class_type):
|
| 155 |
+
if str(cls).startswith(str(BF)):
|
| 156 |
+
local_name = str(cls).replace(str(BF), "")
|
| 157 |
+
if f"bf:{local_name}" not in self.classes:
|
| 158 |
+
self.classes[f"bf:{local_name}"] = {
|
| 159 |
+
"uri": str(cls),
|
| 160 |
+
"label": self._get_label(cls),
|
| 161 |
+
"definition": self._get_comment(cls),
|
| 162 |
+
"subClassOf": self._get_super_classes(cls)
|
| 163 |
+
}
|
| 164 |
+
class_count += 1
|
| 165 |
+
print(f"π Found {class_count} classes of type {type_name}")
|
| 166 |
|
| 167 |
self._loaded = True
|
| 168 |
total_props = len(self.properties)
|