mirror of
https://github.com/semantica-agi/semantica.git
synced 2026-08-29 04:26:20 +00:00
Add a Cite Us section to the README with BibTeX citation info, and align it with docs/citation.md (author/organization: Semantica, 2026). Update LICENSE and docs/project-license.md copyright holder to Semantica, and replace the stale Hawksight-AI GitHub org slug with semantica-agi across READMEs, plugin manifests, cookbook notebooks, and GitHub templates.
223 lines
6.2 KiB
Plaintext
223 lines
6.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"[](https://colab.research.google.com/github/semantica-agi/semantica/blob/main/cookbook/advanced/09_Semantic_Layer_Construction.ipynb)\n",
|
|
"\n",
|
|
"# Semantic Layer Construction\n",
|
|
"\n",
|
|
"## Overview\n",
|
|
"\n",
|
|
"Build an enterprise semantic layer: construct knowledge graph, generate ontology, create semantic layer, export RDF, and store in triplet store.\n",
|
|
"\n",
|
|
"\n",
|
|
"**Documentation**: [API Reference](https://semantica.readthedocs.io/concepts/)\n",
|
|
"\n",
|
|
"## Installation\n",
|
|
"\n",
|
|
"Install Semantica from PyPI:\n",
|
|
"\n",
|
|
"```bash\n",
|
|
"pip install semantica\n",
|
|
"# Or with all optional dependencies:\n",
|
|
"pip install semantica[all]\n",
|
|
"```\n",
|
|
"\n",
|
|
"## Workflow: Build KG → Generate Ontology → Create Semantic Layer → Export RDF \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"!pip install -qU semantica\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"from semantica.kg import GraphBuilder\n",
|
|
"from semantica.ontology import OntologyGenerator\n",
|
|
"from semantica.export import RDFExporter\n",
|
|
"from semantica.triplet_store import TripletStore\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 1: Build Knowledge Graph\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"builder = GraphBuilder()\n",
|
|
"\n",
|
|
"entities = [\n",
|
|
" {\"id\": \"e1\", \"type\": \"Person\", \"name\": \"Alice\", \"properties\": {\"age\": 30, \"role\": \"Engineer\"}},\n",
|
|
" {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Bob\", \"properties\": {\"age\": 35, \"role\": \"Manager\"}},\n",
|
|
" {\"id\": \"e3\", \"type\": \"Organization\", \"name\": \"Tech Corp\", \"properties\": {\"founded\": 2010}},\n",
|
|
" {\"id\": \"e4\", \"type\": \"Project\", \"name\": \"Project Alpha\", \"properties\": {\"status\": \"active\"}},\n",
|
|
"]\n",
|
|
"\n",
|
|
"relationships = [\n",
|
|
" {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"reports_to\"},\n",
|
|
" {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"works_for\"},\n",
|
|
" {\"source\": \"e2\", \"target\": \"e3\", \"type\": \"works_for\"},\n",
|
|
" {\"source\": \"e1\", \"target\": \"e4\", \"type\": \"works_on\"},\n",
|
|
"]\n",
|
|
"\n",
|
|
"knowledge_graph = builder.build(entities, relationships)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 2: Generate Ontology\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"generator = OntologyGenerator()\n",
|
|
"ontology = generator.generate_from_graph(knowledge_graph)\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 3: Create Semantic Layer\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def create_mappings(kg, ontology):\n",
|
|
" mappings = {\n",
|
|
" \"entity_type_mappings\": {},\n",
|
|
" \"relationship_type_mappings\": {},\n",
|
|
" \"property_mappings\": {}\n",
|
|
" }\n",
|
|
" \n",
|
|
" entity_types = set(e.get(\"type\") for e in entities)\n",
|
|
" ontology_classes = ontology.get(\"classes\", [])\n",
|
|
" \n",
|
|
" for entity_type in entity_types:\n",
|
|
" matching_class = next((cls for cls in ontology_classes if cls.get(\"name\") == entity_type), None)\n",
|
|
" if matching_class:\n",
|
|
" mappings[\"entity_type_mappings\"][entity_type] = matching_class.get(\"uri\", entity_type)\n",
|
|
" \n",
|
|
" relationship_types = set(r.get(\"type\") for r in relationships)\n",
|
|
" ontology_properties = ontology.get(\"properties\", [])\n",
|
|
" \n",
|
|
" for rel_type in relationship_types:\n",
|
|
" matching_prop = next((prop for prop in ontology_properties if prop.get(\"name\") == rel_type), None)\n",
|
|
" if matching_prop:\n",
|
|
" mappings[\"relationship_type_mappings\"][rel_type] = matching_prop.get(\"uri\", rel_type)\n",
|
|
" \n",
|
|
" return mappings\n",
|
|
"\n",
|
|
"mappings = create_mappings(knowledge_graph, ontology)\n",
|
|
"\n",
|
|
"semantic_layer = {\n",
|
|
" \"graph\": knowledge_graph,\n",
|
|
" \"ontology\": ontology,\n",
|
|
" \"mappings\": mappings,\n",
|
|
" \"metadata\": {\n",
|
|
" \"version\": \"1.0\",\n",
|
|
" \"created_at\": \"2024-01-01\",\n",
|
|
" \"description\": \"Enterprise semantic layer\"\n",
|
|
" }\n",
|
|
"}\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Step 4: Export RDF\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"exporter = RDFExporter()\n",
|
|
"# Export Knowledge Graph\n",
|
|
"exporter.export(knowledge_graph, \"knowledge_graph.ttl\", format=\"turtle\")\n",
|
|
"print(\"Exported knowledge graph to knowledge_graph.ttl\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Summary\n",
|
|
"\n",
|
|
"Enterprise semantic layer construction:\n",
|
|
"- Knowledge Graph Built\n",
|
|
"- Ontology Generated\n",
|
|
"- Semantic Layer Created with Mappings\n",
|
|
"- RDF Export Completed\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.9"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|