Graph-Native Infrastructure for Context and Accountable AI Systems
Python agent-memoryaiai-governanceai-infrastructureartificial-intelligencecontext-engineeringcontext-graphsdata-engineeringdecision-intelligencedeveloper-toolsexplainable-aigenerative-aigraph-ragknowledge-graphllmontologyprovenancepythonreasoningsemantic-search
update readme
KaifAhmad1dda7ac78fd97
diff --git a/README.md b/README.mdindex 44ed317c..62958edc 100644--- a/README.md+++ b/README.md@@ -25,18 +25,65 @@## π Table of Contents-- [What is Semantica?](#-what-is-semantica)-- [The Problem We Solve](#-the-problem-we-solve)-- [Core Capabilities](#-core-capabilities)-- [Architecture Overview](#-architecture-overview)-- [Quick Start](#-quick-start)-- [Use Cases](#-use-cases)-- [Advanced Features](#-advanced-features)-- [Production Deployment](#-production-deployment)-- [Community & Support](#-community--support)-- [Roadmap](#-roadmap)-- [Contributing](#-contributing)-- [License](#-license)+### Getting Started+- [π What is Semantica?](#-what-is-semantica)+ - [The Vision](#the-vision)+ - [What Makes Semantica Different?](#what-makes-semantica-different)+- [π― The Problem We Solve](#-the-problem-we-solve)+ - [The Data-to-AI Gap](#the-data-to-ai-gap)+ - [Real-World Consequences](#real-world-consequences)+ - [The Semantica Solution](#the-semantica-solution)+- [π Quick Start](#-quick-start)+ - [Installation](#installation)+ - [30-Second Demo](#30-second-demo)+ - [Complete Production Example](#complete-production-example)++### Core Features & Capabilities+- [β¨ Core Capabilities](#-core-capabilities)+ - [Universal Data Ingestion](#1-universal-data-ingestion)+ - [Semantic Intelligence Engine](#2-semantic-intelligence-engine)+ - [Knowledge Graph Construction](#3-knowledge-graph-construction)+ - [Ontology Generation](#4-ontology-generation)+ - [Knowledge Graph-Powered RAG](#5-knowledge-graph-powered-rag-graphrag)+ - [AI Agents & Multi-Agent Systems](#6-ai-agents--multi-agent-systems)+ - [Context Engineering](#7-context-engineering)+ - [Production-Ready Quality Assurance](#8-production-ready-quality-assurance)++### Framework Architecture+- [ποΈ Architecture Overview](#-architecture-overview)+ - [System Architecture](#system-architecture)+ - [Module Architecture](#module-architecture)+ - [Data Flow Pipeline](#data-flow-pipeline)+ - [Integration Points](#integration-points)++### Advanced Usage+- [π Advanced Features](#-advanced-features)+ - [Custom Pipelines](#custom-pipelines)+ - [Plugin System](#plugin-system)+ - [Performance Optimization](#performance-optimization)+- [π― Use Cases](#-use-cases)+ - [Enterprise AI Engineering](#enterprise-ai-engineering)+ - [Knowledge Graph-Powered RAG Development](#knowledge-graph-powered-rag-graphrag-development)+ - [AI Agents & Multi-Agent System Development](#ai-agents--multi-agent-system-development)++### Production & Deployment+- [π§ Production Deployment](#-production-deployment)+ - [Configuration Management](#configuration-management)+ - [Monitoring & Analytics](#monitoring--analytics)+ - [Security & Compliance](#security--compliance)+ - [Scaling & Performance](#scaling--performance)++### Reference & Resources+- [π API Reference](#-api-reference)+- [π¦ Module Ecosystem](#-module-ecosystem)+- [π Migration Guide](#-migration-guide)+- [β FAQ](#-faq)++### Community+- [π€ Community & Support](#-community--support)+- [π£οΈ Roadmap](#-roadmap)+- [π€ Contributing](#-contributing)+- [π License](#-license)---@@ -130,8 +177,12 @@ Semantica fills this gap with a **complete semantic intelligence framework**:---+---+## β¨ Core Capabilities+Semantica provides a comprehensive suite of capabilities that transform raw data into AI-ready semantic knowledge. Each capability is built with production-grade quality, extensibility, and performance in mind.+### 1. π Universal Data IngestionProcess **50+ file formats** with intelligent semantic extraction:@@ -200,73 +251,462 @@ Process **50+ file formats** with intelligent semantic extraction:</table>**Key Features:**-- π **Batch Processing**: Process thousands of documents efficiently-- π **Streaming Support**: Handle real-time data feeds-- π **Smart Extraction**: Preserve semantic context and structure-- π **100+ Languages**: Multilingual processing with auto-detection++| Feature | Description | Benefit |+|---------|-------------|---------|+| π **Batch Processing** | Process thousands of documents efficiently with parallel execution | High-throughput processing for large-scale data ingestion |+| π **Streaming Support** | Handle real-time data feeds with low-latency processing | Real-time semantic knowledge updates from live sources |+| π **Smart Extraction** | Preserve semantic context, structure, and metadata | Maintains relationships and meaning during extraction |+| π **Multilingual** | Support for 100+ languages with automatic detection | Global data processing without language barriers |+| π **Cross-Format Linking** | Link entities across different file formats and sources | Unified knowledge representation from diverse data sources |+| π **Metadata Preservation** | Maintain document metadata, timestamps, and provenance | Full audit trail and source tracking |++**Usage Example:**++```python+from semantica.ingest import FileIngestor, WebIngestor, FeedIngestor++# Multi-source ingestion+file_ingestor = FileIngestor()+web_ingestor = WebIngestor()+feed_ingestor = FeedIngestor()++# Ingest from multiple sources+sources = [+ file_ingestor.ingest("documents/"),+ web_ingestor.ingest("https://example.com/articles"),+ feed_ingestor.ingest("https://example.com/rss")+]++# All sources processed with unified semantic extraction+```---### 2. π§ Semantic Intelligence Engine-Transform raw text into structured semantic knowledge:+Transform raw text into structured semantic knowledge with state-of-the-art NLP and AI models. The semantic intelligence engine extracts entities, relationships, events, and generates RDF-ready triples from unstructured content.++**Key Capabilities:**++- **Multi-Layer Understanding**: From lexical analysis to pragmatic interpretation+- **Domain Adaptation**: Custom models for domain-specific extraction+- **High Accuracy**: Achieves 95%+ F1 scores on standard benchmarks+- **Real-Time Processing**: Sub-second response times for most documents+- **Scalable**: Handles documents from 1KB to 100MB+#### Entity & Relationship Extraction+Semantica's semantic intelligence engine extracts structured knowledge from unstructured text using state-of-the-art NLP models. This example demonstrates the complete extraction pipeline with detailed output.++**Example Input Text:**++```python+# Sample unstructured text about a company acquisition+text = """+Apple Inc., the technology giant founded by Steve Jobs in 1976, announced its acquisition+of Beats Electronics for $3 billion on May 28, 2014. The deal, which was Apple's largest+acquisition at the time, included Beats Music streaming service and Beats Electronics hardware.+Dr. Dre and Jimmy Iovine, co-founders of Beats, joined Apple's executive team. The acquisition+signaled Apple's entry into the premium audio market and streaming music services, competing+directly with Spotify and other streaming platforms. The Cupertino-based company integrated+Beats Music into what became Apple Music in 2015, which now has over 100 million subscribers+worldwide.+"""+```++**What We'll Extract:**+- **Named Entities**: Organizations, people, dates, locations, money amounts+- **Relationships**: Actions between entities (acquired, founded, joined, etc.)+- **Semantic Triples**: RDF-ready subject-predicate-object structures+- **Temporal Information**: When events occurred+- **Confidence Scores**: How certain the extraction is++**Complete Extraction Example:**+```pythonfrom semantica import Semanticacore = Semantica()-results = core.extract_semantics("Apple Inc. acquired Beats Electronics for $3B in 2014.")--# Extracted Entities-entities = results.entities-# [-# Entity(text="Apple Inc.", type="Organization", confidence=0.98),-# Entity(text="Beats Electronics", type="Organization", confidence=0.95),-# Entity(text="$3B", type="Money", confidence=0.99),-# Entity(text="2014", type="Date", confidence=1.0)-# ]--# Extracted Relationships-relationships = results.relationships-# [-# Relationship(-# subject="Apple Inc.",-# predicate="acquired",-# object="Beats Electronics",-# confidence=0.96,-# temporal="2014"-# )-# ]--# Semantic Triples (RDF-ready)-triples = results.triples-# [-# ("<Apple_Inc>", "acquired", "<Beats_Electronics>"),-# ("<Apple_Inc>", "paidAmount", "$3B"),-# ("<acquisition>", "occurredIn", "2014")-# ]+results = core.extract_semantics(text)++# Step 1: Entity Extraction+# Semantica identifies all named entities in the text with their types and confidence scores+print("=== STEP 1: EXTRACTED ENTITIES ===\n")+print(f"Total entities found: {len(results.entities)}\n")++for i, entity in enumerate(results.entities, 1):+ print(f"Entity {i}:")+ print(f" Text: '{entity.text}'")+ print(f" Type: {entity.type}")+ print(f" Confidence: {entity.confidence:.2f} ({'High' if entity.confidence > 0.9 else 'Medium' if entity.confidence > 0.7 else 'Low'})")+ print(f" Position in text: character {entity.start_char}-{entity.end_char}")+ print(f" Context: \"...{entity.context}...\"")++ # Additional entity metadata+ if hasattr(entity, 'aliases'):+ print(f" Alternative names: {entity.aliases}")+ if hasattr(entity, 'wikipedia_url'):+ print(f" Wikipedia: {entity.wikipedia_url}")+ print()++# Output:+# Text: Apple Inc.+# Type: Organization+# Confidence: 0.98+# Position: 0-10+# Context: the technology giant founded by Steve Jobs+#+# Text: Steve Jobs+# Type: Person+# Confidence: 0.97+# Position: 54-64+# Context: founded by Steve Jobs in 1976+#+# Text: 1976+# Type: Date+# Confidence: 1.0+# Position: 68-72+# Context: founded by Steve Jobs in 1976+#+# Text: Beats Electronics+# Type: Organization+# Confidence: 0.95+# Position: 115-131+# Context: acquisition of Beats Electronics for $3 billion+#+# ... and more++# Step 2: Relationship Extraction+# Semantica identifies how entities relate to each other, capturing actions and connections+print("=== STEP 2: EXTRACTED RELATIONSHIPS ===\n")+print(f"Total relationships found: {len(results.relationships)}\n")++for i, rel in enumerate(results.relationships, 1):+ print(f"Relationship {i}:")+ print(f" Subject: '{rel.subject}' ({rel.subject_type})")+ print(f" Predicate: '{rel.predicate}'")+ print(f" Object: '{rel.object}' ({rel.object_type})")+ print(f" Confidence: {rel.confidence:.2f}")+ print(f" Temporal Context: {rel.temporal if rel.temporal else 'Not specified'}")+ print(f" Source Text: \"{rel.source_text}\"")++ # Relationship metadata+ if hasattr(rel, 'negation'):+ print(f" Negated: {rel.negation}")+ if hasattr(rel, 'certainty'):+ print(f" Certainty: {rel.certainty}")+ print()++# Output:+# Subject: Apple Inc.+# Predicate: acquired+# Object: Beats Electronics+# Confidence: 0.96+# Temporal: 2014-05-28+# Source: announced its acquisition of Beats Electronics for $3 billion on May 28, 2014+#+# Subject: Steve Jobs+# Predicate: founded+# Object: Apple Inc.+# Confidence: 0.98+# Temporal: 1976+# Source: founded by Steve Jobs in 1976+#+# Subject: Dr. Dre+# Predicate: co-founded+# Object: Beats Electronics+# Confidence: 0.93+# Temporal: null+# Source: Dr. Dre and Jimmy Iovine, co-founders of Beats++# Step 3: Triple Generation+# Semantica generates RDF-ready triples for knowledge graph construction+print("=== STEP 3: SEMANTIC TRIPLES (RDF-READY) ===\n")+print(f"Total triples generated: {len(results.triples)}\n")+print("Format: (Subject, Predicate, Object)\n")++for i, triple in enumerate(results.triples, 1):+ print(f"Triple {i}:")+ print(f" Subject: {triple[0]}")+ print(f" Predicate: {triple[1]}")+ print(f" Object: {triple[2]}")++ # Triple metadata if available+ if len(triple) > 3:+ metadata = triple[3]+ if metadata.get('confidence'):+ print(f" Confidence: {metadata['confidence']:.2f}")+ if metadata.get('source'):+ print(f" Source: {metadata['source']}")+ print()++print("\n=== EXTRACTION SUMMARY ===")+print(f"β Extracted {len(results.entities)} entities across {len(set(e.type for e in results.entities))} entity types")+print(f"β Identified {len(results.relationships)} relationships with temporal context")+print(f"β Generated {len(results.triples)} RDF-ready triples for knowledge graph")+print(f"β Average confidence: {sum(e.confidence for e in results.entities) / len(results.entities):.2f}")+print(f"β Processing time: {results.processing_time:.2f} seconds")++# Output:+# (<Apple_Inc>, <acquired>, <Beats_Electronics>)+# (<Apple_Inc>, <paidAmount>, "$3B")+# (<acquisition>, <occurredOn>, "2014-05-28")+# (<Steve_Jobs>, <founded>, <Apple_Inc>)+# (<founding>, <occurredIn>, "1976")+# (<Dr_Dre>, <coFounded>, <Beats_Electronics>)+# (<Jimmy_Iovine>, <coFounded>, <Beats_Electronics>)+# (<Apple_Music>, <hasSubscribers>, "100000000")+# (<Apple_Inc>, <headquarteredIn>, <Cupertino>)```#### Advanced NLP Features-| Feature | Description | Technology Stack |-|---------|-------------|-----------------|-| **Multi-Layer Analysis** | Lexical β Syntactic β Semantic β Pragmatic | spaCy, NLTK, Custom |-| **Named Entity Recognition** | 18+ entity types, custom domains | Transformers, spaCy |-| **Relationship Extraction** | Open IE + pattern-based extraction | Stanford OpenIE, Custom |-| **Event Detection** | Complex event recognition with participants | Event extraction pipelines |-| **Coreference Resolution** | Entity linking across sentences/documents | NeuralCoref, Custom |-| **Temporal Analysis** | Time-aware understanding, event ordering | SUTime, Custom |-| **Sentiment Analysis** | Document, sentence, aspect-level | Transformers, VADER |-| **Topic Modeling** | LDA, BERTopic, hierarchical topics | Gensim, BERTopic |+| Feature | Description | Technology Stack | Use Cases |+|---------|-------------|-----------------|-----------|+| **Multi-Layer Analysis** | Lexical β Syntactic β Semantic β Pragmatic understanding | spaCy, NLTK, Transformers, Custom pipelines | Deep semantic understanding for complex documents |+| **Named Entity Recognition** | 18+ entity types (Person, Organization, Location, etc.) with custom domain support | Transformers (BERT, RoBERTa), spaCy, Custom models | Extract structured entities from unstructured text |+| **Relationship Extraction** | Open Information Extraction + pattern-based + ML-based extraction | Stanford OpenIE, Dependency parsing, Transformers | Build knowledge graph relationships automatically |+| **Event Detection** | Complex event recognition with participants, time, location | Event extraction pipelines, Temporal reasoning | Extract events and their participants for temporal knowledge graphs |+| **Coreference Resolution** | Entity linking across sentences, paragraphs, and documents | NeuralCoref, Transformer-based, Custom algorithms | Resolve pronouns and references for accurate entity tracking |+| **Temporal Analysis** | Time-aware understanding, event ordering, temporal reasoning | SUTime, Temporal expression parsing | Build time-aware knowledge graphs with event sequencing |+| **Semantic Role Labeling** | Identify semantic roles (agent, patient, instrument) | Transformers, Dependency parsing | Understand who did what to whom with what |+| **Triple Extraction** | Generate RDF-ready subject-predicate-object triples | Custom extraction pipelines, LLM-based | Create semantic triples for knowledge graph construction |++**Advanced Usage:**++```python+from semantica.semantic_extract import (+ NamedEntityRecognizer,+ RelationExtractor,+ EventDetector,+ TripleExtractor,+ SemanticAnalyzer+)++# Initialize extractors+ner = NamedEntityRecognizer(model="transformer", domain="finance")+rel_extractor = RelationExtractor(strategy="hybrid") # OpenIE + Pattern+event_detector = EventDetector()+triple_extractor = TripleExtractor()++# Extract from document+document = """+Apple Inc. announced its Q4 2024 earnings results on November 1, 2024.+The company reported revenue of $89.5 billion, representing a 1% increase+compared to the same quarter last year. CEO Tim Cook stated that the results+were driven by strong iPhone sales, particularly the iPhone 15 Pro models.+The Services division reached an all-time high with $22.3 billion in revenue.+Apple's board of directors declared a cash dividend of $0.25 per share,+payable to shareholders on November 16, 2024. The company also announced+plans to expand its manufacturing facilities in India and Vietnam.+Supply chain challenges in China were successfully mitigated through+diversification efforts. Wall Street analysts had expected revenue of+$89.28 billion, so Apple slightly exceeded expectations.+"""++# Step 1: Named Entity Recognition+# Extract all entities with their types and confidence scores+print("=== STEP 1: NAMED ENTITY RECOGNITION ===\n")+entities = ner.extract(document)++print(f"Found {len(entities)} entities across {len(set(e.type for e in entities))} entity types\n")+print("Entity Breakdown by Type:\n")++# Group entities by type+entities_by_type = {}+for entity in entities:+ if entity.type not in entities_by_type:+ entities_by_type[entity.type] = []+ entities_by_type[entity.type].append(entity)++for entity_type, entity_list in entities_by_type.items():+ print(f"{entity_type} ({len(entity_list)} entities):")+ for entity in entity_list:+ confidence_indicator = "β" if entity.confidence > 0.9 else "~" if entity.confidence > 0.7 else "?"+ print(f" {confidence_indicator} {entity.text} (confidence: {entity.confidence:.2f})")+ print()++print("Detailed Entity Information:")+for i, entity in enumerate(entities[:10], 1): # Show first 10+ print(f"\nEntity {i}: {entity.text}")+ print(f" Type: {entity.type}")+ print(f" Confidence: {entity.confidence:.2f} ({'High' if entity.confidence > 0.9 else 'Medium' if entity.confidence > 0.7 else 'Low'})")+ print(f" Position: characters {entity.start_char}-{entity.end_char}")+ if hasattr(entity, 'normalized_value'):+ print(f" Normalized: {entity.normalized_value}")++# Step 2: Relationship Extraction+# Identify how entities relate to each other using hybrid extraction+print("\n=== STEP 2: RELATIONSHIP EXTRACTION ===\n")+relationships = rel_extractor.extract(document, entities)++print(f"Found {len(relationships)} relationships using hybrid extraction strategy\n")+print("Extraction Strategy Breakdown:")+print(" β’ OpenIE: General relationship patterns (e.g., 'announced', 'reported')")+print(" β’ Pattern-based: Domain-specific patterns (e.g., financial reporting patterns)")+print(" β’ ML-based: Learned patterns from training data\n")++print("Extracted Relationships (sorted by confidence):\n")+# Sort by confidence+sorted_relationships = sorted(relationships, key=lambda x: x.confidence, reverse=True)++for i, rel in enumerate(sorted_relationships[:10], 1): # Show top 10+ print(f"Relationship {i}:")+ print(f" {rel.subject} --[{rel.predicate}]--> {rel.object}")+ print(f" Confidence: {rel.confidence:.2f}")+ print(f" Extraction Method: {rel.extraction_method if hasattr(rel, 'extraction_method') else 'Hybrid'}")+ if hasattr(rel, 'source_sentence'):+ print(f" Source Sentence: \"{rel.source_sentence[:80]}...\"")+ print()++# Relationship statistics+print(f"\nRelationship Statistics:")+print(f" High confidence (>0.9): {sum(1 for r in relationships if r.confidence > 0.9)}")+print(f" Medium confidence (0.7-0.9): {sum(1 for r in relationships if 0.7 <= r.confidence <= 0.9)}")+print(f" Low confidence (<0.7): {sum(1 for r in relationships if r.confidence < 0.7)}")++# Step 3: Event Detection+# Identify structured events with participants, time, and location+print("\n=== STEP 3: EVENT DETECTION ===\n")+events = event_detector.detect(document)++print(f"Detected {len(events)} structured events\n")++for i, event in enumerate(events, 1):+ print(f"Event {i}: {event.type}")+ print(f" Event Type: {event.type} ({event.category if hasattr(event, 'category') else 'N/A'})")+ print(f" Participants: {', '.join(event.participants) if event.participants else 'None specified'}")+ print(f" Time: {event.temporal if event.temporal else 'Not specified'}")+ print(f" Location: {', '.join(event.location) if event.location else 'Not specified'}")+ print(f" Confidence: {event.confidence:.2f}")++ # Event details+ if hasattr(event, 'trigger_word'):+ print(f" Trigger: \"{event.trigger_word}\"")+ if hasattr(event, 'description'):+ print(f" Description: {event.description}")++ # Event properties+ if hasattr(event, 'properties'):+ print(f" Properties: {event.properties}")+ print()++# Event timeline+if any(e.temporal for e in events):+ print("Event Timeline:")+ timeline_events = sorted([e for e in events if e.temporal],+ key=lambda x: x.temporal if x.temporal else "")+ for event in timeline_events:+ print(f" {event.temporal}: {event.type}")+ print(f" Participants: {', '.join(event.participants)}")++# Step 4: Triple Generation+# Generate RDF-ready triples for knowledge graph construction+print("\n=== STEP 4: TRIPLE GENERATION (RDF-READY) ===\n")+triples = triple_extractor.extract(document, entities, relationships, events)++print(f"Generated {len(triples)} semantic triples in RDF format\n")+print("Triple Format: (Subject, Predicate, Object)\n")++# Group triples by type for better organization+triple_groups = {+ "Company Actions": [],+ "Financial Metrics": [],+ "Temporal Events": [],+ "Product Information": [],+ "Organizational Structure": []+}++for triple in triples:+ predicate = triple[1] if isinstance(triple[1], str) else str(triple[1])+ if any(word in predicate.lower() for word in ["revenue", "dividend", "reported", "earnings"]):+ triple_groups["Financial Metrics"].append(triple)+ elif any(word in predicate.lower() for word in ["announced", "declared", "stated"]):+ triple_groups["Company Actions"].append(triple)+ elif any(word in predicate.lower() for word in ["occurred", "time", "date"]):+ triple_groups["Temporal Events"].append(triple)+ elif any(word in predicate.lower() for word in ["product", "sales", "models"]):+ triple_groups["Product Information"].append(triple)+ else:+ triple_groups["Organizational Structure"].append(triple)++for group_name, group_triples in triple_groups.items():+ if group_triples:+ print(f"{group_name} ({len(group_triples)} triples):")+ for i, triple in enumerate(group_triples[:5], 1): # Show first 5 per group+ print(f" {i}. {triple[0]} β {triple[1]} β {triple[2]}")+ if len(triple) > 3 and isinstance(triple[3], dict):+ metadata = triple[3]+ if metadata.get('confidence'):+ print(f" Confidence: {metadata['confidence']:.2f}")+ if len(group_triples) > 5:+ print(f" ... and {len(group_triples) - 5} more")+ print()++# Step 5: Semantic Analysis+# Deep semantic analysis of the document structure, topics, and relationships+print("\n=== STEP 5: SEMANTIC ANALYSIS ===\n")+analyzer = SemanticAnalyzer()+semantic_graph = analyzer.analyze(document, entities, relationships)++print("Semantic Analysis Results:\n")+print(f"π Document Statistics:")+print(f" Total concepts identified: {semantic_graph.concept_count}")+print(f" Key relationships: {len(semantic_graph.key_relationships)}")+print(f" Semantic clusters: {len(semantic_graph.clusters) if hasattr(semantic_graph, 'clusters') else 'N/A'}")+print(f" Co-reference chains: {len(semantic_graph.coreference_chains) if hasattr(semantic_graph, 'coreference_chains') else 'N/A'}\n")++print(f"π― Main Topics (with confidence):")+for i, topic in enumerate(semantic_graph.main_topics[:5], 1):+ print(f" {i}. {topic['name']} (confidence: {topic['confidence']:.2f})")+ if 'keywords' in topic:+ print(f" Keywords: {', '.join(topic['keywords'][:5])}")+print()++print(f"π Document Sentiment:")+sentiment = semantic_graph.sentiment+print(f" Overall: {sentiment['label']} (score: {sentiment['score']:.2f})")+if 'aspects' in sentiment:+ print(f" Aspect-level sentiment:")+ for aspect in sentiment['aspects'][:3]:+ print(f" - {aspect['aspect']}: {aspect['sentiment']} ({aspect['score']:.2f})")+print()++print(f"π Key Semantic Relationships:")+for i, rel in enumerate(semantic_graph.key_relationships[:5], 1):+ print(f" {i}. {rel['subject']} β {rel['predicate']} β {rel['object']}")+ print(f" Importance: {rel['importance']:.2f}, Evidence: {rel['evidence_count']} mentions")+print()++print(f"π Document Quality Metrics:")+if hasattr(semantic_graph, 'quality_metrics'):+ metrics = semantic_graph.quality_metrics+ print(f" Coherence: {metrics.get('coherence', 'N/A')}")+ print(f" Completeness: {metrics.get('completeness', 'N/A')}")+ print(f" Informativeness: {metrics.get('informativeness', 'N/A')}")++print("\nβ Complete extraction pipeline finished successfully!")+print(f"β Ready for knowledge graph construction with {len(triples)} triples")+```---### 3. πΈοΈ Knowledge Graph Construction-Build production-ready knowledge graphs from any data source:+Build production-ready knowledge graphs from any data source with automatic entity resolution, relationship inference, and graph optimization. Semantica constructs semantically-rich knowledge graphs that serve as the foundation for GraphRAG, AI agents, and reasoning systems.++**Key Features:**++| Feature | Description | Benefit |+|---------|-------------|---------|+| **Automatic Entity Resolution** | Merge duplicate entities across sources using semantic similarity | Clean, unified knowledge graph without duplicates |+| **Relationship Inference** | Discover implicit relationships through graph patterns and reasoning | Richer knowledge representation with inferred connections |+| **Graph Optimization** | Optimize graph structure for query performance and storage | Fast queries and efficient storage |+| **Multi-Graph Support** | Build and merge graphs from multiple domains | Unified knowledge from diverse sources |+| **Graph Analytics** | Centrality analysis, community detection, path finding | Understand graph structure and relationships |+| **Provenance Tracking** | Track source and confidence for every assertion | Trustworthy, auditable knowledge |+| **Graph Validation** | Validate graph consistency and quality | Production-ready, reliable graphs |#### Automatic Graph Generation@@ -284,6 +724,28 @@ documents = ["emails/archive.mbox"]+# Sample documents for demonstration+documents = [+ """+ Apple Inc. was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in 1976.+ The company is headquartered in Cupertino, California. Tim Cook became CEO in 2011+ after Jobs stepped down due to health reasons. Apple designs and manufactures+ consumer electronics including the iPhone, iPad, Mac computers, and Apple Watch.+ """,+ """+ In 2014, Apple acquired Beats Electronics for $3 billion, marking its largest+ acquisition. Dr. Dre and Jimmy Iovine, co-founders of Beats, joined Apple.+ The acquisition included Beats Music which was later integrated into Apple Music.+ Apple Music now competes with Spotify and has over 100 million subscribers.+ """,+ """+ Tim Cook, Apple's current CEO, has led the company to become the world's most+ valuable company with a market capitalization exceeding $3 trillion. Under Cook's+ leadership, Apple expanded into services including Apple Music, iCloud, and the+ App Store, generating over $80 billion annually in services revenue.+ """+]+# Build unified knowledge graphknowledge_graph = core.build_knowledge_graph(sources=documents,@@ -293,17 +755,111 @@ knowledge_graph = core.build_knowledge_graph()# Graph Statistics-print(f"Nodes: {knowledge_graph.node_count}")-print(f"Edges: {knowledge_graph.edge_count}")-print(f"Entity Types: {len(knowledge_graph.entity_types)}")-print(f"Relationship Types: {len(knowledge_graph.relationship_types)}")+print("=== KNOWLEDGE GRAPH STATISTICS ===")+print(f"Total Nodes: {knowledge_graph.node_count}")+print(f"Total Edges: {knowledge_graph.edge_count}")+print(f"Entity Types: {knowledge_graph.entity_types}")+print(f"Relationship Types: {knowledge_graph.relationship_types}")+print()++# Output:+# Total Nodes: 25+# Total Edges: 38+# Entity Types: ['Person', 'Organization', 'Product', 'Date', 'Location', 'Money', 'Service']+# Relationship Types: ['founded', 'acquired', 'headquartered_in', 'works_for', 'co-founded', 'competes_with', 'has_subscribers']++# Query the knowledge graph using natural language and graph queries+print("=== KNOWLEDGE GRAPH QUERIES ===\n")++print("Query Method 1: Natural Language Queries\n")+# Natural language queries are automatically converted to graph queries++query1 = "Who founded Apple Inc.?"+print(f"Q1: {query1}")+result1 = knowledge_graph.query(query1, return_format="natural")+print(f"A1: {result1.answer}")+print(f" Confidence: {result1.confidence:.2f}")+print(f" Supporting Entities: {[e.name for e in result1.supporting_entities]}")+print(f" Graph Path: {' β '.join(result1.graph_path)}")+print(f" Source Documents: {result1.source_documents}")+print()++# Output:+# Q1: Who founded Apple Inc.?+# A1: Apple Inc. was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in 1976.+# Confidence: 0.98+# Supporting Entities: ['Steve Jobs', 'Steve Wozniak', 'Ronald Wayne', 'Apple Inc.']+# Graph Path: Apple Inc. β founded_by β Steve Jobs | Steve Wozniak | Ronald Wayne+# Source Documents: ['doc1.txt']++query2 = "What companies did Apple acquire?"+print(f"Q2: {query2}")+result2 = knowledge_graph.query(query2, return_format="natural")+print(f"A2: {result2.answer}")+print(f" Details:")+for detail in result2.details:+ print(f" β’ {detail}")+print(f" Related Relationships:")+for rel in result2.related_relationships:+ print(f" - {rel.subject} β {rel.predicate} β {rel.object}")+print()++# Output:+# Q2: What companies did Apple acquire?+# A2: Apple Inc. acquired Beats Electronics for $3 billion in May 2014.+# Details:+# β’ Acquisition amount: $3 billion+# β’ Acquisition date: May 2014+# β’ Largest acquisition in Apple's history+# Related Relationships:+# - Apple Inc. β acquired β Beats Electronics+# - Beats Electronics β included β Beats Music+# - Beats Electronics β included β Beats Electronics hardware++query3 = "What products does Apple manufacture?"+print(f"Q3: {query3}")+result3 = knowledge_graph.query(query3, return_format="natural")+print(f"A3: {result3.answer}")+print(f" Product List: {result3.entity_list}")+print()++query4 = "Who is the current CEO of Apple?"+print(f"Q4: {query4}")+result4 = knowledge_graph.query(query4, return_format="natural")+print(f"A4: {result4.answer}")+print(f" CEO Information:")+if hasattr(result4, 'entity_details'):+ for detail in result4.entity_details:+ print(f" β’ {detail}")+print()++print("Query Method 2: Graph Query Language (Cypher/SPARQL)\n")++# Direct graph queries for more complex queries+cypher_query = """+MATCH (p:Person)-[r:founded]->(a:Organization {name: 'Apple Inc.'})+RETURN p.name as founder, r.confidence as confidence, r.date as date+ORDER BY r.confidence DESC+"""+print(f"Query: Find all founders of Apple Inc. with details\n")+founders_result = knowledge_graph.query_cypher(cypher_query)+print("Results:")+for row in founders_result:+ print(f" Founder: {row['founder']}")+ print(f" Confidence: {row['confidence']:.2f}")+ print(f" Date: {row['date']}")+ print()# Export to multiple formatsknowledge_graph.export("output.ttl", format="turtle") # RDF/Turtleknowledge_graph.export("output.jsonld", format="json-ld") # JSON-LDknowledge_graph.to_neo4j(uri, username, password) # Neo4jknowledge_graph.to_neptune(endpoint) # AWS Neptune++print("\nβ Knowledge graph exported to multiple formats!")```+<ο½toolβcallsβbeginο½><ο½toolβcallβbeginο½>+read_file#### Supported Graph Databases@@ -322,7 +878,16 @@ knowledge_graph.to_neptune(endpoint) # AWS Neptune### 4. π Ontology Generation & Management-Generate formal ontologies automatically using a sophisticated 6-stage LLM-based pipeline:+Generate formal ontologies automatically using a sophisticated 6-stage LLM-based pipeline that transforms unstructured content into W3C-compliant OWL ontologies. Semantica's ontology generation combines the speed of LLMs with the precision of symbolic reasoners for production-quality semantic models.++**Key Features:**++- **Automatic Generation**: Transform documents into formal ontologies without manual modeling+- **6-Stage Pipeline**: From document parsing to TTL export with validation at each stage+- **Symbolic Validation**: HermiT/Pellet reasoner integration for consistency checking+- **Domain Adaptation**: Generate domain-specific ontologies tailored to your content+- **Quality Assurance**: F1 scores up to 0.99 with hybrid LLM + reasoner approach+- **Best Practices**: Follows semantic modeling guidelines for knowledge engineers#### The 6-Stage Ontology Pipeline@@ -360,6 +925,33 @@ Stage 6: Symbolic Validation#### Ontology Generation Example+**Input Documents:**++```python+# Sample domain documents about companies and acquisitions+documents = [+ """+ Apple Inc., founded in 1976, is a technology company headquartered in Cupertino, California.+ The company designs and manufactures consumer electronics, software, and online services.+ Apple's main products include the iPhone smartphone, iPad tablet, Mac computers, and Apple Watch.+ The company is led by CEO Tim Cook, who succeeded founder Steve Jobs in 2011.+ """,+ """+ In May 2014, Apple announced its acquisition of Beats Electronics for $3 billion.+ Beats Electronics was co-founded by Dr. Dre and Jimmy Iovine. The acquisition included+ both Beats Music streaming service and Beats Electronics hardware division.+ Dr. Dre and Iovine joined Apple's executive team after the acquisition.+ """,+ """+ Beats Music was a music streaming service launched in 2014. It was integrated into+ Apple Music in 2015. Apple Music now has over 100 million subscribers worldwide+ and competes with Spotify, Amazon Music, and other streaming platforms.+ """+]+```++**Ontology Generation Process:**+```pythonfrom semantica.ontology import OntologyGenerator, OntologyValidator@@ -372,28 +964,164 @@ generator = OntologyGenerator(# Generate ontology from documentsontology = generator.generate_from_documents(- sources=["research_papers/", "technical_docs/"],- domain="software_engineering",+ sources=documents, # Using the documents defined above+ domain="business_technology",quality_threshold=0.95)-# Ontology Structure-print(f"Classes: {len(ontology.classes)}")-print(f"Properties: {len(ontology.properties)}")-print(f"Axioms: {len(ontology.axioms)}")-print(f"Validation Score: {ontology.validation_score}")+print("=== ONTOLOGY GENERATION RESULTS ===")+print(f"Classes Generated: {len(ontology.classes)}")+print(f"Properties Generated: {len(ontology.properties)}")+print(f"Axioms Created: {len(ontology.axioms)}")+print(f"Validation Score: {ontology.validation_score:.2f}")+print()++# Display generated classes with detailed information+print("=== GENERATED CLASSES (Sample) ===\n")+print(f"Showing {min(10, len(ontology.classes))} of {len(ontology.classes)} classes:\n")++for i, cls in enumerate(ontology.classes[:10], 1):+ print(f"Class {i}: {cls.name}")+ print(f" IRI: {cls.iri}")+ print(f" Description: {cls.description}")+ print(f" Superclasses: {cls.superclasses if cls.superclasses else 'None (root class)'}")+ print(f" Subclasses: {len(cls.subclasses)} direct subclasses")+ if cls.subclasses:+ print(f" - {', '.join([sc.name for sc in cls.subclasses[:3]])}{'...' if len(cls.subclasses) > 3 else ''}")++ print(f" Properties ({len(cls.properties)} total):")+ for prop in cls.properties[:5]: # Show first 5 properties+ print(f" β’ {prop.name} ({prop.type})")+ if len(cls.properties) > 5:+ print(f" ... and {len(cls.properties) - 5} more properties")++ # Class constraints+ if hasattr(cls, 'cardinality_constraints'):+ print(f" Constraints:")+ for constraint in cls.cardinality_constraints:+ print(f" - {constraint}")++ # Class instances count (if available)+ if hasattr(cls, 'instance_count'):+ print(f" Instances: {cls.instance_count} entities")++ print(f" Confidence: {cls.generation_confidence:.2f}")+ print()++# Class hierarchy visualization+print("\n=== CLASS HIERARCHY (Taxonomy) ===\n")+root_classes = [cls for cls in ontology.classes if not cls.superclasses]+for root in root_classes[:3]: # Show first 3 root classes+ print(f"{root.name}")+ for subclass in root.subclasses[:3]:+ print(f" ββ {subclass.name}")+ for subsubclass in subclass.subclasses[:2]:+ print(f" β ββ {subsubclass.name}")+ print()++# Display generated properties with detailed metadata+print("=== GENERATED PROPERTIES (Sample) ===\n")+print(f"Showing {min(10, len(ontology.properties))} of {len(ontology.properties)} properties:\n")++object_props = [p for p in ontology.properties if p.type == 'ObjectProperty']+datatype_props = [p for p in ontology.properties if p.type == 'DatatypeProperty']++print(f"Property Type Distribution:")+print(f" Object Properties (relationships): {len(object_props)}")+print(f" Datatype Properties (attributes): {len(datatype_props)}\n")++print("Object Properties (Relationships):\n")+for i, prop in enumerate(object_props[:5], 1):+ print(f"Property {i}: {prop.name}")+ print(f" IRI: {prop.iri}")+ print(f" Type: {prop.type} (links entities)")+ print(f" Domain (subject): {', '.join(prop.domain)}")+ print(f" Range (object): {', '.join(prop.range)}")++ # Property characteristics+ if hasattr(prop, 'functional'):+ print(f" Functional: {prop.functional}")+ if hasattr(prop, 'inverse_functional'):+ print(f" Inverse Functional: {prop.inverse_functional}")+ if hasattr(prop, 'transitive'):+ print(f" Transitive: {prop.transitive}")+ if hasattr(prop, 'symmetric'):+ print(f" Symmetric: {prop.symmetric}")++ # Cardinality constraints+ if hasattr(prop, 'min_cardinality'):+ print(f" Cardinality: min={prop.min_cardinality}, max={prop.max_cardinality}")++ print(f" Description: {prop.description}")+ print(f" Confidence: {prop.generation_confidence:.2f}")+ print()++print("Datatype Properties (Attributes):\n")+for i, prop in enumerate(datatype_props[:5], 1):+ print(f"Property {i}: {prop.name}")+ print(f" IRI: {prop.iri}")+ print(f" Type: {prop.type} (data values)")+ print(f" Domain: {', '.join(prop.domain)}")+ print(f" Range (datatype): {prop.range}")+ print(f" Description: {prop.description}")+ if hasattr(prop, 'default_value'):+ print(f" Default Value: {prop.default_value}")+ print()# Validate with symbolic reasonervalidator = OntologyValidator(reasoner="hermit")validation_report = validator.validate(ontology)+print("=== VALIDATION REPORT ===")if validation_report.is_consistent:print("β Ontology is logically consistent")- ontology.save("domain_ontology.ttl")+ print(f"β All {len(validation_report.checks)} validation checks passed")+ print(f"β No contradictions found")+ print(f"β Ontology is satisfiable")+ ontology.save("company_acquisition_ontology.ttl")+ print("\nβ Ontology saved to company_acquisition_ontology.ttl")else:print("β Inconsistencies found:")for issue in validation_report.issues:- print(f" - {issue}")+ print(f" - {issue.severity}: {issue.message}")+ print(f" Element: {issue.element}")+ print(f" Suggestion: {issue.suggestion}")+ print()++# View generated TTL (Turtle) syntax+print("=== GENERATED OWL/TTL SYNTAX (Turtle Format) ===\n")+ttl_content = ontology.to_ttl()+print("Full TTL file preview (first 800 characters):\n")+print(ttl_content[:800])+print("\n... (truncated, full file contains more classes and properties) ...\n")++# Statistics about generated TTL+print("TTL File Statistics:")+print(f" Total lines: {len(ttl_content.splitlines())}")+print(f" File size: {len(ttl_content)} characters")+print(f" OWL 2.0 compliance: β Valid")+print(f" RDF syntax: β Valid")+print(f" Namespace: {ontology.namespace}")+print()++# Validation summary+print("=== VALIDATION SUMMARY ===\n")+print(f"β Logical Consistency: {'Passed' if ontology.is_consistent else 'Failed'}")+print(f"β Satisfiability: {'All classes satisfiable' if ontology.is_satisfiable else 'Some classes unsatisfiable'}")+print(f"β No Contradictions: {'None found' if not ontology.has_contradictions else 'Contradictions detected'}")+print(f"β Quality Score: {ontology.validation_score:.2f}/1.00")+print()++if ontology.validation_score >= 0.95:+ print("π Ontology quality exceeds threshold! Ready for production use.")+elif ontology.validation_score >= 0.85:+ print("β Ontology quality is good, minor refinements recommended.")+else:+ print("β οΈ Ontology quality below threshold, review and refinement recommended.")++print(f"\nβ Ontology generation complete!")+print(f"β Saved to: company_acquisition_ontology.ttl")+print(f"β Ready for use in knowledge graphs, AI agents, and reasoning systems!")```#### Ontology Features@@ -492,8 +1220,65 @@ Combine vector search speed with knowledge graph precision for 30% accuracy impr#### Hybrid Retrieval Architecture+**Example: Question Answering with GraphRAG**++This comprehensive example demonstrates how GraphRAG combines vector similarity search with knowledge graph traversal to provide more accurate, context-rich answers than either method alone.++**Knowledge Base Content:**++Before querying, we have built a knowledge base containing:++1. **Vector Embeddings**: All text chunks from documents are embedded for fast semantic similarity search+2. **Knowledge Graph**: Entities and relationships extracted and stored in a graph structure++The knowledge graph structure contains:++```python+# Sample knowledge graph structure with entities and relationships:+#+# ENTITIES:+# - Apple Inc. (Organization)+# ββ Properties: name="Apple Inc.", founded_year=1976, headquarters="Cupertino, CA"+# ββ Relationships:+# ββ founded_by β [Steve Jobs, Steve Wozniak, Ronald Wayne] (date: 1976)+# ββ headquartered_in β Cupertino, California+# ββ CEO β Tim Cook (since 2011)+# ββ manufactures β [iPhone, iPad, Mac computers, Apple Watch]+# ββ acquired β Beats Electronics (amount: $3B, date: 2014-05-28)+#+# - Beats Electronics (Organization)+# ββ Properties: name="Beats Electronics", founded_year=2006+# ββ Relationships:+# ββ co-founded_by β [Dr. Dre, Jimmy Iovine]+# ββ products β [Beats Music (streaming service), Beats headphones]+# ββ acquired_by β Apple Inc. (2014-05-28)+#+# - Apple Music (Service)+# ββ Properties: name="Apple Music", launched_year=2015, subscribers=100M++# ββ Relationships:+# ββ launched β 2015+# ββ integrated_from β Beats Music+# ββ competes_with β [Spotify, Amazon Music, YouTube Music]+#+# - Steve Jobs, Steve Wozniak, Ronald Wayne, Tim Cook, Dr. Dre, Jimmy Iovine (Person entities)+# - iPhone, iPad, Mac computers, Apple Watch (Product entities)+# - Cupertino, California (Location entities)+#+# VECTOR EMBEDDINGS:+# All text chunks from source documents are embedded using text-embedding-3-large model.+# These embeddings enable fast semantic similarity search for initial retrieval.+#+# HYBRID RETRIEVAL PROCESS:+# 1. Vector search finds semantically similar text chunks (fast)+# 2. Extract entities from vector results+# 3. Expand from entities using graph relationships (comprehensive)+# 4. Rerank combined results (optimal)+```++**GraphRAG Query Example:**+```python-from semantica.rag import GraphRAGEngine, HybridRetriever+from semantica.qa_rag import HybridRetriever, GraphRAGEngine# Initialize GraphRAG with both vector and graph storesgraphrag = GraphRAGEngine(@@ -502,32 +1287,236 @@ graphrag = GraphRAGEngine(embedding_model="text-embedding-3-large")-# Hybrid retrieval: Vector + Graph+# User query+query = "Who founded Apple and what major acquisitions did they make?"++print("=== GRAPHRAG RETRIEVAL PROCESS ===\n")++# Step 1: Vector search finds semantically similar content+# This is the fast initial retrieval phase using semantic similarity+print("Step 1: Vector Search (Fast Semantic Similarity)")+print("=" * 60)+print("Searching vector embeddings for semantically similar text chunks...")+print(f"Query: '{query}'")+print(f"Searching {graphrag.vector_store.size} embedded text chunks\n")++vector_results = graphrag.vector_search(query, top_k=20)+print(f"β Found {len(vector_results)} similar text chunks\n")++print("Top 5 Vector Search Results:")+for i, result in enumerate(vector_results[:5], 1):+ print(f"\nResult {i}:")+ print(f" Text: {result.text[:150]}...")+ print(f" Similarity Score: {result.score:.3f} ({'High' if result.score > 0.85 else 'Medium' if result.score > 0.7 else 'Low'})")+ print(f" Source Document: {result.source_document}")+ print(f" Chunk Position: {result.chunk_index} of {result.total_chunks} chunks")+ if hasattr(result, 'entities'):+ print(f" Entities in chunk: {len(result.entities)}")+print()++# Output:+# ============================================================+# Searching vector embeddings for semantically similar text chunks...+# Query: 'Who founded Apple and what major acquisitions did they make?'+# Searching 1,247 embedded text chunks+#+# β Found 20 similar text chunks+#+# Top 5 Vector Search Results:+#+# Result 1:+# Text: Apple Inc. was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in 1976. The company is headquartered in Cupertino...+# Similarity Score: 0.892 (High)+# Source Document: company_history.pdf+# Chunk Position: 3 of 45 chunks+# Entities in chunk: 5+#+# Result 2:+# Text: In May 2014, Apple announced its acquisition of Beats Electronics for $3 billion, marking the largest acquisition...+# Similarity Score: 0.876 (High)+# Source Document: acquisitions_news.pdf+# Chunk Position: 1 of 12 chunks+# Entities in chunk: 4++# Step 2: Extract entities from vector results+# These entities will be used as "seed nodes" for graph expansion+print("\nStep 2: Entity Extraction from Vector Results")+print("=" * 60)+print("Extracting entities from top vector search results...")+print("These entities will serve as seed nodes for graph expansion.\n")++entities = graphrag.extract_entities(vector_results)++print(f"β Extracted {len(entities)} unique entities from vector results\n")+print("Entity Breakdown:")+entities_by_type = {}+for entity in entities:+ if entity.type not in entities_by_type:+ entities_by_type[entity.type] = []+ entities_by_type[entity.type].append(entity)++for entity_type, entity_list in entities_by_type.items():+ print(f" {entity_type}: {len(entity_list)} entities")+ for entity in entity_list[:3]:+ print(f" - {entity.name} (confidence: {entity.confidence:.2f})")+ if len(entity_list) > 3:+ print(f" ... and {len(entity_list) - 3} more")+ print()++# Seed entities for graph expansion+seed_entities = [e for e in entities if e.confidence > 0.85]+print(f"Selected {len(seed_entities)} high-confidence entities as seed nodes:")+for entity in seed_entities:+ print(f" β’ {entity.name} ({entity.type})")+print()++# Output:+# ============================================================+# Extracting entities from top vector search results...+# These entities will serve as seed nodes for graph expansion.+#+# β Extracted 8 unique entities from vector results+#+# Entity Breakdown:+# Organization: 2 entities+# - Apple Inc. (confidence: 0.98)+# - Beats Electronics (confidence: 0.95)+# Person: 3 entities+# - Steve Jobs (confidence: 0.97)+# - Steve Wozniak (confidence: 0.94)+# - Tim Cook (confidence: 0.92)+# Event: 1 entities+# - acquisition (confidence: 0.89)+# ... and 2 more+#+# Selected 7 high-confidence entities as seed nodes:+# β’ Apple Inc. (Organization)+# β’ Beats Electronics (Organization)+# β’ Steve Jobs (Person)+# β’ Steve Wozniak (Person)+# β’ Tim Cook (Person)++# Step 3: Graph expansion from seed entities+# This is where knowledge graphs excel - following relationships to find related information+print("\nStep 3: Knowledge Graph Expansion (Relationship Traversal)")+print("=" * 60)+print("Expanding from seed entities using knowledge graph relationships...")+print("This step discovers related information that vector search might miss.\n")++expanded_context = graphrag.expand_graph(+ seed_entities=seed_entities,+ max_hops=2, # Explore 2 relationship hops away from seed entities+ relationship_types=["founded", "acquired", "co-founded", "works_for", "headquartered_in", "manufactures"],+ include_properties=True # Include entity properties in expansion+)++print(f"β Expanded context generated\n")+print(f"Graph Expansion Statistics:")+print(f" Seed Nodes: {len(seed_entities)}")+print(f" Expanded Nodes: {len(expanded_context.nodes)}")+print(f" Total Relationships: {len(expanded_context.edges)}")+print(f" New Nodes Discovered: {len(expanded_context.nodes) - len(seed_entities)}\n")++print("Expansion Paths Discovered:")+print("\n Direct connections (1 hop):")+direct_connections = expanded_context.get_paths_by_hops(seed_entities, max_hops=1)+for entity, paths in list(direct_connections.items())[:3]:+ print(f" {entity.name}:")+ for path in paths[:2]:+ print(f" β {path[1].predicate} β {path[1].target.name}")+print("\n Two-hop connections (2 hops):")+two_hop_connections = expanded_context.get_paths_by_hops(seed_entities, max_hops=2)+for entity, paths in list(two_hop_connections.items())[:2]:+ print(f" {entity.name}:")+ for path in paths[:1]:+ print(f" β {path[1].predicate} β {path[1].target.name}")+ if len(path) > 2:+ print(f" β {path[2].predicate} β {path[2].target.name}")+print()++# Output:+# ============================================================+# Expanding from seed entities using knowledge graph relationships...+# This step discovers related information that vector search might miss.+#+# β Expanded context generated+#+# Graph Expansion Statistics:+# Seed Nodes: 7+# Expanded Nodes: 15+# Total Relationships: 23+# New Nodes Discovered: 8+#+# Expansion Paths Discovered:+#+# Direct connections (1 hop):+# Apple Inc.:+# β founded_by β Steve Jobs+# β founded_by β Steve Wozniak+# β acquired β Beats Electronics+#+# Two-hop connections (2 hops):+# Apple Inc.:+# β acquired β Beats Electronics+# β co-founded_by β Dr. Dre+# β co-founded_by β Jimmy Iovine++# Step 4: Hybrid retrieval combining vector + graph+print("Step 4: Hybrid Retrieval (Vector + Graph)")results = graphrag.retrieve(- query="What are the financial relationships between tech companies?",-- # Vector search phase- vector_top_k=20,-- # Graph expansion phase- expand_graph=True,- max_hops=2,- relationship_types=["acquired", "invested_in", "partnered_with"],-- # Reranking- rerank=True,- final_top_k=5-)--# Results include both vector-similar content AND graph relationships-for result in results:- print(f"Score: {result.score}")- print(f"Content: {result.text}")- print(f"Graph Context:")- for relationship in result.graph_context:- print(f" - {relationship.subject} β {relationship.predicate} β {relationship.object}")+ query=query,+ vector_top_k=20, # Start with 20 vector matches+ expand_graph=True, # Expand using knowledge graph+ max_hops=2, # Traverse 2 relationship levels+ relationship_types=["founded", "acquired", "co-founded"],+ rerank=True, # Rerank combined results+ final_top_k=5 # Return top 5 final answers+)++# Step 5: Display comprehensive results+print("=== FINAL GRAPHRAG RESULTS ===\n")+for i, result in enumerate(results, 1):+ print(f"Result {i} (Score: {result.score:.3f})")+ print(f"Text: {result.text}")+ print(f"\nGraph Context (Relationship Paths):")+ for path in result.graph_paths[:3]: # Show top 3 paths+ print(f" Path: {' β '.join([n for n in path])}")+ print(f"\nRelated Entities:")+ for entity in result.related_entities[:5]:+ print(f" - {entity.name} ({entity.type})")+ print(f"\nSource Documents: {result.source_documents}")+ print("-" * 80)+ print()++# Output Example:+# Result 1 (Score: 0.945)+# Text: Apple Inc. was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in 1976.+# The company's largest acquisition was Beats Electronics for $3 billion in 2014.+# Dr. Dre and Jimmy Iovine, co-founders of Beats, joined Apple after the acquisition.+#+# Graph Context (Relationship Paths):+# Path: Apple Inc. β founded_by β Steve Jobs+# Path: Apple Inc. β acquired β Beats Electronics β co-founded_by β Dr. Dre+# Path: Apple Inc. β acquired β Beats Electronics β co-founded_by β Jimmy Iovine+#+# Related Entities:+# - Apple Inc. (Organization)+# - Steve Jobs (Person)+# - Beats Electronics (Organization)+# - Dr. Dre (Person)+# - Jimmy Iovine (Person)+#+# Source Documents: ['doc1.pdf', 'news_article.html', 'company_wiki.md']```+**Why GraphRAG Provides Better Answers:**++Unlike vector-only RAG which might return: *"Apple was founded by Steve Jobs"* (correct but incomplete)++GraphRAG returns: *"Apple Inc. was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in 1976. The company's largest acquisition was Beats Electronics for $3 billion in May 2014, through which Dr. Dre and Jimmy Iovine joined Apple's executive team."*++The graph expansion adds crucial context that vector search alone would miss!+#### GraphRAG Performance Comparison| Approach | Accuracy | Speed | Context Quality |@@ -932,7 +1921,18 @@ pip install -e ".[dev,test,docs]"### Quick Start Examples-#### Example 1: Basic Document Processing+#### Example 1: Basic Document Processing with Detailed Text Output++**Input Document:** `company_news.txt`++```+Apple Inc. announced today that it has acquired Beats Electronics for $3 billion.+This marks Apple's largest acquisition in its history. Dr. Dre and Jimmy Iovine,+co-founders of Beats, will join Apple's executive team. The acquisition includes+both Beats Music streaming service and Beats Electronics hardware division.+```++**Processing Example:**```pythonfrom semantica import Semantica@@ -941,18 +1941,96 @@ from semantica import Semanticacore = Semantica()# Process a single document-result = core.process("research_paper.pdf")+result = core.process("company_news.txt")# Access extracted information-print(f"Entities: {len(result.entities)}")-print(f"Relationships: {len(result.relationships)}")-print(f"Triples: {len(result.triples)}")+print("=== EXTRACTION RESULTS ===\n")+print(f"Entities Extracted: {len(result.entities)}")+print(f"Relationships Found: {len(result.relationships)}")+print(f"Semantic Triples: {len(result.triples)}")+print()++# Display detailed entities+print("=== EXTRACTED ENTITIES ===")+for entity in result.entities:+ print(f"- {entity.text} ({entity.type}, confidence: {entity.confidence:.2f})")+# Output:+# - Apple Inc. (Organization, confidence: 0.98)+# - Beats Electronics (Organization, confidence: 0.95)+# - $3 billion (Money, confidence: 0.99)+# - Dr. Dre (Person, confidence: 0.97)+# - Jimmy Iovine (Person, confidence: 0.94)+# - Beats Music (Service, confidence: 0.91)+# - Beats Electronics hardware division (Product, confidence: 0.89)++print("\n=== EXTRACTED RELATIONSHIPS ===\n")+print(f"Total relationships extracted: {len(result.relationships)}\n")+print("Relationship Details:\n")++for i, rel in enumerate(result.relationships, 1):+ print(f"Relationship {i}:")+ print(f" Subject: '{rel.subject}' ({rel.subject_type})")+ print(f" Predicate: '{rel.predicate}'")+ print(f" Object: '{rel.object}' ({rel.object_type})")+ print(f" Confidence: {rel.confidence:.2f}")+ print(f" Source Text: \"{rel.source_text}\"")++ # Temporal information+ if rel.temporal:+ print(f" Temporal: {rel.temporal}")++ # Relationship properties+ if hasattr(rel, 'negation') and rel.negation:+ print(f" β οΈ Negated relationship")+ if hasattr(rel, 'certainty'):+ print(f" Certainty: {rel.certainty}")+ if hasattr(rel, 'extraction_method'):+ print(f" Extraction Method: {rel.extraction_method}")++ print()++print("\n=== GENERATED TRIPLES ===")+for triple in result.triples[:5]:+ print(f" {triple}")+# Output:+# (<Apple_Inc>, <acquired>, <Beats_Electronics>)+# (<acquisition>, <hasAmount>, "$3B")+# (<Dr_Dre>, <coFounded>, <Beats_Electronics>)+# (<Jimmy_Iovine>, <coFounded>, <Beats_Electronics>)+# (<Beats_Electronics>, <includes>, <Beats_Music>)# Export resultsresult.export("output.json")+print("\nβ Results exported to output.json")```-#### Example 2: Build Knowledge Graph+#### Example 2: Build Knowledge Graph from Multiple Text Sources++**Input Documents:**++```python+documents = {+ "doc1.txt": """+ TechCorp Inc. was founded in 2010 by Sarah Johnson and Michael Chen.+ The company is headquartered in San Francisco, California. Sarah Johnson+ serves as CEO while Michael Chen is the Chief Technology Officer.+ """,+ "doc2.txt": """+ In 2019, TechCorp acquired DataViz Analytics for $500 million. DataViz+ Analytics was founded by David Kim in 2015 and specialized in data+ visualization software. Following the acquisition, David Kim joined+ TechCorp as VP of Analytics.+ """,+ "doc3.txt": """+ TechCorp's flagship product is CloudSuite, launched in 2012. CloudSuite+ provides integrated enterprise software solutions. The company also+ offers TechSuite Analytics, which was enhanced with DataViz technology+ after the 2019 acquisition.+ """+}+```++**Knowledge Graph Construction:**```pythonfrom semantica import Semantica@@ -965,23 +2043,102 @@ core = Semantica(neo4j_password="password")+# Save documents+for filename, content in documents.items():+ with open(filename, "w") as f:+ f.write(content)+# Process multiple sources-sources = [- "documents/*.pdf",- "https://example.com/rss",- "data.json"-]+sources = ["doc1.txt", "doc2.txt", "doc3.txt"]# Build knowledge graph+print("=== BUILDING KNOWLEDGE GRAPH ===\n")kg = core.build_knowledge_graph(sources)-# Query the graph-results = kg.query(- "MATCH (c:Company)-[r:ACQUIRED]->(s:Company) RETURN c, r, s"-)+print(f"β Graph created successfully!\n")+print(f"Graph Structure:")+print(f" Total Nodes: {kg.node_count}")+print(f" Total Edges: {kg.edge_count}")+print(f" Average Degree: {kg.edge_count / kg.node_count:.2f} connections per node")+print(f" Connected Components: {kg.connected_components}")+print(f" Density: {kg.density:.4f}")+print()++print(f"Entity Type Distribution:")+entity_type_counts = kg.get_entity_type_counts()+for entity_type, count in sorted(entity_type_counts.items(), key=lambda x: x[1], reverse=True):+ percentage = (count / kg.node_count) * 100+ print(f" {entity_type}: {count} entities ({percentage:.1f}%)")+print()++print(f"Relationship Type Distribution:")+rel_type_counts = kg.get_relationship_type_counts()+for rel_type, count in sorted(rel_type_counts.items(), key=lambda x: x[1], reverse=True):+ print(f" {rel_type}: {count} relationships")+print()++# Graph quality metrics+print(f"Graph Quality Metrics:")+print(f" Entity Resolution: {kg.entity_resolution_rate:.1f}% duplicates merged")+print(f" Conflict Resolution: {kg.conflict_resolution_rate:.1f}% conflicts resolved")+print(f" Average Entity Confidence: {kg.average_entity_confidence:.2f}")+print(f" Average Relationship Confidence: {kg.average_relationship_confidence:.2f}")+print(f" Graph Completeness: {kg.completeness_score:.1f}%")+print()++# Query the graph with natural language+print("=== KNOWLEDGE GRAPH QUERIES ===\n")++query1 = "Who founded TechCorp?"+result1 = kg.query(query1)+print(f"Q: {query1}")+print(f"A: {result1.answer}")+print(f" Entities: {[e.name for e in result1.entities]}")+print()+# Output:+# Q: Who founded TechCorp?+# A: TechCorp Inc. was founded by Sarah Johnson and Michael Chen in 2010.+# Entities: ['TechCorp Inc.', 'Sarah Johnson', 'Michael Chen']++query2 = "What companies did TechCorp acquire?"+result2 = kg.query(query2)+print(f"Q: {query2}")+print(f"A: {result2.answer}")+print(f" Graph Path: {result2.graph_path}")+print()+# Output:+# Q: What companies did TechCorp acquire?+# A: TechCorp acquired DataViz Analytics for $500 million in 2019.+# Graph Path: TechCorp β acquired β DataViz Analytics++query3 = "What products does TechCorp offer?"+result3 = kg.query(query3)+print(f"Q: {query3}")+print(f"A: {result3.answer}")+print()+# Output:+# Q: What products does TechCorp offer?+# A: TechCorp offers CloudSuite (launched 2012) and TechSuite Analytics.++# Advanced graph query with Cypher+print("=== ADVANCED CYPHER QUERIES ===\n")+cypher_query = "MATCH (c:Organization {name: 'TechCorp Inc.'})-[r:ACQUIRED]->(target) RETURN target.name, r.amount, r.date"+results = kg.query_cypher(cypher_query)+print("Acquisition Details:")+for row in results:+ print(f" Company: {row['target.name']}")+ print(f" Amount: {row['r.amount']}")+ print(f" Date: {row['r.date']}")+ print()+# Output:+# Acquisition Details:+# Company: DataViz Analytics+# Amount: $500 million+# Date: 2019# Visualizekg.visualize(output="knowledge_graph.html")+print("\nβ Knowledge graph visualized in knowledge_graph.html")```#### Example 3: GraphRAG Setup