{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/05_Multi_Format_Export.ipynb)\n", "\n", "# Advanced Multi-Format Export\n", "\n", "## Overview\n", "\n", "This advanced notebook demonstrates comprehensive export capabilities of Semantica's Export Module, covering all **8 export formats** plus report generation. You'll learn to export the same knowledge graph to multiple formats simultaneously, use advanced features, and leverage the method registry system.\n", "\n", "**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/export/)\n", "\n", "### What You'll Learn\n", "\n", "- Export knowledge graphs to all 8 supported formats\n", "- Use exporter classes directly for fine-grained control\n", "- Generate professional reports in multiple formats\n", "- Work with RDF serialization, validation, and namespace management\n", "- Register and use custom export methods\n", "- Configure export settings programmatically\n", "- Export vectors and embeddings for vector stores\n", "- Export to graph databases using LPG format\n", "\n", "### Export Formats Covered\n", "\n", "1. **JSON/JSON-LD** - Standard JSON and JSON-LD formats\n", "2. **RDF** - Turtle, RDF/XML, JSON-LD, N-Triples, N3\n", "3. **CSV** - Tabular format for entities and relationships\n", "4. **Graph Formats** - GraphML, GEXF, DOT for visualization tools\n", "5. **OWL** - OWL/XML and Turtle for ontologies\n", "6. **Vector** - JSON, NumPy, Binary, FAISS for vector stores\n", "7. **LPG** - Cypher and LPG for graph databases\n", "8. **YAML** - Semantic network and schema YAML\n", "9. **Reports** - HTML, Markdown, JSON, Text reports\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" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "!pip install semantica\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import core modules for building knowledge graph\n", "from semantica.kg import GraphBuilder\n", "from semantica.embeddings import EmbeddingGenerator\n", "from semantica.ontology import OntologyGenerator\n", "import os\n", "\n", "# Create exports directory\n", "os.makedirs(\"exports\", exist_ok=True)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 1: Create Sample Knowledge Graph and Data\n", "\n", "Create a sample knowledge graph with entities, relationships, embeddings, and an ontology for comprehensive export demonstrations.\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_embeddings(texts, data_type=\"text\")\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", "\n", "Export knowledge graph to JSON format using both the class and convenience function approaches.\n", "\n", "**JSONExporter Features:**\n", "- Standard JSON serialization\n", "- JSON-LD format support with @context\n", "- Configurable indentation\n", "- Metadata and provenance tracking\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import JSONExporter\n", "\n", "# Create JSON exporter with custom settings\n", "json_exporter = JSONExporter(indent=2, include_metadata=True)\n", "\n", "# Export to JSON format\n", "json_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.json\")\n", "\n", "# Export to JSON-LD format\n", "json_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.jsonld\", format=\"json-ld\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 3: Export to RDF\n", "\n", "Export knowledge graph to multiple RDF formats (Turtle, RDF/XML, JSON-LD, N-Triples).\n", "\n", "**RDFExporter Features:**\n", "- Multiple RDF format support (Turtle, RDF/XML, JSON-LD, N-Triples, N3)\n", "- Namespace management\n", "- RDF validation\n", "- Format conversion capabilities\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import RDFExporter, RDFSerializer, RDFValidator\n", "\n", "# Create RDF exporter\n", "rdf_exporter = RDFExporter()\n", "\n", "# Export to Turtle format (human-readable)\n", "rdf_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.ttl\", format=\"turtle\")\n", "\n", "# Export to RDF/XML format\n", "rdf_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.rdf\", format=\"rdfxml\")\n", "\n", "# Export to JSON-LD format\n", "rdf_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.jsonld\", format=\"jsonld\")\n", "\n", "# Export to N-Triples format\n", "rdf_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.nt\", format=\"ntriples\")\n", "\n", "# Using RDFSerializer for format conversion\n", "serializer = RDFSerializer()\n", "rdf_data = serializer.convert_kg_to_rdf(knowledge_graph)\n", "turtle_string = serializer.serialize_to_turtle(rdf_data)\n", "\n", "# Using RDFValidator for validation\n", "validator = RDFValidator()\n", "validation_result = validator.validate_rdf_syntax(turtle_string, format=\"turtle\")\n", "print(f\"RDF validation: {validation_result.get('is_valid', False)}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 4: Export to CSV\n", "\n", "Export knowledge graph to CSV format for tabular analysis.\n", "\n", "**CSVExporter Features:**\n", "- Separate files for entities and relationships\n", "- Configurable delimiter (comma, tab, semicolon)\n", "- Automatic header generation\n", "- Metadata serialization\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import CSVExporter\n", "\n", "# Create CSV exporter with custom delimiter\n", "csv_exporter = CSVExporter(delimiter=\",\")\n", "\n", "# Export complete knowledge graph\n", "csv_exporter.export_knowledge_graph(knowledge_graph, \"exports/output\")\n", "\n", "# Export entities separately\n", "entities = knowledge_graph.get(\"entities\", [])\n", "csv_exporter.export_entities(entities, \"exports/entities.csv\")\n", "\n", "# Export relationships separately\n", "relationships = knowledge_graph.get(\"relationships\", [])\n", "csv_exporter.export_relationships(relationships, \"exports/relationships.csv\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 5: Export to Graph Formats (GraphML, GEXF, DOT)\n", "\n", "Export knowledge graph to graph formats for visualization tools.\n", "\n", "**GraphExporter Features:**\n", "- GraphML format (Cytoscape, yEd)\n", "- GEXF format (Gephi)\n", "- DOT format (Graphviz)\n", "- Node and edge attribute mapping\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import GraphExporter\n", "\n", "# Create graph exporter\n", "graph_exporter = GraphExporter()\n", "\n", "# Export to GraphML format (for Cytoscape, yEd)\n", "graph_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.graphml\", format=\"graphml\")\n", "\n", "# Export to GEXF format (for Gephi)\n", "graph_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.gexf\", format=\"gexf\")\n", "\n", "# Export to DOT format (for Graphviz)\n", "graph_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.dot\", format=\"dot\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 6: Export to OWL\n", "\n", "Export ontology to OWL format. **Note:** OWLExporter expects an ontology structure, not a knowledge graph.\n", "\n", "**OWLExporter Features:**\n", "- OWL/XML format\n", "- OWL in Turtle format\n", "- Class hierarchy export\n", "- Property definition export\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import OWLExporter\n", "\n", "# Create OWL exporter with custom URI and version\n", "owl_exporter = OWLExporter(ontology_uri=\"https://example.org/ontology/\", version=\"1.0\")\n", "\n", "# Export complete ontology to OWL/XML\n", "owl_exporter.export(ontology, \"exports/output.owl\", format=\"owl-xml\")\n", "\n", "# Export to OWL in Turtle format\n", "owl_exporter.export(ontology, \"exports/output_owl.ttl\", format=\"turtle\")\n", "\n", "# Export only classes\n", "classes = ontology.get(\"classes\", [])\n", "owl_exporter.export_classes(classes, \"exports/classes.owl\")\n", "\n", "# Export only properties\n", "properties = ontology.get(\"object_properties\", [])\n", "owl_exporter.export_properties(properties, \"exports/properties.owl\", property_type=\"object\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 7: Export to Vector Formats\n", "\n", "Export vector embeddings to various formats for vector stores.\n", "\n", "**VectorExporter Features:**\n", "- JSON format\n", "- NumPy format\n", "- Binary format\n", "- FAISS format\n", "- Vector store integration (Weaviate, Qdrant)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import VectorExporter\n", "\n", "# Create vector exporter\n", "vector_exporter = VectorExporter()\n", "\n", "# Export to JSON format\n", "vector_exporter.export(embeddings, \"exports/output_vectors.json\", format=\"json\")\n", "\n", "# Export to NumPy format\n", "vector_exporter.export(embeddings, \"exports/output_vectors.npy\", format=\"numpy\")\n", "\n", "# Export to Binary format\n", "vector_exporter.export(embeddings, \"exports/output_vectors.bin\", format=\"binary\")\n", "\n", "# Export to FAISS format\n", "vector_exporter.export(embeddings, \"exports/output_vectors.faiss\", format=\"faiss\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 8: Export to LPG (Labeled Property Graph)\n", "\n", "Export knowledge graph to LPG format for graph databases like Neo4j and Memgraph.\n", "\n", "**LPGExporter Features:**\n", "- Cypher query format\n", "- Labeled Property Graph format\n", "- Batch node/relationship export\n", "- Index generation\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import LPGExporter\n", "\n", "# Create LPG exporter\n", "lpg_exporter = LPGExporter()\n", "\n", "# Export to Cypher format (for Neo4j, Memgraph)\n", "lpg_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.cypher\", format=\"cypher\")\n", "\n", "# Export to LPG format\n", "lpg_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.lpg\", format=\"lpg\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 9: Export to YAML\n", "\n", "Export semantic networks and schemas to YAML format.\n", "\n", "**YAML Exporter Features:**\n", "- Semantic network YAML export\n", "- Schema YAML export\n", "- Human-readable format\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import SemanticNetworkYAMLExporter, YAMLSchemaExporter\n", "\n", "# Using SemanticNetworkYAMLExporter for knowledge graphs\n", "yaml_exporter = SemanticNetworkYAMLExporter()\n", "yaml_exporter.export(knowledge_graph, \"exports/output_network.yaml\")\n", "\n", "# Using YAMLSchemaExporter for ontology schemas\n", "schema_exporter = YAMLSchemaExporter()\n", "yaml_content = schema_exporter.export_ontology_schema(ontology)\n", "with open(\"exports/output_schema.yaml\", \"w\") as f:\n", " f.write(yaml_content)\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 10: Generate Reports\n", "\n", "Generate professional reports in multiple formats using ReportGenerator.\n", "\n", "**ReportGenerator Features:**\n", "- HTML reports with styling\n", "- Markdown reports\n", "- JSON reports\n", "- Plain text reports\n", "- Quality metrics aggregation\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import ReportGenerator\n", "\n", "# Prepare report data\n", "report_data = {\n", " \"title\": \"Knowledge Graph Export Report\",\n", " \"summary\": \"Comprehensive export of knowledge graph to multiple formats\",\n", " \"knowledge_graph\": {\n", " \"entities\": len(knowledge_graph.get(\"entities\", [])),\n", " \"relationships\": len(knowledge_graph.get(\"relationships\", []))\n", " },\n", " \"formats_exported\": [\"JSON\", \"RDF\", \"CSV\", \"GraphML\", \"GEXF\", \"OWL\", \"Vector\", \"LPG\", \"YAML\"],\n", " \"export_timestamp\": \"2024-01-01T00:00:00Z\"\n", "}\n", "\n", "# Create report generator\n", "report_generator = ReportGenerator()\n", "\n", "# Generate HTML report\n", "report_generator.generate_report(report_data, \"exports/report.html\", format=\"html\")\n", "\n", "# Generate Markdown report\n", "report_generator.generate_report(report_data, \"exports/report.md\", format=\"markdown\")\n", "\n", "# Generate JSON report\n", "report_generator.generate_report(report_data, \"exports/report.json\", format=\"json\")\n", "\n", "# Generate Text report\n", "report_generator.generate_report(report_data, \"exports/report.txt\", format=\"text\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 11: Method Registry and Custom Methods\n", "\n", "Register and use custom export methods with the MethodRegistry system.\n", "\n", "**MethodRegistry Features:**\n", "- Register custom export methods\n", "- List available methods\n", "- Get methods by name\n", "- Unregister methods\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import MethodRegistry, method_registry, JSONExporter\n", "\n", "# Define a custom export method\n", "def custom_json_export(data, file_path, **kwargs):\n", " \"\"\"Custom JSON export with additional formatting.\"\"\"\n", " import json\n", " with open(file_path, 'w') as f:\n", " json.dump(data, f, indent=4, sort_keys=True)\n", " print(f\"Custom export completed: {file_path}\")\n", "\n", "# Register custom method\n", "MethodRegistry.register(\"json\", \"custom_formatted\", custom_json_export)\n", "\n", "# List all available methods\n", "all_methods = method_registry.list_all()\n", "print(\"Available methods:\", all_methods)\n", "\n", "# List methods for specific task\n", "json_methods = method_registry.list_all(\"json\")\n", "print(\"JSON methods:\", json_methods)\n", "\n", "# Use registered method with JSONExporter\n", "json_exporter = JSONExporter()\n", "# The custom method can be used via the registry system\n", "custom_method = method_registry.get(\"json\", \"custom_formatted\")\n", "if custom_method:\n", " custom_method(knowledge_graph, \"exports/custom_output.json\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Step 12: Configuration Management\n", "\n", "Configure export settings using ExportConfig.\n", "\n", "**ExportConfig Features:**\n", "- Environment variable support\n", "- Config file support\n", "- Programmatic configuration\n", "- Method-specific configuration\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from semantica.export import ExportConfig, export_config, JSONExporter\n", "\n", "# Get current configuration\n", "config = export_config.get(\"default\")\n", "print(\"Default config:\", config)\n", "\n", "# Set configuration programmatically\n", "export_config.set(\"json\", {\"indent\": 4, \"include_metadata\": True})\n", "export_config.set(\"rdf\", {\"format\": \"turtle\", \"base_uri\": \"https://example.org/\"})\n", "\n", "# Get method-specific configuration\n", "json_config = export_config.get_method_config(\"json\")\n", "print(\"JSON config:\", json_config)\n", "\n", "# Set method-specific configuration\n", "export_config.set_method_config(\"csv\", {\"delimiter\": \"\\t\"})\n", "\n", "# Use configured settings\n", "json_exporter = JSONExporter(**export_config.get_method_config(\"json\"))\n", "json_exporter.export_knowledge_graph(knowledge_graph, \"exports/output_configured.json\")\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "## Step 13: Verify All Exports\n", "\n", "Verify that all exported files were created successfully.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# List all exported files\n", "export_files = [\n", " \"exports/output.json\",\n", " \"exports/output.jsonld\",\n", " \"exports/output.ttl\",\n", " \"exports/output.rdf\",\n", " \"exports/output.nt\",\n", " \"exports/output.csv\",\n", " \"exports/entities.csv\",\n", " \"exports/relationships.csv\",\n", " \"exports/output.graphml\",\n", " \"exports/output.gexf\",\n", " \"exports/output.dot\",\n", " \"exports/output.owl\",\n", " \"exports/output_owl.ttl\",\n", " \"exports/output_vectors.json\",\n", " \"exports/output_vectors.npy\",\n", " \"exports/output.cypher\",\n", " \"exports/output_network.yaml\",\n", " \"exports/output_schema.yaml\",\n", " \"exports/report.html\",\n", " \"exports/report.md\",\n", " \"exports/report.json\",\n", " \"exports/report.txt\"\n", "]\n", "\n", "print(\"📊 Export Summary:\")\n", "print(\"=\" * 60)\n", "for file in export_files:\n", " if os.path.exists(file):\n", " size = os.path.getsize(file)\n", " print(f\"✅ {file:50} ({size:>10,} bytes)\")\n", " else:\n", " print(f\"❌ {file:50} (not found)\")\n", "\n", "print(\"=\" * 60)\n", "print(f\"Total files checked: {len(export_files)}\")\n", "print(f\"Files created: {sum(1 for f in export_files if os.path.exists(f))}\")\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 2 }