{
 "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/01_Advanced_Extraction.ipynb)\n",
    "\n",
    "# Advanced Extraction\n",
    "\n",
    "## Overview\n",
    "\n",
    "This notebook demonstrates advanced semantic extraction using EventDetector, CoreferenceResolver, TripleExtractor, SemanticAnalyzer, SemanticNetworkExtractor, LLMEnhancer, and ExtractionValidator.\n",
    "\n",
    "\n",
    "**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/semantic_extract/)\n",
    "\n",
    "### Learning Objectives\n",
    "\n",
    "- Use EventDetector to detect events\n",
    "- Use CoreferenceResolver to resolve coreferences\n",
    "- Use TripleExtractor to extract RDF triples\n",
    "- Use SemanticAnalyzer for semantic analysis\n",
    "- Use SemanticNetworkExtractor to extract semantic networks\n",
    "- Use LLMEnhancer for LLM-based enhancement\n",
    "- Use ExtractionValidator to validate extractions\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",
    "---\n",
    "\n",
    "## Workflow: Event Detection → Coreference Resolution → Triple Extraction → Semantic Analysis → Network Extraction → LLM Enhancement → Validation\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from semantica.semantic_extract import (\n",
    "    EventDetector, CoreferenceResolver, TripleExtractor,\n",
    "    SemanticAnalyzer, SemanticNetworkExtractor, LLMEnhancer, ExtractionValidator\n",
    ")\n",
    "\n",
    "text = \"Apple Inc. was founded by Steve Jobs in 1976. The company is now led by Tim Cook.\"\n",
    "\n",
    "event_detector = EventDetector()\n",
    "events = event_detector.detect_events(text)\n",
    "\n",
    "print(f\"Detected {len(events)} events\")\n",
    "for event in events[:3]:\n",
    "    print(f\"  Event: {event.get('type', 'Unknown')} - {event.get('text', '')[:50]}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 2: Coreference Resolution\n",
    "\n",
    "Resolve coreferences in text.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "coreference_resolver = CoreferenceResolver()\n",
    "\n",
    "coreferences = coreference_resolver.resolve(text)\n",
    "\n",
    "print(f\"Resolved {len(coreferences)} coreference chains\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 3: Triple Extraction\n",
    "\n",
    "Extract RDF triples.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "triple_extractor = TripleExtractor()\n",
    "\n",
    "triples = triple_extractor.extract_triples(text)\n",
    "\n",
    "print(f\"Extracted {len(triples)} triples\")\n",
    "for triple in triples[:3]:\n",
    "    print(f\"  ({triple.get('subject', '')}, {triple.get('predicate', '')}, {triple.get('object', '')})\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 4: Semantic Analysis\n",
    "\n",
    "Perform semantic analysis.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "semantic_analyzer = SemanticAnalyzer()\n",
    "\n",
    "semantic_roles = semantic_analyzer.analyze_semantic_roles(text)\n",
    "\n",
    "print(f\"Analyzed semantic roles: {len(semantic_roles)}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 5: Semantic Network Extraction\n",
    "\n",
    "Extract semantic networks.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "semantic_network_extractor = SemanticNetworkExtractor()\n",
    "\n",
    "semantic_network = semantic_network_extractor.extract_network(text)\n",
    "\n",
    "print(f\"Extracted semantic network with {len(semantic_network.get('nodes', []))} nodes\")\n",
    "print(f\"Edges: {len(semantic_network.get('edges', []))}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 6: LLM Enhancement\n",
    "\n",
    "Enhance extractions using LLM.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_enhancer = LLMEnhancer()\n",
    "\n",
    "enhanced_extractions = llm_enhancer.enhance_extractions(events, text)\n",
    "\n",
    "print(f\"Enhanced {len(enhanced_extractions)} extractions\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Step 7: Extraction Validation\n",
    "\n",
    "Validate extractions.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "extraction_validator = ExtractionValidator()\n",
    "\n",
    "validation_result = extraction_validator.validate(events, text)\n",
    "\n",
    "print(f\"Extraction validation:\")\n",
    "print(f\"  Valid: {validation_result.valid}\")\n",
    "print(f\"  Confidence: {validation_result.confidence:.3f}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Summary\n",
    "\n",
    "You've learned advanced extraction capabilities:\n",
    "\n",
    "- **EventDetector**: Event detection and classification\n",
    "- **CoreferenceResolver**: Coreference resolution\n",
    "- **TripleExtractor**: RDF triple extraction\n",
    "- **SemanticAnalyzer**: Semantic analysis and role labeling\n",
    "- **SemanticNetworkExtractor**: Semantic network extraction\n",
    "- **LLMEnhancer**: LLM-based extraction enhancement\n",
    "- **ExtractionValidator**: Extraction validation\n"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
