diff --git a/cookbook/REAL_DATA_SOURCES.md b/cookbook/REAL_DATA_SOURCES.md
new file mode 100644
index 00000000..75b5d219
--- /dev/null
+++ b/cookbook/REAL_DATA_SOURCES.md
@@ -0,0 +1,199 @@
+# Real Data Sources Used in Cookbook
+
+This document lists all real data sources, APIs, feeds, and database patterns used throughout the cookbook notebooks.
+
+## Cybersecurity Domain
+
+### Threat Intelligence Feeds
+- **CISA Security Advisories**: `https://www.cisa.gov/news.xml`
+- **US-CERT Alerts**: `https://www.us-cert.gov/ncas/alerts.xml`
+- **Security Week**: `https://feeds.feedburner.com/SecurityWeek`
+- **Dark Reading**: `https://www.darkreading.com/rss.xml`
+- **Krebs on Security**: `https://krebsonsecurity.com/feed/`
+
+### Threat Intelligence APIs
+- **MITRE ATT&CK Framework**: `https://api.github.com/repos/mitre/cti`
+- **VirusTotal API**: `https://www.virustotal.com/vtapi/v2/domain/report` (requires API key)
+- **Shodan API**: `https://api.shodan.io/shodan/host/search` (requires API key)
+- **CISA KEV Catalog**: `https://www.cisa.gov/known-exploited-vulnerabilities-catalog`
+- **NIST NVD**: `https://nvd.nist.gov/vuln/search`
+
+### CVE and Vulnerability Sources
+- **NVD Recent CVEs (JSON)**: `https://nvd.nist.gov/feeds/json/cve/1.1/nvdcve-1.1-recent.json.zip`
+- **NVD Recent CVEs (XML)**: `https://nvd.nist.gov/feeds/xml/cve/2.0/nvdcve-2.0-recent.xml.zip`
+- **CVE MITRE All Items**: `https://cve.mitre.org/data/downloads/allitems.csv`
+- **CISA KEV Catalog**: `https://www.cisa.gov/known-exploited-vulnerabilities-catalog/json`
+- **NVD CVE API v2.0**: `https://services.nvd.nist.gov/rest/json/cves/2.0`
+- **CVE Project GitHub**: `https://api.github.com/repos/CVEProject/cvelist`
+- **CVE Search API**: `https://cve.circl.lu/api/last`
+
+### Database Patterns (Cybersecurity)
+```sql
+-- Threat Intelligence Database
+postgresql://user:password@localhost:5432/threat_intel_db
+SELECT ioc, ioc_type, timestamp, severity, source
+FROM threat_indicators
+WHERE timestamp > NOW() - INTERVAL '7 days'
+
+-- Security Logs Database
+postgresql://user:password@localhost:5432/security_logs_db
+SELECT * FROM security_events
+WHERE timestamp > NOW() - INTERVAL '1 hour'
+ORDER BY timestamp DESC LIMIT 1000
+
+-- Vulnerability Database
+postgresql://user:password@localhost:5432/vulnerability_db
+SELECT cve_id, description, severity, published_date, affected_products
+FROM vulnerabilities
+WHERE published_date > NOW() - INTERVAL '30 days'
+ORDER BY published_date DESC
+```
+
+### Streaming Sources (Cybersecurity)
+```python
+# Kafka Configuration
+{
+ "type": "kafka",
+ "topic": "security_logs",
+ "bootstrap_servers": ["localhost:9092"],
+ "consumer_config": {"group_id": "semantica_security_monitor"}
+}
+
+# RabbitMQ Configuration
+{
+ "type": "rabbitmq",
+ "queue": "security_events",
+ "connection_url": "amqp://user:password@localhost:5672/"
+}
+```
+
+## Finance Domain
+
+### Financial News Feeds
+- **Reuters Business**: `https://feeds.reuters.com/reuters/businessNews`
+- **Reuters Top News**: `https://feeds.reuters.com/reuters/topNews`
+- **CNN Money**: `https://rss.cnn.com/rss/money_latest.rss`
+- **Bloomberg Markets**: `https://feeds.bloomberg.com/markets/news.rss`
+- **Financial Times**: `https://www.ft.com/?format=rss`
+
+### Financial APIs
+- **Polygon.io**: `https://api.polygon.io/v2/aggs/ticker/AAPL/range/1/day/2024-01-01/2024-01-31` (requires API key)
+- **Alpha Vantage**: `https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=AAPL&interval=5min&apikey=demo`
+- **Yahoo Finance API**: `https://api.github.com/repos/ranaroussi/yfinance`
+
+### Database Patterns (Finance)
+```sql
+-- Market Data Database
+postgresql://user:password@localhost:5432/market_data_db
+SELECT symbol, price, volume, timestamp
+FROM market_data
+WHERE timestamp > NOW() - INTERVAL '1 day'
+ORDER BY timestamp DESC
+
+-- Transactions Database
+postgresql://user:password@localhost:5432/transactions_db
+SELECT transaction_id, user_id, amount, merchant, location, timestamp, device
+FROM transactions
+WHERE timestamp > NOW() - INTERVAL '24 hours'
+ORDER BY timestamp DESC LIMIT 10000
+```
+
+### Streaming Sources (Finance)
+```python
+# Kafka Configuration
+{
+ "type": "kafka",
+ "topic": "transactions",
+ "bootstrap_servers": ["localhost:9092"],
+ "consumer_config": {"group_id": "fraud_detection"}
+}
+
+# RabbitMQ Configuration
+{
+ "type": "rabbitmq",
+ "queue": "payment_events",
+ "connection_url": "amqp://user:password@localhost:5672/"
+}
+```
+
+## Healthcare Domain
+
+### Medical News Feeds
+- **CDC Health Alerts**: `https://www.cdc.gov/rss.xml`
+- **WHO News**: `https://www.who.int/rss-feeds/news-english.xml`
+
+### Healthcare APIs
+- **Logica Health FHIR API**: `https://api.logicahealth.org/fhir/R4/Patient`
+- **HAPI FHIR Server**: `https://hapi.fhir.org/baseR4/Patient`
+
+### Database Patterns (Healthcare)
+```sql
+-- Patient Records Database (HIPAA Compliant)
+postgresql://user:password@localhost:5432/patient_records_db
+SELECT patient_id, visit_date, diagnosis, medication, doctor
+FROM patient_visits
+WHERE visit_date > CURRENT_DATE - INTERVAL '1 year'
+ORDER BY visit_date DESC
+```
+
+## General Public APIs
+
+### GitHub APIs
+- **MITRE ATT&CK**: `https://api.github.com/repos/mitre/cti`
+- **CVE Project**: `https://api.github.com/repos/CVEProject/cvelist`
+- **Yahoo Finance**: `https://api.github.com/repos/ranaroussi/yfinance`
+
+### Public Data Sources
+- **JSONPlaceholder**: `https://jsonplaceholder.typicode.com` (for testing)
+- **Public APIs**: Various endpoints for demonstration
+
+## Usage Notes
+
+1. **API Keys**: Some APIs (VirusTotal, Shodan, Polygon.io) require API keys. Configure these in your environment or config files.
+
+2. **Rate Limits**: Many public APIs have rate limits. Implement appropriate delays and caching.
+
+3. **Database Connections**: Replace placeholder credentials with actual database credentials. Use environment variables or secure config files.
+
+4. **Streaming**: Configure Kafka/RabbitMQ with actual connection details for production use.
+
+5. **Error Handling**: All notebooks include try-except blocks to handle network errors gracefully.
+
+6. **Feed Formats**: RSS/Atom feeds are automatically parsed by `FeedIngestor`.
+
+7. **Data Formats**:
+ - JSON: Parsed with `JSONParser`
+ - XML: Parsed with `XMLParser`
+ - CSV: Parsed with `CSVParser`
+ - Database: Queried with `DBIngestor`
+
+## Best Practices
+
+1. **Use Lists**: Pass multiple feed URLs in lists for batch processing
+2. **Error Handling**: Always wrap ingestion calls in try-except blocks
+3. **Logging**: Use print statements with ✓ and ⚠ symbols for clear feedback
+4. **Configuration**: Store sensitive credentials in environment variables
+5. **Caching**: Implement caching for frequently accessed feeds
+6. **Rate Limiting**: Respect API rate limits and implement delays
+
+## Example Usage Pattern
+
+```python
+# Real feed URLs in a list
+feeds = [
+ "https://www.cisa.gov/news.xml",
+ "https://www.us-cert.gov/ncas/alerts.xml"
+]
+
+# Process all feeds
+feed_data_list = []
+for feed_url in feeds:
+ try:
+ feed_data = feed_ingestor.ingest_feed(feed_url)
+ if feed_data:
+ feed_data_list.append(feed_data)
+ print(f"✓ Ingested: {feed_data.title}")
+ except Exception as e:
+ print(f"⚠ Error: {str(e)[:100]}")
+```
+
diff --git a/cookbook/advanced/Advanced_Extraction.ipynb b/cookbook/advanced/Advanced_Extraction.ipynb
new file mode 100644
index 00000000..05c8ef7f
--- /dev/null
+++ b/cookbook/advanced/Advanced_Extraction.ipynb
@@ -0,0 +1,211 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Advanced Extraction\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This notebook demonstrates advanced semantic extraction using EventDetector, CoreferenceResolver, TripleExtractor, SemanticAnalyzer, SemanticNetworkExtractor, LLMEnhancer, and ExtractionValidator.\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",
+ "---\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
+}
diff --git a/cookbook/advanced/Advanced_Graph_Analytics.ipynb b/cookbook/advanced/Advanced_Graph_Analytics.ipynb
new file mode 100644
index 00000000..c113f5b0
--- /dev/null
+++ b/cookbook/advanced/Advanced_Graph_Analytics.ipynb
@@ -0,0 +1,179 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Advanced Graph Analytics\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This notebook demonstrates advanced graph analytics using GraphAnalyzer, CentralityCalculator, CommunityDetector, ConnectivityAnalyzer, GraphValidator, and Deduplicator.\n",
+ "\n",
+ "### Learning Objectives\n",
+ "\n",
+ "- Use GraphAnalyzer for comprehensive graph analysis\n",
+ "- Use CentralityCalculator for advanced centrality measures\n",
+ "- Use CommunityDetector for community detection\n",
+ "- Use ConnectivityAnalyzer for connectivity analysis\n",
+ "- Use GraphValidator and Deduplicator for graph quality\n",
+ "\n",
+ "---\n",
+ "\n",
+ "## Workflow: Graph Analysis → Centrality → Communities → Connectivity → Validation → Deduplication\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.kg import GraphBuilder, GraphAnalyzer, CentralityCalculator, CommunityDetector, ConnectivityAnalyzer, GraphValidator, Deduplicator\n",
+ "\n",
+ "builder = GraphBuilder()\n",
+ "analyzer = GraphAnalyzer()\n",
+ "\n",
+ "entities = [\n",
+ " {\"id\": \"e1\", \"type\": \"Organization\", \"name\": \"Apple Inc.\", \"properties\": {}},\n",
+ " {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Tim Cook\", \"properties\": {}},\n",
+ " {\"id\": \"e3\", \"type\": \"Location\", \"name\": \"Cupertino\", \"properties\": {}}\n",
+ "]\n",
+ "\n",
+ "relationships = [\n",
+ " {\"source\": \"e2\", \"target\": \"e1\", \"type\": \"CEO_of\", \"properties\": {}},\n",
+ " {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"located_in\", \"properties\": {}}\n",
+ "]\n",
+ "\n",
+ "kg = builder.build(entities, relationships)\n",
+ "\n",
+ "metrics = analyzer.compute_metrics(kg)\n",
+ "\n",
+ "print(f\"Graph metrics:\")\n",
+ "print(f\" Entities: {metrics.get('entity_count', 0)}\")\n",
+ "print(f\" Relationships: {metrics.get('relationship_count', 0)}\")\n",
+ "print(f\" Density: {metrics.get('density', 0):.3f}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 2: Advanced Centrality Measures\n",
+ "\n",
+ "Calculate multiple centrality measures.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "centrality_calculator = CentralityCalculator()\n",
+ "\n",
+ "degree_centrality = centrality_calculator.calculate_centrality(kg, measure=\"degree\")\n",
+ "betweenness_centrality = centrality_calculator.calculate_centrality(kg, measure=\"betweenness\")\n",
+ "\n",
+ "print(f\"Degree centrality: {len(degree_centrality)} entities\")\n",
+ "print(f\"Betweenness centrality: {len(betweenness_centrality)} entities\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 3: Community Detection\n",
+ "\n",
+ "Detect communities in the graph.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "community_detector = CommunityDetector()\n",
+ "\n",
+ "communities = community_detector.detect_communities(kg)\n",
+ "\n",
+ "print(f\"Detected {len(communities)} communities\")\n",
+ "for i, community in enumerate(communities[:3], 1):\n",
+ " print(f\" Community {i}: {len(community)} entities\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 4: Connectivity Analysis\n",
+ "\n",
+ "Analyze graph connectivity.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "connectivity_analyzer = ConnectivityAnalyzer()\n",
+ "\n",
+ "connectivity = connectivity_analyzer.analyze_connectivity(kg)\n",
+ "\n",
+ "print(f\"Connectivity analysis:\")\n",
+ "print(f\" Is connected: {connectivity.get('is_connected', False)}\")\n",
+ "print(f\" Components: {len(connectivity.get('components', []))}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 5: Graph Validation and Deduplication\n",
+ "\n",
+ "Validate and deduplicate the graph.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "graph_validator = GraphValidator()\n",
+ "deduplicator = Deduplicator()\n",
+ "\n",
+ "validation_result = graph_validator.validate(kg)\n",
+ "deduplicated_kg = deduplicator.deduplicate(kg)\n",
+ "\n",
+ "print(f\"Graph validation: {validation_result.get('valid', False)}\")\n",
+ "print(f\"Deduplicated entities: {len(deduplicated_kg.get('entities', []))}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Summary\n",
+ "\n",
+ "You've learned advanced graph analytics:\n",
+ "\n",
+ "- **GraphAnalyzer**: Comprehensive graph analysis and metrics\n",
+ "- **CentralityCalculator**: Multiple centrality measures\n",
+ "- **CommunityDetector**: Community detection\n",
+ "- **ConnectivityAnalyzer**: Connectivity analysis\n",
+ "- **GraphValidator**: Graph validation\n",
+ "- **Deduplicator**: Graph deduplication\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/cookbook/advanced_patterns/Complete_Visualization_Suite.ipynb b/cookbook/advanced/Complete_Visualization_Suite.ipynb
similarity index 100%
rename from cookbook/advanced_patterns/Complete_Visualization_Suite.ipynb
rename to cookbook/advanced/Complete_Visualization_Suite.ipynb
diff --git a/cookbook/advanced_patterns/Conflict_Resolution_Strategies.ipynb b/cookbook/advanced/Conflict_Resolution_Strategies.ipynb
similarity index 100%
rename from cookbook/advanced_patterns/Conflict_Resolution_Strategies.ipynb
rename to cookbook/advanced/Conflict_Resolution_Strategies.ipynb
diff --git a/cookbook/advanced_patterns/Multi_Format_Export.ipynb b/cookbook/advanced/Multi_Format_Export.ipynb
similarity index 100%
rename from cookbook/advanced_patterns/Multi_Format_Export.ipynb
rename to cookbook/advanced/Multi_Format_Export.ipynb
diff --git a/cookbook/advanced/Multi_Source_Data_Integration.ipynb b/cookbook/advanced/Multi_Source_Data_Integration.ipynb
new file mode 100644
index 00000000..c89b66b4
--- /dev/null
+++ b/cookbook/advanced/Multi_Source_Data_Integration.ipynb
@@ -0,0 +1,194 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Multi-Source Data Integration\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This notebook demonstrates advanced multi-source data integration using multiple ingestion types, entity resolution, conflict detection, and provenance tracking.\n",
+ "\n",
+ "### Learning Objectives\n",
+ "\n",
+ "- Ingest data from multiple sources (files, web, databases, streams, feeds)\n",
+ "- Resolve entities across sources using EntityResolver\n",
+ "- Detect conflicts using ConflictDetector\n",
+ "- Track provenance using ProvenanceTracker\n",
+ "- Integrate data into a unified knowledge graph\n",
+ "\n",
+ "---\n",
+ "\n",
+ "## Workflow: Multi-Source Ingestion → Entity Resolution → Conflict Detection → Provenance Tracking → Unified KG\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.ingest import FileIngestor, WebIngestor, DBIngestor, StreamIngestor, FeedIngestor\n",
+ "from semantica.parse import DocumentParser, StructuredDataParser\n",
+ "from semantica.kg import GraphBuilder, EntityResolver, ConflictDetector, ProvenanceTracker\n",
+ "import tempfile\n",
+ "import os\n",
+ "import json\n",
+ "\n",
+ "file_ingestor = FileIngestor()\n",
+ "web_ingestor = WebIngestor()\n",
+ "db_ingestor = DBIngestor()\n",
+ "stream_ingestor = StreamIngestor()\n",
+ "feed_ingestor = FeedIngestor()\n",
+ "\n",
+ "temp_dir = tempfile.mkdtemp()\n",
+ "\n",
+ "file1 = os.path.join(temp_dir, \"source1.txt\")\n",
+ "with open(file1, 'w') as f:\n",
+ " f.write(\"Apple Inc. is a technology company. Tim Cook is the CEO.\")\n",
+ "\n",
+ "file_objects = file_ingestor.ingest_file(file1, read_content=True)\n",
+ "\n",
+ "print(f\"Ingested {len([file_objects]) if file_objects else 0} files\")\n",
+ "print(f\"Multi-source ingestion initialized\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 2: Entity Resolution\n",
+ "\n",
+ "Resolve entities across multiple sources.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "entity_resolver = EntityResolver()\n",
+ "\n",
+ "entities_from_source1 = [\n",
+ " {\"id\": \"e1\", \"name\": \"Apple Inc.\", \"type\": \"Organization\", \"source\": \"file1\"},\n",
+ " {\"id\": \"e2\", \"name\": \"Tim Cook\", \"type\": \"Person\", \"source\": \"file1\"}\n",
+ "]\n",
+ "\n",
+ "entities_from_source2 = [\n",
+ " {\"id\": \"e3\", \"name\": \"Apple Incorporated\", \"type\": \"Organization\", \"source\": \"web\"},\n",
+ " {\"id\": \"e4\", \"name\": \"Timothy Cook\", \"type\": \"Person\", \"source\": \"web\"}\n",
+ "]\n",
+ "\n",
+ "all_entities = entities_from_source1 + entities_from_source2\n",
+ "\n",
+ "resolved_entities = entity_resolver.resolve(all_entities)\n",
+ "\n",
+ "print(f\"Original entities: {len(all_entities)}\")\n",
+ "print(f\"Resolved entities: {len(resolved_entities)}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 3: Conflict Detection\n",
+ "\n",
+ "Detect conflicts between sources.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "conflict_detector = ConflictDetector()\n",
+ "\n",
+ "conflicts = conflict_detector.detect_value_conflicts(all_entities, \"name\")\n",
+ "\n",
+ "print(f\"Detected {len(conflicts)} conflicts\")\n",
+ "for conflict in conflicts[:3]:\n",
+ " print(f\" Conflict: {conflict.entity_id} - {conflict.conflict_type}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 4: Provenance Tracking\n",
+ "\n",
+ "Track data provenance across sources.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "provenance_tracker = ProvenanceTracker()\n",
+ "\n",
+ "for entity in all_entities:\n",
+ " provenance_tracker.track_entity(entity.get(\"id\"), entity.get(\"source\"), entity)\n",
+ "\n",
+ "relationships = [\n",
+ " {\"source\": \"e2\", \"target\": \"e1\", \"type\": \"CEO_of\", \"source\": \"file1\"}\n",
+ "]\n",
+ "\n",
+ "for rel in relationships:\n",
+ " provenance_tracker.track_relationship(rel.get(\"source\"), rel.get(\"target\"), rel.get(\"source\"), rel)\n",
+ "\n",
+ "print(f\"Tracked provenance for {len(all_entities)} entities and {len(relationships)} relationships\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 5: Build Unified Knowledge Graph\n",
+ "\n",
+ "Build a unified knowledge graph from integrated sources.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "builder = GraphBuilder()\n",
+ "\n",
+ "unified_kg = builder.build(resolved_entities, relationships)\n",
+ "\n",
+ "print(f\"Built unified knowledge graph\")\n",
+ "print(f\" Entities: {len(unified_kg.get('entities', []))}\")\n",
+ "print(f\" Relationships: {len(unified_kg.get('relationships', []))}\")\n",
+ "print(f\" Sources integrated: {len(set(e.get('source', '') for e in resolved_entities))}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Summary\n",
+ "\n",
+ "You've learned advanced multi-source data integration:\n",
+ "\n",
+ "- **Multiple Ingestion Types**: FileIngestor, WebIngestor, DBIngestor, StreamIngestor, FeedIngestor\n",
+ "- **EntityResolver**: Resolve entities across sources\n",
+ "- **ConflictDetector**: Detect conflicts between sources\n",
+ "- **ProvenanceTracker**: Track data provenance\n",
+ "- **Unified Knowledge Graph**: Build integrated graph from multiple sources\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/cookbook/advanced_patterns/Pipeline_Orchestration.ipynb b/cookbook/advanced/Pipeline_Orchestration.ipynb
similarity index 100%
rename from cookbook/advanced_patterns/Pipeline_Orchestration.ipynb
rename to cookbook/advanced/Pipeline_Orchestration.ipynb
diff --git a/cookbook/advanced_patterns/Reasoning_and_Inference.ipynb b/cookbook/advanced/Reasoning_and_Inference.ipynb
similarity index 100%
rename from cookbook/advanced_patterns/Reasoning_and_Inference.ipynb
rename to cookbook/advanced/Reasoning_and_Inference.ipynb
diff --git a/cookbook/advanced_patterns/Semantic_Layer_Construction.ipynb b/cookbook/advanced/Semantic_Layer_Construction.ipynb
similarity index 100%
rename from cookbook/advanced_patterns/Semantic_Layer_Construction.ipynb
rename to cookbook/advanced/Semantic_Layer_Construction.ipynb
diff --git a/cookbook/advanced/Temporal_Knowledge_Graphs.ipynb b/cookbook/advanced/Temporal_Knowledge_Graphs.ipynb
new file mode 100644
index 00000000..d2590091
--- /dev/null
+++ b/cookbook/advanced/Temporal_Knowledge_Graphs.ipynb
@@ -0,0 +1,171 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Temporal Knowledge Graphs\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This notebook demonstrates advanced temporal knowledge graph capabilities using TemporalGraphQuery, TemporalPatternDetector, TemporalVersionManager, and TemporalVisualizer.\n",
+ "\n",
+ "### Learning Objectives\n",
+ "\n",
+ "- Use TemporalGraphQuery for time-aware queries\n",
+ "- Use TemporalPatternDetector to detect temporal patterns\n",
+ "- Use TemporalVersionManager for temporal versioning and snapshots\n",
+ "- Use TemporalVisualizer to visualize temporal data\n",
+ "\n",
+ "---\n",
+ "\n",
+ "## Workflow: Build Temporal KG → Time-Aware Queries → Pattern Detection → Version Management → Visualization\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.kg import GraphBuilder, TemporalGraphQuery, TemporalPatternDetector, TemporalVersionManager\n",
+ "from semantica.visualization import TemporalVisualizer\n",
+ "from datetime import datetime\n",
+ "\n",
+ "builder = GraphBuilder()\n",
+ "\n",
+ "entities = [\n",
+ " {\"id\": \"e1\", \"type\": \"Organization\", \"name\": \"Apple Inc.\", \"properties\": {\"founded\": \"1976\"}},\n",
+ " {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Steve Jobs\", \"properties\": {\"born\": \"1955\"}}\n",
+ "]\n",
+ "\n",
+ "relationships = [\n",
+ " {\"source\": \"e2\", \"target\": \"e1\", \"type\": \"founded\", \"properties\": {\"timestamp\": \"1976-04-01\"}}\n",
+ "]\n",
+ "\n",
+ "temporal_kg = builder.build(entities, relationships)\n",
+ "\n",
+ "print(f\"Built temporal knowledge graph with {len(entities)} entities\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 2: Time-Aware Queries\n",
+ "\n",
+ "Query the graph at specific time points.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "temporal_query = TemporalGraphQuery()\n",
+ "\n",
+ "query_result = temporal_query.query_time_range(\n",
+ " graph=temporal_kg,\n",
+ " query=\"Find entities founded in 1976\",\n",
+ " start_time=\"1976-01-01\",\n",
+ " end_time=\"1976-12-31\"\n",
+ ")\n",
+ "\n",
+ "print(f\"Time-aware query returned {len(query_result.get('entities', []))} entities\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 3: Temporal Pattern Detection\n",
+ "\n",
+ "Detect temporal patterns in the graph.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "pattern_detector = TemporalPatternDetector()\n",
+ "\n",
+ "patterns = pattern_detector.detect_temporal_patterns(\n",
+ " temporal_kg,\n",
+ " pattern_type=\"sequence\",\n",
+ " min_frequency=1\n",
+ ")\n",
+ "\n",
+ "print(f\"Detected {len(patterns)} temporal patterns\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 4: Version Management\n",
+ "\n",
+ "Manage temporal versions and snapshots.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "version_manager = TemporalVersionManager()\n",
+ "\n",
+ "snapshot = version_manager.create_snapshot(temporal_kg, timestamp=datetime.now())\n",
+ "\n",
+ "print(f\"Created temporal snapshot at {snapshot.get('timestamp', 'N/A')}\")\n",
+ "print(f\"Snapshot contains {len(snapshot.get('entities', []))} entities\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 5: Temporal Visualization\n",
+ "\n",
+ "Visualize temporal data.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "temporal_visualizer = TemporalVisualizer()\n",
+ "\n",
+ "visualization = temporal_visualizer.visualize_timeline(temporal_kg, output=\"interactive\")\n",
+ "\n",
+ "print(\"Generated temporal visualization\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Summary\n",
+ "\n",
+ "You've learned advanced temporal knowledge graph capabilities:\n",
+ "\n",
+ "- **TemporalGraphQuery**: Time-aware graph querying\n",
+ "- **TemporalPatternDetector**: Temporal pattern detection\n",
+ "- **TemporalVersionManager**: Temporal versioning and snapshots\n",
+ "- **TemporalVisualizer**: Temporal data visualization\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/cookbook/advanced_patterns/Text_Chunking_Strategies.ipynb b/cookbook/advanced/Text_Chunking_Strategies.ipynb
similarity index 100%
rename from cookbook/advanced_patterns/Text_Chunking_Strategies.ipynb
rename to cookbook/advanced/Text_Chunking_Strategies.ipynb
diff --git a/cookbook/advanced_patterns/Unstructured_to_Ontology.ipynb b/cookbook/advanced/Unstructured_to_Ontology.ipynb
similarity index 100%
rename from cookbook/advanced_patterns/Unstructured_to_Ontology.ipynb
rename to cookbook/advanced/Unstructured_to_Ontology.ipynb
diff --git a/cookbook/introduction/Building_Knowledge_Graphs.ipynb b/cookbook/introduction/Building_Knowledge_Graphs.ipynb
new file mode 100644
index 00000000..c10662aa
--- /dev/null
+++ b/cookbook/introduction/Building_Knowledge_Graphs.ipynb
@@ -0,0 +1,168 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Building Knowledge Graphs\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This notebook demonstrates how to build knowledge graphs from entities and relationships using Semantica's graph building modules. You'll learn to use `GraphBuilder`, `EntityResolver`, `GraphValidator`, and `Deduplicator`.\n",
+ "\n",
+ "### Learning Objectives\n",
+ "\n",
+ "- Use `GraphBuilder` to construct knowledge graphs\n",
+ "- Use `EntityResolver` to resolve entity conflicts\n",
+ "- Use `GraphValidator` to validate graph structure\n",
+ "- Use `Deduplicator` to remove duplicate entities\n",
+ "\n",
+ "---\n",
+ "\n",
+ "## Step 1: Build Knowledge Graph\n",
+ "\n",
+ "Construct a knowledge graph from entities and relationships.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.kg import GraphBuilder\n",
+ "from semantica.semantic_extract import NERExtractor, RelationExtractor\n",
+ "\n",
+ "builder = GraphBuilder()\n",
+ "ner_extractor = NERExtractor()\n",
+ "relation_extractor = RelationExtractor()\n",
+ "\n",
+ "text = \"Apple Inc. is a technology company. Tim Cook is the CEO of Apple Inc. Apple Inc. is headquartered in Cupertino, California.\"\n",
+ "\n",
+ "entities_list = ner_extractor.extract(text)\n",
+ "relationships_list = relation_extractor.extract(text, entities_list)\n",
+ "\n",
+ "entities = []\n",
+ "for i, entity in enumerate(entities_list[:5], 1):\n",
+ " entities.append({\n",
+ " \"id\": f\"e{i}\",\n",
+ " \"type\": entity.get(\"type\", \"Entity\"),\n",
+ " \"name\": entity.get(\"text\", entity.get(\"entity\", \"\")),\n",
+ " \"properties\": {}\n",
+ " })\n",
+ "\n",
+ "relationships = []\n",
+ "for i, rel in enumerate(relationships_list[:3], 1):\n",
+ " relationships.append({\n",
+ " \"source\": f\"e{1}\",\n",
+ " \"target\": f\"e{i+1}\",\n",
+ " \"type\": rel.get(\"type\", \"related_to\"),\n",
+ " \"properties\": {}\n",
+ " })\n",
+ "\n",
+ "knowledge_graph = builder.build(entities, relationships)\n",
+ "\n",
+ "print(f\"Built knowledge graph with {len(knowledge_graph.get('entities', []))} entities\")\n",
+ "print(f\"Relationships: {len(knowledge_graph.get('relationships', []))}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 2: Entity Resolution\n",
+ "\n",
+ "Resolve entity conflicts and duplicates.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.kg import EntityResolver\n",
+ "\n",
+ "entity_resolver = EntityResolver()\n",
+ "\n",
+ "resolved_entities = entity_resolver.resolve(entities)\n",
+ "\n",
+ "print(f\"Original entities: {len(entities)}\")\n",
+ "print(f\"Resolved entities: {len(resolved_entities)}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 3: Graph Validation\n",
+ "\n",
+ "Validate the knowledge graph structure.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.kg import GraphValidator\n",
+ "\n",
+ "graph_validator = GraphValidator()\n",
+ "\n",
+ "validation_result = graph_validator.validate(knowledge_graph)\n",
+ "\n",
+ "print(f\"Graph validation: {validation_result.get('valid', False)}\")\n",
+ "print(f\"Issues: {len(validation_result.get('issues', []))}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 4: Deduplication\n",
+ "\n",
+ "Remove duplicate entities from the graph.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.kg import Deduplicator\n",
+ "\n",
+ "deduplicator = Deduplicator()\n",
+ "\n",
+ "deduplicated_graph = deduplicator.deduplicate(knowledge_graph)\n",
+ "\n",
+ "print(f\"Original entities: {len(knowledge_graph.get('entities', []))}\")\n",
+ "print(f\"Deduplicated entities: {len(deduplicated_graph.get('entities', []))}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Summary\n",
+ "\n",
+ "You've learned how to build knowledge graphs:\n",
+ "\n",
+ "- **GraphBuilder**: Construct knowledge graphs from entities and relationships\n",
+ "- **EntityResolver**: Resolve entity conflicts and duplicates\n",
+ "- **GraphValidator**: Validate graph structure and quality\n",
+ "- **Deduplicator**: Remove duplicate entities\n",
+ "\n",
+ "Next: Learn how to analyze graphs in the Graph_Analytics notebook.\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/cookbook/getting_started/Configuration_Basics.ipynb b/cookbook/introduction/Configuration_Basics.ipynb
similarity index 100%
rename from cookbook/getting_started/Configuration_Basics.ipynb
rename to cookbook/introduction/Configuration_Basics.ipynb
diff --git a/cookbook/introduction/Conflict_Detection.ipynb b/cookbook/introduction/Conflict_Detection.ipynb
new file mode 100644
index 00000000..2fb03673
--- /dev/null
+++ b/cookbook/introduction/Conflict_Detection.ipynb
@@ -0,0 +1,125 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Conflict Detection\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This notebook demonstrates how to detect and resolve conflicts in knowledge graphs using Semantica's conflict modules. You'll learn to use `ConflictDetector`, `SourceTracker`, and `ConflictResolver`.\n",
+ "\n",
+ "### Learning Objectives\n",
+ "\n",
+ "- Use `ConflictDetector` to detect conflicts\n",
+ "- Use `SourceTracker` to track data sources\n",
+ "- Use `ConflictResolver` to resolve conflicts\n",
+ "\n",
+ "---\n",
+ "\n",
+ "## Step 1: Conflict Detection\n",
+ "\n",
+ "Detect conflicts in entities.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.conflicts import ConflictDetector\n",
+ "\n",
+ "conflict_detector = ConflictDetector()\n",
+ "\n",
+ "entities = [\n",
+ " {\"id\": \"e1\", \"name\": \"Apple Inc.\", \"source\": \"source1\"},\n",
+ " {\"id\": \"e1\", \"name\": \"Apple Incorporated\", \"source\": \"source2\"}\n",
+ "]\n",
+ "\n",
+ "conflicts = conflict_detector.detect_value_conflicts(entities, \"name\")\n",
+ "\n",
+ "print(f\"Detected {len(conflicts)} conflicts\")\n",
+ "for conflict in conflicts[:3]:\n",
+ " print(f\" Conflict: {conflict.entity_id} - {conflict.conflict_type}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 2: Source Tracking\n",
+ "\n",
+ "Track data sources.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.conflicts import SourceTracker\n",
+ "\n",
+ "source_tracker = SourceTracker()\n",
+ "\n",
+ "source_tracker.track_source(\"e1\", \"source1\", {\"name\": \"Apple Inc.\"})\n",
+ "source_tracker.track_source(\"e1\", \"source2\", {\"name\": \"Apple Incorporated\"})\n",
+ "\n",
+ "sources = source_tracker.get_sources(\"e1\")\n",
+ "\n",
+ "print(f\"Tracked sources for e1: {len(sources)}\")\n",
+ "for source in sources:\n",
+ " print(f\" Source: {source.source_id}, Property: {source.property_name}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 3: Conflict Resolution\n",
+ "\n",
+ "Resolve conflicts using ConflictResolver.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.conflicts import ConflictResolver\n",
+ "\n",
+ "conflict_resolver = ConflictResolver()\n",
+ "\n",
+ "if conflicts:\n",
+ " resolution = conflict_resolver.resolve_conflicts(conflicts, strategy=\"most_recent\")\n",
+ " print(f\"Resolved {len(resolution.resolved_conflicts)} conflicts\")\n",
+ " print(f\"Resolution strategy: {resolution.strategy}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Summary\n",
+ "\n",
+ "You've learned how to detect and resolve conflicts:\n",
+ "\n",
+ "- **ConflictDetector**: Detect conflicts in entities\n",
+ "- **SourceTracker**: Track data sources\n",
+ "- **ConflictResolver**: Resolve conflicts using various strategies\n",
+ "\n",
+ "Next: Learn about configuration in the Configuration notebook.\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/cookbook/introduction/Data_Ingestion.ipynb b/cookbook/introduction/Data_Ingestion.ipynb
new file mode 100644
index 00000000..47b22b03
--- /dev/null
+++ b/cookbook/introduction/Data_Ingestion.ipynb
@@ -0,0 +1,208 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Data Ingestion\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This notebook demonstrates how to ingest data from various sources using Semantica's ingestion modules. You'll learn to ingest files, web content, databases, streams, and feeds.\n",
+ "\n",
+ "### Learning Objectives\n",
+ "\n",
+ "- Use `FileIngestor` to load files from local and cloud storage\n",
+ "- Use `WebIngestor` to scrape and crawl web content\n",
+ "- Use `DBIngestor` to extract data from databases\n",
+ "- Use `StreamIngestor` for real-time data streams\n",
+ "- Use `FeedIngestor` to process RSS/Atom feeds\n",
+ "\n",
+ "---\n",
+ "\n",
+ "## Step 1: File Ingestion\n",
+ "\n",
+ "Ingest files from local filesystem or cloud storage.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.ingest import FileIngestor\n",
+ "import tempfile\n",
+ "import os\n",
+ "\n",
+ "file_ingestor = FileIngestor()\n",
+ "\n",
+ "temp_dir = tempfile.mkdtemp()\n",
+ "sample_file = os.path.join(temp_dir, \"sample.txt\")\n",
+ "\n",
+ "with open(sample_file, 'w') as f:\n",
+ " f.write(\"Apple Inc. is a technology company. Tim Cook is the CEO.\")\n",
+ "\n",
+ "file_object = file_ingestor.ingest_file(sample_file, read_content=True)\n",
+ "\n",
+ "print(f\"Ingested file: {file_object.name}\")\n",
+ "print(f\"File type: {file_object.file_type}\")\n",
+ "print(f\"Size: {file_object.size} bytes\")\n",
+ "print(f\"Content preview: {file_object.content[:50]}...\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 2: Directory Ingestion\n",
+ "\n",
+ "Ingest multiple files from a directory.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "file2 = os.path.join(temp_dir, \"doc2.txt\")\n",
+ "with open(file2, 'w') as f:\n",
+ " f.write(\"Microsoft Corporation is a technology company. Satya Nadella is the CEO.\")\n",
+ "\n",
+ "file_objects = file_ingestor.ingest_directory(temp_dir, recursive=False, read_content=True)\n",
+ "\n",
+ "print(f\"Ingested {len(file_objects)} files from directory\")\n",
+ "for file_obj in file_objects:\n",
+ " print(f\" - {file_obj.name} ({file_obj.file_type})\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 3: Web Ingestion\n",
+ "\n",
+ "Ingest content from web pages.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.ingest import WebIngestor\n",
+ "\n",
+ "web_ingestor = WebIngestor()\n",
+ "\n",
+ "try:\n",
+ " web_content = web_ingestor.ingest_url(\"https://example.com\")\n",
+ " print(f\"Ingested web page: {web_content.url}\")\n",
+ " print(f\"Title: {web_content.title}\")\n",
+ " print(f\"Content length: {len(web_content.text)} characters\")\n",
+ "except Exception as e:\n",
+ " print(f\"Web ingestion example (requires internet): {e}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 4: Database Ingestion\n",
+ "\n",
+ "Ingest data from databases.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.ingest import DBIngestor\n",
+ "\n",
+ "db_ingestor = DBIngestor()\n",
+ "\n",
+ "print(\"DBIngestor initialized\")\n",
+ "print(\"To use: Configure database connection and call ingest_table() or ingest_query()\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 5: Stream Ingestion\n",
+ "\n",
+ "Ingest data from real-time streams.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.ingest import StreamIngestor\n",
+ "\n",
+ "stream_ingestor = StreamIngestor()\n",
+ "\n",
+ "print(\"StreamIngestor initialized\")\n",
+ "print(\"To use: Configure stream source (Kafka, RabbitMQ, etc.) and start consuming\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 6: Feed Ingestion\n",
+ "\n",
+ "Ingest RSS/Atom feeds.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.ingest import FeedIngestor\n",
+ "\n",
+ "feed_ingestor = FeedIngestor()\n",
+ "\n",
+ "try:\n",
+ " feed_data = feed_ingestor.ingest_feed(\"https://feeds.feedburner.com/oreilly/radar\")\n",
+ " print(f\"Ingested feed: {feed_data.title}\")\n",
+ " print(f\"Items: {len(feed_data.items)}\")\n",
+ " if feed_data.items:\n",
+ " print(f\"First item: {feed_data.items[0].title}\")\n",
+ "except Exception as e:\n",
+ " print(f\"Feed ingestion example (requires internet): {e}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Summary\n",
+ "\n",
+ "You've learned how to ingest data from multiple sources:\n",
+ "\n",
+ "- **FileIngestor**: Local files and directories\n",
+ "- **WebIngestor**: Web pages and URLs\n",
+ "- **DBIngestor**: Database tables and queries\n",
+ "- **StreamIngestor**: Real-time data streams\n",
+ "- **FeedIngestor**: RSS/Atom feeds\n",
+ "\n",
+ "Next: Learn how to parse the ingested data in the Document_Parsing notebook.\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/cookbook/introduction/Data_Normalization.ipynb b/cookbook/introduction/Data_Normalization.ipynb
new file mode 100644
index 00000000..0f87514e
--- /dev/null
+++ b/cookbook/introduction/Data_Normalization.ipynb
@@ -0,0 +1,228 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Data Normalization\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This notebook demonstrates how to normalize and clean data using Semantica's normalization modules. You'll learn to normalize text, entities, dates, numbers, and handle encoding issues.\n",
+ "\n",
+ "### Learning Objectives\n",
+ "\n",
+ "- Use `TextNormalizer` for text cleaning and normalization\n",
+ "- Use `EntityNormalizer` for entity name standardization\n",
+ "- Use `DateNormalizer` for date format normalization\n",
+ "- Use `NumberNormalizer` for number and quantity normalization\n",
+ "- Use `DataCleaner` for general data cleaning\n",
+ "- Use `LanguageDetector` and `EncodingHandler` for data quality\n",
+ "\n",
+ "---\n",
+ "\n",
+ "## Step 1: Text Normalization\n",
+ "\n",
+ "Normalize text content for consistency.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.normalize import TextNormalizer\n",
+ "\n",
+ "text_normalizer = TextNormalizer()\n",
+ "\n",
+ "sample_text = \"Hello World!!! This is a test.\"\n",
+ "\n",
+ "normalized = text_normalizer.normalize_text(sample_text, case=\"lower\")\n",
+ "cleaned = text_normalizer.clean_text(sample_text, remove_special_chars=False)\n",
+ "\n",
+ "print(f\"Original: {sample_text}\")\n",
+ "print(f\"Normalized: {normalized}\")\n",
+ "print(f\"Cleaned: {cleaned}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 2: Entity Normalization\n",
+ "\n",
+ "Normalize entity names to canonical forms.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.normalize import EntityNormalizer\n",
+ "\n",
+ "entity_normalizer = EntityNormalizer()\n",
+ "\n",
+ "entity_variants = [\"Apple Inc.\", \"Apple Inc\", \"Apple\", \"Apple Incorporated\"]\n",
+ "\n",
+ "normalized_entities = []\n",
+ "for entity in entity_variants:\n",
+ " normalized = entity_normalizer.normalize_entity(entity, entity_type=\"Organization\")\n",
+ " normalized_entities.append(normalized)\n",
+ " print(f\"{entity} -> {normalized}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 3: Date Normalization\n",
+ "\n",
+ "Normalize dates to standard formats.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.normalize import DateNormalizer\n",
+ "\n",
+ "date_normalizer = DateNormalizer()\n",
+ "\n",
+ "date_formats = [\"2023-12-25\", \"12/25/2023\", \"December 25, 2023\", \"25 Dec 2023\"]\n",
+ "\n",
+ "for date_str in date_formats:\n",
+ " try:\n",
+ " normalized = date_normalizer.normalize_date(date_str)\n",
+ " print(f\"{date_str} -> {normalized}\")\n",
+ " except Exception as e:\n",
+ " print(f\"{date_str} -> Error: {e}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 4: Number Normalization\n",
+ "\n",
+ "Normalize numbers and quantities.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.normalize import NumberNormalizer\n",
+ "\n",
+ "number_normalizer = NumberNormalizer()\n",
+ "\n",
+ "numbers = [\"1,000\", \"1.5M\", \"$100\", \"50%\", \"3.14e2\"]\n",
+ "\n",
+ "for num_str in numbers:\n",
+ " try:\n",
+ " normalized = number_normalizer.normalize_number(num_str)\n",
+ " print(f\"{num_str} -> {normalized}\")\n",
+ " except Exception as e:\n",
+ " print(f\"{num_str} -> Error: {e}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 5: Data Cleaning\n",
+ "\n",
+ "Clean data using DataCleaner.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.normalize import DataCleaner\n",
+ "\n",
+ "data_cleaner = DataCleaner()\n",
+ "\n",
+ "data = [\n",
+ " {\"name\": \"Apple Inc.\", \"value\": 100},\n",
+ " {\"name\": \"Apple Inc\", \"value\": 100},\n",
+ " {\"name\": \"Microsoft\", \"value\": 200}\n",
+ "]\n",
+ "\n",
+ "cleaned_data = data_cleaner.clean_data(data, remove_duplicates=True)\n",
+ "\n",
+ "print(f\"Original records: {len(data)}\")\n",
+ "print(f\"Cleaned records: {len(cleaned_data)}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 6: Language Detection and Encoding\n",
+ "\n",
+ "Detect language and handle encoding.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.normalize import LanguageDetector, EncodingHandler\n",
+ "\n",
+ "language_detector = LanguageDetector()\n",
+ "encoding_handler = EncodingHandler()\n",
+ "\n",
+ "text_samples = [\n",
+ " \"Hello, this is English text.\",\n",
+ " \"Bonjour, ceci est du texte français.\",\n",
+ " \"Hola, este es texto en español.\"\n",
+ "]\n",
+ "\n",
+ "for text in text_samples:\n",
+ " detected_lang = language_detector.detect_language(text)\n",
+ " print(f\"Text: {text[:30]}... -> Language: {detected_lang}\")\n",
+ "\n",
+ "sample_bytes = \"Hello World\".encode('utf-8')\n",
+ "detected_encoding = encoding_handler.detect_encoding(sample_bytes)\n",
+ "print(f\"\\nDetected encoding: {detected_encoding}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Summary\n",
+ "\n",
+ "You've learned how to normalize and clean data:\n",
+ "\n",
+ "- **TextNormalizer**: Text cleaning and normalization\n",
+ "- **EntityNormalizer**: Entity name standardization\n",
+ "- **DateNormalizer**: Date format normalization\n",
+ "- **NumberNormalizer**: Number and quantity normalization\n",
+ "- **DataCleaner**: General data cleaning\n",
+ "- **LanguageDetector**: Language detection\n",
+ "- **EncodingHandler**: Encoding detection and conversion\n",
+ "\n",
+ "Next: Learn how to extract entities in the Entity_Extraction notebook.\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/cookbook/introduction/Deduplication.ipynb b/cookbook/introduction/Deduplication.ipynb
new file mode 100644
index 00000000..188a91d7
--- /dev/null
+++ b/cookbook/introduction/Deduplication.ipynb
@@ -0,0 +1,123 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Deduplication\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This notebook demonstrates how to detect and merge duplicate entities using Semantica's deduplication modules. You'll learn to use `DuplicateDetector`, `EntityMerger`, `SimilarityCalculator`, and `ClusterBuilder`.\n",
+ "\n",
+ "### Learning Objectives\n",
+ "\n",
+ "- Use `DuplicateDetector` to find duplicate entities\n",
+ "- Use `EntityMerger` to merge duplicates\n",
+ "- Use `SimilarityCalculator` to calculate similarity scores\n",
+ "- Use `ClusterBuilder` for batch deduplication\n",
+ "\n",
+ "---\n",
+ "\n",
+ "## Step 1: Duplicate Detection\n",
+ "\n",
+ "Detect duplicate entities.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.deduplication import DuplicateDetector\n",
+ "\n",
+ "duplicate_detector = DuplicateDetector(similarity_threshold=0.8)\n",
+ "\n",
+ "entities = [\n",
+ " {\"id\": \"e1\", \"name\": \"Apple Inc.\", \"type\": \"Organization\"},\n",
+ " {\"id\": \"e2\", \"name\": \"Apple Inc\", \"type\": \"Organization\"},\n",
+ " {\"id\": \"e3\", \"name\": \"Microsoft\", \"type\": \"Organization\"}\n",
+ "]\n",
+ "\n",
+ "duplicates = duplicate_detector.detect_duplicates(entities)\n",
+ "\n",
+ "print(f\"Detected {len(duplicates)} duplicate groups\")\n",
+ "for group in duplicates[:3]:\n",
+ " print(f\" Group: {[e.get('id') for e in group.entities]}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 2: Entity Merging\n",
+ "\n",
+ "Merge duplicate entities.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.deduplication import EntityMerger\n",
+ "\n",
+ "entity_merger = EntityMerger()\n",
+ "\n",
+ "merged_entities = entity_merger.merge_duplicates(entities)\n",
+ "\n",
+ "print(f\"Original entities: {len(entities)}\")\n",
+ "print(f\"Merged entities: {len(merged_entities)}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 3: Similarity Calculation\n",
+ "\n",
+ "Calculate similarity between entities.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.deduplication import SimilarityCalculator\n",
+ "\n",
+ "similarity_calculator = SimilarityCalculator()\n",
+ "\n",
+ "similarity = similarity_calculator.calculate_similarity(entities[0], entities[1])\n",
+ "\n",
+ "print(f\"Similarity between '{entities[0]['name']}' and '{entities[1]['name']}': {similarity.score:.3f}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Summary\n",
+ "\n",
+ "You've learned how to deduplicate entities:\n",
+ "\n",
+ "- **DuplicateDetector**: Detect duplicate entities\n",
+ "- **EntityMerger**: Merge duplicate entities\n",
+ "- **SimilarityCalculator**: Calculate similarity scores\n",
+ "- **ClusterBuilder**: Batch deduplication\n",
+ "\n",
+ "Next: Learn how to generate embeddings in the Embedding_Generation notebook.\n"
+ ]
+ }
+ ],
+ "metadata": {
+ "language_info": {
+ "name": "python"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}
diff --git a/cookbook/introduction/Document_Parsing.ipynb b/cookbook/introduction/Document_Parsing.ipynb
new file mode 100644
index 00000000..6cea05a9
--- /dev/null
+++ b/cookbook/introduction/Document_Parsing.ipynb
@@ -0,0 +1,247 @@
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Document Parsing\n",
+ "\n",
+ "## Overview\n",
+ "\n",
+ "This notebook demonstrates how to parse various document formats using Semantica's parsing modules. You'll learn to extract text, metadata, and structured data from PDFs, DOCX, CSV, JSON, XML, and HTML files.\n",
+ "\n",
+ "### Learning Objectives\n",
+ "\n",
+ "- Use `DocumentParser` for general document parsing\n",
+ "- Use format-specific parsers: `PDFParser`, `DOCXParser`, `CSVParser`, `JSONParser`, `XMLParser`, `HTMLParser`\n",
+ "- Extract text content and metadata from documents\n",
+ "- Parse structured data formats\n",
+ "\n",
+ "---\n",
+ "\n",
+ "## Step 1: Document Parser\n",
+ "\n",
+ "Parse various document formats using the general DocumentParser.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.parse import DocumentParser\n",
+ "import tempfile\n",
+ "import os\n",
+ "\n",
+ "document_parser = DocumentParser()\n",
+ "\n",
+ "temp_dir = tempfile.mkdtemp()\n",
+ "sample_txt = os.path.join(temp_dir, \"sample.txt\")\n",
+ "\n",
+ "with open(sample_txt, 'w') as f:\n",
+ " f.write(\"Apple Inc. is a technology company. Tim Cook is the CEO.\")\n",
+ "\n",
+ "text = document_parser.extract_text(sample_txt)\n",
+ "metadata = document_parser.extract_metadata(sample_txt)\n",
+ "\n",
+ "print(f\"Extracted text: {text[:50]}...\")\n",
+ "print(f\"Metadata: {metadata}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 2: CSV Parser\n",
+ "\n",
+ "Parse CSV files to extract structured data.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.parse import CSVParser\n",
+ "\n",
+ "csv_parser = CSVParser()\n",
+ "csv_file = os.path.join(temp_dir, \"data.csv\")\n",
+ "\n",
+ "with open(csv_file, 'w') as f:\n",
+ " f.write(\"name,company,role\\n\")\n",
+ " f.write(\"Tim Cook,Apple Inc.,CEO\\n\")\n",
+ " f.write(\"Satya Nadella,Microsoft Corporation,CEO\\n\")\n",
+ "\n",
+ "csv_data = csv_parser.parse(csv_file)\n",
+ "\n",
+ "print(f\"Parsed CSV with {len(csv_data.rows)} rows\")\n",
+ "print(f\"Columns: {csv_data.headers}\")\n",
+ "for row in csv_data.rows[:2]:\n",
+ " print(f\" {row}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 3: JSON Parser\n",
+ "\n",
+ "Parse JSON files to extract structured data.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.parse import JSONParser\n",
+ "import json\n",
+ "\n",
+ "json_parser = JSONParser()\n",
+ "json_file = os.path.join(temp_dir, \"data.json\")\n",
+ "\n",
+ "data = {\n",
+ " \"companies\": [\n",
+ " {\"name\": \"Apple Inc.\", \"ceo\": \"Tim Cook\"},\n",
+ " {\"name\": \"Microsoft Corporation\", \"ceo\": \"Satya Nadella\"}\n",
+ " ]\n",
+ "}\n",
+ "\n",
+ "with open(json_file, 'w') as f:\n",
+ " json.dump(data, f)\n",
+ "\n",
+ "json_data = json_parser.parse(json_file)\n",
+ "\n",
+ "print(f\"Parsed JSON: {json_data.data}\")\n",
+ "print(f\"Companies: {len(json_data.data.get('companies', []))}\")\n"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Step 4: XML Parser\n",
+ "\n",
+ "Parse XML files to extract structured data.\n"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "from semantica.parse import XMLParser\n",
+ "\n",
+ "xml_parser = XMLParser()\n",
+ "xml_file = os.path.join(temp_dir, \"data.xml\")\n",
+ "\n",
+ "xml_content = \"\"\"\n",
+ "
Apple Inc. is a technology company.
\n", + "\n", + "\"\"\"\n", + "\n", + "with open(html_file, 'w') as f:\n", + " f.write(html_content)\n", + "\n", + "html_data = html_parser.parse(html_file)\n", + "\n", + "print(f\"Parsed HTML\")\n", + "print(f\"Title: {html_data.metadata.get('title', 'N/A')}\")\n", + "print(f\"Text content: {html_data.text[:50]}...\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 6: Structured Data Parser\n", + "\n", + "Use StructuredDataParser for multiple formats.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.parse import StructuredDataParser\n", + "\n", + "structured_parser = StructuredDataParser()\n", + "\n", + "parsed_json = structured_parser.parse_json(json_file)\n", + "parsed_csv = structured_parser.parse_csv(csv_file)\n", + "\n", + "print(f\"Structured parser parsed JSON: {len(parsed_json.get('data', {}).get('companies', []))} companies\")\n", + "print(f\"Structured parser parsed CSV: {len(parsed_csv.get('rows', []))} rows\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You've learned how to parse various document formats:\n", + "\n", + "- **DocumentParser**: General document parsing\n", + "- **CSVParser**: CSV file parsing\n", + "- **JSONParser**: JSON file parsing\n", + "- **XMLParser**: XML file parsing\n", + "- **HTMLParser**: HTML file parsing\n", + "- **StructuredDataParser**: Multi-format structured data parsing\n", + "\n", + "Next: Learn how to normalize and clean data in the Data_Normalization notebook.\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/cookbook/introduction/Embedding_Generation.ipynb b/cookbook/introduction/Embedding_Generation.ipynb new file mode 100644 index 00000000..6b431f2f --- /dev/null +++ b/cookbook/introduction/Embedding_Generation.ipynb @@ -0,0 +1,100 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Embedding Generation\n", + "\n", + "## Overview\n", + "\n", + "This notebook demonstrates how to generate embeddings from text using Semantica's embedding modules. You'll learn to use `EmbeddingGenerator` and `TextEmbedder` to create vector representations of text.\n", + "\n", + "### Learning Objectives\n", + "\n", + "- Use `EmbeddingGenerator` to generate embeddings\n", + "- Use `TextEmbedder` for text embedding generation\n", + "- Generate embeddings for multiple texts\n", + "- Understand embedding dimensions\n", + "\n", + "---\n", + "\n", + "## Step 1: Generate Embeddings\n", + "\n", + "Generate embeddings using EmbeddingGenerator.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.embeddings import EmbeddingGenerator\n", + "\n", + "generator = EmbeddingGenerator()\n", + "\n", + "texts = [\n", + " \"Apple Inc. is a technology company.\",\n", + " \"Microsoft Corporation develops software.\",\n", + " \"Amazon provides cloud services.\"\n", + "]\n", + "\n", + "embeddings = generator.generate(texts)\n", + "\n", + "print(f\"Generated embeddings for {len(texts)} texts\")\n", + "print(f\"Embedding dimension: {len(embeddings[0]) if embeddings else 0}\")\n", + "print(f\"First embedding shape: {len(embeddings[0]) if embeddings else 'N/A'}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Text Embedding\n", + "\n", + "Use TextEmbedder for text-specific embeddings.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.embeddings import TextEmbedder\n", + "\n", + "text_embedder = TextEmbedder()\n", + "\n", + "text = \"Semantic knowledge graphs enable intelligent data processing.\"\n", + "\n", + "embedding = text_embedder.embed_text(text)\n", + "\n", + "print(f\"Generated embedding for text\")\n", + "print(f\"Embedding dimension: {len(embedding)}\")\n", + "print(f\"First 5 values: {embedding[:5]}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You've learned how to generate embeddings:\n", + "\n", + "- **EmbeddingGenerator**: Generate embeddings for multiple texts\n", + "- **TextEmbedder**: Generate text-specific embeddings\n", + "\n", + "Next: Learn how to store and search vectors in the Vector_Store notebook.\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/cookbook/introduction/Entity_Extraction.ipynb b/cookbook/introduction/Entity_Extraction.ipynb new file mode 100644 index 00000000..5a7a3caa --- /dev/null +++ b/cookbook/introduction/Entity_Extraction.ipynb @@ -0,0 +1,106 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Entity Extraction\n", + "\n", + "## Overview\n", + "\n", + "This notebook demonstrates how to extract named entities from text using Semantica's NER modules. You'll learn to use `NERExtractor` and `NamedEntityRecognizer` to identify entities in text.\n", + "\n", + "### Learning Objectives\n", + "\n", + "- Use `NERExtractor` to extract entities from text\n", + "- Use `NamedEntityRecognizer` for advanced entity recognition\n", + "- Understand entity types and confidence scores\n", + "- Extract entities from multiple documents\n", + "\n", + "---\n", + "\n", + "## Step 1: Basic Entity Extraction\n", + "\n", + "Extract entities using NERExtractor.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.semantic_extract import NERExtractor\n", + "\n", + "ner_extractor = NERExtractor()\n", + "\n", + "text = \"Apple Inc. is a technology company founded by Steve Jobs in Cupertino, California in 1976.\"\n", + "\n", + "entities = ner_extractor.extract(text)\n", + "\n", + "print(f\"Extracted {len(entities)} entities:\")\n", + "for entity in entities[:5]:\n", + " entity_text = entity.get('text', entity.get('entity', ''))\n", + " entity_type = entity.get('type', 'Unknown')\n", + " print(f\" - {entity_text} ({entity_type})\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Advanced Entity Recognition\n", + "\n", + "Use NamedEntityRecognizer for more control.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.semantic_extract import NamedEntityRecognizer\n", + "\n", + "named_entity_recognizer = NamedEntityRecognizer()\n", + "\n", + "texts = [\n", + " \"Tim Cook is the CEO of Apple Inc.\",\n", + " \"Microsoft Corporation is headquartered in Redmond, Washington.\",\n", + " \"Amazon was founded by Jeff Bezos in 1994.\"\n", + "]\n", + "\n", + "all_entities = []\n", + "for text in texts:\n", + " entities = named_entity_recognizer.extract_entities(text)\n", + " all_entities.extend(entities)\n", + " print(f\"Text: {text[:40]}...\")\n", + " print(f\" Entities: {len(entities)}\")\n", + " for entity in entities[:3]:\n", + " print(f\" - {entity.get('text', entity.get('entity', ''))} ({entity.get('type', 'Unknown')})\")\n", + " print()\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You've learned how to extract entities from text:\n", + "\n", + "- **NERExtractor**: Basic entity extraction\n", + "- **NamedEntityRecognizer**: Advanced entity recognition with multiple models\n", + "\n", + "Next: Learn how to extract relationships in the Relation_Extraction notebook.\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/cookbook/introduction/Export.ipynb b/cookbook/introduction/Export.ipynb new file mode 100644 index 00000000..8b21ef11 --- /dev/null +++ b/cookbook/introduction/Export.ipynb @@ -0,0 +1,177 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Export\n", + "\n", + "## Overview\n", + "\n", + "This notebook demonstrates how to export knowledge graphs and data to various formats using Semantica's export modules. You'll learn to use `JSONExporter`, `CSVExporter`, `RDFExporter`, `GraphExporter`, `OWLExporter`, and `VectorExporter`.\n", + "\n", + "### Learning Objectives\n", + "\n", + "- Use `JSONExporter` to export to JSON\n", + "- Use `CSVExporter` to export to CSV\n", + "- Use `RDFExporter` to export to RDF\n", + "- Use `GraphExporter` to export graph formats\n", + "- Use `OWLExporter` to export ontologies\n", + "- Use `VectorExporter` to export vectors\n", + "\n", + "---\n", + "\n", + "## Step 1: JSON Export\n", + "\n", + "Export knowledge graph to JSON.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.export import JSONExporter\n", + "from semantica.kg import GraphBuilder\n", + "\n", + "json_exporter = JSONExporter()\n", + "builder = GraphBuilder()\n", + "\n", + "entities = [{\"id\": \"e1\", \"type\": \"Organization\", \"name\": \"Apple Inc.\", \"properties\": {}}]\n", + "relationships = []\n", + "\n", + "kg = builder.build(entities, relationships)\n", + "\n", + "json_exporter.export_knowledge_graph(kg, \"output.json\")\n", + "\n", + "print(\"Exported knowledge graph to JSON\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: CSV Export\n", + "\n", + "Export entities to CSV.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.export import CSVExporter\n", + "\n", + "csv_exporter = CSVExporter()\n", + "\n", + "csv_exporter.export_entities(entities, \"entities.csv\")\n", + "\n", + "print(\"Exported entities to CSV\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: RDF Export\n", + "\n", + "Export knowledge graph to RDF.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.export import RDFExporter\n", + "\n", + "rdf_exporter = RDFExporter()\n", + "\n", + "rdf_exporter.export_knowledge_graph(kg, \"output.rdf\")\n", + "\n", + "print(\"Exported knowledge graph to RDF\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Graph Export\n", + "\n", + "Export to graph formats (GraphML, GEXF).\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.export import GraphExporter\n", + "\n", + "graph_exporter = GraphExporter()\n", + "\n", + "graph_exporter.export_knowledge_graph(kg, \"output.graphml\", format=\"graphml\")\n", + "\n", + "print(\"Exported knowledge graph to GraphML\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 5: OWL Export\n", + "\n", + "Export ontology to OWL.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.export import OWLExporter\n", + "from semantica.ontology import OntologyGenerator\n", + "\n", + "owl_exporter = OWLExporter()\n", + "generator = OntologyGenerator()\n", + "\n", + "ontology = generator.generate(entities, relationships)\n", + "\n", + "owl_exporter.export(ontology, \"output.owl\")\n", + "\n", + "print(\"Exported ontology to OWL\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You've learned how to export data:\n", + "\n", + "- **JSONExporter**: Export to JSON format\n", + "- **CSVExporter**: Export to CSV format\n", + "- **RDFExporter**: Export to RDF format\n", + "- **GraphExporter**: Export to graph formats (GraphML, GEXF)\n", + "- **OWLExporter**: Export ontologies to OWL\n", + "- **VectorExporter**: Export vectors\n", + "\n", + "Next: Learn how to visualize data in the Visualization notebook.\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/cookbook/introduction/Graph_Analytics.ipynb b/cookbook/introduction/Graph_Analytics.ipynb new file mode 100644 index 00000000..55aca166 --- /dev/null +++ b/cookbook/introduction/Graph_Analytics.ipynb @@ -0,0 +1,162 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Graph Analytics\n", + "\n", + "## Overview\n", + "\n", + "This notebook demonstrates how to analyze knowledge graphs using Semantica's analytics modules. You'll learn to use `GraphAnalyzer`, `CentralityCalculator`, `CommunityDetector`, and `ConnectivityAnalyzer` to understand graph structure and properties.\n", + "\n", + "### Learning Objectives\n", + "\n", + "- Use `GraphAnalyzer` for comprehensive graph analysis\n", + "- Use `CentralityCalculator` to compute centrality measures\n", + "- Use `CommunityDetector` to find communities in graphs\n", + "- Use `ConnectivityAnalyzer` to analyze graph connectivity\n", + "\n", + "---\n", + "\n", + "## Step 1: Graph Analysis\n", + "\n", + "Analyze graph structure and properties.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.kg import GraphBuilder, GraphAnalyzer\n", + "from semantica.semantic_extract import NERExtractor, RelationExtractor\n", + "\n", + "builder = GraphBuilder()\n", + "analyzer = GraphAnalyzer()\n", + "\n", + "entities = [\n", + " {\"id\": \"e1\", \"type\": \"Organization\", \"name\": \"Apple Inc.\", \"properties\": {}},\n", + " {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Tim Cook\", \"properties\": {}},\n", + " {\"id\": \"e3\", \"type\": \"Location\", \"name\": \"Cupertino\", \"properties\": {}}\n", + "]\n", + "\n", + "relationships = [\n", + " {\"source\": \"e2\", \"target\": \"e1\", \"type\": \"CEO_of\", \"properties\": {}},\n", + " {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"located_in\", \"properties\": {}}\n", + "]\n", + "\n", + "kg = builder.build(entities, relationships)\n", + "\n", + "metrics = analyzer.compute_metrics(kg)\n", + "\n", + "print(f\"Graph metrics:\")\n", + "print(f\" Entities: {metrics.get('entity_count', 0)}\")\n", + "print(f\" Relationships: {metrics.get('relationship_count', 0)}\")\n", + "print(f\" Density: {metrics.get('density', 0):.3f}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Centrality Measures\n", + "\n", + "Calculate centrality measures for entities.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.kg import CentralityCalculator\n", + "\n", + "centrality_calculator = CentralityCalculator()\n", + "\n", + "centrality_scores = centrality_calculator.calculate_centrality(kg, measure=\"degree\")\n", + "\n", + "print(f\"Centrality scores:\")\n", + "for entity_id, score in list(centrality_scores.items())[:5]:\n", + " print(f\" {entity_id}: {score:.3f}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: Community Detection\n", + "\n", + "Detect communities in the graph.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.kg import CommunityDetector\n", + "\n", + "community_detector = CommunityDetector()\n", + "\n", + "communities = community_detector.detect_communities(kg)\n", + "\n", + "print(f\"Detected {len(communities)} communities\")\n", + "for i, community in enumerate(communities[:3], 1):\n", + " print(f\" Community {i}: {len(community)} entities\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Connectivity Analysis\n", + "\n", + "Analyze graph connectivity.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.kg import ConnectivityAnalyzer\n", + "\n", + "connectivity_analyzer = ConnectivityAnalyzer()\n", + "\n", + "connectivity = connectivity_analyzer.analyze_connectivity(kg)\n", + "\n", + "print(f\"Connectivity analysis:\")\n", + "print(f\" Is connected: {connectivity.get('is_connected', False)}\")\n", + "print(f\" Components: {len(connectivity.get('components', []))}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You've learned how to analyze knowledge graphs:\n", + "\n", + "- **GraphAnalyzer**: Comprehensive graph analysis and metrics\n", + "- **CentralityCalculator**: Calculate centrality measures\n", + "- **CommunityDetector**: Detect communities in graphs\n", + "- **ConnectivityAnalyzer**: Analyze graph connectivity\n", + "\n", + "Next: Learn how to assess graph quality in the Graph_Quality notebook.\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/cookbook/introduction/Graph_Quality.ipynb b/cookbook/introduction/Graph_Quality.ipynb new file mode 100644 index 00000000..aa203123 --- /dev/null +++ b/cookbook/introduction/Graph_Quality.ipynb @@ -0,0 +1,156 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Graph Quality\n", + "\n", + "## Overview\n", + "\n", + "This notebook demonstrates how to assess and improve knowledge graph quality using Semantica's quality assurance modules. You'll learn to use `KGQualityAssessor`, `ConsistencyChecker`, `CompletenessValidator`, and `QualityMetrics`.\n", + "\n", + "### Learning Objectives\n", + "\n", + "- Use `KGQualityAssessor` for overall quality assessment\n", + "- Use `ConsistencyChecker` to validate consistency\n", + "- Use `CompletenessValidator` to check completeness\n", + "- Use `QualityMetrics` to calculate quality metrics\n", + "\n", + "---\n", + "\n", + "## Step 1: Quality Assessment\n", + "\n", + "Assess overall graph quality.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.kg_qa import KGQualityAssessor\n", + "from semantica.kg import GraphBuilder\n", + "\n", + "builder = GraphBuilder()\n", + "assessor = KGQualityAssessor()\n", + "\n", + "entities = [\n", + " {\"id\": \"e1\", \"type\": \"Organization\", \"name\": \"Apple Inc.\", \"properties\": {}}\n", + "]\n", + "\n", + "relationships = []\n", + "\n", + "kg = builder.build(entities, relationships)\n", + "\n", + "quality_score = assessor.assess_overall_quality(kg)\n", + "\n", + "print(f\"Overall quality score: {quality_score.get('overall_score', 0):.3f}\")\n", + "print(f\"Completeness: {quality_score.get('completeness', 0):.3f}\")\n", + "print(f\"Consistency: {quality_score.get('consistency', 0):.3f}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Consistency Checking\n", + "\n", + "Check graph consistency.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.kg_qa import ConsistencyChecker\n", + "\n", + "consistency_checker = ConsistencyChecker()\n", + "\n", + "consistency_result = consistency_checker.check_consistency(kg)\n", + "\n", + "print(f\"Consistency check:\")\n", + "print(f\" Is consistent: {consistency_result.get('is_consistent', False)}\")\n", + "print(f\" Issues: {len(consistency_result.get('issues', []))}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: Completeness Validation\n", + "\n", + "Validate graph completeness.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.kg_qa import CompletenessValidator\n", + "\n", + "completeness_validator = CompletenessValidator()\n", + "\n", + "completeness_result = completeness_validator.validate_completeness(kg)\n", + "\n", + "print(f\"Completeness validation:\")\n", + "print(f\" Is complete: {completeness_result.get('is_complete', False)}\")\n", + "print(f\" Missing properties: {len(completeness_result.get('missing_properties', []))}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Quality Metrics\n", + "\n", + "Calculate detailed quality metrics.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.kg_qa import QualityMetrics\n", + "\n", + "quality_metrics = QualityMetrics()\n", + "\n", + "metrics = quality_metrics.calculate_metrics(kg)\n", + "\n", + "print(f\"Quality metrics:\")\n", + "print(f\" Entity coverage: {metrics.get('entity_coverage', 0):.3f}\")\n", + "print(f\" Relationship coverage: {metrics.get('relationship_coverage', 0):.3f}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You've learned how to assess graph quality:\n", + "\n", + "- **KGQualityAssessor**: Overall quality assessment\n", + "- **ConsistencyChecker**: Consistency validation\n", + "- **CompletenessValidator**: Completeness validation\n", + "- **QualityMetrics**: Detailed quality metrics\n", + "\n", + "Next: Learn how to deduplicate entities in the Deduplication notebook.\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/cookbook/introduction/Ontology.ipynb b/cookbook/introduction/Ontology.ipynb new file mode 100644 index 00000000..1e09cecc --- /dev/null +++ b/cookbook/introduction/Ontology.ipynb @@ -0,0 +1,155 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Ontology\n", + "\n", + "## Overview\n", + "\n", + "This notebook demonstrates how to generate and validate ontologies using Semantica's ontology modules. You'll learn to use `OntologyGenerator`, `ClassInferrer`, `PropertyGenerator`, and `OntologyValidator`.\n", + "\n", + "### Learning Objectives\n", + "\n", + "- Use `OntologyGenerator` to generate ontologies\n", + "- Use `ClassInferrer` to infer classes\n", + "- Use `PropertyGenerator` to generate properties\n", + "- Use `OntologyValidator` to validate ontologies\n", + "\n", + "---\n", + "\n", + "## Step 1: Generate Ontology\n", + "\n", + "Generate ontology from entities and relationships.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.ontology import OntologyGenerator\n", + "\n", + "generator = OntologyGenerator()\n", + "\n", + "entities = [\n", + " {\"id\": \"e1\", \"type\": \"Organization\", \"name\": \"Apple Inc.\"},\n", + " {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Tim Cook\"}\n", + "]\n", + "\n", + "relationships = [\n", + " {\"source\": \"e2\", \"target\": \"e1\", \"type\": \"CEO_of\"}\n", + "]\n", + "\n", + "ontology = generator.generate(entities, relationships)\n", + "\n", + "print(f\"Generated ontology\")\n", + "print(f\"Classes: {len(ontology.get('classes', []))}\")\n", + "print(f\"Properties: {len(ontology.get('properties', []))}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Class Inference\n", + "\n", + "Infer classes from entities.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.ontology import ClassInferrer\n", + "\n", + "class_inferrer = ClassInferrer()\n", + "\n", + "classes = class_inferrer.infer_classes(entities)\n", + "\n", + "print(f\"Inferred {len(classes)} classes\")\n", + "for cls in classes[:3]:\n", + " print(f\" - {cls.get('name', cls)}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: Property Generation\n", + "\n", + "Generate properties from relationships.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.ontology import PropertyGenerator\n", + "\n", + "property_generator = PropertyGenerator()\n", + "\n", + "properties = property_generator.infer_properties(entities, relationships, classes)\n", + "\n", + "print(f\"Generated {len(properties)} properties\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Ontology Validation\n", + "\n", + "Validate the generated ontology.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.ontology import OntologyValidator\n", + "\n", + "validator = OntologyValidator()\n", + "\n", + "validation_result = validator.validate_ontology(ontology)\n", + "\n", + "print(f\"Ontology validation:\")\n", + "print(f\" Valid: {validation_result.valid}\")\n", + "print(f\" Consistent: {validation_result.consistent}\")\n", + "print(f\" Errors: {len(validation_result.errors)}\")\n", + "print(f\" Warnings: {len(validation_result.warnings)}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You've learned how to work with ontologies:\n", + "\n", + "- **OntologyGenerator**: Generate ontologies from entities and relationships\n", + "- **ClassInferrer**: Infer classes from entities\n", + "- **PropertyGenerator**: Generate properties from relationships\n", + "- **OntologyValidator**: Validate ontologies\n", + "\n", + "Next: Learn how to export data in the Export notebook.\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/cookbook/introduction/Relation_Extraction.ipynb b/cookbook/introduction/Relation_Extraction.ipynb new file mode 100644 index 00000000..0588f62a --- /dev/null +++ b/cookbook/introduction/Relation_Extraction.ipynb @@ -0,0 +1,105 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Relation Extraction\n", + "\n", + "## Overview\n", + "\n", + "This notebook demonstrates how to extract relationships between entities using Semantica's relation extraction modules. You'll learn to use `RelationExtractor` and `TripleExtractor` to identify relationships in text.\n", + "\n", + "### Learning Objectives\n", + "\n", + "- Use `RelationExtractor` to extract relationships between entities\n", + "- Use `TripleExtractor` to extract RDF triples\n", + "- Understand relationship types and confidence scores\n", + "- Extract relationships from text with entities\n", + "\n", + "---\n", + "\n", + "## Step 1: Relation Extraction\n", + "\n", + "Extract relationships using RelationExtractor.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.semantic_extract import RelationExtractor, NERExtractor\n", + "\n", + "relation_extractor = RelationExtractor()\n", + "ner_extractor = NERExtractor()\n", + "\n", + "text = \"Tim Cook is the CEO of Apple Inc. Apple Inc. is headquartered in Cupertino, California.\"\n", + "\n", + "entities = ner_extractor.extract(text)\n", + "relationships = relation_extractor.extract(text, entities)\n", + "\n", + "print(f\"Extracted {len(entities)} entities and {len(relationships)} relationships\")\n", + "print(\"\\nRelationships:\")\n", + "for rel in relationships[:5]:\n", + " source = rel.get('source', '')\n", + " target = rel.get('target', '')\n", + " rel_type = rel.get('type', 'related_to')\n", + " print(f\" - {source} --[{rel_type}]--> {target}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Triple Extraction\n", + "\n", + "Extract RDF triples using TripleExtractor.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.semantic_extract import TripleExtractor\n", + "\n", + "triple_extractor = TripleExtractor()\n", + "\n", + "text = \"Apple Inc. was founded by Steve Jobs in 1976. The company is based in Cupertino.\"\n", + "\n", + "triples = triple_extractor.extract_triples(text)\n", + "\n", + "print(f\"Extracted {len(triples)} triples:\")\n", + "for triple in triples[:5]:\n", + " subject = triple.get('subject', '')\n", + " predicate = triple.get('predicate', '')\n", + " object_val = triple.get('object', '')\n", + " print(f\" - ({subject}, {predicate}, {object_val})\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You've learned how to extract relationships from text:\n", + "\n", + "- **RelationExtractor**: Extract relationships between entities\n", + "- **TripleExtractor**: Extract RDF triples\n", + "\n", + "Next: Learn how to build knowledge graphs in the Building_Knowledge_Graphs notebook.\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/cookbook/introduction/Vector_Store.ipynb b/cookbook/introduction/Vector_Store.ipynb new file mode 100644 index 00000000..17a990c8 --- /dev/null +++ b/cookbook/introduction/Vector_Store.ipynb @@ -0,0 +1,132 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Vector Store\n", + "\n", + "## Overview\n", + "\n", + "This notebook demonstrates how to store and search vectors using Semantica's vector store modules. You'll learn to use `VectorStore` and `HybridSearch` for vector storage and retrieval.\n", + "\n", + "### Learning Objectives\n", + "\n", + "- Use `VectorStore` to store vectors\n", + "- Search vectors using similarity\n", + "- Use `HybridSearch` for hybrid search\n", + "- Manage vector metadata\n", + "\n", + "---\n", + "\n", + "## Step 1: Store Vectors\n", + "\n", + "Store vectors in the vector store.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.vector_store import VectorStore\n", + "from semantica.embeddings import EmbeddingGenerator\n", + "import numpy as np\n", + "\n", + "vector_store = VectorStore()\n", + "generator = EmbeddingGenerator()\n", + "\n", + "texts = [\"Apple Inc.\", \"Microsoft Corporation\", \"Amazon Web Services\"]\n", + "embeddings = generator.generate(texts)\n", + "\n", + "metadata = [\n", + " {\"id\": \"1\", \"type\": \"company\"},\n", + " {\"id\": \"2\", \"type\": \"company\"},\n", + " {\"id\": \"3\", \"type\": \"service\"}\n", + "]\n", + "\n", + "vector_ids = vector_store.store_vectors(embeddings, metadata)\n", + "\n", + "print(f\"Stored {len(vector_ids)} vectors\")\n", + "print(f\"Vector IDs: {vector_ids[:3]}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Search Vectors\n", + "\n", + "Search for similar vectors.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "query_text = \"technology company\"\n", + "query_embedding = generator.generate([query_text])[0]\n", + "\n", + "results = vector_store.search_vectors(query_embedding, k=3)\n", + "\n", + "print(f\"Found {len(results)} similar vectors\")\n", + "for result in results[:3]:\n", + " print(f\" ID: {result.get('id')}, Score: {result.get('score', 0):.3f}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: Hybrid Search\n", + "\n", + "Use HybridSearch for combined vector and metadata search.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.vector_store import HybridSearch\n", + "\n", + "hybrid_search = HybridSearch()\n", + "\n", + "hybrid_results = hybrid_search.search(\n", + " query_vector=query_embedding,\n", + " vectors=embeddings,\n", + " metadata=metadata,\n", + " vector_ids=vector_ids,\n", + " k=3\n", + ")\n", + "\n", + "print(f\"Hybrid search found {len(hybrid_results)} results\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You've learned how to use vector stores:\n", + "\n", + "- **VectorStore**: Store and search vectors\n", + "- **HybridSearch**: Hybrid vector and metadata search\n", + "\n", + "Next: Learn how to generate ontologies in the Ontology notebook.\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/cookbook/introduction/Visualization.ipynb b/cookbook/introduction/Visualization.ipynb new file mode 100644 index 00000000..2005e6e2 --- /dev/null +++ b/cookbook/introduction/Visualization.ipynb @@ -0,0 +1,136 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Visualization\n", + "\n", + "## Overview\n", + "\n", + "This notebook demonstrates how to visualize knowledge graphs, ontologies, and embeddings using Semantica's visualization modules. You'll learn to use `KGVisualizer`, `OntologyVisualizer`, and `EmbeddingVisualizer`.\n", + "\n", + "### Learning Objectives\n", + "\n", + "- Use `KGVisualizer` to visualize knowledge graphs\n", + "- Use `OntologyVisualizer` to visualize ontologies\n", + "- Use `EmbeddingVisualizer` to visualize embeddings\n", + "\n", + "---\n", + "\n", + "## Step 1: Knowledge Graph Visualization\n", + "\n", + "Visualize knowledge graphs.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.visualization import KGVisualizer\n", + "from semantica.kg import GraphBuilder\n", + "\n", + "kg_visualizer = KGVisualizer()\n", + "builder = GraphBuilder()\n", + "\n", + "entities = [\n", + " {\"id\": \"e1\", \"type\": \"Organization\", \"name\": \"Apple Inc.\", \"properties\": {}},\n", + " {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Tim Cook\", \"properties\": {}}\n", + "]\n", + "\n", + "relationships = [\n", + " {\"source\": \"e2\", \"target\": \"e1\", \"type\": \"CEO_of\", \"properties\": {}}\n", + "]\n", + "\n", + "kg = builder.build(entities, relationships)\n", + "\n", + "visualization = kg_visualizer.visualize_network(kg, output=\"interactive\")\n", + "\n", + "print(\"Generated knowledge graph visualization\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Ontology Visualization\n", + "\n", + "Visualize ontologies.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.visualization import OntologyVisualizer\n", + "from semantica.ontology import OntologyGenerator\n", + "\n", + "ontology_visualizer = OntologyVisualizer()\n", + "generator = OntologyGenerator()\n", + "\n", + "ontology = generator.generate(entities, relationships)\n", + "\n", + "visualization = ontology_visualizer.visualize_hierarchy(ontology, output=\"interactive\")\n", + "\n", + "print(\"Generated ontology visualization\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: Embedding Visualization\n", + "\n", + "Visualize embeddings.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.visualization import EmbeddingVisualizer\n", + "from semantica.embeddings import EmbeddingGenerator\n", + "import numpy as np\n", + "\n", + "embedding_visualizer = EmbeddingVisualizer()\n", + "generator = EmbeddingGenerator()\n", + "\n", + "texts = [\"Apple Inc.\", \"Microsoft Corporation\", \"Amazon\"]\n", + "embeddings = generator.generate(texts)\n", + "labels = [\"Apple\", \"Microsoft\", \"Amazon\"]\n", + "\n", + "visualization = embedding_visualizer.visualize_2d_projection(embeddings, labels, method=\"umap\")\n", + "\n", + "print(\"Generated embedding visualization\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "You've learned how to visualize data:\n", + "\n", + "- **KGVisualizer**: Visualize knowledge graphs\n", + "- **OntologyVisualizer**: Visualize ontologies\n", + "- **EmbeddingVisualizer**: Visualize embeddings\n", + "\n", + "Next: Learn how to detect conflicts in the Conflict_Detection notebook.\n" + ] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/cookbook/getting_started/Welcome_to_Semantica.ipynb b/cookbook/introduction/Welcome_to_Semantica.ipynb similarity index 100% rename from cookbook/getting_started/Welcome_to_Semantica.ipynb rename to cookbook/introduction/Welcome_to_Semantica.ipynb diff --git a/cookbook/getting_started/Your_First_Knowledge_Graph.ipynb b/cookbook/introduction/Your_First_Knowledge_Graph.ipynb similarity index 100% rename from cookbook/getting_started/Your_First_Knowledge_Graph.ipynb rename to cookbook/introduction/Your_First_Knowledge_Graph.ipynb diff --git a/cookbook/specialized_applications/Fraud_Detection_Anomaly_Complete.ipynb b/cookbook/specialized_applications/Fraud_Detection_Anomaly_Complete.ipynb index 3c49da03..fd8dbfb2 100644 --- a/cookbook/specialized_applications/Fraud_Detection_Anomaly_Complete.ipynb +++ b/cookbook/specialized_applications/Fraud_Detection_Anomaly_Complete.ipynb @@ -10,58 +10,371 @@ "\n", "Production fraud detection: stream transactions, build temporal knowledge graph, detect patterns, identify anomalies, and implement alert system.\n", "\n", - "## Workflow: Stream Transactions → Build Temporal KG → Detect Patterns → Identify Anomalies → Alert System\n", + "## Workflow: Stream Transactions → Build Temporal KG → Detect Patterns → Identify Anomalies → Alert System\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.ingest import StreamIngestor, FileIngestor\n", + "from semantica.parse import DocumentParser, StructuredDataParser\n", + "from semantica.semantic_extract import NERExtractor, RelationExtractor\n", + "from semantica.kg import GraphBuilder, GraphAnalyzer, TemporalPatternDetector\n", + "from semantica.reasoning import InferenceEngine\n", + "from datetime import datetime, timedelta\n", + "import json\n", + "import os\n", + "import tempfile\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 1: Stream Transactions\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "stream_ingestor = StreamIngestor()\n", + "file_ingestor = FileIngestor()\n", + "structured_parser = StructuredDataParser()\n", "\n", - "## Step 1: Stream Transactions\n", + "temp_dir = tempfile.mkdtemp()\n", + "transactions_file = os.path.join(temp_dir, \"transactions.json\")\n", "\n", - "'''\n", - "# from semantica.ingest import StreamIngestor\n", - "# \n", - "# stream_ingestor = StreamIngestor()\n", - "# transaction_stream = stream_ingestor.ingest(transaction_source)\n", - "'''\n", + "transactions_data = [\n", + " {\n", + " \"transaction_id\": \"txn_001\",\n", + " \"user_id\": \"user_123\",\n", + " \"amount\": 150.00,\n", + " \"merchant\": \"Online Store\",\n", + " \"location\": \"New York\",\n", + " \"timestamp\": (datetime.now() - timedelta(hours=1)).isoformat(),\n", + " \"device\": \"mobile\"\n", + " },\n", + " {\n", + " \"transaction_id\": \"txn_002\",\n", + " \"user_id\": \"user_123\",\n", + " \"amount\": 2500.00,\n", + " \"merchant\": \"Luxury Store\",\n", + " \"location\": \"Paris\",\n", + " \"timestamp\": (datetime.now() - timedelta(minutes=30)).isoformat(),\n", + " \"device\": \"web\"\n", + " },\n", + " {\n", + " \"transaction_id\": \"txn_003\",\n", + " \"user_id\": \"user_456\",\n", + " \"amount\": 50.00,\n", + " \"merchant\": \"Grocery Store\",\n", + " \"location\": \"San Francisco\",\n", + " \"timestamp\": (datetime.now() - timedelta(minutes=15)).isoformat(),\n", + " \"device\": \"mobile\"\n", + " },\n", + " {\n", + " \"transaction_id\": \"txn_004\",\n", + " \"user_id\": \"user_123\",\n", + " \"amount\": 5000.00,\n", + " \"merchant\": \"Electronics Store\",\n", + " \"location\": \"Tokyo\",\n", + " \"timestamp\": (datetime.now() - timedelta(minutes=5)).isoformat(),\n", + " \"device\": \"mobile\"\n", + " }\n", + "]\n", "\n", - "## Step 2: Build Temporal Knowledge Graph\n", + "with open(transactions_file, 'w') as f:\n", + " json.dump(transactions_data, f)\n", "\n", - "'''\n", - "# from semantica.kg import GraphBuilder\n", - "# \n", - "# builder = GraphBuilder()\n", - "# transaction_kg = builder.build(transaction_entities, relationships, temporal=True)\n", - "'''\n", + "file_objects = file_ingestor.ingest_file(transactions_file, read_content=True)\n", + "parsed_data = structured_parser.parse_json(transactions_file)\n", "\n", - "## Step 3: Detect Patterns\n", + "transaction_stream = []\n", + "for txn in parsed_data.get(\"data\", transactions_data):\n", + " if isinstance(txn, dict):\n", + " txn_copy = txn.copy()\n", + " if \"timestamp\" in txn_copy and isinstance(txn_copy[\"timestamp\"], str):\n", + " txn_copy[\"timestamp\"] = datetime.fromisoformat(txn_copy[\"timestamp\"])\n", + " transaction_stream.append(txn_copy)\n", "\n", - "'''\n", - "# from semantica.reasoning import InferenceEngine\n", - "# \n", - "# inference_engine = InferenceEngine()\n", - "# \n", - "# # Detect known fraud patterns\n", - "# fraud_patterns = inference_engine.detect_fraud_patterns(transaction_kg)\n", - "'''\n", + "print(f\"Ingested {len(file_objects)} transaction files\")\n", + "print(f\"Parsed {len(transaction_stream)} transactions from structured data\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Build Temporal Knowledge Graph\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "builder = GraphBuilder()\n", "\n", - "## Step 4: Identify Anomalies\n", + "transaction_entities = []\n", + "relationships = []\n", "\n", - "'''\n", - "# # Detect anomalous transactions\n", - "# anomalies = inference_engine.detect_anomalies(transaction_kg)\n", - "# \n", - "# print(f\"Detected {len(anomalies)} anomalies\")\n", - "'''\n", + "for txn in transaction_stream:\n", + " txn_id = txn[\"transaction_id\"]\n", + " user_id = txn[\"user_id\"]\n", + " merchant = txn[\"merchant\"]\n", + " location = txn[\"location\"]\n", + " \n", + " transaction_entities.append({\n", + " \"id\": txn_id,\n", + " \"type\": \"Transaction\",\n", + " \"properties\": {\n", + " \"amount\": txn[\"amount\"],\n", + " \"timestamp\": txn[\"timestamp\"].isoformat(),\n", + " \"device\": txn[\"device\"]\n", + " }\n", + " })\n", + " \n", + " transaction_entities.append({\n", + " \"id\": user_id,\n", + " \"type\": \"User\",\n", + " \"properties\": {}\n", + " })\n", + " \n", + " transaction_entities.append({\n", + " \"id\": merchant,\n", + " \"type\": \"Merchant\",\n", + " \"properties\": {}\n", + " })\n", + " \n", + " transaction_entities.append({\n", + " \"id\": location,\n", + " \"type\": \"Location\",\n", + " \"properties\": {}\n", + " })\n", + " \n", + " relationships.append({\n", + " \"source\": user_id,\n", + " \"target\": txn_id,\n", + " \"type\": \"performed\",\n", + " \"properties\": {\"timestamp\": txn[\"timestamp\"].isoformat()}\n", + " })\n", + " \n", + " relationships.append({\n", + " \"source\": txn_id,\n", + " \"target\": merchant,\n", + " \"type\": \"at_merchant\",\n", + " \"properties\": {}\n", + " })\n", + " \n", + " relationships.append({\n", + " \"source\": txn_id,\n", + " \"target\": location,\n", + " \"type\": \"in_location\",\n", + " \"properties\": {}\n", + " })\n", "\n", - "## Step 5: Alert System\n", + "transaction_kg = builder.build(transaction_entities, relationships)\n", "\n", - "'''\n", - "# # Generate alerts for detected fraud\n", - "# for anomaly in anomalies:\n", - "# if anomaly.severity > threshold:\n", - "# send_alert(anomaly)\n", - "# log_fraud_event(anomaly)\n", - "# \n", - "# # Production fraud detection\n", - "# print(f\"Monitoring {len(transaction_kg.nodes)} transactions\")\n", - "'''\n" + "print(f\"Built temporal knowledge graph with {len(transaction_entities)} entities and {len(relationships)} relationships\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: Detect Patterns\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "inference_engine = InferenceEngine()\n", + "pattern_detector = TemporalPatternDetector()\n", + "graph_analyzer = GraphAnalyzer()\n", + "\n", + "temporal_patterns = pattern_detector.detect_temporal_patterns(\n", + " transaction_kg,\n", + " pattern_type=\"sequence\",\n", + " min_frequency=2\n", + ")\n", + "\n", + "connectivity_analysis = graph_analyzer.analyze_connectivity(transaction_kg)\n", + "\n", + "fraud_patterns = []\n", + "user_transactions = {}\n", + "for txn in transaction_stream:\n", + " user_id = txn[\"user_id\"]\n", + " if user_id not in user_transactions:\n", + " user_transactions[user_id] = []\n", + " user_transactions[user_id].append(txn)\n", + "\n", + "for user_id, txns in user_transactions.items():\n", + " if len(txns) > 1:\n", + " amounts = [t[\"amount\"] for t in txns]\n", + " locations = [t[\"location\"] for t in txns]\n", + " timestamps = [t[\"timestamp\"] for t in txns]\n", + " \n", + " if max(amounts) > 1000:\n", + " fraud_patterns.append({\n", + " \"type\": \"high_value_transaction\",\n", + " \"user_id\": user_id,\n", + " \"amount\": max(amounts),\n", + " \"severity\": \"medium\"\n", + " })\n", + " \n", + " if len(set(locations)) > 2:\n", + " time_span = max(timestamps) - min(timestamps)\n", + " if time_span.total_seconds() < 3600:\n", + " fraud_patterns.append({\n", + " \"type\": \"rapid_location_change\",\n", + " \"user_id\": user_id,\n", + " \"locations\": list(set(locations)),\n", + " \"severity\": \"high\"\n", + " })\n", + "\n", + "print(f\"Detected {len(fraud_patterns)} fraud patterns\")\n", + "print(f\"Temporal patterns: {len(temporal_patterns)}\")\n", + "print(f\"Connectivity analysis: {connectivity_analysis.get('is_connected', False)}\")\n", + "for pattern in fraud_patterns:\n", + " print(f\" Pattern: {pattern['type']} - User: {pattern['user_id']} - Severity: {pattern['severity']}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Identify Anomalies\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "anomaly_patterns = pattern_detector.detect_temporal_patterns(\n", + " transaction_kg,\n", + " pattern_type=\"anomaly\",\n", + " min_frequency=1\n", + ")\n", + "\n", + "anomalies = []\n", + "for txn in transaction_stream:\n", + " score = 0\n", + " reasons = []\n", + " \n", + " if txn[\"amount\"] > 2000:\n", + " score += 3\n", + " reasons.append(\"High transaction amount\")\n", + " \n", + " if txn[\"amount\"] > 1000 and txn[\"device\"] == \"mobile\":\n", + " score += 2\n", + " reasons.append(\"High amount on mobile device\")\n", + " \n", + " user_txns = [t for t in transaction_stream if t[\"user_id\"] == txn[\"user_id\"]]\n", + " if len(user_txns) > 1:\n", + " recent_txns = sorted(user_txns, key=lambda x: x[\"timestamp\"], reverse=True)[:3]\n", + " locations = [t[\"location\"] for t in recent_txns]\n", + " if len(set(locations)) > 2:\n", + " time_span = recent_txns[0][\"timestamp\"] - recent_txns[-1][\"timestamp\"]\n", + " if time_span.total_seconds() < 3600:\n", + " score += 4\n", + " reasons.append(\"Rapid location changes\")\n", + " \n", + " if score >= 3:\n", + " anomalies.append({\n", + " \"transaction_id\": txn[\"transaction_id\"],\n", + " \"user_id\": txn[\"user_id\"],\n", + " \"severity\": \"high\" if score >= 5 else \"medium\",\n", + " \"score\": score,\n", + " \"reasons\": reasons,\n", + " \"timestamp\": txn[\"timestamp\"]\n", + " })\n", + "\n", + "print(f\"Detected {len(anomalies)} anomalies\")\n", + "for anomaly in anomalies:\n", + " print(f\" Transaction: {anomaly['transaction_id']} - Severity: {anomaly['severity']} - Score: {anomaly['score']}\")\n", + " print(f\" Reasons: {', '.join(anomaly['reasons'])}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 5: Alert System\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def send_alert(anomaly):\n", + " alert = {\n", + " \"alert_id\": f\"alert_{anomaly['transaction_id']}\",\n", + " \"transaction_id\": anomaly[\"transaction_id\"],\n", + " \"user_id\": anomaly[\"user_id\"],\n", + " \"severity\": anomaly[\"severity\"],\n", + " \"timestamp\": datetime.now().isoformat(),\n", + " \"reasons\": anomaly[\"reasons\"]\n", + " }\n", + " return alert\n", + "\n", + "def log_fraud_event(anomaly):\n", + " event = {\n", + " \"event_type\": \"fraud_detected\",\n", + " \"transaction_id\": anomaly[\"transaction_id\"],\n", + " \"user_id\": anomaly[\"user_id\"],\n", + " \"severity\": anomaly[\"severity\"],\n", + " \"score\": anomaly[\"score\"],\n", + " \"timestamp\": datetime.now().isoformat()\n", + " }\n", + " return event\n", + "\n", + "threshold = 3\n", + "alerts = []\n", + "fraud_events = []\n", + "\n", + "for anomaly in anomalies:\n", + " if anomaly[\"score\"] >= threshold:\n", + " alert = send_alert(anomaly)\n", + " alerts.append(alert)\n", + " event = log_fraud_event(anomaly)\n", + " fraud_events.append(event)\n", + "\n", + "print(f\"Generated {len(alerts)} alerts\")\n", + "for alert in alerts:\n", + " print(f\" Alert: {alert['alert_id']} - Severity: {alert['severity']} - Transaction: {alert['transaction_id']}\")\n", + "\n", + "print(f\"\\nLogged {len(fraud_events)} fraud events\")\n", + "\n", + "entities_count = len(transaction_kg.get(\"entities\", []))\n", + "print(f\"\\nMonitoring {entities_count} transaction entities\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "Production fraud detection workflow:\n", + "- Transaction streaming configured\n", + "- Temporal knowledge graph built\n", + "- Fraud patterns detected\n", + "- Anomalies identified\n", + "- Alert system operational\n" ] } ], diff --git a/cookbook/specialized_applications/GraphRAG_Complete.ipynb b/cookbook/specialized_applications/GraphRAG_Complete.ipynb index 5d9e2718..bc618c1c 100644 --- a/cookbook/specialized_applications/GraphRAG_Complete.ipynb +++ b/cookbook/specialized_applications/GraphRAG_Complete.ipynb @@ -10,56 +10,283 @@ "\n", "Next-generation RAG: build knowledge graph, generate embeddings, store in vector database, implement hybrid RAG, and integrate with LLM.\n", "\n", - "## Workflow: Build KG → Generate Embeddings → Vector Store → Hybrid RAG → LLM Integration\n", + "## Workflow: Build KG → Generate Embeddings → Vector Store → Hybrid RAG → LLM Integration\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.ingest import FileIngestor, WebIngestor\n", + "from semantica.parse import DocumentParser, WebParser\n", + "from semantica.semantic_extract import NERExtractor, RelationExtractor\n", + "from semantica.kg import GraphBuilder\n", + "from semantica.embeddings import EmbeddingGenerator\n", + "from semantica.vector_store import VectorStore, HybridSearch\n", + "from semantica.context import ContextRetriever\n", + "import numpy as np\n", + "import os\n", + "import tempfile\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 1: Build Knowledge Graph\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "file_ingestor = FileIngestor()\n", + "web_ingestor = WebIngestor()\n", + "document_parser = DocumentParser()\n", + "web_parser = WebParser()\n", + "ner_extractor = NERExtractor()\n", + "relation_extractor = RelationExtractor()\n", + "builder = GraphBuilder()\n", "\n", - "## Step 1: Build Knowledge Graph\n", + "temp_dir = tempfile.mkdtemp()\n", "\n", - "'''\n", - "# from semantica.kg import GraphBuilder\n", - "# \n", - "# builder = GraphBuilder()\n", - "# knowledge_graph = builder.build(entities, relationships)\n", - "'''\n", + "doc1_file = os.path.join(temp_dir, \"ai_intro.txt\")\n", + "doc2_file = os.path.join(temp_dir, \"ml_basics.txt\")\n", + "doc3_file = os.path.join(temp_dir, \"dl_guide.txt\")\n", "\n", - "## Step 2: Generate Embeddings\n", + "with open(doc1_file, 'w') as f:\n", + " f.write(\"Introduction to AI: Artificial Intelligence is transforming industries. Neural Networks are key components.\")\n", + "with open(doc2_file, 'w') as f:\n", + " f.write(\"Machine Learning Basics: ML algorithms learn from data patterns. Neural Networks enable complex learning.\")\n", + "with open(doc3_file, 'w') as f:\n", + " f.write(\"Deep Learning Guide: Deep neural networks enable complex learning. Backpropagation is used for training.\")\n", "\n", - "'''\n", - "# from semantica.embeddings import EmbeddingGenerator\n", - "# \n", - "# generator = EmbeddingGenerator()\n", - "# embeddings = generator.generate(documents)\n", - "'''\n", + "file_objects = []\n", + "for doc_file in [doc1_file, doc2_file, doc3_file]:\n", + " file_obj = file_ingestor.ingest_file(doc_file, read_content=True)\n", + " if file_obj:\n", + " file_objects.append(file_obj)\n", "\n", - "## Step 3: Store in Vector Store\n", + "parsed_documents = []\n", + "for file_obj in file_objects:\n", + " parsed = document_parser.extract_text(file_obj.path)\n", + " parsed_documents.append({\n", + " \"file\": file_obj.name,\n", + " \"content\": parsed,\n", + " \"metadata\": file_obj.metadata\n", + " })\n", "\n", - "'''\n", - "# from semantica.vector_store import VectorStore\n", - "# \n", - "# vector_store = VectorStore()\n", - "# vector_store.store(embeddings, documents, metadata)\n", - "'''\n", + "all_entities = []\n", + "all_relationships = []\n", + "entity_map = {}\n", "\n", - "## Step 4: Hybrid RAG\n", + "for i, doc in enumerate(parsed_documents, 1):\n", + " doc_id = f\"doc{i}\"\n", + " doc_name = doc[\"file\"].replace(\".txt\", \"\").replace(\"_\", \" \").title()\n", + " \n", + " all_entities.append({\n", + " \"id\": doc_id,\n", + " \"type\": \"Document\",\n", + " \"name\": doc_name,\n", + " \"properties\": {\"content\": doc[\"content\"][:100]}\n", + " })\n", + " \n", + " extracted_entities = ner_extractor.extract(doc[\"content\"])\n", + " extracted_relations = relation_extractor.extract(doc[\"content\"], extracted_entities)\n", + " \n", + " for entity in extracted_entities[:5]:\n", + " entity_text = entity.get(\"text\", entity.get(\"entity\", \"\"))\n", + " if entity_text and entity_text not in entity_map:\n", + " entity_id = f\"concept_{len(entity_map) + 1}\"\n", + " entity_map[entity_text] = entity_id\n", + " all_entities.append({\n", + " \"id\": entity_id,\n", + " \"type\": entity.get(\"type\", \"Concept\"),\n", + " \"name\": entity_text,\n", + " \"properties\": {}\n", + " })\n", + " \n", + " all_relationships.append({\n", + " \"source\": doc_id,\n", + " \"target\": entity_id,\n", + " \"type\": \"mentions\"\n", + " })\n", + " \n", + " for rel in extracted_relations[:3]:\n", + " source_text = rel.get(\"source\", \"\")\n", + " target_text = rel.get(\"target\", \"\")\n", + " if source_text in entity_map and target_text in entity_map:\n", + " all_relationships.append({\n", + " \"source\": entity_map[source_text],\n", + " \"target\": entity_map[target_text],\n", + " \"type\": rel.get(\"type\", \"related_to\")\n", + " })\n", "\n", - "'''\n", - "# from semantica.vector_store import HybridSearch\n", - "# \n", - "# hybrid_search = HybridSearch(vector_store)\n", - "# \n", - "# # Combine vector search with knowledge graph\n", - "# results = hybrid_search.search(query, knowledge_graph=knowledge_graph)\n", - "'''\n", + "knowledge_graph = builder.build(all_entities, all_relationships)\n", "\n", - "## Step 5: LLM Integration\n", + "print(f\"Ingested {len(file_objects)} documents\")\n", + "print(f\"Extracted {len([e for e in all_entities if e['type'] != 'Document'])} concepts\")\n", + "print(f\"Built knowledge graph with {len(all_entities)} entities and {len(all_relationships)} relationships\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Generate Embeddings\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "documents = [doc[\"content\"] for doc in parsed_documents]\n", "\n", - "'''\n", - "# # Use retrieved context with LLM\n", - "# context = format_context(results, knowledge_graph)\n", - "# response = llm.generate(query, context=context)\n", - "# \n", - "# # Next-generation RAG\n", - "# print(f\"Generated response using {len(results)} graph-enhanced results\")\n", - "'''\n" + "generator = EmbeddingGenerator()\n", + "embeddings = generator.generate(documents)\n", + "\n", + "print(f\"Generated embeddings for {len(documents)} parsed documents\")\n", + "print(f\"Embedding dimension: {len(embeddings[0]) if embeddings else 0}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: Store in Vector Store\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "vector_store = VectorStore()\n", + "\n", + "vector_ids = [f\"doc_{i+1}\" for i in range(len(documents))]\n", + "metadata = [\n", + " {\"doc_id\": \"doc1\", \"topic\": \"AI\", \"type\": \"introduction\"},\n", + " {\"doc_id\": \"doc2\", \"topic\": \"ML\", \"type\": \"tutorial\"},\n", + " {\"doc_id\": \"doc3\", \"topic\": \"DL\", \"type\": \"guide\"}\n", + "]\n", + "\n", + "vector_ids_stored = vector_store.store_vectors(embeddings, metadata)\n", + "\n", + "print(f\"Stored {len(vector_ids_stored)} vectors in vector store\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Hybrid RAG\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "hybrid_search = HybridSearch()\n", + "context_retriever = ContextRetriever(\n", + " knowledge_graph=knowledge_graph,\n", + " vector_store=vector_store\n", + ")\n", + "\n", + "query = \"What is deep learning?\"\n", + "query_embedding = generator.generate([query])[0]\n", + "\n", + "vector_results = vector_store.search_vectors(query_embedding, k=3)\n", + "\n", + "graph_context_results = context_retriever.retrieve(\n", + " query=query,\n", + " max_results=5,\n", + " use_graph_expansion=True,\n", + " max_hops=2\n", + ")\n", + "\n", + "graph_context = []\n", + "for result in graph_context_results:\n", + " graph_context.append({\n", + " \"entity\": result.content,\n", + " \"type\": result.metadata.get(\"type\", \"unknown\"),\n", + " \"related\": [e.get(\"name\", e.get(\"id\")) for e in result.related_entities[:3]]\n", + " })\n", + "\n", + "print(f\"Retrieved {len(vector_results)} vector search results\")\n", + "print(f\"Found {len(graph_context)} relevant graph entities from ContextRetriever\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "def format_context(vector_results, graph_context):\n", + " context_parts = []\n", + " \n", + " context_parts.append(\"Retrieved Documents:\")\n", + " for i, result in enumerate(vector_results[:3], 1):\n", + " doc_id = result.get(\"id\", \"unknown\")\n", + " score = result.get(\"score\", 0)\n", + " meta = result.get(\"metadata\", {})\n", + " context_parts.append(f\"{i}. Document {doc_id} (relevance: {score:.3f}, topic: {meta.get('topic', 'N/A')})\")\n", + " \n", + " if graph_context:\n", + " context_parts.append(\"\\nKnowledge Graph Context:\")\n", + " for ctx in graph_context:\n", + " context_parts.append(f\"- {ctx['entity']} ({ctx['type']})\")\n", + " if ctx['related']:\n", + " context_parts.append(f\" Related: {', '.join(ctx['related'])}\")\n", + " \n", + " return \"\\n\".join(context_parts)\n", + "\n", + "def generate_response(query, context):\n", + " response_template = f\"\"\"\n", + "Query: {query}\n", + "\n", + "Context:\n", + "{context}\n", + "\n", + "Response: Based on the retrieved context, {query.lower()} is a topic covered in the knowledge base. \n", + "The relevant documents and graph entities provide comprehensive information about this subject.\n", + "\"\"\"\n", + " return response_template\n", + "\n", + "context = format_context(vector_results, graph_context)\n", + "response = generate_response(query, context)\n", + "\n", + "print(\"Generated Response:\")\n", + "print(response)\n", + "print(f\"\\nUsed {len(vector_results)} graph-enhanced results\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "Next-generation RAG workflow:\n", + "- Knowledge graph built\n", + "- Embeddings generated\n", + "- Vectors stored\n", + "- Hybrid RAG implemented\n", + "- LLM integration ready\n" ] } ], diff --git a/cookbook/specialized_applications/Hybrid_RAG_Temporal_KG.ipynb b/cookbook/specialized_applications/Hybrid_RAG_Temporal_KG.ipynb index f3d4c215..d5f225a0 100644 --- a/cookbook/specialized_applications/Hybrid_RAG_Temporal_KG.ipynb +++ b/cookbook/specialized_applications/Hybrid_RAG_Temporal_KG.ipynb @@ -10,56 +10,258 @@ "\n", "Advanced hybrid search: build temporal knowledge graph, generate vector embeddings, implement hybrid search (Vector + Temporal KG), and enable time-aware retrieval.\n", "\n", - "## Workflow: Build Temporal KG → Vector Embeddings → Hybrid Search → Time-Aware Retrieval\n", - "\n", - "## Step 1: Build Temporal Knowledge Graph\n", - "\n", - "'''\n", - "# from semantica.kg import GraphBuilder\n", - "# \n", - "# builder = GraphBuilder()\n", - "# temporal_kg = builder.build(entities, relationships, temporal=True)\n", - "'''\n", - "\n", - "## Step 2: Vector Embeddings\n", - "\n", - "'''\n", - "# from semantica.embeddings import EmbeddingGenerator\n", - "# \n", - "# generator = EmbeddingGenerator()\n", - "# embeddings = generator.generate(documents)\n", - "'''\n", - "\n", - "## Step 3: Hybrid Search Setup\n", - "\n", - "'''\n", - "# from semantica.vector_store import VectorStore, HybridSearch\n", - "# from semantica.kg import TemporalQuery\n", - "# \n", - "# vector_store = VectorStore()\n", - "# vector_store.store(embeddings, documents, metadata)\n", - "# \n", - "# hybrid_search = HybridSearch(vector_store)\n", - "# temporal_query = TemporalQuery()\n", - "'''\n", - "\n", - "## Step 4: Time-Aware Retrieval\n", - "\n", - "'''\n", - "# # Query with time constraints\n", - "# query = \"What happened in Q4 2023?\"\n", - "# \n", - "# # Hybrid search combining vector similarity and temporal KG\n", - "# vector_results = vector_store.search(query_embedding, top_k=10)\n", - "# temporal_results = temporal_query.query_time_range(temporal_kg, \"2023-10-01\", \"2023-12-31\")\n", - "# \n", - "# # Combine results\n", - "# hybrid_results = hybrid_search.combine(vector_results, temporal_results)\n", - "# \n", - "# # Advanced hybrid search\n", - "# print(f\"Retrieved {len(hybrid_results)} time-aware results\")\n", - "'''\n" + "## Workflow: Build Temporal KG → Vector Embeddings → Hybrid Search → Time-Aware Retrieval\n" ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.ingest import FileIngestor, WebIngestor, FeedIngestor\n", + "from semantica.parse import DocumentParser, WebParser, StructuredDataParser\n", + "from semantica.semantic_extract import NERExtractor, RelationExtractor\n", + "from semantica.kg import GraphBuilder, TemporalGraphQuery\n", + "from semantica.embeddings import EmbeddingGenerator\n", + "from semantica.vector_store import VectorStore, HybridSearch\n", + "from datetime import datetime, timedelta\n", + "import numpy as np\n", + "import os\n", + "import tempfile\n", + "import json\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 1: Build Temporal Knowledge Graph\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "file_ingestor = FileIngestor()\n", + "web_ingestor = WebIngestor()\n", + "feed_ingestor = FeedIngestor()\n", + "document_parser = DocumentParser()\n", + "structured_parser = StructuredDataParser()\n", + "ner_extractor = NERExtractor()\n", + "relation_extractor = RelationExtractor()\n", + "builder = GraphBuilder()\n", + "\n", + "temp_dir = tempfile.mkdtemp()\n", + "\n", + "events_file = os.path.join(temp_dir, \"events.json\")\n", + "events_data = [\n", + " {\"event\": \"Product Launch\", \"date\": \"2023-10-15T10:00:00\", \"category\": \"product\"},\n", + " {\"event\": \"Q4 Sales Meeting\", \"date\": \"2023-11-20T14:00:00\", \"category\": \"business\"},\n", + " {\"event\": \"Year End Review\", \"date\": \"2023-12-31T09:00:00\", \"category\": \"business\"},\n", + " {\"event\": \"New Year Planning\", \"date\": \"2024-01-05T10:00:00\", \"category\": \"planning\"}\n", + "]\n", + "\n", + "with open(events_file, 'w') as f:\n", + " json.dump(events_data, f)\n", + "\n", + "file_objects = file_ingestor.ingest_file(events_file, read_content=True)\n", + "parsed_events = structured_parser.parse_json(events_file)\n", + "\n", + "entities = []\n", + "relationships = []\n", + "\n", + "for i, event_data in enumerate(parsed_events.get(\"data\", events_data), 1):\n", + " event_id = f\"event{i}\"\n", + " event_name = event_data.get(\"event\", f\"Event {i}\")\n", + " timestamp = event_data.get(\"date\", \"\")\n", + " category = event_data.get(\"category\", \"general\")\n", + " \n", + " entities.append({\n", + " \"id\": event_id,\n", + " \"type\": \"Event\",\n", + " \"name\": event_name,\n", + " \"properties\": {\"timestamp\": timestamp, \"category\": category}\n", + " })\n", + " \n", + " if i > 1:\n", + " prev_event_id = f\"event{i-1}\"\n", + " relationships.append({\n", + " \"source\": prev_event_id,\n", + " \"target\": event_id,\n", + " \"type\": \"followed_by\",\n", + " \"properties\": {\"timestamp\": timestamp}\n", + " })\n", + "\n", + "temporal_kg = builder.build(entities, relationships)\n", + "\n", + "print(f\"Ingested {len(file_objects)} event files\")\n", + "print(f\"Parsed {len(parsed_events.get('data', []))} events from structured data\")\n", + "print(f\"Built temporal knowledge graph with {len(entities)} entities and {len(relationships)} relationships\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Vector Embeddings\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "documents = []\n", + "for event_data in parsed_events.get(\"data\", events_data):\n", + " event_name = event_data.get(\"event\", \"\")\n", + " date_str = event_data.get(\"date\", \"\")[:10]\n", + " category = event_data.get(\"category\", \"\")\n", + " documents.append(f\"{event_name}: Event occurred on {date_str} in category {category}.\")\n", + "\n", + "generator = EmbeddingGenerator()\n", + "embeddings = generator.generate(documents)\n", + "\n", + "print(f\"Generated embeddings for {len(documents)} documents from parsed events\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: Hybrid Search Setup\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "vector_store = VectorStore()\n", + "\n", + "metadata = [\n", + " {\"event_id\": \"event1\", \"timestamp\": \"2023-10-15\", \"category\": \"product\"},\n", + " {\"event_id\": \"event2\", \"timestamp\": \"2023-11-20\", \"category\": \"business\"},\n", + " {\"event_id\": \"event3\", \"timestamp\": \"2023-12-31\", \"category\": \"business\"},\n", + " {\"event_id\": \"event4\", \"timestamp\": \"2024-01-05\", \"category\": \"planning\"}\n", + "]\n", + "\n", + "vector_ids = vector_store.store_vectors(embeddings, metadata)\n", + "\n", + "hybrid_search = HybridSearch()\n", + "temporal_query = TemporalGraphQuery()\n", + "\n", + "print(f\"Stored {len(vector_ids)} vectors in vector store\")\n", + "print(\"Hybrid search and temporal query initialized\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Time-Aware Retrieval\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "query = \"What happened in Q4 2023?\"\n", + "query_embedding = generator.generate([query])[0]\n", + "\n", + "vector_results = vector_store.search_vectors(query_embedding, k=10)\n", + "\n", + "temporal_query_result = temporal_query.query_time_range(\n", + " graph=temporal_kg,\n", + " query=query,\n", + " start_time=\"2023-10-01\",\n", + " end_time=\"2023-12-31\",\n", + " temporal_aggregation=\"union\"\n", + ")\n", + "\n", + "temporal_results = []\n", + "entities_list = temporal_kg.get(\"entities\", [])\n", + "entity_map = {e.get(\"id\"): e for e in entities_list}\n", + "\n", + "for rel in temporal_query_result.get(\"relationships\", []):\n", + " source_id = rel.get(\"source\")\n", + " target_id = rel.get(\"target\")\n", + " if source_id in entity_map:\n", + " entity = entity_map[source_id]\n", + " temporal_results.append({\n", + " \"entity_id\": source_id,\n", + " \"name\": entity.get(\"name\"),\n", + " \"timestamp\": entity.get(\"properties\", {}).get(\"timestamp\", \"\"),\n", + " \"type\": entity.get(\"type\")\n", + " })\n", + "\n", + "def combine_results(vector_results, temporal_results):\n", + " combined = []\n", + " \n", + " vector_dict = {r.get(\"id\", \"\"): r for r in vector_results}\n", + " \n", + " for temp_result in temporal_results:\n", + " entity_id = temp_result.get(\"entity_id\", \"\")\n", + " if entity_id in vector_dict:\n", + " combined.append({\n", + " \"id\": entity_id,\n", + " \"name\": temp_result.get(\"name\"),\n", + " \"vector_score\": vector_dict[entity_id].get(\"score\", 0),\n", + " \"timestamp\": temp_result.get(\"timestamp\"),\n", + " \"type\": \"hybrid\"\n", + " })\n", + " else:\n", + " combined.append({\n", + " \"id\": entity_id,\n", + " \"name\": temp_result.get(\"name\"),\n", + " \"vector_score\": 0,\n", + " \"timestamp\": temp_result.get(\"timestamp\"),\n", + " \"type\": \"temporal_only\"\n", + " })\n", + " \n", + " for vec_result in vector_results:\n", + " vec_id = vec_result.get(\"id\", \"\")\n", + " if not any(c.get(\"id\") == vec_id for c in combined):\n", + " combined.append({\n", + " \"id\": vec_id,\n", + " \"vector_score\": vec_result.get(\"score\", 0),\n", + " \"type\": \"vector_only\"\n", + " })\n", + " \n", + " combined.sort(key=lambda x: x.get(\"vector_score\", 0), reverse=True)\n", + " return combined\n", + "\n", + "hybrid_results = combine_results(vector_results, temporal_results)\n", + "\n", + "print(f\"Retrieved {len(hybrid_results)} time-aware results\")\n", + "print(f\" Vector results: {len(vector_results)}\")\n", + "print(f\" Temporal results: {len(temporal_results)}\")\n", + "print(f\" Hybrid results: {len([r for r in hybrid_results if r.get('type') == 'hybrid'])}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "Advanced hybrid search workflow:\n", + "- Temporal knowledge graph built\n", + "- Vector embeddings generated\n", + "- Hybrid search configured\n", + "- Time-aware retrieval implemented\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] } ], "metadata": { diff --git a/cookbook/specialized_applications/Multi_Agent_System_KG_Powered.ipynb b/cookbook/specialized_applications/Multi_Agent_System_KG_Powered.ipynb index 112ba7ab..0b624cec 100644 --- a/cookbook/specialized_applications/Multi_Agent_System_KG_Powered.ipynb +++ b/cookbook/specialized_applications/Multi_Agent_System_KG_Powered.ipynb @@ -10,64 +10,343 @@ "\n", "AI agent systems: build knowledge graph, implement agent memory, create context graphs, enable multi-agent coordination, and share knowledge.\n", "\n", - "## Workflow: Build KG → Agent Memory → Context Graphs → Multi-Agent Coordination → Shared Knowledge\n", + "## Workflow: Build KG → Agent Memory → Context Graphs → Multi-Agent Coordination → Shared Knowledge\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.ingest import FileIngestor, DBIngestor\n", + "from semantica.parse import DocumentParser, StructuredDataParser\n", + "from semantica.semantic_extract import NERExtractor, RelationExtractor\n", + "from semantica.kg import GraphBuilder\n", + "from semantica.context import AgentMemory, ContextRetriever\n", + "from semantica.reasoning import InferenceEngine\n", + "from datetime import datetime\n", + "import os\n", + "import tempfile\n", + "import json\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 1: Build Knowledge Graph\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "file_ingestor = FileIngestor()\n", + "db_ingestor = DBIngestor()\n", + "structured_parser = StructuredDataParser()\n", + "ner_extractor = NERExtractor()\n", + "relation_extractor = RelationExtractor()\n", + "builder = GraphBuilder()\n", "\n", - "## Step 1: Build Knowledge Graph\n", + "temp_dir = tempfile.mkdtemp()\n", "\n", - "'''\n", - "# from semantica.kg import GraphBuilder\n", - "# \n", - "# builder = GraphBuilder()\n", - "# knowledge_graph = builder.build(entities, relationships)\n", - "'''\n", + "agents_file = os.path.join(temp_dir, \"agents.json\")\n", + "tasks_file = os.path.join(temp_dir, \"tasks.json\")\n", "\n", - "## Step 2: Agent Memory\n", + "agents_data = [\n", + " {\"agent_id\": \"agent_1\", \"name\": \"Research Agent\", \"role\": \"researcher\"},\n", + " {\"agent_id\": \"agent_2\", \"name\": \"Analysis Agent\", \"role\": \"analyst\"}\n", + "]\n", "\n", - "'''\n", - "# from semantica.context import AgentMemory\n", - "# \n", - "# agent_memory = AgentMemory(knowledge_graph)\n", - "# \n", - "# # Store agent experiences in KG\n", - "# agent_memory.store_experience(agent_id, experience)\n", - "'''\n", + "tasks_data = [\n", + " {\"task_id\": \"task_1\", \"name\": \"Data Collection\", \"status\": \"completed\", \"assigned_to\": \"agent_1\"},\n", + " {\"task_id\": \"task_2\", \"name\": \"Data Analysis\", \"status\": \"in_progress\", \"assigned_to\": \"agent_2\"}\n", + "]\n", "\n", - "## Step 3: Context Graphs\n", + "with open(agents_file, 'w') as f:\n", + " json.dump(agents_data, f)\n", + "with open(tasks_file, 'w') as f:\n", + " json.dump(tasks_data, f)\n", "\n", - "'''\n", - "# from semantica.context import ContextGraph\n", - "# \n", - "# context_graph = ContextGraph(knowledge_graph)\n", - "# \n", - "# # Create context for agent decisions\n", - "# context = context_graph.get_context(agent_id, query)\n", - "'''\n", + "file_objects = file_ingestor.ingest_directory(temp_dir, recursive=False)\n", + "parsed_agents = structured_parser.parse_json(agents_file)\n", + "parsed_tasks = structured_parser.parse_json(tasks_file)\n", "\n", - "## Step 4: Multi-Agent Coordination\n", + "entities = []\n", + "relationships = []\n", "\n", - "'''\n", - "# # Coordinate multiple agents using shared KG\n", - "# shared_knowledge = knowledge_graph\n", - "# \n", - "# # Agents can query and update shared knowledge\n", - "# agent_1_context = context_graph.get_context(\"agent_1\", task)\n", - "# agent_2_context = context_graph.get_context(\"agent_2\", task)\n", - "'''\n", + "for agent_data in parsed_agents.get(\"data\", agents_data):\n", + " entities.append({\n", + " \"id\": agent_data.get(\"agent_id\", \"\"),\n", + " \"type\": \"Agent\",\n", + " \"name\": agent_data.get(\"name\", \"\"),\n", + " \"properties\": {\"role\": agent_data.get(\"role\", \"\")}\n", + " })\n", "\n", - "## Step 5: Shared Knowledge\n", + "for task_data in parsed_tasks.get(\"data\", tasks_data):\n", + " entities.append({\n", + " \"id\": task_data.get(\"task_id\", \"\"),\n", + " \"type\": \"Task\",\n", + " \"name\": task_data.get(\"name\", \"\"),\n", + " \"properties\": {\"status\": task_data.get(\"status\", \"\")}\n", + " })\n", + " \n", + " assigned_agent = task_data.get(\"assigned_to\", \"\")\n", + " if assigned_agent:\n", + " relationships.append({\n", + " \"source\": assigned_agent,\n", + " \"target\": task_data.get(\"task_id\", \"\"),\n", + " \"type\": \"assigned_to\"\n", + " })\n", "\n", - "'''\n", - "# from semantica.reasoning import InferenceEngine\n", - "# \n", - "# inference_engine = InferenceEngine()\n", - "# \n", - "# # Agents contribute to shared knowledge\n", - "# new_knowledge = inference_engine.infer_from_agent_actions(agent_actions)\n", - "# knowledge_graph.update(new_knowledge)\n", - "# \n", - "# # AI agent systems\n", - "# print(f\"Shared knowledge graph has {len(knowledge_graph.nodes)} nodes\")\n", - "'''\n" + "knowledge_content = \"Market Trends: Analysis shows increasing demand in technology sector.\"\n", + "extracted_entities = ner_extractor.extract(knowledge_content)\n", + "extracted_relations = relation_extractor.extract(knowledge_content, extracted_entities)\n", + "\n", + "for entity in extracted_entities[:3]:\n", + " entity_id = f\"knowledge_{len([e for e in entities if e['type'] == 'Knowledge']) + 1}\"\n", + " entities.append({\n", + " \"id\": entity_id,\n", + " \"type\": \"Knowledge\",\n", + " \"name\": entity.get(\"text\", entity.get(\"entity\", \"\")),\n", + " \"properties\": {}\n", + " })\n", + " \n", + " relationships.append({\n", + " \"source\": \"agent_1\",\n", + " \"target\": entity_id,\n", + " \"type\": \"discovered\"\n", + " })\n", + " \n", + " relationships.append({\n", + " \"source\": \"task_1\",\n", + " \"target\": entity_id,\n", + " \"type\": \"produced\"\n", + " })\n", + "\n", + "knowledge_graph = builder.build(entities, relationships)\n", + "\n", + "print(f\"Ingested {len(file_objects)} files\")\n", + "print(f\"Parsed {len(parsed_agents.get('data', []))} agents and {len(parsed_tasks.get('data', []))} tasks\")\n", + "print(f\"Extracted {len([e for e in entities if e['type'] == 'Knowledge'])} knowledge entities\")\n", + "print(f\"Built knowledge graph with {len(entities)} entities and {len(relationships)} relationships\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Agent Memory\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "agent_memory = AgentMemory(knowledge_graph=knowledge_graph)\n", + "\n", + "agent_experiences = [\n", + " {\n", + " \"agent_id\": \"agent_1\",\n", + " \"content\": \"Completed data collection task successfully\",\n", + " \"metadata\": {\"task\": \"task_1\", \"timestamp\": datetime.now().isoformat()}\n", + " },\n", + " {\n", + " \"agent_id\": \"agent_2\",\n", + " \"content\": \"Started analyzing collected data\",\n", + " \"metadata\": {\"task\": \"task_2\", \"timestamp\": datetime.now().isoformat()}\n", + " }\n", + "]\n", + "\n", + "for experience in agent_experiences:\n", + " memory_id = agent_memory.store(\n", + " content=experience[\"content\"],\n", + " metadata=experience[\"metadata\"],\n", + " entities=[{\"id\": experience[\"agent_id\"], \"type\": \"Agent\"}]\n", + " )\n", + " print(f\"Stored experience for {experience['agent_id']}: {memory_id}\")\n", + "\n", + "print(f\"\\nTotal memories stored: {agent_memory.stats.get('total_items', 0)}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: Context Graphs\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "context_retriever = ContextRetriever(knowledge_graph=knowledge_graph)\n", + "\n", + "def get_agent_context(agent_id, query, kg, retriever):\n", + " context_query = f\"{query} for agent {agent_id}\"\n", + " retrieved_context = retriever.retrieve(\n", + " query=context_query,\n", + " max_results=5,\n", + " use_graph_expansion=True,\n", + " max_hops=2,\n", + " entity_ids=[agent_id]\n", + " )\n", + " \n", + " context_items = []\n", + " if retrieved_context:\n", + " context_items.append({\n", + " \"type\": \"agent_info\",\n", + " \"data\": {\"agent_id\": agent_id}\n", + " })\n", + " \n", + " related_tasks = []\n", + " related_knowledge = []\n", + " \n", + " for ctx in retrieved_context:\n", + " for entity in ctx.related_entities:\n", + " if entity.get(\"type\") == \"Task\":\n", + " related_tasks.append(entity)\n", + " elif entity.get(\"type\") == \"Knowledge\":\n", + " related_knowledge.append(entity)\n", + " \n", + " context_items.append({\n", + " \"type\": \"related_tasks\",\n", + " \"data\": related_tasks\n", + " })\n", + " context_items.append({\n", + " \"type\": \"related_knowledge\",\n", + " \"data\": related_knowledge\n", + " })\n", + " \n", + " return context_items\n", + "\n", + "context_agent_1 = get_agent_context(\"agent_1\", \"What should I work on?\", knowledge_graph, context_retriever)\n", + "print(f\"Context for agent_1: {len(context_agent_1)} context items\")\n", + "for item in context_agent_1:\n", + " print(f\" - {item['type']}: {len(item['data']) if isinstance(item['data'], list) else 1} items\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Multi-Agent Coordination\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "shared_knowledge = knowledge_graph\n", + "\n", + "task_1 = \"Analyze market trends\"\n", + "task_2 = \"Review analysis results\"\n", + "\n", + "agent_1_context = get_agent_context(\"agent_1\", task_1, shared_knowledge, context_retriever)\n", + "agent_2_context = get_agent_context(\"agent_2\", task_2, shared_knowledge, context_retriever)\n", + "\n", + "print(\"Agent 1 Context:\")\n", + "for item in agent_1_context:\n", + " if isinstance(item['data'], list):\n", + " print(f\" {item['type']}: {[d.get('name', d.get('id')) for d in item['data']]}\")\n", + " else:\n", + " print(f\" {item['type']}: {item['data'].get('name', item['data'].get('id'))}\")\n", + "\n", + "print(\"\\nAgent 2 Context:\")\n", + "for item in agent_2_context:\n", + " if isinstance(item['data'], list):\n", + " print(f\" {item['type']}: {[d.get('name', d.get('id')) for d in item['data']]}\")\n", + " else:\n", + " print(f\" {item['type']}: {item['data'].get('name', item['data'].get('id'))}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 5: Shared Knowledge\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "inference_engine = InferenceEngine()\n", + "\n", + "inference_engine.add_rule(\"IF agent performs action ON entity THEN agent action entity\")\n", + "\n", + "agent_actions = [\n", + " {\"agent\": \"agent_1\", \"action\": \"discovered\", \"entity\": \"knowledge_1\"},\n", + " {\"agent\": \"agent_2\", \"action\": \"analyzed\", \"entity\": \"knowledge_1\"},\n", + "]\n", + "\n", + "for action in agent_actions:\n", + " inference_engine.add_fact(action)\n", + "\n", + "inferred_results = inference_engine.forward_chain()\n", + "\n", + "new_relationships = []\n", + "for action in agent_actions:\n", + " new_relationships.append({\n", + " \"source\": action[\"agent\"],\n", + " \"target\": action[\"entity\"],\n", + " \"type\": action[\"action\"],\n", + " \"properties\": {\"timestamp\": datetime.now().isoformat(), \"inferred\": False}\n", + " })\n", + "\n", + "for result in inferred_results:\n", + " if hasattr(result, 'conclusion') and isinstance(result.conclusion, dict):\n", + " if \"agent\" in result.conclusion and \"entity\" in result.conclusion:\n", + " new_relationships.append({\n", + " \"source\": result.conclusion.get(\"agent\", \"\"),\n", + " \"target\": result.conclusion.get(\"entity\", \"\"),\n", + " \"type\": result.conclusion.get(\"action\", \"\"),\n", + " \"properties\": {\"timestamp\": datetime.now().isoformat(), \"inferred\": True}\n", + " })\n", + "\n", + "new_knowledge = {\n", + " \"entities\": [],\n", + " \"relationships\": new_relationships\n", + "}\n", + "\n", + "if new_knowledge[\"relationships\"]:\n", + " updated_kg = builder.build(\n", + " knowledge_graph.get(\"entities\", []) + new_knowledge[\"entities\"],\n", + " knowledge_graph.get(\"relationships\", []) + new_knowledge[\"relationships\"]\n", + " )\n", + " print(f\"Updated knowledge graph with {len(new_knowledge['relationships'])} new relationships\")\n", + "else:\n", + " updated_kg = knowledge_graph\n", + "\n", + "entities_count = len(updated_kg.get(\"entities\", []))\n", + "relationships_count = len(updated_kg.get(\"relationships\", []))\n", + "\n", + "print(f\"\\nShared knowledge graph has {entities_count} entities and {relationships_count} relationships\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "Multi-agent system workflow:\n", + "- Knowledge graph built\n", + "- Agent memory implemented\n", + "- Context graphs created\n", + "- Multi-agent coordination enabled\n", + "- Shared knowledge maintained\n" ] } ], diff --git a/cookbook/specialized_applications/Supply_Chain_End_to_End.ipynb b/cookbook/specialized_applications/Supply_Chain_End_to_End.ipynb index b3a2d599..bdccc1c4 100644 --- a/cookbook/specialized_applications/Supply_Chain_End_to_End.ipynb +++ b/cookbook/specialized_applications/Supply_Chain_End_to_End.ipynb @@ -10,67 +10,406 @@ "\n", "Complete supply chain intelligence: multi-source data ingestion, build supply chain knowledge graph, analyze dependencies, optimize flow, and predict disruptions.\n", "\n", - "## Workflow: Multi-Source Data → Build Supply Chain KG → Analyze Dependencies → Optimize → Predict Disruptions\n", + "## Workflow: Multi-Source Data → Build Supply Chain KG → Analyze Dependencies → Optimize → Predict Disruptions\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.ingest import FileIngestor, WebIngestor, DBIngestor, StreamIngestor\n", + "from semantica.parse import DocumentParser, WebParser, StructuredDataParser\n", + "from semantica.semantic_extract import NERExtractor, RelationExtractor\n", + "from semantica.kg import GraphBuilder, GraphAnalyzer, ConnectivityAnalyzer, TemporalPatternDetector\n", + "from semantica.reasoning import InferenceEngine\n", + "from datetime import datetime, timedelta\n", + "import os\n", + "import tempfile\n", + "import json\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 1: Multi-Source Data Ingestion\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "file_ingestor = FileIngestor()\n", + "web_ingestor = WebIngestor()\n", + "db_ingestor = DBIngestor()\n", + "stream_ingestor = StreamIngestor()\n", + "document_parser = DocumentParser()\n", + "web_parser = WebParser()\n", + "structured_parser = StructuredDataParser()\n", "\n", - "## Step 1: Multi-Source Data Ingestion\n", + "temp_dir = tempfile.mkdtemp()\n", "\n", - "'''\n", - "# from semantica.ingest import FileIngestor, WebIngestor, DBIngestor, StreamIngestor\n", - "# \n", - "# file_ingestor = FileIngestor()\n", - "# web_ingestor = WebIngestor()\n", - "# db_ingestor = DBIngestor()\n", - "# stream_ingestor = StreamIngestor()\n", - "# \n", - "# # Ingest from multiple sources\n", - "# all_data = file_ingestor.ingest(\"supply_chain_reports\") + \\\n", - "# web_ingestor.ingest(\"https://supplychain.example.com\") + \\\n", - "# db_ingestor.ingest(\"SELECT * FROM supply_chain\") + \\\n", - "# stream_ingestor.ingest(supply_chain_stream)\n", - "'''\n", + "report_file = os.path.join(temp_dir, \"supply_chain_report.txt\")\n", + "with open(report_file, 'w') as f:\n", + " f.write(\"Supplier A delivers components to Factory B. Supplier C supplies Factory D.\")\n", "\n", - "## Step 2: Build Supply Chain Knowledge Graph\n", + "file_objects = file_ingestor.ingest_file(report_file, read_content=True)\n", + "parsed_file_content = document_parser.extract_text(report_file) if file_objects else \"\"\n", "\n", - "'''\n", - "# from semantica.kg import GraphBuilder\n", - "# \n", - "# builder = GraphBuilder()\n", - "# supply_chain_kg = builder.build(supply_chain_entities, relationships, temporal=True)\n", - "'''\n", + "web_content = \"Shipping delays reported in region X due to weather conditions.\"\n", + "parsed_web_content = web_parser.parse_text(web_content) if web_content else \"\"\n", "\n", - "## Step 3: Analyze Dependencies\n", + "db_data_file = os.path.join(temp_dir, \"suppliers_db.json\")\n", + "db_data = [\n", + " {\"supplier\": \"Supplier A\", \"factory\": \"Factory B\", \"status\": \"active\"},\n", + " {\"supplier\": \"Supplier C\", \"factory\": \"Factory D\", \"status\": \"active\"}\n", + "]\n", + "with open(db_data_file, 'w') as f:\n", + " json.dump(db_data, f)\n", "\n", - "'''\n", - "# from semantica.kg import GraphAnalyzer\n", - "# \n", - "# analyzer = GraphAnalyzer()\n", - "# dependencies = analyzer.analyze_dependencies(supply_chain_kg)\n", - "'''\n", + "parsed_db_data = structured_parser.parse_json(db_data_file)\n", "\n", - "## Step 4: Optimize Flow\n", + "stream_events = [\n", + " {\"event\": \"shipment_delayed\", \"supplier\": \"Supplier A\", \"timestamp\": datetime.now().isoformat()}\n", + "]\n", "\n", - "'''\n", - "# from semantica.reasoning import InferenceEngine\n", - "# \n", - "# inference_engine = InferenceEngine()\n", - "# \n", - "# # Optimize supply chain flow\n", - "# optimized_flow = inference_engine.optimize_flow(supply_chain_kg)\n", - "'''\n", + "all_data = []\n", + "if parsed_file_content:\n", + " all_data.append({\"source\": \"file\", \"content\": parsed_file_content, \"type\": \"report\"})\n", + "if parsed_web_content:\n", + " all_data.append({\"source\": \"web\", \"content\": parsed_web_content, \"type\": \"news\"})\n", + "for db_record in parsed_db_data.get(\"data\", db_data):\n", + " all_data.append({\"source\": \"db\", **db_record})\n", + "for stream_event in stream_events:\n", + " all_data.append({\"source\": \"stream\", **stream_event})\n", "\n", - "## Step 5: Predict Disruptions\n", + "print(f\"Ingested data from {len(set(d.get('source') for d in all_data))} sources\")\n", + "print(f\" File sources: {len([d for d in all_data if d.get('source') == 'file'])}\")\n", + "print(f\" Web sources: {len([d for d in all_data if d.get('source') == 'web'])}\")\n", + "print(f\" Database sources: {len([d for d in all_data if d.get('source') == 'db'])}\")\n", + "print(f\" Stream sources: {len([d for d in all_data if d.get('source') == 'stream'])}\")\n", + "print(f\"Total data items: {len(all_data)}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Build Supply Chain Knowledge Graph\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ner_extractor = NERExtractor()\n", + "relation_extractor = RelationExtractor()\n", + "builder = GraphBuilder()\n", "\n", - "'''\n", - "# from semantica.kg import TemporalQuery\n", - "# \n", - "# temporal_query = TemporalQuery()\n", - "# \n", - "# # Predict potential disruptions\n", - "# disruptions = inference_engine.predict_disruptions(supply_chain_kg)\n", - "# \n", - "# # Complete supply chain intelligence\n", - "# print(f\"Analyzed {len(supply_chain_kg.nodes)} supply chain nodes\")\n", - "'''\n" + "supply_chain_entities = []\n", + "relationships = []\n", + "entity_map = {}\n", + "\n", + "for data_item in all_data:\n", + " content = data_item.get(\"content\", \"\")\n", + " if not content:\n", + " content = str(data_item)\n", + " \n", + " extracted_entities = ner_extractor.extract(content)\n", + " extracted_relations = relation_extractor.extract(content, extracted_entities)\n", + " \n", + " for entity in extracted_entities:\n", + " entity_text = entity.get(\"text\", entity.get(\"entity\", \"\"))\n", + " entity_type = entity.get(\"type\", \"Entity\")\n", + " \n", + " if entity_text and entity_text not in entity_map:\n", + " entity_id = entity_text.lower().replace(\" \", \"_\")\n", + " entity_map[entity_text] = entity_id\n", + " \n", + " if \"supplier\" in entity_text.lower() or \"supplier\" in entity_type.lower():\n", + " entity_type = \"Supplier\"\n", + " elif \"factory\" in entity_text.lower() or \"factory\" in entity_type.lower():\n", + " entity_type = \"Factory\"\n", + " elif \"warehouse\" in entity_text.lower():\n", + " entity_type = \"Warehouse\"\n", + " elif \"product\" in entity_text.lower():\n", + " entity_type = \"Product\"\n", + " \n", + " supply_chain_entities.append({\n", + " \"id\": entity_id,\n", + " \"type\": entity_type,\n", + " \"name\": entity_text,\n", + " \"properties\": {}\n", + " })\n", + " \n", + " for rel in extracted_relations:\n", + " source_text = rel.get(\"source\", \"\")\n", + " target_text = rel.get(\"target\", \"\")\n", + " rel_type = rel.get(\"type\", \"related_to\")\n", + " \n", + " if source_text in entity_map and target_text in entity_map:\n", + " relationships.append({\n", + " \"source\": entity_map[source_text],\n", + " \"target\": entity_map[target_text],\n", + " \"type\": rel_type,\n", + " \"properties\": {\"timestamp\": datetime.now().isoformat()}\n", + " })\n", + "\n", + "for db_record in parsed_db_data.get(\"data\", db_data):\n", + " supplier_name = db_record.get(\"supplier\", \"\")\n", + " factory_name = db_record.get(\"factory\", \"\")\n", + " \n", + " if supplier_name and factory_name:\n", + " supplier_id = supplier_name.lower().replace(\" \", \"_\")\n", + " factory_id = factory_name.lower().replace(\" \", \"_\")\n", + " \n", + " if supplier_id not in entity_map:\n", + " entity_map[supplier_name] = supplier_id\n", + " supply_chain_entities.append({\n", + " \"id\": supplier_id,\n", + " \"type\": \"Supplier\",\n", + " \"name\": supplier_name,\n", + " \"properties\": {}\n", + " })\n", + " \n", + " if factory_id not in entity_map:\n", + " entity_map[factory_name] = factory_id\n", + " supply_chain_entities.append({\n", + " \"id\": factory_id,\n", + " \"type\": \"Factory\",\n", + " \"name\": factory_name,\n", + " \"properties\": {\"capacity\": 1000}\n", + " })\n", + " \n", + " relationships.append({\n", + " \"source\": supplier_id,\n", + " \"target\": factory_id,\n", + " \"type\": \"supplies\",\n", + " \"properties\": {\"timestamp\": datetime.now().isoformat(), \"status\": \"active\"}\n", + " })\n", + "\n", + "if \"warehouse_1\" not in entity_map:\n", + " supply_chain_entities.append({\n", + " \"id\": \"warehouse_1\",\n", + " \"type\": \"Warehouse\",\n", + " \"name\": \"Warehouse 1\",\n", + " \"properties\": {}\n", + " })\n", + " entity_map[\"Warehouse 1\"] = \"warehouse_1\"\n", + "\n", + "if \"factory_b\" in entity_map and \"warehouse_1\" in entity_map:\n", + " relationships.append({\n", + " \"source\": \"factory_b\",\n", + " \"target\": \"warehouse_1\",\n", + " \"type\": \"ships_to\",\n", + " \"properties\": {\"timestamp\": datetime.now().isoformat()}\n", + " })\n", + "\n", + "supply_chain_kg = builder.build(supply_chain_entities, relationships)\n", + "\n", + "print(f\"Extracted {len([e for e in supply_chain_entities if e['type'] in ['Supplier', 'Factory']])} supply chain entities from parsed data\")\n", + "print(f\"Built supply chain knowledge graph with {len(supply_chain_entities)} entities and {len(relationships)} relationships\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 3: Analyze Dependencies\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "analyzer = GraphAnalyzer()\n", + "connectivity_analyzer = ConnectivityAnalyzer()\n", + "\n", + "connectivity_analysis = connectivity_analyzer.analyze_connectivity(supply_chain_kg)\n", + "graph_metrics = analyzer.compute_metrics(supply_chain_kg)\n", + "\n", + "entities_list = supply_chain_kg.get(\"entities\", [])\n", + "relationships_list = supply_chain_kg.get(\"relationships\", [])\n", + "entity_map = {e.get(\"id\"): e for e in entities_list}\n", + "\n", + "dependencies = []\n", + "for entity in entities_list:\n", + " entity_id = entity.get(\"id\")\n", + " incoming = [r for r in relationships_list if r.get(\"target\") == entity_id]\n", + " outgoing = [r for r in relationships_list if r.get(\"source\") == entity_id]\n", + " \n", + " if incoming or outgoing:\n", + " dependencies.append({\n", + " \"entity_id\": entity_id,\n", + " \"entity_type\": entity.get(\"type\"),\n", + " \"name\": entity.get(\"name\"),\n", + " \"incoming_dependencies\": len(incoming),\n", + " \"outgoing_dependencies\": len(outgoing),\n", + " \"depends_on\": [entity_map.get(r.get(\"source\"), {}).get(\"name\", r.get(\"source\")) for r in incoming if entity_map.get(r.get(\"source\"))],\n", + " \"supports\": [entity_map.get(r.get(\"target\"), {}).get(\"name\", r.get(\"target\")) for r in outgoing if entity_map.get(r.get(\"target\"))]\n", + " })\n", + "\n", + "print(f\"Analyzed dependencies for {len(dependencies)} entities\")\n", + "print(f\"Graph connectivity: {connectivity_analysis.get('is_connected', False)}\")\n", + "print(f\"Connected components: {len(connectivity_analysis.get('components', []))}\")\n", + "for dep in dependencies:\n", + " print(f\" {dep['name']} ({dep['entity_type']}): {dep['incoming_dependencies']} incoming, {dep['outgoing_dependencies']} outgoing\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Optimize Flow\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "inference_engine = InferenceEngine()\n", + "\n", + "inference_engine.add_rule(\"IF factory has less than 2 suppliers THEN suggest add_redundancy\")\n", + "inference_engine.add_rule(\"IF entity has no incoming dependencies AND has more than 2 outgoing THEN suggest bottleneck_mitigation\")\n", + "\n", + "optimization_facts = []\n", + "for dep in dependencies:\n", + " if dep[\"entity_type\"] == \"Factory\" and dep[\"incoming_dependencies\"] < 2:\n", + " optimization_facts.append({\n", + " \"entity\": dep[\"entity_id\"],\n", + " \"type\": \"factory\",\n", + " \"supplier_count\": dep[\"incoming_dependencies\"]\n", + " })\n", + " if dep[\"incoming_dependencies\"] == 0 and dep[\"outgoing_dependencies\"] > 2:\n", + " optimization_facts.append({\n", + " \"entity\": dep[\"entity_id\"],\n", + " \"type\": \"bottleneck\",\n", + " \"outgoing_count\": dep[\"outgoing_dependencies\"]\n", + " })\n", + "\n", + "if optimization_facts:\n", + " inference_engine.add_facts(optimization_facts)\n", + " inferred_results = inference_engine.forward_chain()\n", + "else:\n", + " inferred_results = []\n", + "\n", + "optimized_flow = []\n", + "factories = [e for e in entities_list if e.get(\"type\") == \"Factory\"]\n", + "for factory in factories:\n", + " factory_id = factory.get(\"id\")\n", + " incoming = [r for r in relationships_list if r.get(\"target\") == factory_id and r.get(\"type\") == \"supplies\"]\n", + " if len(incoming) < 2:\n", + " optimized_flow.append({\n", + " \"type\": \"add_redundancy\",\n", + " \"entity\": factory.get(\"name\"),\n", + " \"suggestion\": f\"Add backup supplier for {factory.get('name')} to reduce risk\"\n", + " })\n", + "\n", + "bottlenecks = [d for d in dependencies if d[\"incoming_dependencies\"] == 0 and d[\"outgoing_dependencies\"] > 2]\n", + "if bottlenecks:\n", + " optimized_flow.append({\n", + " \"type\": \"bottleneck_detected\",\n", + " \"entities\": [b[\"name\"] for b in bottlenecks],\n", + " \"suggestion\": \"Consider adding parallel paths for critical nodes\"\n", + " })\n", + "\n", + "if inferred_results:\n", + " print(f\"Inference engine generated {len(inferred_results)} optimization inferences\")\n", + "\n", + "print(f\"Generated {len(optimized_flow)} optimization suggestions\")\n", + "for suggestion in optimized_flow:\n", + " print(f\" {suggestion['type']}: {suggestion['suggestion']}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 5: Predict Disruptions\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "pattern_detector = TemporalPatternDetector()\n", + "\n", + "temporal_patterns = pattern_detector.detect_temporal_patterns(\n", + " supply_chain_kg,\n", + " pattern_type=\"anomaly\",\n", + " min_frequency=1\n", + ")\n", + "\n", + "disruptions = []\n", + "\n", + "delay_events = [d for d in all_data if d.get(\"event\") == \"shipment_delayed\" or \"delay\" in str(d.get(\"content\", \"\")).lower()]\n", + "\n", + "if delay_events:\n", + " disruptions.append({\n", + " \"type\": \"delivery_delay\",\n", + " \"severity\": \"high\",\n", + " \"affected_entities\": [\"Supplier A\"],\n", + " \"description\": \"Shipping delays detected in supply chain\",\n", + " \"recommendation\": \"Activate backup suppliers or adjust production schedules\"\n", + " })\n", + "\n", + "single_supplier_factories = []\n", + "for factory in [e for e in supply_chain_kg.get(\"entities\", []) if e.get(\"type\") == \"Factory\"]:\n", + " factory_id = factory.get(\"id\")\n", + " suppliers = [r for r in supply_chain_kg.get(\"relationships\", []) if r.get(\"target\") == factory_id and r.get(\"type\") == \"supplies\"]\n", + " if len(suppliers) == 1:\n", + " single_supplier_factories.append(factory.get(\"name\"))\n", + "\n", + "if single_supplier_factories:\n", + " disruptions.append({\n", + " \"type\": \"single_point_of_failure\",\n", + " \"severity\": \"medium\",\n", + " \"affected_entities\": single_supplier_factories,\n", + " \"description\": \"Factories with single supplier dependency detected\",\n", + " \"recommendation\": \"Add redundant supplier relationships\"\n", + " })\n", + "\n", + "if temporal_patterns:\n", + " disruptions.append({\n", + " \"type\": \"temporal_anomaly\",\n", + " \"severity\": \"medium\",\n", + " \"description\": f\"Detected {len(temporal_patterns)} temporal anomalies in supply chain\",\n", + " \"recommendation\": \"Review temporal patterns for potential disruptions\"\n", + " })\n", + "\n", + "print(f\"Predicted {len(disruptions)} potential disruptions\")\n", + "for disruption in disruptions:\n", + " print(f\" {disruption['type']} ({disruption['severity']}): {disruption['description']}\")\n", + " print(f\" Recommendation: {disruption['recommendation']}\")\n", + "\n", + "entities_count = len(supply_chain_kg.get(\"entities\", []))\n", + "print(f\"\\nAnalyzed {entities_count} supply chain nodes\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Summary\n", + "\n", + "Complete supply chain intelligence workflow:\n", + "- Multi-source data ingested\n", + "- Supply chain knowledge graph built\n", + "- Dependencies analyzed\n", + "- Flow optimization suggested\n", + "- Disruptions predicted\n" ] } ], diff --git a/cookbook/use_cases/cybersecurity/Anomaly_Detection_Real_Time.ipynb b/cookbook/use_cases/cybersecurity/Anomaly_Detection_Real_Time.ipynb index 211c9e0a..17a5969c 100644 --- a/cookbook/use_cases/cybersecurity/Anomaly_Detection_Real_Time.ipynb +++ b/cookbook/use_cases/cybersecurity/Anomaly_Detection_Real_Time.ipynb @@ -4,65 +4,460 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Anomaly Detection Real-Time\n", + "# Real-Time Anomaly Detection Pipeline\n", "\n", "## Overview\n", "\n", - "Real-time security monitoring: stream security logs, real-time parsing, build temporal knowledge graph, detect anomalies, and alert.\n", + "This notebook demonstrates a complete real-time anomaly detection pipeline for cybersecurity: stream security logs from multiple sources, parse in real-time, build temporal knowledge graph, detect anomalies using pattern detection and inference, generate alerts, and monitor continuously.\n", "\n", - "## Workflow: Stream Security Logs → Real-Time Parsing → Build Temporal KG → Detect Anomalies → Alert\n", + "### Modules Used (20+)\n", "\n", - "## Step 1: Stream Security Logs\n", + "- **Ingestion**: StreamIngestor, FileIngestor, DBIngestor, FeedIngestor\n", + "- **Parsing**: JSONParser, StructuredDataParser, DocumentParser\n", + "- **Extraction**: NERExtractor, RelationExtractor, EventDetector, TripleExtractor\n", + "- **KG**: GraphBuilder, TemporalPatternDetector, TemporalGraphQuery, GraphAnalyzer\n", + "- **Analytics**: CentralityCalculator, CommunityDetector, ConnectivityAnalyzer\n", + "- **Reasoning**: InferenceEngine, RuleManager, ExplanationGenerator\n", + "- **Quality**: KGQualityAssessor, AutomatedFixer\n", + "- **Export**: JSONExporter, CSVExporter, ReportGenerator\n", + "- **Visualization**: KGVisualizer, TemporalVisualizer, AnalyticsVisualizer\n", "\n", - "'''\n", - "# from semantica.ingest import StreamIngestor\n", - "# \n", - "# stream_ingestor = StreamIngestor()\n", - "# log_stream = stream_ingestor.ingest(stream_source)\n", - "'''\n", + "### Pipeline\n", "\n", - "## Step 2: Real-Time Parsing\n", + "**Stream Security Logs → Real-Time Parsing → Extract Entities → Build Temporal KG → Pattern Detection → Anomaly Detection → Generate Alerts → Monitor → Visualize**\n", "\n", - "'''\n", - "# from semantica.parse import DocumentParser\n", - "# \n", - "# parser = DocumentParser()\n", - "# # Process stream in real-time\n", - "# for log_batch in log_stream:\n", - "# parsed_logs = parser.parse(log_batch)\n", - "'''\n", + "---\n", "\n", + "## Step 1: Stream Security Logs from Multiple Sources\n", + "\n", + "Stream security logs from files, databases, and real-time sources.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.ingest import StreamIngestor, FileIngestor, DBIngestor, FeedIngestor\n", + "from semantica.parse import JSONParser, StructuredDataParser, DocumentParser\n", + "from semantica.semantic_extract import NERExtractor, RelationExtractor, EventDetector, TripleExtractor\n", + "from semantica.kg import GraphBuilder, TemporalPatternDetector, TemporalGraphQuery, GraphAnalyzer\n", + "from semantica.kg import CentralityCalculator, CommunityDetector, ConnectivityAnalyzer\n", + "from semantica.reasoning import InferenceEngine, RuleManager, ExplanationGenerator\n", + "from semantica.kg_qa import KGQualityAssessor, AutomatedFixer\n", + "from semantica.export import JSONExporter, CSVExporter, ReportGenerator\n", + "from semantica.visualization import KGVisualizer, TemporalVisualizer, AnalyticsVisualizer\n", + "import tempfile\n", + "import os\n", + "import json\n", + "import time\n", + "from datetime import datetime, timedelta\n", + "from collections import deque\n", + "\n", + "stream_ingestor = StreamIngestor()\n", + "file_ingestor = FileIngestor()\n", + "db_ingestor = DBIngestor()\n", + "feed_ingestor = FeedIngestor()\n", + "\n", + "# Real streaming sources configuration\n", + "stream_sources = [\n", + " {\n", + " \"type\": \"kafka\",\n", + " \"topic\": \"security_logs\",\n", + " \"bootstrap_servers\": [\"localhost:9092\"],\n", + " \"consumer_config\": {\"group_id\": \"semantica_security_monitor\"}\n", + " },\n", + " {\n", + " \"type\": \"rabbitmq\",\n", + " \"queue\": \"security_events\",\n", + " \"connection_url\": \"amqp://user:password@localhost:5672/\"\n", + " }\n", + "]\n", + "\n", + "# Real database connection for security logs\n", + "db_connection_string = \"postgresql://user:password@localhost:5432/security_logs_db\"\n", + "db_query = \"SELECT * FROM security_events WHERE timestamp > NOW() - INTERVAL '1 hour' ORDER BY timestamp DESC LIMIT 1000\"\n", + "\n", + "# Real security feed URLs for threat intelligence\n", + "security_feeds = [\n", + " \"https://www.cisa.gov/news.xml\",\n", + " \"https://www.us-cert.gov/ncas/alerts.xml\"\n", + "]\n", + "\n", + "json_parser = JSONParser()\n", + "structured_parser = StructuredDataParser()\n", + "document_parser = DocumentParser()\n", + "\n", + "temp_dir = tempfile.mkdtemp()\n", + "\n", + "# Real-world streaming security log format (simulating real-time stream)\n", + "security_log_stream_file = os.path.join(temp_dir, \"security_log_stream.json\")\n", + "stream_logs = [\n", + " {\n", + " \"timestamp\": (datetime.now() - timedelta(minutes=5)).isoformat(),\n", + " \"source_ip\": \"192.168.1.50\",\n", + " \"destination_ip\": \"10.0.0.100\",\n", + " \"event_type\": \"normal_traffic\",\n", + " \"bytes_sent\": 1024,\n", + " \"bytes_received\": 2048,\n", + " \"protocol\": \"TCP\",\n", + " \"port\": 80\n", + " },\n", + " {\n", + " \"timestamp\": (datetime.now() - timedelta(minutes=4)).isoformat(),\n", + " \"source_ip\": \"203.0.113.100\",\n", + " \"destination_ip\": \"10.0.0.100\",\n", + " \"event_type\": \"suspicious_connection\",\n", + " \"bytes_sent\": 5000000,\n", + " \"bytes_received\": 1000,\n", + " \"protocol\": \"TCP\",\n", + " \"port\": 443\n", + " },\n", + " {\n", + " \"timestamp\": (datetime.now() - timedelta(minutes=3)).isoformat(),\n", + " \"source_ip\": \"192.168.1.50\",\n", + " \"destination_ip\": \"10.0.0.100\",\n", + " \"event_type\": \"normal_traffic\",\n", + " \"bytes_sent\": 512,\n", + " \"bytes_received\": 1024,\n", + " \"protocol\": \"UDP\",\n", + " \"port\": 53\n", + " },\n", + " {\n", + " \"timestamp\": (datetime.now() - timedelta(minutes=2)).isoformat(),\n", + " \"source_ip\": \"198.51.100.50\",\n", + " \"destination_ip\": \"10.0.0.100\",\n", + " \"event_type\": \"port_scan\",\n", + " \"bytes_sent\": 100,\n", + " \"bytes_received\": 0,\n", + " \"protocol\": \"TCP\",\n", + " \"port\": 22\n", + " },\n", + " {\n", + " \"timestamp\": (datetime.now() - timedelta(minutes=1)).isoformat(),\n", + " \"source_ip\": \"203.0.113.100\",\n", + " \"destination_ip\": \"10.0.0.100\",\n", + " \"event_type\": \"data_exfiltration\",\n", + " \"bytes_sent\": 10000000,\n", + " \"bytes_received\": 500,\n", + " \"protocol\": \"TCP\",\n", + " \"port\": 443\n", + " }\n", + "]\n", + "\n", + "with open(security_log_stream_file, 'w') as f:\n", + " json.dump(stream_logs, f, indent=2)\n", + "\n", + "# Simulate streaming by processing logs in batches\n", + "log_stream = deque(stream_logs)\n", + "file_objects = file_ingestor.ingest_file(security_log_stream_file, read_content=True)\n", + "\n", + "# Parse streaming logs\n", + "parsed_stream = json_parser.parse(security_log_stream_file)\n", + "\n", + "print(f\"Streaming security logs initialized\")\n", + "print(f\"Ingested {len([file_objects]) if file_objects else 0} log stream files\")\n", + "print(f\"Parsed {len(parsed_stream.data) if parsed_stream and parsed_stream.data else 0} log entries\")\n", + "print(f\"Stream ready for real-time processing\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 2: Real-Time Parsing and Entity Extraction\n", + "\n", + "Parse streaming logs in real-time and extract security entities.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "ner_extractor = NERExtractor()\n", + "relation_extractor = RelationExtractor()\n", + "event_detector = EventDetector()\n", + "triple_extractor = TripleExtractor()\n", + "\n", + "# Real-time processing loop (simulated)\n", + "security_entities = []\n", + "stream_relationships = []\n", + "detected_events = []\n", + "\n", + "# Process logs in real-time batches\n", + "for log_entry in parsed_stream.data if parsed_stream and parsed_stream.data else []:\n", + " if isinstance(log_entry, dict):\n", + " log_text = f\"{log_entry.get('event_type', '')} from {log_entry.get('source_ip', '')} to {log_entry.get('destination_ip', '')} on port {log_entry.get('port', '')}\"\n", + " \n", + " entities = ner_extractor.extract(log_text)\n", + " relationships = relation_extractor.extract(log_text, entities)\n", + " events = event_detector.detect_events(log_text)\n", + " \n", + " security_entities.append({\n", + " \"id\": log_entry.get(\"source_ip\", \"\"),\n", + " \"type\": \"IP_Address\",\n", + " \"name\": log_entry.get(\"source_ip\", \"\"),\n", + " \"properties\": {\n", + " \"timestamp\": log_entry.get(\"timestamp\", \"\"),\n", + " \"source\": \"stream\"\n", + " }\n", + " })\n", + " security_entities.append({\n", + " \"id\": log_entry.get(\"destination_ip\", \"\"),\n", + " \"type\": \"IP_Address\",\n", + " \"name\": log_entry.get(\"destination_ip\", \"\"),\n", + " \"properties\": {\n", + " \"timestamp\": log_entry.get(\"timestamp\", \"\"),\n", + " \"source\": \"stream\"\n", + " }\n", + " })\n", + " security_entities.append({\n", + " \"id\": log_entry.get(\"event_type\", \"\"),\n", + " \"type\": \"Security_Event\",\n", + " \"name\": log_entry.get(\"event_type\", \"\"),\n", + " \"properties\": {\n", + " \"timestamp\": log_entry.get(\"timestamp\", \"\"),\n", + " \"bytes_sent\": log_entry.get(\"bytes_sent\", 0),\n", + " \"bytes_received\": log_entry.get(\"bytes_received\", 0),\n", + " \"protocol\": log_entry.get(\"protocol\", \"\"),\n", + " \"port\": log_entry.get(\"port\", 0)\n", + " }\n", + " })\n", + " \n", + " stream_relationships.append({\n", + " \"source\": log_entry.get(\"source_ip\", \"\"),\n", + " \"target\": log_entry.get(\"event_type\", \"\"),\n", + " \"type\": \"triggered\",\n", + " \"properties\": {\"timestamp\": log_entry.get(\"timestamp\", \"\")}\n", + " })\n", + " stream_relationships.append({\n", + " \"source\": log_entry.get(\"event_type\", \"\"),\n", + " \"target\": log_entry.get(\"destination_ip\", \"\"),\n", + " \"type\": \"targeted\",\n", + " \"properties\": {\"timestamp\": log_entry.get(\"timestamp\", \"\")}\n", + " })\n", + " \n", + " detected_events.extend(events)\n", + "\n", + "print(f\"Real-time processing complete\")\n", + "print(f\"Extracted {len(security_entities)} security entities\")\n", + "print(f\"Extracted {len(stream_relationships)} relationships\")\n", + "print(f\"Detected {len(detected_events)} events\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ "## Step 3: Build Temporal Knowledge Graph\n", "\n", - "'''\n", - "# from semantica.kg import GraphBuilder\n", - "# \n", - "# builder = GraphBuilder()\n", - "# # Continuously update temporal KG\n", - "# temporal_kg = builder.build(entities, relationships, temporal=True)\n", - "'''\n", + "Build and continuously update temporal knowledge graph from streaming data.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "builder = GraphBuilder()\n", + "temporal_pattern_detector = TemporalPatternDetector()\n", + "temporal_query = TemporalGraphQuery()\n", + "graph_analyzer = GraphAnalyzer()\n", "\n", - "## Step 4: Detect Anomalies\n", + "# Build temporal KG from streaming data\n", + "temporal_kg = builder.build(security_entities, stream_relationships)\n", "\n", - "'''\n", - "# from semantica.reasoning import InferenceEngine\n", - "# \n", - "# inference_engine = InferenceEngine()\n", - "# \n", - "# # Real-time anomaly detection\n", - "# anomalies = inference_engine.detect_anomalies(temporal_kg)\n", - "'''\n", + "# Analyze graph structure in real-time\n", + "metrics = graph_analyzer.compute_metrics(temporal_kg)\n", + "centrality_calculator = CentralityCalculator()\n", + "community_detector = CommunityDetector()\n", + "connectivity_analyzer = ConnectivityAnalyzer()\n", "\n", - "## Step 5: Alert\n", + "centrality_scores = centrality_calculator.calculate_centrality(temporal_kg, measure=\"degree\")\n", + "communities = community_detector.detect_communities(temporal_kg)\n", + "connectivity = connectivity_analyzer.analyze_connectivity(temporal_kg)\n", "\n", - "'''\n", - "# # Generate alerts for detected anomalies\n", - "# for anomaly in anomalies:\n", - "# send_alert(anomaly)\n", - "# \n", - "# # Real-time security monitoring\n", - "# print(f\"Monitoring {len(temporal_kg.nodes)} entities in real-time\")\n", - "'''\n" + "print(f\"Built temporal knowledge graph from stream\")\n", + "print(f\" Entities: {len(temporal_kg.get('entities', []))}\")\n", + "print(f\" Relationships: {len(temporal_kg.get('relationships', []))}\")\n", + "print(f\" Graph density: {metrics.get('density', 0):.3f}\")\n", + "print(f\" Communities: {len(communities)}\")\n", + "print(f\" Central entities: {len([e for e, score in centrality_scores.items() if score > 0])}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 4: Real-Time Pattern Detection\n", + "\n", + "Detect temporal patterns and anomalies in real-time.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Detect temporal patterns\n", + "temporal_patterns = temporal_pattern_detector.detect_temporal_patterns(\n", + " temporal_kg,\n", + " pattern_type=\"anomaly\",\n", + " min_frequency=1\n", + ")\n", + "\n", + "# Real-time anomaly detection using inference\n", + "inference_engine = InferenceEngine()\n", + "rule_manager = RuleManager()\n", + "explanation_generator = ExplanationGenerator()\n", + "\n", + "# Define real-time anomaly detection rules\n", + "inference_engine.add_rule(\"IF bytes_sent > 1000000 AND bytes_received < 1000 THEN potential_data_exfiltration\")\n", + "inference_engine.add_rule(\"IF event_type is port_scan AND port is 22 THEN ssh_brute_force\")\n", + "inference_engine.add_rule(\"IF multiple events from same source_ip in short time THEN suspicious_activity\")\n", + "\n", + "# Add facts from streaming logs\n", + "for log_entry in parsed_stream.data if parsed_stream and parsed_stream.data else []:\n", + " if isinstance(log_entry, dict):\n", + " inference_engine.add_fact({\n", + " \"source_ip\": log_entry.get(\"source_ip\", \"\"),\n", + " \"event_type\": log_entry.get(\"event_type\", \"\"),\n", + " \"bytes_sent\": log_entry.get(\"bytes_sent\", 0),\n", + " \"bytes_received\": log_entry.get(\"bytes_received\", 0),\n", + " \"port\": log_entry.get(\"port\", 0),\n", + " \"timestamp\": log_entry.get(\"timestamp\", \"\")\n", + " })\n", + "\n", + "inferred_anomalies = inference_engine.forward_chain()\n", + "\n", + "# Real-time anomaly scoring\n", + "real_time_anomalies = []\n", + "for log_entry in parsed_stream.data if parsed_stream and parsed_stream.data else []:\n", + " if isinstance(log_entry, dict):\n", + " anomaly_score = 0\n", + " reasons = []\n", + " \n", + " if log_entry.get(\"bytes_sent\", 0) > 1000000:\n", + " anomaly_score += 5\n", + " reasons.append(\"Unusually large data transfer\")\n", + " \n", + " if log_entry.get(\"event_type\") in [\"port_scan\", \"data_exfiltration\"]:\n", + " anomaly_score += 4\n", + " reasons.append(\"High-risk event type\")\n", + " \n", + " if log_entry.get(\"bytes_sent\", 0) > log_entry.get(\"bytes_received\", 0) * 100:\n", + " anomaly_score += 3\n", + " reasons.append(\"Asymmetric traffic pattern\")\n", + " \n", + " if anomaly_score >= 3:\n", + " real_time_anomalies.append({\n", + " \"source_ip\": log_entry.get(\"source_ip\", \"\"),\n", + " \"destination_ip\": log_entry.get(\"destination_ip\", \"\"),\n", + " \"event_type\": log_entry.get(\"event_type\", \"\"),\n", + " \"severity\": \"high\" if anomaly_score >= 5 else \"medium\",\n", + " \"score\": anomaly_score,\n", + " \"reasons\": reasons,\n", + " \"timestamp\": log_entry.get(\"timestamp\", \"\")\n", + " })\n", + "\n", + "print(f\"Detected {len(temporal_patterns)} temporal patterns\")\n", + "print(f\"Inferred {len(inferred_anomalies)} anomalies from rules\")\n", + "print(f\"Identified {len(real_time_anomalies)} real-time anomalies\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 5: Generate Real-Time Alerts\n", + "\n", + "Generate and send alerts for detected anomalies.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "quality_assessor = KGQualityAssessor()\n", + "json_exporter = JSONExporter()\n", + "csv_exporter = CSVExporter()\n", + "report_generator = ReportGenerator()\n", + "\n", + "quality_score = quality_assessor.assess_overall_quality(temporal_kg)\n", + "\n", + "# Generate alerts\n", + "alerts = []\n", + "for anomaly in real_time_anomalies:\n", + " alert = {\n", + " \"alert_id\": f\"alert_{anomaly['source_ip']}_{int(time.time())}\",\n", + " \"severity\": anomaly[\"severity\"],\n", + " \"source_ip\": anomaly[\"source_ip\"],\n", + " \"destination_ip\": anomaly[\"destination_ip\"],\n", + " \"event_type\": anomaly[\"event_type\"],\n", + " \"score\": anomaly[\"score\"],\n", + " \"reasons\": anomaly[\"reasons\"],\n", + " \"timestamp\": anomaly[\"timestamp\"],\n", + " \"status\": \"active\"\n", + " }\n", + " alerts.append(alert)\n", + "\n", + "# Export alerts\n", + "json_exporter.export_knowledge_graph(temporal_kg, os.path.join(temp_dir, \"realtime_kg.json\"))\n", + "csv_exporter.export_entities(security_entities, os.path.join(temp_dir, \"realtime_entities.csv\"))\n", + "\n", + "report_data = {\n", + " \"summary\": f\"Real-time anomaly detection identified {len(real_time_anomalies)} anomalies\",\n", + " \"total_events\": len(parsed_stream.data) if parsed_stream and parsed_stream.data else 0,\n", + " \"anomalies\": len(real_time_anomalies),\n", + " \"alerts\": len(alerts),\n", + " \"quality_score\": quality_score.get('overall_score', 0),\n", + " \"high_severity\": len([a for a in alerts if a.get('severity') == 'high'])\n", + "}\n", + "\n", + "report = report_generator.generate_report(report_data, format=\"markdown\")\n", + "\n", + "print(f\"Generated {len(alerts)} real-time alerts\")\n", + "print(f\"High severity alerts: {len([a for a in alerts if a.get('severity') == 'high'])}\")\n", + "print(f\"Report length: {len(report)} characters\")\n", + "print(f\"Graph quality score: {quality_score.get('overall_score', 0):.3f}\")\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Step 6: Real-Time Monitoring and Visualization\n", + "\n", + "Monitor security events in real-time and visualize results.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "kg_visualizer = KGVisualizer()\n", + "temporal_visualizer = TemporalVisualizer()\n", + "analytics_visualizer = AnalyticsVisualizer()\n", + "\n", + "kg_viz = kg_visualizer.visualize_network(temporal_kg, output=\"interactive\")\n", + "temporal_viz = temporal_visualizer.visualize_timeline(temporal_kg, output=\"interactive\")\n", + "analytics_viz = analytics_visualizer.visualize_analytics(temporal_kg, output=\"interactive\")\n", + "\n", + "print(f\"Real-time monitoring active\")\n", + "print(f\"Monitoring {len(temporal_kg.get('entities', []))} entities in real-time\")\n", + "print(f\"Active alerts: {len(alerts)}\")\n", + "print(\"Generated visualizations for knowledge graph, temporal patterns, and analytics\")\n", + "print(f\"Total modules used: 20+\")\n", + "print(f\"Pipeline complete: Stream Logs → Real-Time Parse → Extract → Temporal KG → Pattern Detection → Anomaly Detection → Alerts → Monitor → Visualize\")\n" ] } ], diff --git a/cookbook/use_cases/cybersecurity/Incident_Analysis.ipynb b/cookbook/use_cases/cybersecurity/Incident_Analysis.ipynb index 23392e86..f6121bba 100644 --- a/cookbook/use_cases/cybersecurity/Incident_Analysis.ipynb +++ b/cookbook/use_cases/cybersecurity/Incident_Analysis.ipynb @@ -4,68 +4,444 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Incident Analysis\n", + "# Incident Analysis Pipeline\n", "\n", "## Overview\n", "\n", - "Security incident investigation: ingest security logs, parse, extract entities, build knowledge graph, analyze relationships, and detect anomalies.\n", + "This notebook demonstrates a complete security incident analysis pipeline: ingest security logs from multiple sources (files, databases, streams), parse structured and unstructured logs, extract security entities, build knowledge graph, analyze relationships, detect anomalies, and generate incident reports.\n", "\n", - "## Workflow: Ingest Security Logs → Parse → Extract Entities → Build KG → Analyze Relationships → Detect Anomalies\n", + "### Modules Used (20+)\n", "\n", - "## Step 1: Ingest Security Logs\n", + "- **Ingestion**: FileIngestor, DBIngestor, StreamIngestor, FeedIngestor\n", + "- **Parsing**: JSONParser, XMLParser, StructuredDataParser, DocumentParser\n", + "- **Extraction**: NERExtractor, RelationExtractor, EventDetector, TripleExtractor\n", + "- **KG**: GraphBuilder, GraphAnalyzer, ConnectivityAnalyzer, CentralityCalculator\n", + "- **Reasoning**: InferenceEngine, RuleManager, ExplanationGenerator\n", + "- **Quality**: KGQualityAssessor, ConflictDetector, ProvenanceTracker\n", + "- **Export**: JSONExporter, RDFExporter, ReportGenerator\n", + "- **Visualization**: KGVisualizer, AnalyticsVisualizer, TemporalVisualizer\n", "\n", - "'''\n", - "# from semantica.ingest import FileIngestor\n", - "# \n", - "# ingestor = FileIngestor()\n", - "# security_logs = ingestor.ingest(\"security_logs.json\")\n", - "'''\n", + "### Pipeline\n", "\n", - "## Step 2: Parse Logs\n", + "**Multiple Security Sources → Parse Logs → Extract Security Entities → Build Incident KG → Analyze Relationships → Detect Anomalies → Generate Reports → Visualize**\n", "\n", - "'''\n", - "# from semantica.parse import DocumentParser\n", - "# \n", - "# parser = DocumentParser()\n", - "# parsed_logs = parser.parse(security_logs)\n", - "'''\n", + "---\n", "\n", - "## Step 3: Extract Entities\n", + "## Step 1: Ingest Security Logs from Multiple Sources\n", "\n", - "'''\n", - "# from semantica.semantic_extract import NERExtractor\n", - "# \n", - "# extractor = NERExtractor()\n", - "# entities = extractor.extract(parsed_logs)\n", - "'''\n", + "Ingest security logs from files, databases, streams, and threat intelligence feeds.\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from semantica.ingest import FileIngestor, DBIngestor, StreamIngestor, FeedIngestor\n", + "from semantica.parse import JSONParser, XMLParser, StructuredDataParser, DocumentParser\n", + "from semantica.semantic_extract import NERExtractor, RelationExtractor, EventDetector, TripleExtractor\n", + "from semantica.kg import GraphBuilder, GraphAnalyzer, ConnectivityAnalyzer, CentralityCalculator\n", + "from semantica.reasoning import InferenceEngine, RuleManager, ExplanationGenerator\n", + "from semantica.kg_qa import KGQualityAssessor\n", + "from semantica.conflicts import ConflictDetector\n", + "from semantica.kg import ProvenanceTracker\n", + "from semantica.export import JSONExporter, RDFExporter, ReportGenerator\n", + "from semantica.visualization import KGVisualizer, AnalyticsVisualizer, TemporalVisualizer\n", + "import tempfile\n", + "import os\n", + "import json\n", + "from datetime import datetime, timedelta\n", "\n", - "## Step 4: Build Knowledge Graph\n", + "file_ingestor = FileIngestor()\n", + "db_ingestor = DBIngestor()\n", + "stream_ingestor = StreamIngestor()\n", + "feed_ingestor = FeedIngestor()\n", "\n", - "'''\n", - "# from semantica.kg import GraphBuilder\n", - "# \n", - "# builder = GraphBuilder()\n", - "# incident_kg = builder.build(entities, relationships)\n", - "'''\n", + "json_parser = JSONParser()\n", + "xml_parser = XMLParser()\n", + "structured_parser = StructuredDataParser()\n", + "document_parser = DocumentParser()\n", "\n", - "## Step 5: Analyze Relationships\n", + "temp_dir = tempfile.mkdtemp()\n", "\n", - "'''\n", - "# from semantica.kg import GraphAnalyzer\n", - "# \n", - "# analyzer = GraphAnalyzer()\n", - "# relationships = analyzer.analyze_relationships(incident_kg)\n", - "'''\n", + "# Real-world security log formats\n", + "security_logs_json = os.path.join(temp_dir, \"security_logs.json\")\n", + "security_logs_data = [\n", + " {\n", + " \"timestamp\": (datetime.now() - timedelta(hours=2)).isoformat(),\n", + " \"source_ip\": \"192.168.1.100\",\n", + " \"destination_ip\": \"10.0.0.50\",\n", + " \"event_type\": \"failed_login\",\n", + " \"user\": \"admin\",\n", + " \"severity\": \"medium\",\n", + " \"message\": \"Multiple failed login attempts detected\"\n", + " },\n", + " {\n", + " \"timestamp\": (datetime.now() - timedelta(hours=1)).isoformat(),\n", + " \"source_ip\": \"203.0.113.45\",\n", + " \"destination_ip\": \"10.0.0.50\",\n", + " \"event_type\": \"port_scan\",\n", + " \"severity\": \"high\",\n", + " \"message\": \"Port scanning activity detected from external IP\"\n", + " },\n", + " {\n", + " \"timestamp\": (datetime.now() - timedelta(minutes=30)).isoformat(),\n", + " \"source_ip\": \"192.168.1.100\",\n", + " \"destination_ip\": \"10.0.0.75\",\n", + " \"event_type\": \"data_exfiltration\",\n", + " \"user\": \"user123\",\n", + " \"severity\": \"critical\",\n", + " \"message\": \"Large data transfer detected to external server\"\n", + " }\n", + "]\n", "\n", - "## Step 6: Detect Anomalies\n", + "with open(security_logs_json, 'w') as f:\n", + " json.dump(security_logs_data, f, indent=2)\n", "\n", - "'''\n", - "# # Detect suspicious patterns\n", - "# anomalies = analyzer.detect_anomalies(incident_kg)\n", - "# \n", - "# # Security incident investigation\n", - "# print(f\"Found {len(anomalies)} anomalies\")\n", - "'''\n" + "# XML format security events (common in SIEM systems)\n", + "security_events_xml = os.path.join(temp_dir, \"security_events.xml\")\n", + "xml_content = \"\"\"\n", + "