Skip to content

Graph-Native Infrastructure for Context and Accountable AI Systems

Fix Ontology notebook and related library bugs (missing entities, punctuation handling, imports key, version recursion)

KaifAhmad1ea6cfdf6a887
diff --git a/cookbook/introduction/14_Ontology.ipynb b/cookbook/introduction/14_Ontology.ipynb
index 747152b7..5b957edf 100644
--- a/cookbook/introduction/14_Ontology.ipynb
+++ b/cookbook/introduction/14_Ontology.ipynb
@@ -73,7 +73,7 @@
"\n",
"# Initialize the Engine\n",
"# base_uri defines the namespace root for your ontology\n",
- "engine = OntologyEngine(base_uri=\"https://docs.semantica.dev/ontology/\")\n",
+ "engine = OntologyEngine(base_uri=\"https://docs.semantica.dev/ontology/\", min_occurrences=1)\n",
"\n",
"print(\"Ontology Engine initialized successfully!\")"
]
@@ -106,7 +106,7 @@
{
"data": {
"text/html": [
- "<div style='font-family: monospace;'><h4>🧠 Semantica - πŸ“Š Current Progress</h4><table style='width: 100%; border-collapse: collapse;'><tr><th>Status</th><th>Action</th><th>Module</th><th>Submodule</th><th>File</th><th>Time</th></tr><tr><td>πŸ”„</td><td>Semantica is generating</td><td>πŸ“š ontology</td><td>OntologyEngine</td><td>-</td><td>53.81s</td></tr><tr><td>βœ…</td><td>Semantica is generating</td><td>πŸ“š ontology</td><td>OntologyGenerator</td><td>-</td><td>0.03s</td></tr><tr><td>βœ…</td><td>Semantica is generating</td><td>πŸ“š ontology</td><td>ClassInferrer</td><td>-</td><td>0.00s</td></tr><tr><td>βœ…</td><td>Semantica is generating</td><td>πŸ“š ontology</td><td>PropertyGenerator</td><td>-</td><td>0.00s</td></tr><tr><td>βœ…</td><td>Semantica is visualizing</td><td>πŸ“ˆ visualization</td><td>OntologyVisualizer</td><td>-</td><td>0.15s</td></tr><tr><td>πŸ”„</td><td>Semantica is generating</td><td>πŸ“š ontology</td><td>LLMOntologyGenerator</td><td>-</td><td>18.17s</td></tr><tr><td>βœ…</td><td>Semantica is generating</td><td>πŸ“š ontology</td><td>CompetencyQuestionsManager</td><td>-</td><td>0.00s</td></tr><tr><td>βœ…</td><td>Semantica is generating</td><td>πŸ“š ontology</td><td>VersionManager</td><td>-</td><td>0.01s</td></tr><tr><td>βœ…</td><td>Semantica is generating</td><td>πŸ“š ontology</td><td>ReuseManager</td><td>-</td><td>0.00s</td></tr><tr><td>βœ…</td><td>Semantica is generating</td><td>πŸ“š ontology</td><td>OWLGenerator</td><td>-</td><td>0.01s</td></tr></table></div>"
+ "<div style='font-family: monospace;'><h4>\ud83e\udde0 Semantica - \ud83d\udcca Current Progress</h4><table style='width: 100%; border-collapse: collapse;'><tr><th>Status</th><th>Action</th><th>Module</th><th>Submodule</th><th>File</th><th>Time</th></tr><tr><td>\ud83d\udd04</td><td>Semantica is generating</td><td>\ud83d\udcda ontology</td><td>OntologyEngine</td><td>-</td><td>53.81s</td></tr><tr><td>\u2705</td><td>Semantica is generating</td><td>\ud83d\udcda ontology</td><td>OntologyGenerator</td><td>-</td><td>0.03s</td></tr><tr><td>\u2705</td><td>Semantica is generating</td><td>\ud83d\udcda ontology</td><td>ClassInferrer</td><td>-</td><td>0.00s</td></tr><tr><td>\u2705</td><td>Semantica is generating</td><td>\ud83d\udcda ontology</td><td>PropertyGenerator</td><td>-</td><td>0.00s</td></tr><tr><td>\u2705</td><td>Semantica is visualizing</td><td>\ud83d\udcc8 visualization</td><td>OntologyVisualizer</td><td>-</td><td>0.15s</td></tr><tr><td>\ud83d\udd04</td><td>Semantica is generating</td><td>\ud83d\udcda ontology</td><td>LLMOntologyGenerator</td><td>-</td><td>18.17s</td></tr><tr><td>\u2705</td><td>Semantica is generating</td><td>\ud83d\udcda ontology</td><td>CompetencyQuestionsManager</td><td>-</td><td>0.00s</td></tr><tr><td>\u2705</td><td>Semantica is generating</td><td>\ud83d\udcda ontology</td><td>VersionManager</td><td>-</td><td>0.01s</td></tr><tr><td>\u2705</td><td>Semantica is generating</td><td>\ud83d\udcda ontology</td><td>ReuseManager</td><td>-</td><td>0.00s</td></tr><tr><td>\u2705</td><td>Semantica is generating</td><td>\ud83d\udcda ontology</td><td>OWLGenerator</td><td>-</td><td>0.01s</td></tr></table></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
@@ -132,14 +132,25 @@
" {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Alice\", \"role\": \"CEO\"},\n",
" {\"id\": \"e3\", \"type\": \"Person\", \"name\": \"Bob\", \"role\": \"CTO\"},\n",
" {\"id\": \"e4\", \"type\": \"Department\", \"name\": \"Engineering\"},\n",
- " {\"id\": \"e5\", \"type\": \"Project\", \"name\": \"Project Phoenix\"}\n",
+ " {\"id\": \"e5\", \"type\": \"Project\", \"name\": \"Project Phoenix\"},\n",
+ " # Added entities to ensure robust class inference\n",
+ " {\"id\": \"e6\", \"type\": \"Company\", \"name\": \"TechSolutions\", \"founded\": \"2015\"},\n",
+ " {\"id\": \"e7\", \"type\": \"Person\", \"name\": \"Charlie\", \"role\": \"Lead\"},\n",
+ " {\"id\": \"e8\", \"type\": \"Person\", \"name\": \"Dave\", \"role\": \"Manager\"},\n",
+ " {\"id\": \"e9\", \"type\": \"Department\", \"name\": \"Research\"},\n",
+ " {\"id\": \"e10\", \"type\": \"Project\", \"name\": \"Project Orion\"}\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"e2\", \"target\": \"e1\", \"type\": \"leads\"},\n",
" {\"source\": \"e3\", \"target\": \"e4\", \"type\": \"manages\"},\n",
" {\"source\": \"e4\", \"target\": \"e1\", \"type\": \"part_of\"},\n",
- " {\"source\": \"e3\", \"target\": \"e5\", \"type\": \"works_on\"}\n",
+ " {\"source\": \"e3\", \"target\": \"e5\", \"type\": \"works_on\"},\n",
+ " # Additional relationships for new entities\n",
+ " {\"source\": \"e7\", \"target\": \"e6\", \"type\": \"leads\"},\n",
+ " {\"source\": \"e8\", \"target\": \"e9\", \"type\": \"manages\"},\n",
+ " {\"source\": \"e9\", \"target\": \"e6\", \"type\": \"part_of\"},\n",
+ " {\"source\": \"e7\", \"target\": \"e10\", \"type\": \"works_on\"}\n",
"]\n",
"\n",
"data = {\n",
@@ -3283,8 +3294,8 @@
"output_type": "stream",
"text": [
"Answerable Questions: 0 / 2\n",
- "❌ Who leads TechCorp?\n",
- "❌ Which projects does Bob manage?\n"
+ "\u274c Who leads TechCorp?\n",
+ "\u274c Which projects does Bob manage?\n"
]
}
],
@@ -3302,7 +3313,7 @@
"\n",
"print(f\"Answerable Questions: {validation_results['answerable']} / {validation_results['total_questions']}\")\n",
"for q in cq_manager.questions:\n",
- " status = \"βœ…\" if q.answerable else \"❌\"\n",
+ " status = \"\u2705\" if q.answerable else \"\u274c\"\n",
" print(f\"{status} {q.question}\")"
]
},
@@ -3456,4 +3467,4 @@
},
"nbformat": 4,
"nbformat_minor": 5
-}
+}
\ No newline at end of file
diff --git a/semantica/ontology/competency_questions.py b/semantica/ontology/competency_questions.py
index 24b5f299..65278054 100644
--- a/semantica/ontology/competency_questions.py
+++ b/semantica/ontology/competency_questions.py
@@ -239,16 +239,21 @@ class CompetencyQuestionsManager:
self, ontology: Dict[str, Any], question: CompetencyQuestion
) -> bool:
"""Check if ontology can answer the question (basic heuristic)."""
- # Extract keywords from question
- question_lower = question.question.lower()
+ # Helper to clean text
+ def clean_text(text):
+ import string
+ return text.translate(str.maketrans("", "", string.punctuation))
+ # Extract keywords from question
+ question_clean = clean_text(question.question.lower())
+
# Check if ontology has relevant classes
classes = ontology.get("classes", [])
for cls in classes:
class_name_lower = cls.get("name", "").lower()
if any(
word in class_name_lower
- for word in question_lower.split()
+ for word in question_clean.split()
if len(word) > 3
):
return True
@@ -259,7 +264,7 @@ class CompetencyQuestionsManager:
prop_name_lower = prop.get("name", "").lower()
if any(
word in prop_name_lower
- for word in question_lower.split()
+ for word in question_clean.split()
if len(word) > 3
):
return True
@@ -290,8 +295,9 @@ class CompetencyQuestionsManager:
```
"""
elements = []
- question_lower = question.question.lower()
- keywords = [w for w in question_lower.split() if len(w) > 3]
+ import string
+ question_clean = question.question.lower().translate(str.maketrans("", "", string.punctuation))
+ keywords = [w for w in question_clean.split() if len(w) > 3]
# Find relevant classes
classes = ontology.get("classes", [])
diff --git a/semantica/ontology/ontology_generator.py b/semantica/ontology/ontology_generator.py
index 74562194..f1147619 100644
--- a/semantica/ontology/ontology_generator.py
+++ b/semantica/ontology/ontology_generator.py
@@ -462,6 +462,7 @@ class OntologyGenerator:
"version": self.namespace_manager.version,
"classes": classes,
"properties": properties,
+ "imports": [],
"metadata": {
**typed_definitions.get("metadata", {}),
"class_count": len(classes),
diff --git a/semantica/ontology/version_manager.py b/semantica/ontology/version_manager.py
index b5dfff97..55463da5 100644
--- a/semantica/ontology/version_manager.py
+++ b/semantica/ontology/version_manager.py
@@ -165,7 +165,10 @@ class VersionManager:
base_uri = base_uri.rstrip("/")
# Add version to IRI
- return f"{base_uri}/v{version}"
+ suffix = f"/v{version}"
+ if base_uri.endswith(suffix):
+ return base_uri
+ return f"{base_uri}{suffix}"
def generate_element_iri(
self, element_name: str, element_type: str = "class"