{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Multi-Format Export\n", "\n", "## Overview\n", "\n", "Export knowledge graphs and data to multiple formats: JSON, RDF, CSV, Graph formats, OWL, and Vector formats.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import (\n", " JSONExporter,\n", " RDFExporter,\n", " CSVExporter,\n", " GraphExporter,\n", " OWLExporter,\n", " VectorExporter\n", ")\n", "from semantica.kg import GraphBuilder\n", "from semantica.embeddings import EmbeddingGenerator\n", "from semantica.ontology import OntologyGenerator\n", "import os\n", "\n", "os.makedirs(\"exports\", exist_ok=True)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Create Sample Knowledge Graph and Data\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}},\n", " {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Bob\", \"properties\": {\"age\": 35}},\n", " {\"id\": \"e3\", \"type\": \"Organization\", \"name\": \"Tech Corp\", \"properties\": {\"founded\": 2010}},\n", "]\n", "\n", "relationships = [\n", " {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"knows\"},\n", " {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"works_for\"},\n", "]\n", "\n", "knowledge_graph = builder.build(entities, relationships)\n", "\n", "embedding_generator = EmbeddingGenerator()\n", "texts = [e[\"name\"] for e in entities]\n", "embeddings = embedding_generator.generate(texts)\n", "\n", "ontology_generator = OntologyGenerator()\n", "ontology = ontology_generator.generate_from_graph(knowledge_graph)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 2: Export to JSON\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "json_exporter = JSONExporter()\n", "json_exporter.export(knowledge_graph, \"exports/output.json\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Export to RDF\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "rdf_exporter = RDFExporter()\n", "rdf_exporter.export(knowledge_graph, \"exports/output.rdf\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4: Export to CSV\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "csv_exporter = CSVExporter()\n", "csv_exporter.export(knowledge_graph, \"exports/output.csv\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 5: Export to Graph Formats (GraphML, GEXF)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "graph_exporter = GraphExporter()\n", "graph_exporter.export(knowledge_graph, \"exports/output.graphml\", format=\"graphml\")\n", "graph_exporter.export(knowledge_graph, \"exports/output.gexf\", format=\"gexf\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 6: Export to OWL\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "owl_exporter = OWLExporter()\n", "owl_exporter.export(ontology, \"exports/output.owl\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 7: Export to Vector Formats\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "vector_exporter = VectorExporter()\n", "vector_exporter.export(embeddings, \"exports/output.vectors\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "Export formats:\n", "- JSON\n", "- RDF\n", "- CSV\n", "- GraphML\n", "- GEXF\n", "- OWL\n", "- Vector format\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "export_files = [\n", " \"exports/output.json\",\n", " \"exports/output.rdf\",\n", " \"exports/output.csv\",\n", " \"exports/output.graphml\",\n", " \"exports/output.gexf\",\n", " \"exports/output.owl\",\n", " \"exports/output.vectors\"\n", "]\n", "\n", "for file in export_files:\n", " if os.path.exists(file):\n", " size = os.path.getsize(file)\n", " print(f\"{file} ({size} bytes)\")\n" ] } ], "metadata": { "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 2 }