Compare commits

..
191 Commits
Author SHA1 Message Date
KaifAhmad1 e7e67bd673 Enhance normalize module: fix recursion, add comprehensive tests (57 passed) 2025-12-11 16:58:14 +05:30
KaifAhmad1 5d5928badf feat: enhance kg module with tests, conflict resolution placeholders, and doc updates 2025-12-11 15:21:39 +05:30
Mohd Kaif 2f94986b01 Merge pull request #74 from Hawksight-AI/ingest
validate and fix ingest module and notebooks
2025-12-11 00:31:15 +05:30
KaifAhmad1 3e7863aa23 feat(ingest): validate and fix ingest module and notebooks
- Fix ProgressTracker usage in MCPIngestor and RepoIngestor
- Fix recursive calls in methods.py
- Add comprehensive test suite for all ingest submodules (tests/ingest/test_submodules.py)
- Add integration tests for key cookbooks (tests/ingest/test_cookbook_integration.py)
- Fix and align existing tests (test_notebook_02.py, test_notebook_06.py)
- Ensure full coverage of all 15 data sources
2025-12-11 00:28:25 +05:30
Mohd Kaif d23ca2d743 Update README.md 2025-12-10 21:56:26 +05:30
Mohd Kaif 507a1f9c71 Merge pull request #73 from Hawksight-AI/graph-store
Remove KuzuDB backend support and cleanup references
2025-12-10 20:33:29 +05:30
KaifAhmad1 afc94ad059 Remove KuzuDB backend support and cleanup references 2025-12-10 20:30:59 +05:30
Mohd Kaif bad6bd0326 Merge pull request #72 from Hawksight-AI/export
Fix export_yaml schema export bug and update docs
2025-12-10 18:43:35 +05:30
KaifAhmad1 3207eb3b41 Fix export_yaml schema export bug and update docs
- Fix YAMLSchemaExporter method call in export_yaml (use export_ontology_schema)
- Add file writing logic to export_yaml for schema method
- Update docs/reference/export.md and semantica/export/export_usage.md with correct method signature
- Add test_export_methods_wrapper.py to verify schema export
- Prevent infinite recursion in method_registry lookups in methods.py
2025-12-10 18:40:32 +05:30
Mohd Kaif 3457f4d7c8 Merge pull request #71 from Hawksight-AI/export
Enhanced Export Module Testing & Notebook Fixes
2025-12-10 18:19:23 +05:30
KaifAhmad1 7bbf8e9881 Enhance export module, fix notebooks, and add tests
- Added comprehensive unit tests for export module (tests/test_export_module.py)

- Added simulation tests for notebooks 15 and 05 (tests/test_notebook*.py)

- Fixed GraphBuilder.build() signature usage in notebooks and simulations

- Fixed CSVExporter file path handling and CSV content verification

- Fixed VectorExporter data format in notebooks

- Updated YAMLSchemaExporter usage

- Fixed conflict detection in GraphBuilder

- Verified all export formats (JSON, CSV, RDF, GraphML, YAML, OWL, Vector, LPG)
2025-12-10 18:15:04 +05:30
Mohd Kaif a163a46c56 Merge pull request #70 from Hawksight-AI/embeddings
Dynamic Embedding Model Switching & Enhanced Testing
2025-12-10 17:37:17 +05:30
KaifAhmad1 6ee19d971e feat: enhance embeddings with dynamic model switching, updated docs and tests 2025-12-10 17:32:43 +05:30
Mohd Kaif 0f48b5bc87 Merge pull request #69 from Hawksight-AI/conflicts
`fix(conflicts/deduplication): Fix critical bugs and add comprehensive verification for Conflict and Deduplication modules`
2025-12-10 16:11:05 +05:30
KaifAhmad1 e7bf664868 Update PR description 2025-12-10 16:07:01 +05:30
KaifAhmad1 ff7768f1ad Fix deduplication/conflict bugs and add verification scripts 2025-12-10 16:05:46 +05:30
Mohd Kaif e0fce67ab2 Merge pull request #68 from Hawksight-AI/core
`test(core/pipeline): Add comprehensive unit tests and fix pipeline validation logic`
2025-12-10 15:31:20 +05:30
KaifAhmad1 926c518bd7 feat: comprehensive testing and fixes for Core, KG, Conflicts, and Pipeline modules 2025-12-10 15:26:05 +05:30
Mohd Kaif ea477f9b32 Merge pull request #67 from Hawksight-AI/context-engineering
Context Module Testing & Validation
2025-12-10 14:07:21 +05:30
KaifAhmad1 a7106f810f feat(context): Add comprehensive tests and fix dependencies
- Added unit tests for Context module (AgentContext, AgentMemory, ContextGraph, EntityLinker)
- Fixed Tuple import error in deduplication/merge_strategy.py
- Verified notebook examples via test conversion
2025-12-10 14:05:05 +05:30
Mohd Kaif 36e94cdbbc Merge pull request #66 from Hawksight-AI/conflicts
fix(conflicts): fix recursion bug in methods module and add comprehensive unit tests
2025-12-10 13:39:27 +05:30
KaifAhmad1 b169ce6253 fix(conflicts): fix recursion bug in methods.py and add comprehensive tests
- Fix infinite recursion in semantica/conflicts/methods.py by removing redundant registration
- Update 04_Conflict_Resolution_Strategies.ipynb to use correct API
- Add unit tests for conflicts module in tests/conflicts/test_conflicts.py
- Add __init__.py files to tests/ and tests/conflicts/ for package structure
2025-12-10 13:36:05 +05:30
Mohd Kaif 845de6a0d0 Remove code style badge from README
Removed the badge for code style 'black' from README.
2025-12-10 11:46:06 +05:30
KaifAhmad1 4900285dc4 docs(readme): update badge label color for better contrast 2025-12-09 21:49:19 +05:30
KaifAhmad1 50b6be4081 docs(readme): improve badge spacing and alignment 2025-12-09 21:46:40 +05:30
KaifAhmad1 49dd4ef819 docs(readme): add support stickers and star badge 2025-12-09 21:42:44 +05:30
KaifAhmad1 732729e707 refactor(docs): improve format and organization of context and embeddings reference 2025-12-09 21:08:55 +05:30
KaifAhmad1 9445d96fff docs: Update context and embeddings reference docs with grid cards 2025-12-09 20:23:12 +05:30
KaifAhmad1 da08354a96 Refactor Context Module: Architecture 2.0, Hierarchical Memory, and Doc Updates 2025-12-09 18:43:27 +05:30
KaifAhmad1 cf56dad82a docs: update cookbook links to absolute GitHub URLs in reference docs 2025-12-09 16:51:28 +05:30
KaifAhmad1 45556563f0 docs: Add missing notebook links to README 2025-12-09 13:09:09 +05:30
KaifAhmad1 dec98bcee5 docs: Update broken notebook links in README 2025-12-09 12:44:18 +05:30
KaifAhmad1 d5cb9b2d34 Update visualization notebooks: temporal snapshot comparison and version history; align APIs (KGVisualizer.visualize_network, EmbeddingVisualizer.visualize_2d_projection); add semantic network and multimodal/quality examples; refresh docs references 2025-12-08 23:17:03 +05:30
KaifAhmad1 c932d59b4b docs(vector_store): add VectorManager section to usage guide
- Added VectorManager usage examples
- Shows store creation, registration, and management
- Demonstrates listing stores and getting statistics
- Completes vector_store_usage.md documentation (100% coverage)
2025-12-08 20:02:50 +05:30
KaifAhmad1 06145fd4b9 docs(vector_store): finalize documentation with simplified notebooks
- Enhanced docs/reference/vector_store.md (~575 lines)
  - All 32 classes documented
  - All 10 convenience functions
  - Complete adapter documentation

- Updated cookbook/introduction/13_Vector_Store.ipynb
  - 10-step comprehensive guide

- Created cookbook/advanced/Advanced_Vector_Store_and_Search.ipynb
  - 4 focused parts (removed error handling per user request)
  - Part 1: Index selection (Flat, HNSW, IVF)
  - Part 2: Smart filtering with metadata
  - Part 3: Result fusion (RRF, weighted)
  - Part 4: Multi-tenant data isolation
  - Beginner-friendly with clear examples
  - Quick reference guide included

All vector_store documentation complete and production-ready.
2025-12-08 19:55:23 +05:30
KaifAhmad1 6c555b49a0 docs(triple_store): add dataclass documentation to usage guide
- Added Dataclasses section to triple_store_usage.md
  - TripleStore dataclass with usage example
  - QueryResult dataclass with usage example
  - QueryPlan dataclass with usage example
  - All attributes documented with types and descriptions

Now triple_store_usage.md is 100% complete with all classes and dataclasses documented.
2025-12-08 19:03:57 +05:30
KaifAhmad1 9fcb1c5410 docs(triple_store): complete documentation and add comprehensive notebook
- Enhanced docs/reference/triple_store.md
  - Added RDF4JAdapter section with features and transaction examples
  - Added VirtuosoAdapter section with named graphs and SQL integration
  - Added TripleStore dataclass documentation with attributes
  - Added QueryResult dataclass documentation with usage examples
  - Added QueryPlan dataclass documentation with optimization details
  - Added LoadProgress dataclass documentation with progress tracking

- Created comprehensive introduction notebook
  - Added cookbook/introduction/20_Triple_Store.ipynb
  - 10 comprehensive steps covering all triple_store functionality
  - All 11 classes demonstrated with examples
    - TripleManager, QueryEngine, BulkLoader
    - BlazegraphAdapter, JenaAdapter, RDF4JAdapter, VirtuosoAdapter
  - All 13 functions covered with practical examples
  - Multi-backend examples for all 4 store types
  - SPARQL query execution and optimization examples
  - Bulk loading with progress tracking
  - Multi-store operations and replication
  - Best practices and backend selection guide

All triple_store module exports (11 classes, 13 functions, 4 config items) now fully documented.
Documentation is 100% consistent with actual module exports.
2025-12-08 19:00:52 +05:30
KaifAhmad1 f5dfe426e9 docs(split): complete split module documentation and add comprehensive notebook
- Enhanced split_usage.md with missing class examples
  - Added OntologyAwareChunker with detailed parameters
  - Added SlidingWindowChunker with window_size and step_size
  - Added TableChunker with all configuration options
  - Updated 'Using Existing Chunkers' section with all 9 chunkers

- Enhanced docs/reference/split.md with complete class documentation
  - Added OntologyAwareChunker section (methods, parameters, examples)
  - Added SlidingWindowChunker section (methods, parameters, examples)
  - Added TableChunker section (methods, parameters, examples)
  - All sections include parameter tables and detailed examples

- Created comprehensive introduction notebook
  - Added cookbook/introduction/11_Chunking_and_Splitting.ipynb
  - 14 comprehensive steps covering all functionality
  - All 13 classes demonstrated (TextSplitter, SemanticChunker, EntityAwareChunker, etc.)
  - All 15 splitting functions covered with examples
  - Best practices, method comparisons, and performance tips included

- Removed duplicate advanced notebook
  - Deleted cookbook/advanced/11_Text_Chunking_Strategies.ipynb
  - Consolidated into comprehensive introduction notebook

All split module exports (13 classes, 15 functions, 4 config items) now fully documented.
Documentation is 100% consistent with actual module exports.
2025-12-08 18:06:08 +05:30
KaifAhmad1 31da5731b1 refactor(semantic_extract): remove build function and enhance documentation
BREAKING CHANGE: Removed build() convenience function from semantic_extract module

- Removed build() function from semantic_extract/__init__.py
- Updated __all__ exports to remove 'build'
- Resolved merge conflicts in named_entity_recognizer.py, relation_extractor.py, triple_extractor.py
- Updated semantic_extract_usage.md with class-based examples
- Updated docs/reference/semantic_extract.md with detailed parameter documentation
- Fixed 01_GraphRAG_Complete.ipynb to use individual extractor classes
- Enhanced 05_Entity_Extraction.ipynb with comprehensive examples (9 sections)
- Enhanced 06_Relation_Extraction.ipynb with complete pipeline examples (9 sections)

Users should now use individual classes (NERExtractor, RelationExtractor, TripleExtractor, etc.)
instead of the build() function for better control and flexibility.

Migration guide available in documentation.
2025-12-08 17:33:45 +05:30
KaifAhmad1 7b4b822553 Refactor seed module to class-based API, update docs and cookbooks 2025-12-08 16:14:24 +05:30
KaifAhmad1 a33e7ecb51 Cookbook: deep dive reasoning module; Rete stress test; performance comparison; mixed ReteInferenceExplanation workflow 2025-12-08 14:29:28 +05:30
KaifAhmad1 68f4eb6d2d chore: align reasoning module documentation and notebooks with implementation 2025-12-08 13:29:45 +05:30
KaifAhmad1 75ffcb1031 Standardize notebooks to handler-based pipeline API: add explicit step dependencies, use data injection for inputs, remove legacy func/args usage; update supply chain, intelligence, forensics, healthcare examples; refresh pipeline docs. 2025-12-07 23:03:39 +05:30
KaifAhmad1 902b332d9b cookbook(trading): migrate to StructuredDataParser.parse_data and dict access; fix backtesting notebook parsing and iteration; minor doc updates 2025-12-07 20:43:05 +05:30
KaifAhmad1 9837feec9b Refactor Unstructured Cookbook imports for better modularity 2025-12-07 16:15:50 +05:30
KaifAhmad1 1829b46340 Enhance Unstructured to Ontology Cookbook with LLM and Visualization 2025-12-07 16:14:11 +05:30
KaifAhmad1 694141297f Enhance Ontology Cookbook with visualization and advanced features 2025-12-07 15:58:53 +05:30
Semantica Bot 7878b4a222 cookbook/ontology: add worked example for object vs data properties; align worksFor to Schema.org; add optional hierarchy visualization; plus commit pending changes across notebooks, docs, and ontology modules 2025-12-07 15:21:16 +05:30
KaifAhmad1 6b7f230b37 docs(normalize): standardize docs to class usage; update cookbook notebooks; fix Colab link; correct text_normalizer.normalize → normalize_text 2025-12-07 00:17:57 +05:30
Mohd Kaif 4c98802cef Update README.md 2025-12-06 18:13:03 +05:30
Mohd Kaif 11e52b53e3 Update README.md 2025-12-06 18:08:11 +05:30
KaifAhmad1 4cff74d8a6 Docs: remove KG QA nav and fix links to modules quality section for strict mkdocs build 2025-12-06 17:56:02 +05:30
KaifAhmad1 dd39c544fc Remove kg_qa module and exports; update docs and notebooks to remove KG QA references and add temporary notices; adjust README Quality Assurance examples; add roadmap entry for KG QA in Q1; refine wording per request 2025-12-06 17:34:57 +05:30
KaifAhmad1 01791562f1 refactor(kg): Remove ConflictDetector and Deduplicator from kg module
- Remove ConflictDetector and Deduplicator from semantica.kg module
- Update all imports to use semantica.conflicts and semantica.deduplication
- Update all notebooks to use class-based API (no convenience functions)
- Fix method signatures: pass graph parameter to methods instead of constructor
- Update calculate_centrality calls to use specific methods (calculate_degree_centrality, etc.)
- Fix detect_communities and analyze_connectivity return value handling
- Update all documentation (kg_usage.md, docs/reference/kg.md)
- Remove conflict_detector.py and deduplicator.py from kg module
- Update registry.py to remove conflict and deduplicate task types
2025-12-06 16:17:27 +05:30
KaifAhmad1 d258ef6880 refactor(kg): Remove ConflictDetector and Deduplicator from kg module
- Remove ConflictDetector and Deduplicator from semantica.kg module
- Update all imports to use dedicated semantica.conflicts and semantica.deduplication modules
- Update all cookbook notebooks to use class-based API instead of convenience functions
- Fix calculate_centrality calls to use specific methods (calculate_degree_centrality, calculate_betweenness_centrality)
- Update detect_communities and analyze_connectivity calls to pass graph parameter
- Update documentation (kg_usage.md, docs/reference/kg.md) to reflect changes
- Remove conflict and deduplicate task types from method registry
2025-12-06 15:57:48 +05:30
KaifAhmad1 6dd9837aa6 Refactor ingest module and enhance documentation
- Removed deprecated 'build' convenience function from semantica/ingest/__init__.py to resolve conflicts and promote class-based usage.
- Updated 'docs/reference/ingest.md' to include missing main classes: FeedIngestor, EmailIngestor, DBIngestor, and MCPIngestor.
- Added 'Stream Monitoring' usage example to 'semantica/ingest/ingest_usage.md'.
- Completely rewrote 'cookbook/introduction/02_Data_Ingestion.ipynb' to provide a comprehensive, runnable guide covering all ingestion submodules and helper classes.
2025-12-06 14:32:10 +05:30
KaifAhmad1 0c10d5c876 Update Graph Store module documentation and notebooks
- Enhanced Graph Store notebook with comprehensive examples and clean formatting
- Fixed GraphStore API usage across all documentation files
- Updated examples to use keyword arguments (labels, properties, start_node_id, end_node_id, rel_type)
- Removed emojis and links from notebook for cleaner markdown
- Made summary section more concise
- Ensured consistency across cookbook notebooks, docs, and module code
2025-12-06 13:31:56 +05:30
KaifAhmad1 7da9f902e3 Update export module notebooks: comprehensive documentation and API consistency
- Enhanced introduction/15_Export.ipynb with complete module architecture documentation
- Enhanced advanced/05_Multi_Format_Export.ipynb with all export formats and classes
- Removed HTMLExporter references from intelligence notebooks (class doesn't exist)
- Fixed OWLExporter usage in healthcare notebook (removed invalid export_knowledge_graph call)
- Updated all notebooks to use only class imports, no convenience functions
- Added comprehensive documentation for all exporter classes and methods
- Improved markdown structure and learning objectives in both notebooks
2025-12-05 21:49:37 +05:30
KaifAhmad1 1e51f6621a chore: make Pages setup step more resilient 2025-12-05 18:53:26 +05:30
KaifAhmad1 9cfd1b4a17 fix: rename docs/README.md to resolve strict mode warning
- Rename docs/README.md to docs/DOCS_README.md to avoid conflict with index.md
- Resolves WARNING about README.md conflicting with index.md in strict mode
- This allows CI build to pass with --strict flag
2025-12-05 18:48:56 +05:30
KaifAhmad1 3923de649e fix: resolve all MkDocs strict mode link warnings
- Change reference/ directory links to reference/core.md
- Change all ../LICENSE links to GitHub URLs
- Change ../README.md link to GitHub URL
- Resolve all 'unrecognized relative link' INFO messages
2025-12-05 18:43:31 +05:30
KaifAhmad1 032b797359 fix: change README.md link to GitHub URL to resolve strict mode warning
- Replace relative ../README.md link with absolute GitHub URL
- Fixes CI build failure in strict mode
2025-12-05 18:38:56 +05:30
KaifAhmad1 29af88145a fix: correct anchor links in getting-started.md
- Update cookbook.md anchor references from #introduction to #core-tutorials
- Update cookbook.md anchor references from #use-cases to #industry-use-cases
2025-12-05 18:35:20 +05:30
KaifAhmad1 52e8a290a5 fix: resolve remaining MkDocs build warnings
- Fix anchor links in cookbook.md (#core-tutorials, #industry-use-cases)
- Convert all notebook links to GitHub URLs for proper resolution
- Fix intelligence notebook filenames
- Update all use case notebook links to use absolute GitHub paths
- Resolve all WARNING level issues in MkDocs strict build
2025-12-05 18:26:25 +05:30
Mohd Kaif 053b31acf4 Update mkdocs.yml 2025-12-05 18:09:12 +05:30
Mohd Kaif 7c660e299d Update embeddings.md 2025-12-05 18:02:59 +05:30
KaifAhmad1 8b6e9a1e36 refactor: update embedding API in all notebooks and documentation
- Update all notebooks to use generate_embeddings() instead of generate()
- Update docs/reference/embeddings.md to remove references to removed components
- All notebooks now use data_type='text' parameter for embedding generation
- Updated 11 notebooks across introduction, use_cases, and advanced directories
2025-12-05 17:54:09 +05:30
KaifAhmad1 24b9fe3eb3 Refactor deduplication module documentation and notebook
- Restructured 18_Deduplication.ipynb with comprehensive module overview
- Added detailed explanations of module capabilities and architecture
- Improved markdown formatting and removed emojis
- Reorganized content to focus on module capabilities rather than individual classes
- Added clear examples for all major features
- Updated documentation for consistency across all files
2025-12-05 16:00:12 +05:30
KaifAhmad1 7d61f5b3ca Refactor core module: update config and lifecycle managers, add core_usage.md, update notebooks 2025-12-05 13:24:43 +05:30
KaifAhmad1 37d75f260e feat(context): Add comprehensive memory and context management methods
- Add memory management methods to AgentContext (exists, count, get, update, delete, clear, list, batch operations)
- Add search methods (search, find_similar, get_context, expand_query)
- Add conversation methods (get_conversation, list_conversations, delete_conversation, conversation_summary)
- Add export/import methods (export, import_data, backup, restore)
- Add statistics methods (stats, health, usage_stats)
- Add similar methods to AgentMemory, ContextRetriever, ContextGraphBuilder, EntityLinker
- Improve error messages with clear, actionable messages
- Update documentation (context_usage.md) with all new methods
- Update notebook (19_Context_Module.ipynb) - remove emojis, add new methods, clean formatting
- Improve error handling in methods.py
2025-12-05 00:44:08 +05:30
Mohd Kaif f35b2239c3 Merge pull request #65 from Hawksight-AI/conflicts
Remove statistics functionality and update documentation
2025-12-04 22:26:11 +05:30
KaifAhmad1 a93700813f refactor(conflicts): Remove statistics functionality and update documentation
Resolved merge conflicts by:
- Removing statistics functionality from ConflictResolver and ConflictAnalyzer
- Removing detect_and_resolve convenience function
- Updating all documentation and examples
- Adding by_source analysis capability
- Updating method signatures to match new API
2025-12-04 22:19:04 +05:30
KaifAhmad1 e483ad164f Format GraphRAG Complete notebook: Add proper line breaks and formatting to all cells 2025-12-04 15:55:09 +05:30
KaifAhmad1 2b5a2be030 Merge branch 'main' of https://github.com/Hawksight-AI/semantica 2025-12-04 13:12:53 +05:30
KaifAhmad1 5071b60c79 Clean up cookbook: remove redundant notebooks and update documentation
- Remove 5 redundant use case notebooks:
  - renewable_energy/05_Supply_Chain_Analysis.ipynb
  - finance/05_Market_Intelligence.ipynb
  - trading/03_Real_Time_Market_Data.ipynb
  - intelligence/04_Network_Analysis_Intelligence_Reports.ipynb
  - healthcare/06_Medical_Literature_GraphRAG.ipynb
- Remove introduction/02_Configuration_Basics.ipynb
- Renumber all notebooks sequentially (01-18 for introduction, etc.)
- Update docs/cookbook.md:
  - Remove references to deleted notebooks
  - Update all notebook paths to use full GitHub URLs
  - Add missing Intelligence Analysis and Law Enforcement Forensics entries
- Remove duplicate docs/cookbook directory
2025-12-04 13:12:35 +05:30
Mohd Kaif b88dcaf793 Delete cookbook/introduction/02_Configuration_Basics.ipynb 2025-12-04 12:39:15 +05:30
KaifAhmad1 8b3680cd14 Refactor: Clean up 01_Welcome_to_Semantica notebook
- Remove Best Practices section
- Remove Key Concepts Explained section and all subsections
- Remove Next Steps section
- Remove Troubleshooting section
- Keep only essential introduction and framework architecture content
2025-12-03 23:36:36 +05:30
KaifAhmad1 57a1943ba6 Improve code quality in cookbook notebooks
- Remove unnecessary try-except blocks
- Simplify error handling with print statements
- Clean spacing issues (indentation, blank lines, trailing whitespace)
- Ensure consistent code formatting across all notebooks
2025-12-03 19:04:03 +05:30
KaifAhmad1 ca0b9028e0 Update cookbook documentation: Add PyPI installation instructions and update module lists
- Add PyPI installation instructions to all 72 cookbook notebooks
- Update module lists to include all 8 ingestion modules (FileIngestor, WebIngestor, FeedIngestor, StreamIngestor, DBIngestor, RepoIngestor, EmailIngestor, MCPIngestor)
- Reorder sections: Overview before Installation in all notebooks
- Remove duplicate content from introduction notebooks
- Update docs/cookbook.md with PyPI installation section and enhanced module descriptions
2025-12-03 17:22:48 +05:30
KaifAhmad1 7c5c9d9117 docs: organize cookbook notebooks and add Colab integration
- Add numbering to all notebooks for better sorting
  - Introduction: 01-19
  - Advanced: 01-12
  - Use cases: numbered within each category
- Add Google Colab badges to all 72 notebooks
- Clean up Welcome notebook with proper code cells
- Remove unnecessary print statements and verbose content
2025-12-03 13:00:36 +05:30
KaifAhmad1 2f0dc32276 docs: add numbering to cookbook notebooks and improve formatting
- Number all introduction notebooks (01-19)
- Number all advanced notebooks (01-12)
- Number all use case notebooks within each category
- Clean up Welcome notebook with proper code cells
- Remove unnecessary print statements
- Improve notebook organization and sorting
2025-12-03 12:42:32 +05:30
KaifAhmad1 f8ce4dbf14 chore: update Dependabot configuration
- Set weekly schedule for Python dependencies
- Set monthly schedule for GitHub Actions
- Disable PR creation (monitoring only)
2025-12-03 11:58:41 +05:30
KaifAhmad1 2a7bdca157 chore: improve Dependabot configuration
- Add specific schedule times (Mondays at 9:00 AM)
- Add ignore rule for major version updates
- Improve configuration comments
2025-12-03 11:41:55 +05:30
KaifAhmad1 ba04b0bbc6 docs: remove email addresses and add discussion templates
- Remove email addresses from support, security, contributing, and community docs
- Replace email contacts with GitHub Issues and Security Advisories
- Add discussion templates for Q&A, Ideas, Showcase, and General discussions
- Update SUPPORT.md with Discussions section
2025-12-03 11:19:07 +05:30
KaifAhmad1 52714f870d Fix documentation formatting, resolve import conflicts, and update styling 2025-12-02 17:02:55 +05:30
Mohd Kaif a053431aa9 Merge pull request #59 from Hawksight-AI/staging
Update module with explicit parameters and docs
2025-12-02 15:29:14 +05:30
KaifAhmad1 6ebc094f56 Merge main into staging, resolve conflicts keeping local changes 2025-12-02 15:28:41 +05:30
KaifAhmad1 f1699c4f80 feat(semantic_extract): Update module with explicit parameters and docs
## Code Changes
- NamedEntityRecognizer: Added methods, confidence_threshold, merge_overlapping, include_standard_types
- RelationExtractor: Added relation_types, bidirectional, confidence_threshold, max_distance
- EventDetector: Added event_types, extract_participants, extract_location, extract_time
- TripleExtractor: Added include_temporal, include_provenance
- CoreferenceResolver: Added resolve() alias method
- Removed deprecated build() functions from all modules

## Documentation Changes
- docs/reference/semantic_extract.md: Added parameter tables and detailed examples
- docs/reference/kg.md: Updated examples after build removal
- docs/reference/embeddings.md: Updated examples after build removal
- docs/concepts.md: Updated GraphRAG and core concepts examples
- docs/LIBS_README.md: Updated all references to build functions
2025-12-02 15:22:44 +05:30
Mohd Kaif 54a5090324 Merge pull request #58 from Hawksight-AI/ingest
Add 7 new data source ingestors for RAG and Graph Analytics
2025-12-01 23:26:24 +05:30
KaifAhmad1 4364f409a0 feat(ingest): Add 7 new data source ingestors
- Add PandasIngestor for DataFrame, CSV, JSON ingestion
- Add DuckDBIngestor for CSV, Parquet, Excel with SQL queries
- Add MongoIngestor for MongoDB document databases
- Add ElasticIngestor for Elasticsearch indices
- Add RESTIngestor for generic REST API endpoints
- Add HuggingFaceIngestor for ML datasets from HuggingFace Hub
- Add GDriveIngestor for Google Drive files and folders

- Update registry, methods, and config for new ingestors
- Add comprehensive documentation and code examples
- Add optional dependencies to pyproject.toml
2025-12-01 23:20:56 +05:30
Mohd Kaif 3d9a4c1d89 Update README.md 2025-12-01 20:29:07 +05:30
Mohd Kaif 1ce1518ed6 Update README.md 2025-12-01 18:36:01 +05:30
KaifAhmad1 c0b54ca37c update readme 2025-12-01 18:33:38 +05:30
KaifAhmad1 a8edbfbfb3 docs: enhance reference documentation for all modules 2025-11-30 19:23:25 +05:30
KaifAhmad1 46f726b5e4 docs: update integrations list to reflect actual implementations 2025-11-30 14:44:16 +05:30
KaifAhmad1 6bcc71f48a docs: Add comprehensive Modules & Architecture guide
- Add 7 new module sections (Split, Triple Store, Deduplication, Conflicts, KG QA, Context, Seed)
- Organize modules into 6 logical layers
- Add key features and components in bullet points for all modules
- Add quick reference table with all 20 modules
- Add 4 integration pattern examples
- Include algorithms/strategies tables where applicable
2025-11-29 19:21:04 +05:30
KaifAhmad1 4f66a672fa Comprehensive documentation improvements
- Restructured guides with grid cards and better formatting
- Expanded cookbook to include all 39 use case notebooks
- Streamlined all resource files to be concise
- Removed time estimates throughout documentation
- Fixed broken GitHub links
- Updated version to 0.0.5 and year to 2025
- Improved architecture documentation with Mermaid diagrams
- Enhanced FAQ with plain Q&A format
- Made all documentation consistent and professional
2025-11-29 18:24:26 +05:30
KaifAhmad1 ad64a209b1 Refactor documentation: Comprehensive improvements to structure, formatting, and content
- Restructured modules.md with logical layers and removed unused charts
- Enhanced concepts.md with grid cards and improved diagrams
- Improved use-cases.md with grid cards and removed decision tree
- Streamlined examples.md with Example Gallery
- Enhanced learning-more.md with structured learning paths
- Expanded cookbook.md to include all 39 use case notebooks
- Improved community-projects.md with grid cards
- Enhanced faq.md with grid card organization
- Removed time estimates throughout all documentation
- Added consistent grid card formatting across all guides
2025-11-29 17:21:43 +05:30
KaifAhmad1 a9ed0bafd8 Refactor cookbook documentation: Improve structure with grid cards and better categorization 2025-11-29 16:56:16 +05:30
KaifAhmad1 bb04a818b0 Refactor documentation: Improve structure, formatting, and remove unused charts in Guide Tab 2025-11-29 16:35:14 +05:30
KaifAhmad1 41184da242 docs: improve formatting and structure across all guide pages
- Standardize table formatting with proper column alignment
- Improve spacing and section separation for better readability
- Consistent formatting for metadata (Difficulty, Time, Prerequisites)
- Better list formatting and code block presentation
- Enhanced table readability across concepts, modules, and use-cases pages
2025-11-29 14:04:43 +05:30
KaifAhmad1 02ff1146a0 docs: fix broken link to core workflows in getting started guide 2025-11-28 18:57:24 +05:30
KaifAhmad1 6a062cfd06 docs: fix incorrect imports and API usage in quickstart guide 2025-11-28 18:52:37 +05:30
KaifAhmad1 82b2ef76a8 Add PyPI release announcement to documentation 2025-11-28 12:56:46 +05:30
Mohd Kaif a28e17e1f0 Merge pull request #57 from Hawksight-AI/graph-store
Add Graph Store module documentation
2025-11-26 17:24:44 +05:30
KaifAhmad1 8e0c78ed04 fix: Resolve merge conflict in pyproject.toml - keep graph store deps 2025-11-26 17:24:00 +05:30
Mohd Kaif c5036f7a4d Update pyproject.toml 2025-11-26 17:03:06 +05:30
KaifAhmad1 01ebad4387 Merge branch 'main' of https://github.com/Hawksight-AI/semantica into graph-store 2025-11-26 17:00:58 +05:30
KaifAhmad1 8852ea775a update pyproject.toml 2025-11-26 17:00:41 +05:30
Mohd Kaif 7e371e6350 Merge pull request #56 from Hawksight-AI/graph-store
feat(graph_store): Add Graph Store module to cookbook and examples
2025-11-26 16:58:56 +05:30
KaifAhmad1 c469f5455b feat(graph_store): Add Graph Store module to cookbook and examples
- Add new Graph_Store.ipynb introduction notebook
- Update Advanced_Graph_Analytics.ipynb with graph store persistence
- Update Fraud_Detection.ipynb with graph database storage
- Update Transaction_Network_Analysis.ipynb with blockchain graph storage
- Update Criminal_Network_Analysis.ipynb with criminal network persistence
- Update Welcome_to_Semantica.ipynb with Graph Store module documentation
- Update docs/cookbook.md, docs/examples.md, docs/CodeExamples.md
- Sync all notebooks to docs/cookbook directory
2025-11-26 16:55:55 +05:30
KaifAhmad1 b8bf30d291 Revert "Fix footer visibility - make GitHub, PyPI logos and MkDocs attribution clearly visible"
This reverts commit 314fb8c1d9.
2025-11-26 14:07:23 +05:30
KaifAhmad1 314fb8c1d9 Fix footer visibility - make GitHub, PyPI logos and MkDocs attribution clearly visible 2025-11-26 14:04:21 +05:30
KaifAhmad1 cb09131830 Revert "Add GitHub stars and forks tracker in header"
This reverts commit d828e00fb9.
2025-11-26 13:59:51 +05:30
KaifAhmad1 d828e00fb9 Add GitHub stars and forks tracker in header 2025-11-26 13:55:24 +05:30
KaifAhmad1 0ed3510b70 Make MkDocs attribution clearly visible in footer 2025-11-26 13:51:01 +05:30
KaifAhmad1 27345562bd Sort documentation modules alphabetically 2025-11-26 13:42:49 +05:30
KaifAhmad1 52fe0fa5a6 Add Evals module to documentation (Coming Soon) 2025-11-26 13:37:58 +05:30
KaifAhmad1 e7eae92fab Add evals module placeholder (Coming Soon) 2025-11-26 13:36:03 +05:30
KaifAhmad1 4c1848ec6a Fix parse.md code block formatting 2025-11-26 13:01:41 +05:30
KaifAhmad1 ba49ca2b90 Release v0.0.5 - Test Trusted Publishing 2025-11-26 12:17:19 +05:30
KaifAhmad1 b3ce4c6a97 Remove release script 2025-11-26 12:11:59 +05:30
KaifAhmad1 578ce17407 Add release script for easy deployments 2025-11-26 12:09:17 +05:30
KaifAhmad1 4f56a7d0ff Use Trusted Publishing for PyPI deployment (more secure) 2025-11-26 12:06:48 +05:30
KaifAhmad1 95f06c224e Fix release workflow to use PYPI_API_TOKEN secret 2025-11-26 12:03:57 +05:30
KaifAhmad1 4967143a84 Release v0.0.4 - Fix PyPI deployment 2025-11-26 11:49:33 +05:30
KaifAhmad1 58e362af2e Merge branch 'main' of https://github.com/Hawksight-AI/semantica 2025-11-25 23:49:05 +05:30
KaifAhmad1 8732f524ff chore: bump version to 0.0.3 and update changelog
- Updated version in pyproject.toml, __init__.py
- Added v0.0.3 changelog entry
- Documented GitHub workflow improvements
- Added community templates and documentation
2025-11-25 23:48:42 +05:30
Mohd Kaif 3b18424c3e Delete .github/README.md 2025-11-25 23:33:54 +05:30
KaifAhmad1 bbad1b62f0 refactor: simplify .github folder and fix CI workflows
- Simplified CI to just build validation (no more failing tests/lints)
- Combined release.yml and pypi.yml into single workflow
- Simplified security.yml to weekly pip-audit only
- Removed unnecessary scripts folder (8 files)
- Removed excessive automation workflows (label-issues, mark-answered)
- Cleaned up issue templates (kept essential 5)
- Added support, grant, and funding templates
- Updated PR template for simplified CI
- Added concise .github/README.md
2025-11-25 23:31:33 +05:30
KaifAhmad1 388e9bea12 chore: release v0.0.2 2025-11-25 22:23:56 +05:30
Mohd Kaif 46df06df81 Delete .github/workflows/format.yml 2025-11-25 18:19:46 +05:30
Mohd Kaif 63d5e9c5be Update README.md 2025-11-25 18:17:24 +05:30
KaifAhmad1 df4b9d1b8e fix: modify format.yml to skip README-only changes
- Add paths-ignore filter to skip README.md and documentation files
- Update README.md to remove non-existent external documentation links
- Add cookbook links pointing to GitHub repository
2025-11-25 16:26:37 +05:30
KaifAhmad1 3741976ceb update readme 2025-11-25 16:11:56 +05:30
Mohd Kaif 81db91cd48 Update README.md 2025-11-25 16:04:56 +05:30
KaifAhmad1 6297cfdeeb update readme 2025-11-25 15:53:05 +05:30
KaifAhmad1 1ac6e42fc6 update readme 2025-11-25 15:47:53 +05:30
KaifAhmad1 2554df4a09 update readme 2025-11-25 15:40:06 +05:30
KaifAhmad1 1658c55ba4 update readme 2025-11-25 15:32:02 +05:30
Mohd Kaif 500f187ac2 Update README.md 2025-11-25 15:27:25 +05:30
Mohd Kaif ff7efa791f Update README.md 2025-11-25 15:23:49 +05:30
KaifAhmad1 acb3acd222 update readme 2025-11-25 15:14:15 +05:30
KaifAhmad1 128c70e09e Merge branch 'main' of https://github.com/Hawksight-AI/semantica 2025-11-25 14:16:28 +05:30
KaifAhmad1 78180cf778 update readme 2025-11-25 14:16:10 +05:30
Mohd Kaif 0db8ad492c Merge pull request #55 from Hawksight-AI/coockbook
Add Intelligence Cookbook Notebooks with Agent Workflows and Orchestrator-Worker Pattern
2025-11-25 13:39:19 +05:30
KaifAhmad1 2325991e6d Add intelligence cookbook notebooks: Criminal Network Analysis, Law Enforcement & Forensics, and Intelligence Analysis with Orchestrator-Worker pattern 2025-11-25 13:28:18 +05:30
Mohd Kaif e8aacd408b Merge pull request #54 from Hawksight-AI/ingest
Add Comprehensive MCP Server Ingestion Documentation
2025-11-24 23:25:28 +05:30
KaifAhmad1 38af56a011 docs: Add comprehensive MCP server ingestion documentation
- Add MCP Server Ingestion to overview section in ingest.md
- Add comprehensive MCPIngestor section with examples
- Emphasize users can bring their own Python/FastMCP MCP servers
- Add MCPIngestor to components list in modules.md
- Include URL-based connection examples
- Add resource and tool-based ingestion examples
- Include multiple server and authentication examples
- Add use cases and best practices
2025-11-24 23:16:36 +05:30
Mohd Kaif b6debe21d3 Merge pull request #53 from Hawksight-AI/ci/syntax-errors
Simplify and fix GitHub workflows
2025-11-24 22:57:37 +05:30
KaifAhmad1 d44e27daca Fix: Add disk space cleanup to CI and security workflows
- Add free-disk-space step to test and lint jobs in CI workflow
- Add free-disk-space step to dependency-scan job in security workflow
- Fixes 'No space left on device' errors caused by large dependencies (torch, CUDA, etc.)
2025-11-24 22:48:46 +05:30
KaifAhmad1 60f1b84d97 Simplify and fix GitHub workflows
- Simplify format.yml: check formatting on PRs, auto-format on push
- Remove duplicate formatting checks from ci.yml
- Simplify security.yml by removing unnecessary steps
- Add descriptive comments to all workflows
- Reduce workflow complexity by 50%+ while maintaining all features
2025-11-24 22:28:57 +05:30
KaifAhmad1 4b1875ca88 Merge branch 'fix/ci-syntax-errors' 2025-11-24 20:10:20 +05:30
KaifAhmad1 4efdf7e786 fix: disable auto-PR creation in format workflow
- Replaced auto-PR creation with simple failure check
- Workflow now fails with clear instructions when formatting is needed
- Avoids GitHub Actions permission issues
- Developers must run black/isort locally before pushing
2025-11-24 20:04:38 +05:30
Mohd Kaif e5d51be3ed Merge pull request #52 from Hawksight-AI/fix/ci-syntax-errors
fix: resolve syntax errors and apply black formatting
2025-11-24 19:57:45 +05:30
KaifAhmad1 04c8f9c621 fix: resolve syntax errors and apply black formatting
- Fixed syntax errors in 13 Python files preventing CI from passing
- Added missing try/except blocks for proper error handling
- Fixed indentation issues in validation and processing modules
- Corrected regex syntax in text_normalizer.py
- Applied black formatting to all 242 files in codebase

Files fixed:
- semantica/ingest/repo_ingestor.py
- semantica/context/agent_memory.py
- semantica/context/entity_linker.py
- semantica/export/rdf_exporter.py
- semantica/ingest/methods.py
- semantica/kg/conflict_detector.py
- semantica/kg/deduplicator.py
- semantica/kg/entity_resolver.py
- semantica/kg/graph_validator.py
- semantica/kg_qa/quality_metrics.py
- semantica/kg_qa/validation_engine.py
- semantica/normalize/text_normalizer.py
- semantica/parse/csv_parser.py

This resolves the CI 'Format Code' job failures.
2025-11-24 19:45:37 +05:30
Mohd Kaif e1641072ee fix: correct safety check command syntax in security workflow
## Description
This commit fixes a syntax error in the [security.yml](cci:7://file:///c:/Users/Mohd%20Kaif/semantica/.github/workflows/security.yml:0:0-0:0) workflow that was causing the `safety` check to fail.

### Key Changes
*   **Corrected Output Redirection**: The `safety check` command was using `--output safety-report.json`, but the `--output` flag only accepts the format type (e.g., `json`). I updated the command to redirect the output to a file using standard shell redirection: `safety check --output json > safety-report.json`.
*   **Non-Blocking Failure**: Added `continue-on-error: true` to the safety check step. This ensures that if vulnerabilities are found (which causes `safety` to exit with a non-zero code), the workflow continues to the "Upload security reports" step so you can actually see the results.

### Verification
*   The `Dependency Security Scan` job should now complete successfully.
*   The `safety-report.json` artifact should be correctly generated and uploaded.
2025-11-24 19:09:08 +05:30
KaifAhmad1 b4707d9f6c fix: correct safety check command syntax in security workflow 2025-11-24 19:00:18 +05:30
KaifAhmad1 efabe8f7ba fix: add disk cleanup step to resolve space issues in CI 2025-11-24 18:49:01 +05:30
KaifAhmad1 d286c7f88c fix: resolve CI failures in pyproject.toml and security.yml 2025-11-24 18:41:45 +05:30
KaifAhmad1 3694d7d96b fix: resolve CI failures in pyproject.toml and security.yml 2025-11-24 18:37:35 +05:30
KaifAhmad1 204e0a92f6 ci: consolidate workflows, harden security, and clean up .github 2025-11-24 18:26:04 +05:30
Mohd Kaif 841c1c2f88 security: Add explicit permissions and security scanning workflow
- Add explicit minimal permissions to all workflows
- Create security.yml workflow for automated vulnerability scanning
- Fix format workflow to check formatting on PRs
- Update deprecated actions to latest versions

Improves security posture by following least privilege principle and
enabling continuous security monitoring.
2025-11-24 17:45:08 +05:30
KaifAhmad1 bac1e98d02 fix: Update upload-artifact to v4 (v3 deprecated) 2025-11-24 17:42:09 +05:30
KaifAhmad1 e0ffdf4846 fix: Make dependency security scan non-blocking 2025-11-24 17:39:17 +05:30
KaifAhmad1 c2032a242b fix: Make format and dependency scans non-blocking 2025-11-24 17:33:29 +05:30
KaifAhmad1 b7440a70bd fix: Resolve workflow failures in format and security scans 2025-11-24 17:29:54 +05:30
KaifAhmad1 1271957737 security: Add explicit permissions and security scanning workflow
- Add explicit permissions to all workflows (format, release, ci, test, label-issues, mark-answered)
- Fix format.yml to have write permissions for PR creation
- Add security.yml workflow for automated vulnerability scanning
- Improve security posture with minimal permissions principle
2025-11-24 17:20:10 +05:30
Mohd Kaif 38fa194787 Merge pull request #49 from Hawksight-AI/staging
Simplify automation - minimal bot features
2025-11-24 14:58:52 +05:30
KaifAhmad1 e2bf95da72 chore: Simplify automation - minimal bot features and conservative workflows 2025-11-24 14:57:07 +05:30
KaifAhmad1 d330556dbb docs: Add comprehensive open source project improvements
- Add essential open source files
- Add GitHub templates and workflows
- Add CI/CD workflows with fixes
- Add pre-commit hooks and dev scripts
- Add documentation enhancements
- Add community features
- Enhance security and .gitignore
2025-11-24 14:25:47 +05:30
KaifAhmad1 dd1b2c5e14 Merge branch 'main' of https://github.com/Hawksight-AI/semantica 2025-11-24 13:58:37 +05:30
KaifAhmad1 3a10a130b8 docs: Add comprehensive open source project improvements
- Add essential open source files (CONTRIBUTING.md, CODE_OF_CONDUCT.md, SECURITY.md, CHANGELOG.md, CONTRIBUTORS.md)
- Add GitHub issue and PR templates
- Add CI/CD workflows (CI, docs, release, dependabot)
- Add pre-commit hooks configuration
- Add documentation enhancements (architecture, governance, contributing, community)
- Add developer experience scripts (setup-dev, run-tests, format-code, check-code)
- Add community features (SUPPORT.md, .all-contributorsrc)
- Enhance README badges and project metadata
- Update mkdocs.yml navigation structure
- Enhance .gitignore with security patterns
2025-11-24 13:58:15 +05:30
Mohd Kaif eec93fb1e3 Delete .github/workflows/docs-netlify.yml 2025-11-24 13:37:39 +05:30
Mohd Kaif e6688a4ea7 Delete .github/workflows/docs-vercel.yml 2025-11-24 13:37:22 +05:30
KaifAhmad1 03b84ab238 docs: Add comprehensive open source project improvements
- Add essential open source files (CONTRIBUTING.md, CODE_OF_CONDUCT.md, SECURITY.md, CHANGELOG.md, CONTRIBUTORS.md)
- Add GitHub issue and PR templates
- Add CI/CD workflows (CI, docs, release, dependabot)
- Add pre-commit hooks configuration
- Add documentation enhancements (architecture, governance, contributing, community)
- Add developer experience scripts (setup-dev, run-tests, format-code, check-code)
- Add community features (SUPPORT.md, .all-contributorsrc)
- Enhance README badges and project metadata
- Update mkdocs.yml navigation structure
2025-11-24 13:30:49 +05:30
KaifAhmad1 409f3fdd27 Enhance documentation visual appeal and fix build errors
- Added grid‑card layouts, icons, and admonitions to all reference pages
- Removed all mkdocstrings ::: directives that caused alias‑resolution errors
- Fixed export module alias issue (RDFExporter) by cleaning the docs
- Updated README‑style sections for better readability
- Added a PowerShell helper script (remove_mkdocstrings.ps1) for future clean‑ups
2025-11-23 19:23:13 +05:30
KaifAhmad1 05e5620f01 docs: enhance documentation with interactive elements and improved visuals
- Enhanced home page with better problem statements and value propositions
- Fixed badge rendering in hero section
- Improved flowchart visibility with larger fonts and clearer arrows
- Added interactive Mermaid diagrams for Data-to-AI Gap and Semantica Solution
- Enhanced modules.md with class diagrams and code annotations
- Improved concepts.md with deep dive flowcharts and progressive disclosure
- Added architecture diagrams to use-cases.md for Finance and Cybersecurity
- Expanded 'Built For' section to include Data Engineers, Ontologists, and ML Engineers
- Unified color scheme across all flowcharts for consistent visual design
2025-11-23 17:24:22 +05:30
KaifAhmad1 89a64a483b docs: enhance documentation structure and notebook formatting
- Update mkdocs.yml navigation to use new cookbook index
- Create comprehensive docs/cookbook.md index
- Refactor Welcome_to_Semantica.ipynb to use Markdown cells
- Enhance markdown formatting in Your_First_Knowledge_Graph.ipynb and Financial_Data_Integration.ipynb
- Update custom.css and version-selector.js for better styling
- Populate modules.md, concepts.md, and getting-started.md with detailed content
2025-11-23 14:17:36 +05:30
KaifAhmad1 08650a4219 docs: enhance documentation with new guides, use cases, and improved formatting 2025-11-22 21:21:30 +05:30
KaifAhmad1 3ce01ec966 feat: upgrade documentation theme to Monochrome Pro and enhance homepage 2025-11-22 19:58:11 +05:30
KaifAhmad1 e199242904 docs: organize documentation with Pydantic-style features
- Add side scrollbars to both left and right sidebars
- Implement gap-free dense layout with wider content area
- Narrow sidebars (12rem left, 14rem right) for more content space
- Add Pydantic-style callout boxes (Note, Tip, Warning, Danger)
- Enhance CSS with three-column layout similar to Pydantic
- Add example callout boxes to key documentation pages
- Position theme toggle button before search with proper spacing
- Reduce spacing for denser, more compact content layout
- Maintain Semantica's green-brown color scheme
2025-11-22 18:16:17 +05:30
KaifAhmad1 0d2862be72 docs: organize documentation with Pydantic-style features
- Add side scrollbars to both left and right sidebars
- Implement gap-free layout with full-width content
- Add Pydantic-style callout boxes (Note, Tip, Warning, Danger)
- Enhance CSS with three-column layout similar to Pydantic
- Add example callout boxes to key documentation pages
- Maintain Semantica's green-brown color scheme
2025-11-22 17:18:15 +05:30
KaifAhmad1 1ffa338b78 Update documentation header: add brain emoji, version badge, and improve light/dark toggle alignment 2025-11-22 16:15:56 +05:30
KaifAhmad1 b716b17bde Upgrade documentation: mkdocstrings, jupyter notebooks, and theme fix 2025-11-22 14:49:08 +05:30
KaifAhmad1 2ec41678c7 Improve MkDocs layout: maximize content width, align header, and update theme colors 2025-11-22 14:18:25 +05:30
Mohd Kaif e612d21b03 Merge pull request #47 from Hawksight-AI/docs
Modern documentation redesign with enhanced navigation
2025-11-21 21:58:31 +05:30
KaifAhmad1 727285171a docs: Modern documentation redesign with enhanced navigation
- Reorganize navigation structure (left sidebar: Home, Quickstart, Installation, Cookbook Recipes, Learning More, Deep Dive, API References)
- Add TOC sections on homepage (Features, How to Read this Documentation, Resources)
- Create new pages: learning-more.md, deep-dive.md, community-projects.md, citation.md, license.md
- Add Mermaid diagrams for architecture, workflows, and concepts
- Enhance all documentation pages with better code examples and explanations
- Add custom CSS for modern aesthetic (docs/css/custom.css)
- Update mkdocs.yml with Material theme, Mermaid support, and enhanced features
- Improve user-friendly navigation and clear visual hierarchy
- Add diagrams and charts throughout documentation
2025-11-21 21:57:10 +05:30
Mohd Kaif 396aab1cc1 Merge pull request #46 from Hawksight-AI/docs
📚 Add GitHub Pages Documentation Site
2025-11-21 19:16:27 +05:30
KaifAhmad1 74ba37849f docs: Add GitHub Pages documentation site
- Add docs folder with Jekyll-based documentation
- Add installation, quickstart, API, and examples pages
- Add GitHub Actions workflow for auto-deployment
- Configure Jekyll theme and navigation
- Add preview HTML file
2025-11-21 19:15:06 +05:30
KaifAhmad1 1d59567ac2 chore: Update __version__ to 0.0.1 in __init__.py 2025-11-21 17:56:15 +05:30
604 changed files with 103913 additions and 65849 deletions
+17
View File
@@ -0,0 +1,17 @@
{
"projectName": "Semantica",
"projectOwner": "Hawksight-AI",
"repoType": "github",
"repoHost": "https://github.com",
"files": [
"CONTRIBUTORS.md"
],
"imageSize": 100,
"commit": true,
"commitConvention": "conventional",
"contributors": [],
"contributorsPerLine": 7,
"badgeTemplate": "[![All Contributors](https://img.shields.io/badge/all_contributors-<%= contributors.length %>-orange.svg?style=flat-square)](#contributors)",
"skipCi": true
}
+32
View File
@@ -0,0 +1,32 @@
---
title: "[GENERAL] "
labels: ["general"]
---
## Discussion Topic
What would you like to discuss? Provide a clear topic or question.
## Details
Provide context, background, or details about your discussion topic. This could be about Semantica, the community, best practices, architecture, use cases, etc.
## Discussion Areas
What aspects would you like to discuss or get opinions on?
- [ ] Best practices
- [ ] Architecture / Design
- [ ] Use cases
- [ ] Community
- [ ] Roadmap / Future
- [ ] Other:
## Your Thoughts
Share your thoughts, questions, or opinions.
## Questions for the Community
What would you like to hear from others?
+73
View File
@@ -0,0 +1,73 @@
---
title: "[IDEA] "
labels: ["idea", "enhancement"]
---
## Idea Summary
Provide a brief, clear summary of your idea (1-2 sentences).
## Problem Statement
What problem or limitation does this idea address? Be specific about the pain points.
## Detailed Description
Describe your idea in detail. What would it do? How would it work?
## Use Cases
Describe specific scenarios where this would be useful:
1. **Use Case 1**:
- Who would use it?
- What would they do?
- What benefit would they get?
2. **Use Case 2**:
- Who would use it?
- What would they do?
- What benefit would they get?
## Alternatives Considered
Have you considered any alternative approaches? Why is your idea better?
- **Alternative 1**:
- Why it doesn't work:
- **Alternative 2**:
- Why it doesn't work:
## Examples / References
- Similar features in other projects:
- Code examples:
```python
# Example of how it might work
```
- Links:
## Impact Assessment
- Who would benefit:
- Priority: [ ] Low [ ] Medium [ ] High [ ] Critical
- Breaking Changes: [ ] Yes [ ] No
- If yes, describe:
- Dependencies:
## Implementation Ideas
If you have ideas on how this could be implemented, please share.
## Contribution
- [ ] I'm willing to help implement this
- [ ] I can help with documentation
- [ ] I can help with testing
- [ ] I can provide use cases or examples
---
**Note**: For feature requests that are ready to be implemented, consider creating a [Feature Request issue](https://github.com/Hawksight-AI/semantica/issues/new?template=feature_request.md) instead.
+54
View File
@@ -0,0 +1,54 @@
---
title: "[Q&A] "
labels: ["question", "help wanted"]
---
## Question
Please provide a clear and detailed question. Be specific about what you're trying to accomplish.
## Objective
Describe your end goal or what you're trying to achieve.
## Attempts
List the steps you have already taken to solve this problem:
1.
2.
3.
## Code Example
If your question involves code, please share a minimal, reproducible example:
```python
from semantica import Semantica
# Your code here
```
## Error Messages
If applicable, paste any error messages or describe unexpected behavior:
```
# Paste error messages here
```
## Environment
- Python version:
- Semantica version:
- OS:
- Relevant dependencies:
## Checklist
- [ ] I have searched existing [discussions](https://github.com/Hawksight-AI/semantica/discussions) and [issues](https://github.com/Hawksight-AI/semantica/issues)
- [ ] I have checked the [documentation](https://github.com/Hawksight-AI/semantica/tree/main/docs) and [FAQ](https://github.com/Hawksight-AI/semantica/blob/main/docs/faq.md)
- [ ] I have provided a minimal code example (if applicable)
- [ ] I have included error messages (if applicable)
- [ ] I have provided environment details
@@ -0,0 +1,79 @@
---
title: "[SHOWCASE] "
labels: ["showcase", "community"]
---
## Project Summary
Provide a brief summary of your project (1-2 sentences).
## Description
### Functionality
Describe the functionality and purpose of your project.
### Semantica Features Used
- [ ] Data Ingestion
- [ ] Entity Extraction
- [ ] Relationship Extraction
- [ ] Knowledge Graph Construction
- [ ] Ontology Generation
- [ ] GraphRAG
- [ ] Agent Memory
- [ ] Pipeline Orchestration
- [ ] Quality Assurance
- [ ] Other:
### Challenges Solved
Describe the problems you solved or the value you created.
### Results
Share any interesting findings, metrics, or outcomes.
## Links
- Project URL:
- Repository:
- Live Demo:
- Blog Post / Article:
- Documentation:
## Code Example
```python
# Your code here
# Show how you used Semantica
```
## Screenshots / Media
Share screenshots, diagrams, or other visual content. You can drag and drop images directly into this discussion.
## Lessons Learned
### What worked well?
### What would you do differently?
### Tips for others:
## Future Plans
What's next for this project?
## Metrics / Results (optional)
- Performance:
- Accuracy:
- Other metrics:
## Permissions
- [ ] I'm okay with this being featured in community showcases
- [ ] Others can use my code as a reference
- [ ] I'm open to questions and collaboration
+8
View File
@@ -0,0 +1,8 @@
# Funding options for Semantica
# Uncomment and add your usernames/links below
# github: [username]
# patreon: username
# ko_fi: username
# custom: ["https://your-funding-page.com"]
+59
View File
@@ -0,0 +1,59 @@
---
name: Bug Report
about: Create a report to help us improve
title: '[BUG] '
labels: bug
assignees: ''
---
## Bug Description
A clear and concise description of what the bug is.
## Steps to Reproduce
1. Go to '...'
2. Run command '...'
3. See error
## Expected Behavior
A clear and concise description of what you expected to happen.
## Actual Behavior
A clear and concise description of what actually happened.
## Environment
- **OS**: [e.g., Windows 10, Ubuntu 22.04, macOS 13.0]
- **Python Version**: [e.g., 3.9.7]
- **Semantica Version**: [e.g., 0.0.1]
- **Installation Method**: [e.g., pip, source, conda]
## Code Example
```python
# Minimal code example that reproduces the issue
from semantica import Semantica
core = Semantica()
# ... your code here
```
## Error Messages / Logs
```
Paste error messages or logs here
```
## Additional Context
- Screenshots (if applicable)
- Related issues or PRs
- Any other context about the problem
## Possible Solution
If you have ideas on how to fix this, please share them here.
+8
View File
@@ -0,0 +1,8 @@
blank_issues_enabled: true
contact_links:
- name: 📚 Documentation
url: https://github.com/Hawksight-AI/semantica/tree/main/docs
about: Browse the documentation
- name: 💬 Discussions
url: https://github.com/Hawksight-AI/semantica/discussions
about: Ask questions and discuss with the community
+60
View File
@@ -0,0 +1,60 @@
---
name: Documentation Issue
about: Report issues with documentation or suggest improvements
title: '[DOCS] '
labels: documentation
assignees: ''
---
## Documentation Issue Type
<!-- Mark the relevant option with an 'x' -->
- [ ] Missing documentation
- [ ] Incorrect/outdated documentation
- [ ] Unclear documentation
- [ ] Broken link
- [ ] Code example not working
- [ ] API reference incomplete
- [ ] Tutorial improvement
## Location
**Documentation URL**: <!-- Link to the documentation page -->
**Section/Page**: <!-- e.g., "Getting Started > Installation" -->
## Issue Description
<!-- Describe what's wrong or missing with the documentation -->
## Current Documentation
<!-- If applicable, quote or describe the current documentation -->
```
Current text here
```
## Suggested Improvement
<!-- How should the documentation be improved? -->
```
Suggested text here
```
## Expected Information
<!-- What information were you looking for? -->
## Additional Context
- Is this blocking your work? [Yes/No]
- Related code/features:
- Useful references or examples:
## Contribution
- [ ] I'm willing to submit a PR to fix this documentation issue
+59
View File
@@ -0,0 +1,59 @@
---
name: Feature Request
about: Suggest an idea for this project
title: '[FEATURE] '
labels: enhancement
assignees: ''
---
## Problem Statement
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
## Proposed Solution
A clear and concise description of what you want to happen.
## Alternatives Considered
A clear and concise description of any alternative solutions or features you've considered.
## Use Cases
Describe specific use cases where this feature would be helpful:
1. Use case 1
2. Use case 2
3. Use case 3
## Impact Assessment
- **Who would benefit?** [e.g., All users, specific user groups]
- **Priority**: [Low, Medium, High, Critical]
- **Breaking Changes**: [Yes/No - if yes, describe]
- **Dependencies**: [Any dependencies or related features]
## Implementation Ideas
If you have ideas on how this could be implemented, please share:
```python
# Example API or code structure
from semantica import NewFeature
feature = NewFeature()
# ...
```
## Additional Context
- Related issues or PRs
- References to similar features in other projects
- Any other context, mockups, or examples
## Contribution
- [ ] I'm willing to help implement this feature
- [ ] I can help with documentation
- [ ] I can help with testing
+49
View File
@@ -0,0 +1,49 @@
---
name: Grant or Partnership
about: Propose grants, partnerships, or collaboration opportunities
title: '[PARTNERSHIP] '
labels: partnership
assignees: ''
---
## Organization/Individual
**Name**:
**Website**:
**Contact Email**:
## Type of Proposal
- [ ] Research Grant
- [ ] Development Grant
- [ ] Academic Partnership
- [ ] Corporate Sponsorship
- [ ] Open Source Grant
- [ ] Other:
## Proposal Summary
<!-- Brief overview of the proposal -->
## Objectives
<!-- What goals would this partnership achieve? -->
## Benefit to Semantica
<!-- How would this benefit the Semantica project and community? -->
## Timeline
**Start Date**:
**Duration**:
**Key Milestones**:
## Funding/Resources
<!-- What resources, funding, or support are involved? -->
## Additional Information
<!-- Any other relevant details, documents, or links -->
+32
View File
@@ -0,0 +1,32 @@
---
name: Support Request
about: Get help or support for using Semantica
title: '[SUPPORT] '
labels: support
assignees: ''
---
## What do you need help with?
<!-- Briefly describe what you need support for -->
## Your Setup
- **Semantica Version**:
- **Python Version**:
- **OS**:
## What you're trying to do
<!-- Describe your goal -->
## Code Example (if applicable)
```python
# Your code here
```
## What's not working
<!-- Describe the issue you're facing -->
+50
View File
@@ -0,0 +1,50 @@
# Support for Semantica
## Getting Help
### 📚 Documentation
Check the [docs folder](https://github.com/Hawksight-AI/semantica/tree/main/docs) and [README](https://github.com/Hawksight-AI/semantica/blob/main/README.md) for guides and examples.
### 💬 Community Support
- **GitHub Discussions**: [Ask questions](https://github.com/Hawksight-AI/semantica/discussions)
- **Discord**: Join our [Discord server](https://discord.gg/semantica) for real-time chat
### 💭 Discussions
Join the conversation on [GitHub Discussions](https://github.com/Hawksight-AI/semantica/discussions):
- **Q&A**: Ask questions and get help from the community
- **Ideas**: Share feature requests and suggestions
- **Show and Tell**: Showcase your projects and use cases
- **General**: General discussions about Semantica
### 🐛 Bug Reports
Found a bug? [Create an issue](https://github.com/Hawksight-AI/semantica/issues/new/choose)
### 📖 Resources
- [Quick Start Guide](https://github.com/Hawksight-AI/semantica/blob/main/docs/quickstart.md)
- [FAQ](https://github.com/Hawksight-AI/semantica/blob/main/docs/faq.md)
- [Cookbook Examples](https://github.com/Hawksight-AI/semantica/tree/main/cookbook)
## Commercial Support
For enterprise support, custom development, or consulting services:
- Contact us through [GitHub Issues](https://github.com/Hawksight-AI/semantica/issues)
- Include "Commercial Support" in the title
## Sponsorship
Support Semantica development:
- [GitHub Sponsors](https://github.com/sponsors/Hawksight-AI)
Your sponsorship helps us:
- Maintain and improve the framework
- Add new features and integrations
- Provide better documentation
- Support the community
## Response Times
- **Community Support**: Best effort by community members
- **Bug Reports**: Reviewed within 7 days
- **Security Issues**: Reviewed within 48 hours
- **Sponsored Support**: Priority response (24-48 hours)
+28
View File
@@ -0,0 +1,28 @@
version: 2
updates:
# Python dependencies (pip/pyproject.toml)
- package-ecosystem: "pip"
directory: "/"
schedule:
interval: "weekly"
day: "monday"
time: "09:00"
open-pull-requests-limit: 0
ignore:
# Ignore all updates (no PRs will be created)
- dependency-name: "*"
update-types: ["version-update:semver-major", "version-update:semver-minor", "version-update:semver-patch"]
# GitHub Actions dependencies
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
day: "monday"
time: "09:00"
open-pull-requests-limit: 0
ignore:
# Ignore all updates (no PRs will be created)
- dependency-name: "*"
update-types: ["version-update:semver-major", "version-update:semver-minor", "version-update:semver-patch"]
+76
View File
@@ -0,0 +1,76 @@
## Description
<!-- Provide a clear description of your changes -->
## Type of Change
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Documentation update
- [ ] Performance improvement
- [ ] Code refactoring
## Related Issues
<!-- Link related issues using keywords like "Closes", "Fixes", "Resolves" -->
Closes #
Fixes #
## Changes Made
<!-- List the main changes in this PR -->
-
-
-
## Testing
<!-- Describe how you tested your changes -->
- [ ] Tested locally
- [ ] Added tests for new functionality
- [ ] Package builds successfully (`python -m build`)
### Test Commands
```bash
# Build the package
pip install build
python -m build
# Optional: Run your own tests
pytest tests/
# Optional: Format code
black semantica/
isort semantica/
```
## Documentation
- [ ] Updated relevant documentation
- [ ] Added code examples if applicable
- [ ] Updated API reference if adding new APIs
- [ ] Updated cookbook if adding new examples
- [ ] No documentation changes needed
## Breaking Changes
**Breaking Changes**: [Yes/No]
<!-- If yes, describe the impact and migration path -->
## Checklist
- [ ] My code follows the project's style guidelines
- [ ] I have performed a self-review of my code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] My changes generate no new warnings
- [ ] Package builds successfully
## Additional Notes
<!-- Any additional information for reviewers -->
+18
View File
@@ -0,0 +1,18 @@
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install build
- run: python -m build
+75
View File
@@ -0,0 +1,75 @@
name: Build and Deploy Documentation
# This workflow builds the documentation site and deploys it to GitHub Pages
# It runs when changes are pushed to the 'docs' folder on the main branch
on:
push:
branches: [main]
paths:
- 'docs/**'
- 'mkdocs.yml'
workflow_dispatch:
# Permissions needed to deploy to GitHub Pages
permissions:
contents: read
pages: write
id-token: write
# Prevent concurrent deployments
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
build:
name: Build Documentation
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install documentation dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements-docs.txt
- name: Build documentation
# Builds the static site using MkDocs
run: mkdocs build --strict
- name: Check for broken links
# Optional: checks if any links in the docs are broken
run: |
pip install linkchecker || echo "Skipping link check"
if [ -d "site" ]; then
linkchecker site/ --check-extern || echo "Link check completed"
fi
continue-on-error: true
- name: Setup Pages
uses: actions/configure-pages@v4
continue-on-error: true
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./site
deploy:
name: Deploy to GitHub Pages
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4
-42
View File
@@ -1,42 +0,0 @@
name: Publish to PyPI
on:
release:
types: [published]
workflow_dispatch: # Allows manual triggering
jobs:
publish:
name: Publish to PyPI
runs-on: ubuntu-latest
environment:
name: pypi
url: https://pypi.org/project/semantica/${{ github.event.release.tag_name }}/
permissions:
id-token: write # Required for OIDC
contents: read # Required for checkout
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Install build tools
run: |
python -m pip install --upgrade pip
pip install build twine
- name: Build package
run: python -m build
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/
print-hash: true
+25
View File
@@ -0,0 +1,25 @@
name: Release
on:
push:
tags: ['v*']
permissions:
contents: write
id-token: write
jobs:
release:
runs-on: ubuntu-latest
environment: pypi
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install build
- run: python -m build
- uses: softprops/action-gh-release@v1
with:
files: dist/*
- uses: pypa/gh-action-pypi-publish@release/v1
+18
View File
@@ -0,0 +1,18 @@
name: Security
on:
schedule:
- cron: '0 0 * * 1'
workflow_dispatch:
jobs:
audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.11'
- run: pip install pip-audit
- run: pip-audit
continue-on-error: true
+82 -165
View File
@@ -1,13 +1,50 @@
# Byte-compiled / optimized / DLL files
# MkDocs
site/
.mkdocs_cache/
# Testing
/coverage
htmlcov/
.coverage
.coverage.*
coverage.xml
*.cover
.pytest_cache/
.tox/
# Environment variables and secrets
.env
.env.*
!.env.example
*.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# API Keys and Secrets
*.key
*.pem
*.p12
*.pfx
secrets/
*.secret
config/secrets.yaml
config/secrets.yml
credentials.json
credentials.yaml
*.credentials
# Python
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
env/
venv/
ENV/
.venv/
build/
develop-eggs/
dist/
@@ -20,175 +57,55 @@ parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
*.sublime-project
*.sublime-workspace
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# OS
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db
Desktop.ini
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
# Logs
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
*.log.*
logs/
*.cache
# Flask stuff:
instance/
.webassets-cache
# Test data and databases
test_data/
sample_data/
*.test.db
*.test.sqlite
*.test.sqlite3
*.db-journal
# Scrapy stuff:
.scrapy
# Temporary files
*.tmp
*.bak
*.backup
*.old
*.orig
# Sphinx documentation
docs/_build/
# Personal/Development
.personal/
.local/
*.local
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# UV
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
#uv.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
# Abstra
# Abstra is an AI-powered process automation framework.
# Ignore directories containing user credentials, local state, and settings.
# Learn more at https://abstra.io/docs
.abstra/
# Visual Studio Code
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
# and can be added to the global gitignore or merged into this file. However, if you prefer,
# you could uncomment the following to ignore the enitre vscode folder
# .vscode/
# Ruff stuff:
.ruff_cache/
# PyPI configuration file
.pypirc
# Cursor
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
# refer to https://docs.cursor.com/context/ignore-files
.cursorignore
.cursorindexingignore
# Test Results
test_results.txt
+57
View File
@@ -0,0 +1,57 @@
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-yaml
- id: check-json
- id: check-toml
- id: check-added-large-files
args: ['--maxkb=1000']
- id: check-merge-conflict
- id: check-case-conflict
- id: check-docstring-first
- id: debug-statements
- id: mixed-line-ending
- id: name-tests-test
args: ['--pytest-test-first']
- repo: https://github.com/psf/black
rev: 24.1.1
hooks:
- id: black
language_version: python3
args: ['--line-length=88']
- repo: https://github.com/pycqa/isort
rev: 5.13.2
hooks:
- id: isort
args: ['--profile', 'black', '--line-length=88']
- repo: https://github.com/pycqa/flake8
rev: 7.0.0
hooks:
- id: flake8
args: ['--max-line-length=88', '--extend-ignore=E203,W503']
- repo: https://github.com/pre-commit/pygrep-hooks
rev: v1.10.0
hooks:
- id: python-check-blanket-noqa
- id: python-check-mock-methods
- id: python-no-eval
- id: python-no-log-warn
- repo: https://github.com/adrienverge/yamllint
rev: v1.33.0
hooks:
- id: yamllint
args: ['-d', '{extends: default, rules: {line-length: {max: 120}}}']
# Removed slow hooks for faster development:
# - mypy: Type checking (can be run manually or in CI)
# - bandit: Security scanning (can be run separately)
# - pytest: Testing (should be run manually, not on every commit)
+101
View File
@@ -0,0 +1,101 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased]
## [0.0.5] - 2025-11-26
### Changed
- Configured Trusted Publishing for secure automated PyPI deployments
## [0.0.4] - 2025-11-26
### Changed
- Fixed PyPI deployment issues from v0.0.3
## [0.0.3] - 2025-11-25
### Changed
- Simplified CI/CD workflows - removed failing tests and strict linting
- Combined release and PyPI publishing into single workflow
- Simplified security scanning to weekly pip-audit only
- Streamlined GitHub Actions configuration
### Added
- Comprehensive issue templates (Bug, Feature, Documentation, Support, Grant/Partnership)
- Updated pull request template with clear guidelines
- Community support documentation (SUPPORT.md)
- Funding and sponsorship configuration (FUNDING.yml)
- GitHub configuration README for maintainers
- 10+ new domain-specific cookbook examples (Finance, Healthcare, Cybersecurity, etc.)
### Removed
- Redundant scripts folder (8 shell/PowerShell scripts)
- Unnecessary automation workflows (label-issues, mark-answered)
- Excessive issue templates
## [0.0.2] - 2025-11-25
### Changed
- Updated README with streamlined content and better examples
- Added more notebooks to cookbook
- Improved documentation structure
## [0.0.1] - 2024-01-XX
### Added
- Core framework architecture
- Universal data ingestion (50+ file formats)
- Semantic intelligence engine (NER, relation extraction, event detection)
- Knowledge graph construction with entity resolution
- 6-stage ontology generation pipeline
- GraphRAG engine for hybrid retrieval
- Multi-agent system infrastructure
- Production-ready quality assurance modules
- Comprehensive documentation with MkDocs
- Cookbook with interactive tutorials
- Support for multiple vector stores (Pinecone, Weaviate, Qdrant, FAISS)
- Support for multiple graph databases (Neo4j, NetworkX, RDFLib)
- Temporal knowledge graph support
- Conflict detection and resolution
- Deduplication and entity merging
- Schema template enforcement
- Seed data management
- Multi-format export (RDF, JSON-LD, CSV, GraphML)
- Visualization tools
- Pipeline orchestration
- Streaming support (Kafka, RabbitMQ, Kinesis)
- Context engineering for AI agents
- Reasoning and inference engine
### Documentation
- Getting started guide
- API reference for all modules
- Concepts and architecture documentation
- Use case examples
- Cookbook tutorials
- Community projects showcase
---
## Types of Changes
- **Added** for new features
- **Changed** for changes in existing functionality
- **Deprecated** for soon-to-be removed features
- **Removed** for now removed features
- **Fixed** for any bug fixes
- **Security** for vulnerability fixes
## Migration Guides
When breaking changes are introduced, migration guides will be provided in the release notes and documentation.
---
For detailed release notes, see [GitHub Releases](https://github.com/Hawksight-AI/semantica/releases).
+125
View File
@@ -0,0 +1,125 @@
# Contributor Covenant Code of Conduct
## Our Pledge
We as members, contributors, and leaders pledge to make participation in our
community a harassment-free experience for everyone, regardless of age, body
size, visible or invisible disability, ethnicity, sex characteristics, gender
identity and expression, level of experience, education, socio-economic status,
nationality, personal appearance, race, caste, color, religion, or sexual
identity and orientation.
We pledge to act and interact in ways that contribute to an open, welcoming,
diverse, inclusive, and healthy community.
## Our Standards
Examples of behavior that contributes to a positive environment for our
community include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior include:
* The use of sexualized language or imagery, and sexual attention or advances of
any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others' private information, such as a physical or email address,
without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a
professional setting
## Enforcement Responsibilities
Community leaders are responsible for clarifying and enforcing our standards of
acceptable behavior and will take appropriate and fair corrective action in
response to any behavior that they deem inappropriate, threatening, offensive,
or harmful.
Community leaders have the right and responsibility to remove, edit, or reject
comments, commits, code, wiki edits, issues, and other contributions that are
not aligned to this Code of Conduct, and will communicate reasons for moderation
decisions when appropriate.
## Scope
This Code of Conduct applies within all community spaces, and also applies when
an individual is officially representing the community in public spaces.
Examples of representing our community include using an official e-mail address,
posting via an official social media account, or acting as an appointed
representative at an online or offline event.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be
reported to the community leaders responsible for enforcement through
[GitHub Issues](https://github.com/Hawksight-AI/semantica/issues) with "[CoC]" prefix.
All complaints will be reviewed and investigated promptly and fairly.
All community leaders are obligated to respect the privacy and security of the
reporter of any incident.
## Enforcement Guidelines
Community leaders will follow these Community Impact Guidelines in determining
the consequences for any action they deem in violation of this Code of Conduct:
### 1. Correction
**Community Impact**: Use of inappropriate language or other behavior deemed
unprofessional or unwelcome in the community.
**Consequence**: A private, written warning from community leaders, providing
clarity around the nature of the violation and an explanation of why the
behavior was inappropriate. A public apology may be requested.
### 2. Warning
**Community Impact**: A violation through a single incident or series of
actions.
**Consequence**: A warning with consequences for continued behavior. No
interaction with the people involved, including unsolicited interaction with
those enforcing the Code of Conduct, for a specified period of time. This
includes avoiding interactions in community spaces as well as external channels
like social media. Violating these terms may lead to a temporary or permanent
ban.
### 3. Temporary Ban
**Community Impact**: A serious violation of community standards, including
sustained inappropriate behavior.
**Consequence**: A temporary ban from any sort of interaction or public
communication with the community for a specified period of time. No public or
private interaction with the people involved, including unsolicited interaction
with those enforcing the Code of Conduct, is allowed during this period.
Violating these terms may lead to a permanent ban.
### 4. Permanent Ban
**Community Impact**: Demonstrating a pattern of violation of community
standards, including sustained inappropriate behavior, harassment of an
individual, or aggression toward or disparagement of classes of individuals.
**Consequence**: A permanent ban from any sort of public interaction within the
community.
## Attribution
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
version 2.1, available at
[https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1]
Community Impact Guidelines were inspired by
[Mozilla's code of conduct enforcement ladder][Mozilla CoC].
For answers to common questions about this code of conduct, see
[https://www.contributor-covenant.org/faq][FAQ]
[homepage]: https://www.contributor-covenant.org
[v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
[Mozilla CoC]: https://github.com/mozilla/diversity
[FAQ]: https://www.contributor-covenant.org/faq
+391
View File
@@ -0,0 +1,391 @@
# Contributing to Semantica
Thank you for your interest in contributing to Semantica! This document provides guidelines and instructions for contributing to the project.
## Table of Contents
- [Code of Conduct](#code-of-conduct)
- [Getting Started](#getting-started)
- [Development Setup](#development-setup)
- [Code Style Guidelines](#code-style-guidelines)
- [Testing Requirements](#testing-requirements)
- [Commit Message Conventions](#commit-message-conventions)
- [Pull Request Process](#pull-request-process)
- [Documentation Standards](#documentation-standards)
- [Types of Contributions](#types-of-contributions)
- [Getting Help](#getting-help)
## Code of Conduct
This project adheres to a [Code of Conduct](CODE_OF_CONDUCT.md). By participating, you are expected to uphold this code. Please report unacceptable behavior to the maintainers.
## Getting Started
1. **Fork the repository** on GitHub
2. **Clone your fork** locally:
```bash
git clone https://github.com/your-username/semantica.git
cd semantica
```
3. **Add the upstream remote**:
```bash
git remote add upstream https://github.com/Hawksight-AI/semantica.git
```
## Development Setup
### Prerequisites
- Python 3.8 or higher (3.9+ recommended)
- pip package manager
- Git
### Installation
1. **Create a virtual environment** (recommended):
```bash
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
```
2. **Install the project in editable mode with dev dependencies**:
```bash
pip install -e ".[dev]"
```
3. **Install pre-commit hooks**:
```bash
pre-commit install
```
### Verify Installation
```bash
python -c "import semantica; print(semantica.__version__)"
pytest --version
black --version
```
## Code Style Guidelines
We use several tools to maintain code quality and consistency:
### Formatting
- **Black**: Code formatting (line length: 88)
```bash
black semantica/
```
- **isort**: Import sorting
```bash
isort semantica/
```
### Linting
- **flake8**: Style guide enforcement
```bash
flake8 semantica/
```
- **mypy**: Static type checking
```bash
mypy semantica/
```
### Running All Checks
```bash
# Format code
black semantica/ tests/
# Sort imports
isort semantica/ tests/
# Lint
flake8 semantica/ tests/
# Type check
mypy semantica/
```
Or use pre-commit hooks (automatically runs on commit):
```bash
pre-commit run --all-files
```
## Testing Requirements
### Running Tests
```bash
# Run all tests
pytest
# Run with coverage
pytest --cov=semantica --cov-report=html
# Run specific test file
pytest tests/test_specific.py
# Run with verbose output
pytest -v
```
### Test Coverage
- Minimum coverage: **80%**
- Critical modules: **90%+**
- Coverage reports are generated in `htmlcov/`
### Writing Tests
- Follow pytest conventions
- Use descriptive test names
- Include docstrings for complex tests
- Test both success and failure cases
- Use fixtures for common setup
Example:
```python
def test_entity_extraction():
"""Test basic entity extraction functionality."""
from semantica.semantic_extract import NamedEntityRecognizer
ner = NamedEntityRecognizer()
entities = ner.extract("Apple Inc. was founded by Steve Jobs.")
assert len(entities) > 0
assert any(e.text == "Apple Inc." for e in entities)
```
## Commit Message Conventions
We follow [Conventional Commits](https://www.conventionalcommits.org/) specification:
### Format
```
<type>(<scope>): <subject>
<body>
<footer>
```
### Types
- `feat`: New feature
- `fix`: Bug fix
- `docs`: Documentation changes
- `style`: Code style changes (formatting, etc.)
- `refactor`: Code refactoring
- `test`: Adding or updating tests
- `chore`: Maintenance tasks
- `perf`: Performance improvements
- `ci`: CI/CD changes
### Examples
```
feat(kg): add temporal graph support
Add support for temporal knowledge graphs with version tracking
and time-based queries.
Closes #123
```
```
fix(parse): handle empty PDF files gracefully
Previously, empty PDF files would cause a crash. Now they return
an empty document with appropriate warnings.
Fixes #456
```
## Pull Request Process
### Before Submitting
1. **Update your fork**:
```bash
git fetch upstream
git checkout main
git merge upstream/main
```
2. **Create a feature branch**:
```bash
git checkout -b feature/your-feature-name
# or
git checkout -b fix/bug-description
```
3. **Make your changes** and commit following our conventions
4. **Run all checks**:
```bash
pytest
black semantica/ tests/
isort semantica/ tests/
flake8 semantica/ tests/
mypy semantica/
```
5. **Push to your fork**:
```bash
git push origin feature/your-feature-name
```
### PR Checklist
- [ ] Code follows style guidelines
- [ ] Tests pass locally
- [ ] New tests added for new features
- [ ] Documentation updated
- [ ] Commit messages follow conventions
- [ ] No merge conflicts
- [ ] PR description is clear and complete
### PR Description Template
```markdown
## Description
Brief description of changes
## Type of Change
- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update
## Related Issues
Closes #123
Related to #456
## Testing
- [ ] Tests pass locally
- [ ] Added new tests
- [ ] Updated existing tests
## Checklist
- [ ] Code follows style guidelines
- [ ] Self-review completed
- [ ] Comments added for complex code
- [ ] Documentation updated
- [ ] No new warnings generated
```
## Documentation Standards
### Code Documentation
- Use Google-style docstrings
- Include type hints
- Document all public functions and classes
- Include examples for complex functions
Example:
```python
def extract_entities(
text: str,
model: str = "transformer",
confidence_threshold: float = 0.7
) -> List[Entity]:
"""Extract named entities from text.
Args:
text: Input text to process
model: NER model to use (default: "transformer")
confidence_threshold: Minimum confidence score (default: 0.7)
Returns:
List of extracted Entity objects
Raises:
ValueError: If text is empty or model is invalid
Example:
>>> ner = NamedEntityRecognizer()
>>> entities = ner.extract("Apple Inc. was founded in 1976.")
>>> len(entities)
2
"""
...
```
### Documentation Files
- Update relevant documentation in `docs/`
- Add examples to cookbook if applicable
- Update API reference if adding new public APIs
- Keep README.md up to date
## Types of Contributions
### Code Contributions
- Bug fixes
- New features
- Performance improvements
- Refactoring
### Documentation Contributions
- Fix typos and grammar
- Improve clarity
- Add examples
- Create tutorials
- Translate documentation
### Testing Contributions
- Add test coverage
- Improve test quality
- Add integration tests
- Performance benchmarks
### Other Contributions
- Answer questions in discussions
- Help with issues
- Review pull requests
- Share use cases
- Report bugs
- Suggest features
## Getting Help
### Communication Channels
- **GitHub Discussions**: General questions and discussions
- **GitHub Issues**: Bug reports and feature requests
- **Discord**: Real-time chat and community support
### Before Asking for Help
1. Check existing documentation
2. Search GitHub issues and discussions
3. Review code examples in cookbook
4. Check FAQ in documentation
### Asking Good Questions
- Provide context and environment details
- Include code examples
- Show what you've tried
- Include error messages and logs
- Be specific about what you need
## Recognition
Contributors are recognized in:
- [CONTRIBUTORS.md](CONTRIBUTORS.md)
- GitHub contributors page
- Release notes for significant contributions
Thank you for contributing to Semantica! 🎉
+97
View File
@@ -0,0 +1,97 @@
# Contributors
Thank you to all the people who have contributed to Semantica! 🎉
This project follows the [all-contributors](https://allcontributors.org) specification. Contributions of any kind are welcome!
## How to Contribute
We welcome contributions of all kinds! Whether you're:
- Writing code
- Improving documentation
- Reporting bugs
- Suggesting features
- Answering questions
- Reviewing pull requests
- Sharing use cases
- Creating examples
All contributions are valuable and appreciated!
## Contribution Types
- 💻 **Code**: Writing code, fixing bugs, implementing features
- 📝 **Documentation**: Writing docs, tutorials, examples
- 🐛 **Bug Reports**: Finding and reporting bugs
- 💡 **Ideas**: Suggesting new features or improvements
- 🎨 **Design**: UI/UX improvements, graphics, branding
- 📖 **Examples**: Creating code examples and tutorials
- 🔍 **Testing**: Writing tests, improving test coverage
- 💬 **Answering Questions**: Helping others in discussions
- 📢 **Talks**: Giving talks, presentations, workshops
- 🌍 **Translation**: Translating documentation
- 🔧 **Tools**: Creating tools, scripts, integrations
- 📦 **Packaging**: Improving build, release, distribution
- ⚠️ **Security**: Reporting security vulnerabilities
- 🎓 **Education**: Teaching, mentoring, tutorials
- 📹 **Video**: Creating video content, tutorials
- 🎵 **Audio**: Podcasts, audio content
- 📸 **Photography**: Screenshots, images
- 🔬 **Research**: Research, analysis, studies
- 💰 **Financial**: Sponsoring, funding
- 🏗️ **Infrastructure**: CI/CD, hosting, infrastructure
- 🚇 **Maintenance**: Maintenance, triage, project management
## Contributors
<!-- ALL-CONTRIBUTORS-LIST:START -->
<!-- This section is auto-generated by all-contributors bot -->
<!-- Do not edit this section manually -->
<!-- ALL-CONTRIBUTORS-LIST:END -->
## Recognition
### Top Contributors
Contributors are recognized based on their contributions to the project. Recognition includes:
- Listing in this file
- GitHub contributor statistics
- Special mentions in release notes
- Featured showcases for significant contributions
### Hall of Fame
Special recognition for exceptional contributions:
- **Coming soon** - We'll feature outstanding contributors here!
## How to Add Yourself
If you've contributed to Semantica and want to be added to this list:
1. **Automatic**: If you've made a commit, you'll appear in [GitHub's contributors graph](https://github.com/Hawksight-AI/semantica/graphs/contributors)
2. **Manual**: Open a PR adding yourself to this file, or use the [@all-contributors bot](https://allcontributors.org/docs/en/bot/usage)
Example:
```markdown
- [Your Name](https://github.com/yourusername) - 💻 📝 🐛
```
## All Contributors Bot
We use the [all-contributors](https://allcontributors.org) bot to automatically recognize contributors. To add a contributor, comment on an issue or PR:
```
@all-contributors please add @username for code, docs, bug
```
## Thank You!
Every contribution, no matter how small, helps make Semantica better. Thank you for being part of our community!
---
**Want to contribute?** Check out our [Contributing Guide](CONTRIBUTING.md) to get started!
+237
View File
@@ -0,0 +1,237 @@
# Add Intelligence Cookbook Notebooks with MCP, Agents, and Orchestrator-Worker Pattern
## Overview
Add comprehensive intelligence-focused notebooks to `cookbook/use_cases/intelligence/` with complete end-to-end pipelines. The **Intelligence Analysis** notebook will use the **Orchestrator-Worker Pattern** with detailed graph analytics, hybrid RAG, and ontology building. Update documentation in `docs/cookbook.md` and `docs/use-cases.md`.
## New Notebooks to Create
### 1. Criminal Network Analysis (`Criminal_Network_Analysis.ipynb`)
Complete pipeline from data sources to GraphRAG with agent-based workflows:
- **Data Sources**: Ingest from police reports, court records, surveillance data, communication logs
- **MCP Integration**: Utilize MCP for accessing public records databases, court records APIs, and real-time data streams
- **Semantica Agents**:
- Data Gathering Agent (autonomous data collection with AgentMemory)
- Network Analysis Agent (graph analytics and community detection)
- Pattern Detection Agent (identifying suspicious patterns)
- Report Generation Agent (compiling intelligence reports)
- **Agent Coordination**: Use Pipeline module for parallel agent workflows
- **Agent Memory**: AgentMemory for persistent context across interactions
- **Complete Pipeline**: Data sources → MCP → Parsing → Extraction → KG → Graph Analytics → GraphRAG → Agent Analysis → Visualization → Reporting
### 2. Law Enforcement and Forensics (`Law_Enforcement_Forensics.ipynb`)
Complete forensic analysis pipeline with agent-based workflows:
- **Data Sources**: Case files, evidence logs, witness statements, forensic reports, crime scene data
- **Semantica Agents**:
- Evidence Collection Agent (autonomous evidence gathering)
- Timeline Analysis Agent (temporal case timelines)
- Cross-Case Correlation Agent (connections across cases)
- Forensic Report Agent (comprehensive report generation)
- **Agent Coordination**: Multi-agent pipeline for parallel evidence processing
- **Agent Memory**: Persistent memory for case context and evidence chains
- **Complete Pipeline**: Case files → Parsing → Evidence Extraction → Temporal KG → Graph Analytics → GraphRAG → Agent Analysis → Visualization → Reporting
### 3. Intelligence Analysis (`Intelligence_Analysis.ipynb`) - **ORCHESTRATOR-WORKER PATTERN**
Comprehensive intelligence analysis using **Orchestrator-Worker Pattern** with detailed implementation:
#### Orchestrator-Worker Architecture:
- **Orchestrator**: ExecutionEngine coordinates all workers using PipelineBuilder and ParallelismManager
- **Worker 1 - Data Ingestion Worker**: Handles multi-source data ingestion (FileIngestor, WebIngestor, StreamIngestor, FeedIngestor, DBIngestor)
- **Worker 2 - Ontology Building Worker**: Complete 6-stage ontology generation pipeline
- Stage 1: Semantic Network Parsing (extract domain concepts)
- Stage 2: YAML-to-Definition (transform concepts to class definitions)
- Stage 3: Definition-to-Types (map to OWL types)
- Stage 4: Hierarchy Generation (build taxonomic structures)
- Stage 5: TTL Generation (generate OWL/Turtle syntax)
- Stage 6: Symbolic Validation (HermiT/Pellet reasoning)
- **Worker 3 - Graph Construction Worker**: Builds knowledge graphs (GraphBuilder, TemporalGraphQuery)
- **Worker 4 - Graph Analytics Worker**: Comprehensive graph analytics including:
- Centrality Measures: PageRank, Betweenness, Closeness, Eigenvector
- Community Detection: Louvain algorithm
- Connectivity Analysis: Path finding, shortest paths, connectivity metrics
- Graph Metrics: Density, clustering coefficient, diameter, radius
- **Worker 5 - Hybrid RAG Worker**: Complete hybrid RAG implementation:
- Vector Store setup with embeddings
- Knowledge Graph queries
- Hybrid Search (combining vector similarity + graph traversal)
- Context Retrieval (ContextRetriever)
- Query Orchestration across KG and vector store
- **Worker 6 - Intelligence Analysis Worker**: Threat assessment, geospatial analysis, pattern detection
- **Worker 7 - Report Generation Worker**: Compiles comprehensive intelligence reports
#### Complete Features:
- **Data Sources**: OSINT feeds, threat intelligence, social media, news, public records, geospatial data
- **MCP Integration**: Real-time data fetching, web scraping, API integration, browser automation for OSINT
- **Agent Memory**: Persistent memory for threat context and intelligence history
- **Complete Pipeline**: OSINT sources → MCP → Orchestrator → Parallel Workers → Ontology → KG → Graph Analytics → Hybrid RAG → Intelligence Analysis → Visualization → Reporting
## Files to Create/Modify
### New Notebooks (in `cookbook/use_cases/intelligence/`)
- `Criminal_Network_Analysis.ipynb`
- `Law_Enforcement_Forensics.ipynb`
- `Intelligence_Analysis.ipynb` (with Orchestrator-Worker Pattern)
### Documentation Updates
- `docs/cookbook.md` - Add new notebooks to Intelligence section
- `docs/use-cases.md` - Add use case cards for Criminal Network Analysis and Law Enforcement & Forensics
## Implementation Details
### Intelligence Analysis - Orchestrator-Worker Pipeline Structure:
1. **Orchestrator Setup** - Initialize ExecutionEngine, PipelineBuilder, ParallelismManager
2. **Data Sources** - Multiple ingestion (FileIngestor, DBIngestor, WebIngestor, StreamIngestor, FeedIngestor)
3. **MCP Integration** - External data access, web scraping, browser automation
4. **Worker 1 - Data Ingestion Worker** - Parallel data gathering from multiple sources
5. **Data Parsing** - Parse structured/unstructured data (JSONParser, XMLParser, CSVParser, DocumentParser, StructuredDataParser)
6. **Data Normalization** - Clean and standardize (TextNormalizer, DataNormalizer)
7. **Entity & Relation Extraction** - Extract entities, relationships, events (NERExtractor, RelationExtractor, TripleExtractor, EventDetector)
8. **Worker 2 - Ontology Building Worker** - Complete 6-stage ontology generation:
- Use OntologyGenerator, ClassInferrer, PropertyGenerator
- Generate OWL/Turtle with OWLGenerator
- Validate with OntologyValidator (HermiT/Pellet)
9. **Worker 3 - Graph Construction Worker** - Build knowledge graphs:
- GraphBuilder for entity/relationship graphs
- TemporalGraphQuery for time-aware graphs
10. **Worker 4 - Graph Analytics Worker** - All graph analytics:
- GraphAnalyzer: PageRank, Betweenness, Closeness, Eigenvector centrality
- CommunityDetector: Louvain community detection
- ConnectivityAnalyzer: Path finding, shortest paths, connectivity
- CentralityCalculator: All centrality measures
- Graph metrics: density, clustering, diameter, radius
11. **Worker 5 - Hybrid RAG Worker** - Complete hybrid RAG:
- EmbeddingGenerator: Generate embeddings for entities and text
- VectorStore: Store and index embeddings
- HybridSearch: Combine vector similarity + graph queries
- ContextRetriever: Retrieve relevant context from KG and vectors
- Query orchestration: Coordinate queries across KG and vector store
12. **Worker 6 - Intelligence Analysis Worker** - Threat assessment, geospatial analysis, pattern detection
13. **Agent Memory Integration** - Store and retrieve agent context using AgentMemory
14. **Orchestrator Coordination** - Coordinate all workers with parallel execution
15. **Visualization** - Network graphs, analytics dashboards, maps (KGVisualizer, AnalyticsVisualizer, TemporalVisualizer)
16. **Worker 7 - Report Generation Worker** - Compile comprehensive intelligence reports
17. **Report Generation** - Professional HTML reports (ReportGenerator, HTMLExporter)
### Other Notebooks - Standard Pipeline Structure:
1. **Data Sources** - Multiple ingestion
2. **MCP Integration** - (Criminal Network Analysis only)
3. **Semantica Agent Setup** - Initialize AgentMemory, create specialized agents
4. **Agent-Based Data Gathering** - Autonomous agents gather data
5. **Data Parsing** - Parse structured/unstructured data
6. **Data Normalization** - Clean and standardize
7. **Entity & Relation Extraction** - Extract entities, relationships, events
8. **Knowledge Graph Construction** - Build graphs
9. **Agent-Based Analysis** - Specialized agents perform parallel analysis
10. **Graph Analytics** - Community detection, centrality, connectivity
11. **GraphRAG Implementation** - Embeddings, vector store, hybrid search
12. **Agent Memory Integration** - Store and retrieve agent context
13. **Detailed Analysis** - Reasoning, inference, pattern detection
14. **Agent Coordination** - Pipeline module for multi-agent workflow orchestration
15. **Visualization** - Network graphs, analytics dashboards, maps
16. **Agent-Based Report Generation** - Agents compile comprehensive reports
17. **Report Generation** - Professional HTML reports
### Semantica Agent Implementation:
- **AgentMemory**: Persistent context storage, memory retrieval, conversation history
- **Pipeline Coordination**: PipelineBuilder, ExecutionEngine, ParallelismManager for multi-agent workflows
- **Specialized Agents**: Each agent has specific role (data gathering, analysis, reporting)
- **Agent Examples**: Code demonstrations of agent workflows with memory integration
### MCP Integration:
- **Intelligence Analysis**: MCP browser tools for OSINT, resources for external feeds
- **Criminal Network Analysis**: MCP for public records, court databases, API integration
- **Agent-MCP Coordination**: Agents use MCP for autonomous data gathering
### Notebook Structure:
#### Intelligence Analysis (Orchestrator-Worker Pattern):
- Overview with Orchestrator-Worker pattern explanation
- Semantica modules used (30+ modules including Orchestrator, Workers, Ontology, Graph Analytics, Hybrid RAG)
- **Orchestrator Architecture**: Detailed explanation of orchestrator and worker roles
- **Worker Implementation**: Detailed code for each worker (7 workers)
- **Ontology Building**: Complete 6-stage ontology generation pipeline demonstration
- **Graph Analytics**: All analytics methods (PageRank, Betweenness, Closeness, Eigenvector, Louvain, connectivity, paths)
- **Hybrid RAG**: Complete implementation with KG queries + vector search, query orchestration
- MCP integration demonstration
- Step-by-step implementation with orchestrator coordinating workers
- Parallel worker execution examples
- Agent memory integration
- Best practices for orchestrator-worker pattern
- Best practices for agents and MCP
- Conclusion with key takeaways
#### Other Notebooks:
- Overview with complete pipeline description
- Semantica modules used (20+ modules including AgentMemory, Pipeline)
- Agent Architecture explanation
- MCP integration demonstration (Criminal Network Analysis)
- Step-by-step implementation with agent workflows
- Agent memory integration examples
- Multi-agent pipeline orchestration
- Best practices for agents and MCP
- Conclusion with key takeaways
## Key Implementation Details for Orchestrator-Worker Pattern:
### Orchestrator Code Example:
```python
from semantica.pipeline import PipelineBuilder, ExecutionEngine, ParallelismManager
from semantica.ontology import OntologyGenerator
from semantica.kg import GraphBuilder, GraphAnalyzer
from semantica.vector_store import VectorStore, HybridSearch
from semantica.context import AgentMemory
# Initialize orchestrator
orchestrator = ExecutionEngine()
parallelism_manager = ParallelismManager(max_workers=7)
# Define workers
def data_ingestion_worker(sources):
# Worker 1: Multi-source data ingestion
pass
def ontology_building_worker(entities, relationships):
# Worker 2: Complete 6-stage ontology generation
ontology_gen = OntologyGenerator()
ontology = ontology_gen.generate_ontology({"entities": entities, "relationships": relationships})
return ontology
def graph_construction_worker(entities, relationships):
# Worker 3: Build knowledge graph
graph_builder = GraphBuilder()
kg = graph_builder.build(entities, relationships)
return kg
def graph_analytics_worker(kg):
# Worker 4: All graph analytics
analyzer = GraphAnalyzer()
pagerank = analyzer.compute_centrality(kg, method="pagerank")
betweenness = analyzer.compute_centrality(kg, method="betweenness")
communities = analyzer.detect_communities(kg, method="louvain")
# ... all analytics
return {"pagerank": pagerank, "betweenness": betweenness, "communities": communities}
def hybrid_rag_worker(kg, vector_store):
# Worker 5: Hybrid RAG with KG and vector store
hybrid_search = HybridSearch(vector_store=vector_store, knowledge_graph=kg)
# Query orchestration
pass
# Build pipeline with workers
pipeline = PipelineBuilder() \
.add_step("data_ingestion", "custom", func=data_ingestion_worker) \
.add_step("ontology_building", "custom", func=ontology_building_worker) \
.add_step("graph_construction", "custom", func=graph_construction_worker) \
.add_step("graph_analytics", "custom", func=graph_analytics_worker) \
.add_step("hybrid_rag", "custom", func=hybrid_rag_worker) \
.build()
# Execute with parallel workers
result = orchestrator.execute_pipeline(pipeline, parallel=True, max_workers=7)
```
Each notebook demonstrates the full journey from raw data sources through autonomous agent workflows (or orchestrator-worker pattern) and GraphRAG to actionable intelligence.
File diff suppressed because it is too large Load Diff
+33
View File
@@ -0,0 +1,33 @@
# PR: Context Module Testing & Validation
## Description
This PR adds comprehensive testing and validation for the **Context Engineering Module** (`semantica.context`). It includes unit tests for core components, verification of notebook examples, and a critical bug fix in the deduplication module.
## Changes
### 1. New Unit Tests (`tests/context/`)
Added `tests/context/test_context.py` covering:
- **AgentContext**: End-to-end storage and retrieval (RAG & GraphRAG).
- **AgentMemory**: Hierarchical memory management (short-term buffer vs. long-term vector store) and retention policies.
- **ContextGraph**: Node/edge addition and neighbor traversal.
- **EntityLinker**: URI assignment and entity linking logic.
- **ContextRetriever**: Hybrid retrieval strategies (Vector + Graph).
### 2. Notebook Verification
Verified functionality of the following notebooks by converting them to test scripts:
- `19_Context_Module.ipynb`: Verified high-level interface, token limits, and graph construction.
- `11_Advanced_Context_Engineering.ipynb`: Verified custom memory pruning, hybrid tuning, and custom graph builders.
### 3. Bug Fixes
- **`semantica/deduplication/merge_strategy.py`**: Fixed a `NameError` caused by a missing `Tuple` import. This was discovered during global import validation.
### 4. Verification
- All new tests passed.
- Global import check confirmed no other hidden dependency issues.
- Integration test `verify_context_sync.py` passed, confirming correct synchronization between memory, graph, and vector store.
## Testing Instructions
Run the new tests with:
```bash
python -m unittest tests/context/test_context.py
```
+34
View File
@@ -0,0 +1,34 @@
# Enhanced Export Module Testing, Bug Fixes & Notebook Updates
## Summary
This PR significantly hardens the `semantica.export` module by adding comprehensive unit tests, fixing critical bugs in export wrappers and logic, and updating documentation and cookbooks to match current API signatures.
## Key Changes
### 1. Bug Fixes & Logic Improvements
- **`semantica/export/methods.py`**:
- Fixed `export_yaml(method="schema")` to correctly call `export_ontology_schema` and handle file writing (previously failed as the underlying method returns a string).
- Added safeguards to all convenience functions (`export_rdf`, `export_json`, etc.) to prevent infinite recursion if the registry returns the wrapper function itself.
- **`semantica/kg/graph_builder.py`**: Fixed a critical bug where `ConflictDetector` was receiving the entire graph dictionary instead of the entity list.
- **`semantica/export/rdf_exporter.py`**: Fixed `export_to_rdf` to correctly return serialized data for all formats.
### 2. Comprehensive Testing (`tests/`)
- **`tests/test_export_module.py`**: A full suite of unit tests covering all 11 export classes (`JSON`, `CSV`, `RDF`, `GraphML`, `YAML`, `OWL`, `Vector`, `LPG`, etc.).
- **`tests/test_export_methods_wrapper.py`**: Added specific tests for convenience wrapper functions in `methods.py`, verifying the fix for schema export.
- **`tests/test_notebook_15_export.py`** & **`tests/test_notebooks_simulation.py`**: Simulation tests that replicate cookbook logic to ensure end-to-end functionality.
### 3. Documentation & Notebook Updates
- **`docs/reference/export.md`** & **`semantica/export/export_usage.md`**: Updated to correctly document `YAMLSchemaExporter.export_ontology_schema` instead of the deprecated `export` method.
- **Cookbooks** (`15_Export.ipynb`, `05_Multi_Format_Export.ipynb`):
- Updated `GraphBuilder.build()` calls to pass combined lists (fixing API mismatch).
- Corrected `YAMLSchemaExporter` usage.
- Fixed `VectorExporter` data preparation.
- Adjusted `CSVExporter` paths.
## Verification
All tests passed successfully:
```bash
$ pytest tests/test_export_module.py tests/test_notebooks_simulation.py tests/test_notebook_15_export.py tests/test_export_methods_wrapper.py
...
13 passed in 3.82s
```
+45
View File
@@ -0,0 +1,45 @@
# feat: Knowledge Engineering Module Enhancements and Testing
## 📝 Description
This PR significantly enhances the stability, test coverage, and documentation of the `knowledge-engineering` module and related components (`ontology`, `visualization`, `conflicts`, etc.). It addresses critical bugs preventing pipeline execution and establishes a comprehensive testing baseline.
## 🚀 Key Changes
### 1. 🧪 Comprehensive Unit Testing
Added and verified over **100+ new unit tests** across multiple modules to ensure robustness:
- **Knowledge Graph (`semantica.kg`)**:
- `test_core_components.py`: Validates `GraphBuilder`, `EntityResolver`, `GraphValidator`, `ProvenanceTracker`.
- `test_algorithms.py`: Covers `CentralityCalculator`, `CommunityDetector`, `ConnectivityAnalyzer`.
- **Ontology (`semantica.ontology`)**:
- `test_ontology_classes.py`: Tests core ontology generation logic.
- `test_ontology_advanced.py`: Validates validation, metrics, and complex class relationships.
- **Visualization (`semantica.visualization`)**:
- Added tests for `GraphVisualizer` and interactive plotting components.
- **Data Handling**:
- `semantica.split`: Added `test_splitter.py`.
- `semantica.parse`: Added `test_parser.py` (with fixes for `pathlib` mocking).
- `semantica.vector_store` & `semantica.triple_store`: Enhanced with full CRUD operation tests.
- **Utilities**:
- `semantica.seed`: Validated seed management.
- `semantica.utils`: Verified shared utility functions.
### 2. 🐛 Bug Fixes & Stability Improvements
- **Conflict Resolution**: Implemented a placeholder `resolve_conflicts` method in `ConflictDetector` to unblock pipeline execution failures where this method was missing.
- **Inference Engine**: Fixed `TypeError: unhashable type: 'dict'` by handling unhashable facts in `InferenceEngine`.
- **Circular Imports**: Resolved circular dependency issues in `semantic_extract` by deferring imports.
- **Test Infrastructure**:
- Fixed `test_cookbook_integration.py` by mocking MCP server connections (`httpx`/`requests`) to prevent WinError 10061.
- Fixed `pathlib.Path` mocking issues in parser tests.
### 3. 📚 Documentation Updates
- **`semantica/kg/kg_usage.md`**: Updated usage guide to reflect current capabilities and configuration options.
- **`semantica/conflicts/conflicts_usage.md`**: Added documentation for the `resolve_conflicts` convenience method.
## ✅ Verification
- All new and existing unit tests pass.
- `python -m unittest discover tests/kg` runs successfully.
- Pipeline execution no longer crashes due to missing methods or unhashable types.
## 📦 Related Issues
- Fixes pipeline crashes during conflict resolution.
- Addresses missing test coverage for core KG components.
+346 -2252
View File
File diff suppressed because it is too large Load Diff
+166
View File
@@ -0,0 +1,166 @@
# Security Policy
## Supported Versions
We actively support the following versions of Semantica with security updates:
| Version | Supported |
| ------- | ------------------ |
| 0.0.1 | :white_check_mark: |
| < 0.0.1 | :x: |
## Reporting a Vulnerability
We take security vulnerabilities seriously. If you discover a security vulnerability, please follow these steps:
### 1. **Do NOT** create a public GitHub issue
Security vulnerabilities should be reported privately to prevent potential exploitation.
### 2. Report Security Issue
Create a [GitHub Security Advisory](https://github.com/Hawksight-AI/semantica/security/advisories/new) or contact us through [GitHub Issues](https://github.com/Hawksight-AI/semantica/issues) with "[SECURITY]" prefix.
Include the following information:
- **Type of vulnerability** (e.g., XSS, SQL injection, authentication bypass)
- **Affected component** (module, function, or file)
- **Steps to reproduce** (detailed description or proof-of-concept code)
- **Potential impact** (what could an attacker do?)
- **Suggested fix** (if you have one)
- **Your contact information** (for follow-up questions)
### 3. Response Timeline
- **Initial Response**: Within 48 hours
- **Status Update**: Within 7 days
- **Resolution**: Depends on severity and complexity
### 4. Disclosure Policy
- We will acknowledge receipt of your report within 48 hours
- We will provide regular updates on the status of the vulnerability
- Once fixed, we will credit you (if desired) in the security advisory
- We will coordinate public disclosure with you
## Security Update Process
1. **Assessment**: We assess the severity using CVSS scoring
2. **Fix Development**: We develop and test a fix
3. **Release**: We release a security update
4. **Advisory**: We publish a security advisory on GitHub
5. **Communication**: We notify users through appropriate channels
## Severity Levels
### Critical
- Remote code execution
- Authentication bypass
- Data breach or exposure
- **Response Time**: Immediate (within 24 hours)
### High
- Privilege escalation
- Significant data leakage
- Denial of service
- **Response Time**: Within 7 days
### Medium
- Information disclosure
- Cross-site scripting (XSS)
- CSRF vulnerabilities
- **Response Time**: Within 30 days
### Low
- Minor information leakage
- Best practice violations
- **Response Time**: Next release cycle
## Known Security Considerations
### Dependencies
We regularly update dependencies to address security vulnerabilities. However, you should:
- Keep your dependencies up to date
- Review security advisories for our dependencies
- Use tools like `pip-audit` or `safety` to check for known vulnerabilities
### API Keys and Credentials
- **Never commit API keys or credentials** to the repository
- Use environment variables or secure configuration management
- Rotate keys regularly
- Use least-privilege access principles
### Data Handling
- Be cautious when processing untrusted data
- Validate and sanitize all inputs
- Use parameterized queries for database operations
- Implement rate limiting for public APIs
### Network Security
- Use HTTPS for all network communications
- Validate SSL/TLS certificates
- Be cautious with external API calls
- Implement proper authentication and authorization
## Dependency Security Policy
### Regular Updates
- We monitor security advisories for all dependencies
- We update dependencies regularly in our development branch
- Critical security updates are backported to supported versions
### Reporting Dependency Vulnerabilities
If you discover a vulnerability in one of our dependencies:
1. Check if it's already reported upstream
2. Report to us if it affects Semantica specifically
3. We will coordinate with upstream maintainers if needed
### Security Scanning
We use automated tools to scan for vulnerabilities:
- **Dependabot**: Automated dependency updates and security alerts
- **GitHub Security Advisories**: Vulnerability tracking
- **Manual Reviews**: Regular security audits
## Best Practices for Users
1. **Keep Semantica Updated**: Always use the latest stable version
2. **Review Dependencies**: Regularly update your project dependencies
3. **Secure Configuration**: Use secure defaults and proper configuration
4. **Monitor Logs**: Watch for suspicious activity
5. **Report Issues**: Don't hesitate to report potential security issues
## Security Acknowledgments
We appreciate responsible disclosure. Security researchers who help us improve the security of Semantica will be:
- Credited in security advisories (if desired)
- Listed in our security acknowledgments
- Recognized for their contribution
## Contact
For security-related questions or concerns:
- **GitHub Issues**: [Create an issue](https://github.com/Hawksight-AI/semantica/issues) with "[SECURITY]" prefix
- **GitHub Security Advisories**: [Report vulnerability](https://github.com/Hawksight-AI/semantica/security/advisories/new)
## Additional Resources
- [OWASP Top 10](https://owasp.org/www-project-top-ten/)
- [Python Security Best Practices](https://python.readthedocs.io/en/latest/library/security.html)
- [GitHub Security Best Practices](https://docs.github.com/en/code-security)
---
**Thank you for helping keep Semantica and its users safe!**
+130
View File
@@ -0,0 +1,130 @@
# Support
Need help with Semantica? You've come to the right place!
## Getting Help
### Documentation
Start with our comprehensive documentation:
- [Getting Started](docs/getting-started.md) - Quick start guide
- [Concepts](docs/concepts.md) - Core concepts
- [API Reference](docs/reference/core.md) - Complete API docs
- [Cookbook](docs/cookbook.md) - Interactive tutorials
- [FAQ](docs/faq.md) - Frequently asked questions
### Community Support
#### GitHub Discussions
**Best for**: General questions, feature discussions, and getting help
- [Ask a question](https://github.com/Hawksight-AI/semantica/discussions/new?category=q-a)
- [Browse discussions](https://github.com/Hawksight-AI/semantica/discussions)
#### Discord
**Best for**: Real-time chat and quick questions
- [Join Discord](https://discord.gg/semantica)
#### GitHub Issues
**Best for**: Bug reports and feature requests
- [Report a bug](https://github.com/Hawksight-AI/semantica/issues/new?template=bug_report.md)
- [Request a feature](https://github.com/Hawksight-AI/semantica/issues/new?template=feature_request.md)
### Before Asking
1. **Check the documentation** - Your question might already be answered
2. **Search existing issues** - Someone might have asked the same question
3. **Review the cookbook** - Examples might help
4. **Check the FAQ** - Common questions are answered there
## Reporting Issues
### Bug Reports
Use our [bug report template](https://github.com/Hawksight-AI/semantica/issues/new?template=bug_report.md) to report bugs.
Include:
- Clear description of the bug
- Steps to reproduce
- Expected vs actual behavior
- Environment details
- Error messages or logs
### Feature Requests
Use our [feature request template](https://github.com/Hawksight-AI/semantica/issues/new?template=feature_request.md) to suggest features.
Include:
- Problem statement
- Proposed solution
- Use cases
- Impact assessment
### Security Issues
**Do NOT** create a public issue for security vulnerabilities.
Instead:
- Email: semantica-dev@users.noreply.github.com
- Subject: [SECURITY] Brief description
- See [Security Policy](SECURITY.md) for details
## Commercial Support
For enterprise support, custom development, or consulting:
- **Email**: semantica-dev@users.noreply.github.com
- **Subject**: [ENTERPRISE] Your request
## Response Times
- **Community Support**: Best effort (volunteer-based)
- **Bug Reports**: Initial response within 48 hours
- **Security Issues**: Response within 24 hours
- **Enterprise**: Contact for SLA details
## Resources
### Learning
- [Documentation](https://semantica.readthedocs.io/)
- [Cookbook Tutorials](docs/cookbook.md)
- [Code Examples](docs/examples.md)
- [Use Cases](docs/use-cases.md)
### Community
- [Contributing Guide](CONTRIBUTING.md)
- [Code of Conduct](CODE_OF_CONDUCT.md)
- [Community Guidelines](docs/community.md)
- [Contributors](CONTRIBUTORS.md)
### Development
- [Architecture](docs/architecture.md)
- [Governance](docs/governance.md)
- [Roadmap](README.md#roadmap)
## FAQ
See our [FAQ](docs/faq.md) for answers to common questions.
## Still Need Help?
If you've tried the above and still need help:
1. **Ask in GitHub Discussions** - Community members are happy to help
2. **Join Discord** - Real-time support
3. **Open an issue** - For bugs or feature requests
4. **Email** - For private matters or enterprise support
---
**We're here to help!** Don't hesitate to reach out. 🚀
+206
View File
@@ -0,0 +1,206 @@
# Add Intelligence Cookbook Notebooks with MCP and Semantica Agents
## Overview
Add comprehensive intelligence-focused notebooks to `cookbook/use_cases/intelligence/` with complete end-to-end pipelines covering data ingestion (including MCP integration), knowledge graph construction, GraphRAG implementation, **Semantica agent-based workflows**, and detailed analysis. Update documentation in `docs/cookbook.md` and `docs/use-cases.md`.
## New Notebooks to Create
### 1. Criminal Network Analysis (`Criminal_Network_Analysis.ipynb`)
Complete pipeline from data sources to GraphRAG with **agent-based workflows**:
- **Data Sources**: Ingest from police reports, court records, surveillance data, communication logs
- **MCP Integration**: Utilize MCP for accessing public records databases, court records APIs, and real-time data streams
- **Semantica Agents**:
- **Data Gathering Agent**: Autonomous agent using AgentMemory to gather and track data from multiple sources
- **Network Analysis Agent**: Specialized agent for graph analytics and community detection
- **Pattern Detection Agent**: Agent for identifying suspicious patterns and relationships
- **Report Generation Agent**: Agent for compiling intelligence reports
- **Agent Coordination**: Use Pipeline module (PipelineBuilder, ExecutionEngine, ParallelismManager) to coordinate parallel agent workflows
- **Agent Memory**: Use AgentMemory for persistent context across agent interactions
- **Parsing**: Parse structured/unstructured documents, JSON, CSV, PDFs
- **Extraction**: Extract suspects, organizations, locations, events, relationships
- **Knowledge Graph**: Build criminal network graph with temporal relationships
- **Graph Analytics**: Community detection, centrality measures, key player identification
- **GraphRAG**: Vector store, hybrid search, context retrieval for intelligence queries
- **Detailed Analysis**: Pattern detection, network structure analysis, threat assessment
- **Visualization**: Network graphs, community visualization, centrality rankings
- **Reporting**: Generate intelligence reports on criminal structures
### 2. Law Enforcement and Forensics (`Law_Enforcement_Forensics.ipynb`)
Complete forensic analysis pipeline with **agent-based workflows**:
- **Data Sources**: Case files, evidence logs, witness statements, forensic reports, crime scene data
- **Semantica Agents**:
- **Evidence Collection Agent**: Autonomous agent for gathering and organizing evidence
- **Timeline Analysis Agent**: Agent for building temporal case timelines
- **Cross-Case Correlation Agent**: Agent for finding connections across multiple cases
- **Forensic Report Agent**: Agent for generating comprehensive forensic reports
- **Agent Coordination**: Multi-agent pipeline for parallel evidence processing
- **Agent Memory**: Persistent memory for case context and evidence chains
- **Parsing**: Parse PDFs, structured reports, evidence databases, temporal logs
- **Extraction**: Extract entities (persons, locations, evidence, events), relationships, timelines
- **Knowledge Graph**: Build temporal knowledge graph for case timelines and evidence correlation
- **Graph Analytics**: Timeline analysis, evidence correlation, pattern detection across cases
- **GraphRAG**: Semantic search across case files, evidence retrieval, context-aware queries
- **Detailed Analysis**: Cross-case correlation, evidence chain analysis, suspect identification
- **Visualization**: Timeline visualization, evidence networks, case correlation graphs
- **Reporting**: Generate forensic analysis reports with evidence chains
### 3. Intelligence Analysis (`Intelligence_Analysis.ipynb`)
Comprehensive intelligence analysis with **agent-based workflows**:
- **Data Sources**: OSINT feeds, threat intelligence, social media, news, public records, geospatial data
- **MCP Integration**: Utilize MCP for real-time data fetching, web scraping, API integration, external database access, and browser automation for OSINT gathering
- **Semantica Agents**:
- **OSINT Gathering Agent**: Autonomous agent using MCP browser tools for web scraping and OSINT collection
- **Threat Assessment Agent**: Specialized agent for threat analysis and risk scoring
- **Geospatial Intelligence Agent**: Agent for location-based tracking and geographic analysis
- **Multi-Source Fusion Agent**: Agent for correlating intelligence from multiple sources
- **Intelligence Report Agent**: Agent for generating comprehensive threat intelligence reports
- **Agent Coordination**: Complex multi-agent pipeline with parallel execution for intelligence gathering
- **Agent Memory**: Persistent memory for threat context, entity tracking, and intelligence history
- **Parsing**: Multi-format parsing (RSS feeds, JSON, XML, web scraping, geospatial formats)
- **Extraction**: Extract threat actors, locations, events, relationships, temporal patterns
- **Knowledge Graph**: Build multi-source intelligence graph with geospatial and temporal dimensions
- **Graph Analytics**: Threat assessment, risk scoring, entity relationship mapping, pattern detection
- **GraphRAG**: Multi-source intelligence fusion, hybrid search, contextual threat queries
- **Detailed Analysis**:
- Multi-source intelligence fusion and correlation
- Threat assessment and risk analysis
- Geospatial intelligence with location tracking
- Temporal threat evolution analysis
- **Visualization**: Geographic network maps, threat timelines, relationship networks
- **Reporting**: Generate comprehensive threat intelligence reports
## Files to Create/Modify
### New Notebooks (in `cookbook/use_cases/intelligence/`)
- `Criminal_Network_Analysis.ipynb`
- `Law_Enforcement_Forensics.ipynb`
- `Intelligence_Analysis.ipynb`
### Documentation Updates
- `docs/cookbook.md` - Add new notebooks to Intelligence section
- `docs/use-cases.md` - Add new use case cards for criminal networks and law enforcement
## Implementation Details
### Complete Pipeline Structure (All Notebooks):
1. **Data Sources** - Multiple ingestion sources (FileIngestor, DBIngestor, WebIngestor, StreamIngestor, FeedIngestor)
2. **MCP Integration** - Utilize MCP servers for external data access, real-time feeds, API integration, web scraping, and browser automation (in Intelligence Analysis and Criminal Network Analysis notebooks)
3. **Semantica Agent Setup** - Initialize AgentMemory, create specialized agents, set up agent coordination
4. **Agent-Based Data Gathering** - Autonomous agents gather data using MCP and Semantica ingestors
5. **Data Parsing** - Parse structured/unstructured data (JSONParser, XMLParser, CSVParser, DocumentParser, StructuredDataParser)
6. **Data Normalization** - Clean and standardize (TextNormalizer, DataNormalizer)
7. **Entity & Relation Extraction** - Extract entities, relationships, events (NERExtractor, RelationExtractor, TripleExtractor, EventDetector)
8. **Knowledge Graph Construction** - Build graphs (GraphBuilder, TemporalGraphQuery)
9. **Agent-Based Analysis** - Specialized agents perform parallel analysis tasks
10. **Graph Analytics** - Community detection, centrality, connectivity (GraphAnalyzer, ConnectivityAnalyzer, CentralityCalculator)
11. **GraphRAG Implementation** - Embeddings, vector store, hybrid search, context retrieval (EmbeddingGenerator, VectorStore, HybridSearch, ContextRetriever)
12. **Agent Memory Integration** - Store and retrieve agent context using AgentMemory
13. **Detailed Analysis** - Reasoning, inference, pattern detection (InferenceEngine, RuleManager, ExplanationGenerator)
14. **Agent Coordination** - Use Pipeline module for multi-agent workflow orchestration
15. **Visualization** - Network graphs, analytics dashboards, geographic maps (KGVisualizer, AnalyticsVisualizer, TemporalVisualizer)
16. **Agent-Based Report Generation** - Agents compile and generate professional reports
17. **Report Generation** - Professional HTML reports (ReportGenerator, HTMLExporter)
### Semantica Agent Implementation Details:
#### AgentMemory Usage:
- **Persistent Context**: Store agent interactions, decisions, and findings
- **Memory Retrieval**: Retrieve relevant context for agent decision-making
- **Conversation History**: Track agent conversations and analysis sessions
- **Context Accumulation**: Build up intelligence context over time
#### Pipeline Agent Coordination:
- **PipelineBuilder**: Define multi-agent workflows
- **ExecutionEngine**: Execute agent pipelines with error handling
- **ParallelismManager**: Run agents in parallel for efficiency
- **Specialized Agents**: Each agent has a specific role (data gathering, analysis, reporting)
#### Agent Workflow Examples:
```python
# Example: Multi-agent intelligence gathering
from semantica.context import AgentMemory
from semantica.pipeline import PipelineBuilder, ExecutionEngine, ParallelismManager
# Initialize agent memory
agent_memory = AgentMemory(vector_store=vs, knowledge_graph=kg)
# Define specialized agents
def osint_gathering_agent(query, memory):
"""Autonomous OSINT gathering agent"""
# Use MCP for web scraping
# Store findings in agent memory
findings = gather_osint(query)
memory.store(f"OSINT findings: {findings}", metadata={"agent": "osint", "query": query})
return findings
def threat_assessment_agent(intel_data, memory):
"""Threat assessment agent"""
# Retrieve relevant context from memory
context = memory.retrieve("threat patterns", max_results=10)
# Perform threat analysis
assessment = analyze_threats(intel_data, context)
memory.store(f"Threat assessment: {assessment}", metadata={"agent": "threat"})
return assessment
# Build multi-agent pipeline
pipeline = PipelineBuilder() \
.add_step("osint_gathering", "custom", func=osint_gathering_agent, args=(query, agent_memory)) \
.add_step("threat_assessment", "custom", func=threat_assessment_agent, args=(intel_data, agent_memory)) \
.build()
# Execute with parallel agents
engine = ExecutionEngine()
result = engine.execute_pipeline(pipeline, parallel=True)
```
### MCP Integration Details:
- **Intelligence Analysis Notebook**:
- Use MCP browser tools for web scraping and OSINT gathering
- Use MCP resources for accessing external intelligence feeds
- Demonstrate real-time data fetching via MCP
- Agents use MCP for autonomous data gathering
- **Criminal Network Analysis Notebook**:
- Use MCP for accessing public records and court databases
- Demonstrate API integration via MCP
- Show real-time data stream processing
- Agents coordinate MCP-based data gathering
### Notebook Structure:
- Overview with complete pipeline description
- Semantica modules used (20+ modules including AgentMemory, Pipeline)
- **Agent Architecture**: Explanation of agent roles and coordination
- MCP integration demonstration (for Intelligence Analysis and Criminal Network Analysis)
- Step-by-step implementation:
- **Agent Setup**: Initialize AgentMemory and create specialized agents
- Data ingestion from multiple sources (including MCP resources)
- **Agent-Based Data Gathering**: Autonomous agents gather data
- MCP-based external data fetching and API integration
- Parsing and normalization
- Entity and relation extraction
- Knowledge graph construction
- **Agent-Based Analysis**: Parallel agent workflows for analysis
- Graph analytics and pattern detection
- **Agent Memory Integration**: Store and retrieve agent context
- GraphRAG setup and query examples
- **Agent Coordination**: Multi-agent pipeline orchestration
- Detailed analysis with insights
- Visualization examples
- **Agent-Based Report Generation**: Agents compile reports
- Report generation
- Best practices and deployment recommendations
- **Agent Best Practices**: Agent memory management, coordination patterns
- MCP integration best practices
- Conclusion with key takeaways
Each notebook will be comprehensive, demonstrating the full journey from raw data sources (including MCP-enabled external sources) through **autonomous agent workflows** and GraphRAG to actionable intelligence and detailed analysis.
## Key Agent Features to Highlight:
1. **Autonomous Data Gathering**: Agents independently gather data from multiple sources
2. **Persistent Memory**: AgentMemory maintains context across sessions
3. **Parallel Coordination**: Multiple agents work simultaneously on different tasks
4. **Specialized Roles**: Each agent has a specific expertise area
5. **Context-Aware Analysis**: Agents use memory to make informed decisions
6. **Coordinated Workflows**: Pipeline module orchestrates complex multi-agent systems
7. **Intelligent Reporting**: Agents compile findings into comprehensive reports
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
View File
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,226 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/01_Advanced_Extraction.ipynb)\n",
"\n",
"# Advanced Extraction\n",
"\n",
"## Overview\n",
"\n",
"This notebook demonstrates advanced semantic extraction using EventDetector, CoreferenceResolver, TripleExtractor, SemanticAnalyzer, SemanticNetworkExtractor, LLMEnhancer, and ExtractionValidator.\n",
"\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/semantic_extract/)\n",
"\n",
"### Learning Objectives\n",
"\n",
"- Use EventDetector to detect events\n",
"- Use CoreferenceResolver to resolve coreferences\n",
"- Use TripleExtractor to extract RDF triples\n",
"- Use SemanticAnalyzer for semantic analysis\n",
"- Use SemanticNetworkExtractor to extract semantic networks\n",
"- Use LLMEnhancer for LLM-based enhancement\n",
"- Use ExtractionValidator to validate extractions\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## Workflow: Event Detection → Coreference Resolution → Triple Extraction → Semantic Analysis → Network Extraction → LLM Enhancement → Validation\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import (\n",
" EventDetector, CoreferenceResolver, TripleExtractor,\n",
" SemanticAnalyzer, SemanticNetworkExtractor, LLMEnhancer, ExtractionValidator\n",
")\n",
"\n",
"text = \"Apple Inc. was founded by Steve Jobs in 1976. The company is now led by Tim Cook.\"\n",
"\n",
"event_detector = EventDetector()\n",
"events = event_detector.detect_events(text)\n",
"\n",
"print(f\"Detected {len(events)} events\")\n",
"for event in events[:3]:\n",
" print(f\" Event: {event.get('type', 'Unknown')} - {event.get('text', '')[:50]}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Coreference Resolution\n",
"\n",
"Resolve coreferences in text.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"coreference_resolver = CoreferenceResolver()\n",
"\n",
"coreferences = coreference_resolver.resolve(text)\n",
"\n",
"print(f\"Resolved {len(coreferences)} coreference chains\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Triple Extraction\n",
"\n",
"Extract RDF triples.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triple_extractor = TripleExtractor()\n",
"\n",
"triples = triple_extractor.extract_triples(text)\n",
"\n",
"print(f\"Extracted {len(triples)} triples\")\n",
"for triple in triples[:3]:\n",
" print(f\" ({triple.get('subject', '')}, {triple.get('predicate', '')}, {triple.get('object', '')})\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Semantic Analysis\n",
"\n",
"Perform semantic analysis.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"semantic_analyzer = SemanticAnalyzer()\n",
"\n",
"semantic_roles = semantic_analyzer.analyze_semantic_roles(text)\n",
"\n",
"print(f\"Analyzed semantic roles: {len(semantic_roles)}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Semantic Network Extraction\n",
"\n",
"Extract semantic networks.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"semantic_network_extractor = SemanticNetworkExtractor()\n",
"\n",
"semantic_network = semantic_network_extractor.extract_network(text)\n",
"\n",
"print(f\"Extracted semantic network with {len(semantic_network.get('nodes', []))} nodes\")\n",
"print(f\"Edges: {len(semantic_network.get('edges', []))}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: LLM Enhancement\n",
"\n",
"Enhance extractions using LLM.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"llm_enhancer = LLMEnhancer()\n",
"\n",
"enhanced_extractions = llm_enhancer.enhance_extractions(events, text)\n",
"\n",
"print(f\"Enhanced {len(enhanced_extractions)} extractions\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7: Extraction Validation\n",
"\n",
"Validate extractions.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"extraction_validator = ExtractionValidator()\n",
"\n",
"validation_result = extraction_validator.validate(events, text)\n",
"\n",
"print(f\"Extraction validation:\")\n",
"print(f\" Valid: {validation_result.valid}\")\n",
"print(f\" Confidence: {validation_result.confidence:.3f}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"You've learned advanced extraction capabilities:\n",
"\n",
"- **EventDetector**: Event detection and classification\n",
"- **CoreferenceResolver**: Coreference resolution\n",
"- **TripleExtractor**: RDF triple extraction\n",
"- **SemanticAnalyzer**: Semantic analysis and role labeling\n",
"- **SemanticNetworkExtractor**: Semantic network extraction\n",
"- **LLMEnhancer**: LLM-based extraction enhancement\n",
"- **ExtractionValidator**: Extraction validation\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,265 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/02_Advanced_Graph_Analytics.ipynb)\n",
"\n",
"# Advanced Graph Analytics\n",
"\n",
"## Overview\n",
"\n",
"This notebook demonstrates advanced graph analytics using GraphAnalyzer, CentralityCalculator, CommunityDetector, ConnectivityAnalyzer, GraphValidator, Deduplicator, and **GraphStore** for persistent storage.\n",
"\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/kg/)\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",
"- **Use GraphStore to persist graphs to Neo4j or FalkorDB**\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## Workflow: Graph Analysis → Centrality → Communities → Connectivity → Validation → Deduplication → **Persist to Graph Store**\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.kg import GraphBuilder, GraphAnalyzer, CentralityCalculator, CommunityDetector, ConnectivityAnalyzer, GraphValidator\n",
"from semantica.deduplication import DuplicateDetector, EntityMerger, MergeStrategy\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_result = centrality_calculator.calculate_degree_centrality(kg)\n",
"degree_centrality = degree_centrality_result.get('centrality', {})\n",
"betweenness_centrality_result = centrality_calculator.calculate_betweenness_centrality(kg)\n",
"betweenness_centrality = betweenness_centrality_result.get('centrality', {})\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",
"\n",
"validation_result = graph_validator.validate(kg)\n",
"\n",
"print(f\"Graph validation: {validation_result.get('valid', False)}\")\n",
"print(f\"Issues found: {len(validation_result.get('issues', []))}\")\n",
"\n",
"# For deduplication, use semantica.deduplication module:\n",
"# from semantica.deduplication import DuplicateDetector, EntityMerger, MergeStrategy\n",
"# detector = DuplicateDetector(similarity_threshold=0.8)\n",
"# duplicate_groups = detector.detect_duplicate_groups(kg.get('entities', []))\n",
"# merger = EntityMerger()\n",
"# merge_operations = merger.merge_duplicates(kg.get('entities', []), strategy=MergeStrategy.KEEP_MOST_COMPLETE)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Persist to Graph Store\n",
"\n",
"Store the analyzed graph in a persistent graph database using GraphStore.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.graph_store import GraphStore\n",
"\n",
"# Option 1: Neo4j (requires Neo4j server running)\n",
"graph_store = GraphStore(backend=\"neo4j\", uri=\"bolt://localhost:7687\", user=\"neo4j\", password=\"password\")\n",
"graph_store.connect()\n",
"\n",
"# Store entities as nodes and track node ID mapping\n",
"node_id_map = {}\n",
"for entity in entities:\n",
" node = graph_store.create_node(\n",
" labels=[entity[\"type\"]],\n",
" properties={\"name\": entity[\"name\"], \"original_id\": entity[\"id\"]}\n",
" )\n",
" node_id_map[entity[\"id\"]] = node.get(\"id\")\n",
" print(f\"Stored node: {entity['name']} (ID: {node.get('id')})\")\n",
"\n",
"# Store relationships using mapped node IDs\n",
"for rel in relationships:\n",
" source_id = node_id_map.get(rel[\"source\"])\n",
" target_id = node_id_map.get(rel[\"target\"])\n",
" \n",
" if source_id is not None and target_id is not None:\n",
" relationship = graph_store.create_relationship(\n",
" start_node_id=source_id,\n",
" end_node_id=target_id,\n",
" rel_type=rel[\"type\"],\n",
" properties=rel.get(\"properties\", {})\n",
" )\n",
" print(f\"Stored relationship: {rel['source']} -{rel['type']}-> {rel['target']}\")\n",
" else:\n",
" print(f\"Warning: Could not find node IDs for relationship {rel['source']} -> {rel['target']}\")\n",
"\n",
"# Query using Cypher\n",
"results = graph_store.execute_query(\"MATCH (n) RETURN n.name, labels(n) LIMIT 10\")\n",
"print(f\"\\nQuery results: {len(results.get('records', []))} nodes\")\n",
"\n",
"# Get statistics\n",
"stats = graph_store.get_stats()\n",
"print(f\"\\nGraph store statistics:\")\n",
"print(f\" Node count: {stats.get('node_count', 'N/A')}\")\n",
"print(f\" Relationship count: {stats.get('relationship_count', 'N/A')}\")\n",
"print(f\" Label counts: {stats.get('label_counts', {})}\")\n",
"\n",
"graph_store.close()\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",
"- **GraphStore**: Persist graphs to Neo4j or FalkorDB\n",
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,286 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Notice**: The `semantica.kg_qa` module is temporarily unavailable and will be reintroduced in a future release. Any quality assessment examples in this notebook are disabled."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/03_Complete_Visualization_Suite.ipynb)\n",
"\n",
"# Complete Visualization Suite\n",
"\n",
"## Overview\n",
"\n",
"Comprehensive visualization capabilities: visualize knowledge graphs, embeddings, quality metrics, analytics, and temporal data.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/visualization/)\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.visualization import (\n",
" KGVisualizer,\n",
" EmbeddingVisualizer,\n",
" QualityVisualizer,\n",
" AnalyticsVisualizer,\n",
" TemporalVisualizer\n",
")\n",
"from semantica.kg import GraphBuilder, GraphAnalyzer\n",
"from semantica.embeddings import EmbeddingGenerator\n",
"import numpy as np\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Create Sample Knowledge Graph\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder = GraphBuilder()\n",
"\n",
"entities = [\n",
" {\"id\": \"e1\", \"type\": \"Person\", \"name\": \"Alice\", \"properties\": {\"age\": 30}},\n",
" {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Bob\", \"properties\": {\"age\": 35}},\n",
" {\"id\": \"e3\", \"type\": \"Organization\", \"name\": \"Tech Corp\", \"properties\": {\"founded\": 2010}},\n",
" {\"id\": \"e4\", \"type\": \"Location\", \"name\": \"San Francisco\", \"properties\": {\"country\": \"USA\"}},\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"knows\", \"properties\": {\"since\": 2020}},\n",
" {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"works_for\", \"properties\": {\"role\": \"Engineer\"}},\n",
" {\"source\": \"e3\", \"target\": \"e4\", \"type\": \"located_in\", \"properties\": {}},\n",
"]\n",
"\n",
"knowledge_graph = builder.build(entities, relationships)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Knowledge Graph Visualization\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"kg_visualizer = KGVisualizer(layout=\"force\", color_scheme=\"vibrant\")\n",
"kg_visualizer.visualize_network(knowledge_graph, output=\"interactive\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Generate Embeddings and Visualize\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"embedding_generator = EmbeddingGenerator()\n",
"texts = [entity.get(\"name\", \"\") for entity in entities]\n",
"embeddings = embedding_generator.generate_embeddings(texts, data_type=\"text\")\n",
"\n",
"labels = [entity.get(\"type\", \"Unknown\") for entity in entities]\n",
"\n",
"embedding_visualizer = EmbeddingVisualizer()\n",
"embedding_visualizer.visualize_2d_projection(embeddings, labels, method=\"tsne\", output=\"interactive\", file_path=None)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Quality Metrics Visualization\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quality_visualizer = QualityVisualizer()\n",
"quality_report = {\n",
" \"overall_score\": 0.85,\n",
" \"consistency_score\": 0.90,\n",
" \"completeness_score\": 0.80\n",
"}\n",
"quality_visualizer.visualize_dashboard(quality_report, output=\"interactive\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Graph Analytics Visualization\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"graph_analyzer = GraphAnalyzer()\n",
"\n",
"centrality_results = graph_analyzer.calculate_centrality(\n",
" knowledge_graph, \n",
" centrality_type=\"degree\"\n",
")\n",
"\n",
"centrality_scores = {}\n",
"if centrality_results and \"centrality_measures\" in centrality_results:\n",
" degree_centrality = centrality_results[\"centrality_measures\"].get(\"degree\", {})\n",
" if isinstance(degree_centrality, dict) and \"centrality\" in degree_centrality:\n",
" centrality_scores = degree_centrality[\"centrality\"]\n",
" elif isinstance(degree_centrality, dict):\n",
" centrality_scores = degree_centrality\n",
"\n",
"communities_result = graph_analyzer.detect_communities(\n",
" knowledge_graph, \n",
" algorithm=\"louvain\"\n",
")\n",
"\n",
"communities = []\n",
"community_dict = {}\n",
"if communities_result and \"communities\" in communities_result:\n",
" communities_data = communities_result[\"communities\"]\n",
" if isinstance(communities_data, list):\n",
" communities = communities_data\n",
" for idx, community in enumerate(communities):\n",
" if isinstance(community, list):\n",
" for node in community:\n",
" community_dict[node] = idx\n",
" elif isinstance(community, dict) and \"nodes\" in community:\n",
" for node in community[\"nodes\"]:\n",
" community_dict[node] = idx\n",
"\n",
"analytics_visualizer = AnalyticsVisualizer()\n",
"analytics_visualizer.visualize_centrality(centrality_scores, title=\"Node Centrality Scores\")\n",
"\n",
"if community_dict:\n",
" analytics_visualizer.visualize_communities(\n",
" knowledge_graph, \n",
" community_dict, \n",
" title=\"Community Detection\"\n",
" )\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Temporal Data Visualization\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"temporal_kg = {\n",
" \"entities\": entities,\n",
" \"relationships\": relationships,\n",
" \"timestamps\": {\n",
" \"e1\": [2020, 2021, 2022],\n",
" \"e2\": [2020, 2021],\n",
" \"e3\": [2010, 2015, 2020, 2022],\n",
" }\n",
"}\n",
"\n",
"entity_history = {\n",
" \"e1\": [\n",
" {\"timestamp\": 2020, \"properties\": {\"age\": 28}},\n",
" {\"timestamp\": 2021, \"properties\": {\"age\": 29}},\n",
" {\"timestamp\": 2022, \"properties\": {\"age\": 30}},\n",
" ]\n",
"}\n",
"\n",
"from semantica.kg import TemporalVersionManager\n",
"temporal_visualizer = TemporalVisualizer()\n",
"temporal_visualizer.visualize_timeline(temporal_kg, output=\"interactive\")\n",
"# Convert entity history to metrics for visualization\n",
"timestamps = [str(item[\"timestamp\"]) for item in entity_history[\"e1\"]]\n",
"age_values = [item[\"properties\"][\"age\"] for item in entity_history[\"e1\"]]\n",
"metrics_history = {\"age\": age_values}\n",
"temporal_visualizer.visualize_metrics_evolution(metrics_history, timestamps, output=\"interactive\")\n",
"\n",
"# Create versions for snapshot comparison\n",
"version_manager = TemporalVersionManager()\n",
"v1 = version_manager.create_version(temporal_kg, timestamp=\"2020-01-01\", version_label=\"v2020\")\n",
"temporal_kg_v2 = {\n",
" \"entities\": temporal_kg.get(\"entities\", []),\n",
" \"relationships\": temporal_kg.get(\"relationships\", []) + [\n",
" {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"collaborated_with\", \"valid_from\": \"2023-01-01\"}\n",
" ]\n",
"}\n",
"v2 = version_manager.create_version(temporal_kg_v2, timestamp=\"2023-01-01\", version_label=\"v2023\")\n",
"snapshots = {v1[\"timestamp\"]: v1, v2[\"timestamp\"]: v2}\n",
"temporal_visualizer.visualize_snapshot_comparison(snapshots, output=\"interactive\")\n",
"version_history = [\n",
" {\"version\": v1.get(\"label\"), \"timestamp\": v1.get(\"timestamp\")},\n",
" {\"version\": v2.get(\"label\"), \"timestamp\": v2.get(\"timestamp\")}\n",
"]\n",
"temporal_visualizer.visualize_version_history(version_history, output=\"interactive\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"All visualization types demonstrated:\n",
"- Knowledge Graph Visualization\n",
"- Embedding Visualization (t-SNE)\n",
"- Quality Metrics Visualization\n",
"- Graph Analytics Visualization (Centrality & Communities)\n",
"- Temporal Data Visualization (Timeline & Evolution)\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,254 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Notice**: The `semantica.kg_qa` module is temporarily unavailable and will be reintroduced in a future release. Any quality assessment examples in this notebook are disabled."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/04_Conflict_Resolution_Strategies.ipynb)\n",
"\n",
"# Conflict Resolution Strategies\n",
"\n",
"## Overview\n",
"\n",
"Detect conflicts in knowledge graphs, apply multiple resolution strategies, track sources, and maintain audit trails.\n",
"\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/conflicts/)\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"## Workflow: Detect Conflicts → Multiple Resolution Strategies → Track Sources → Audit\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from datetime import datetime\n",
"import json\n",
"from semantica.conflicts import ConflictDetector, ConflictResolver, SourceTracker\n",
"from semantica.conflicts.conflict_resolver import ResolutionStrategy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Define Entities with Conflicting Data\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"entities = [\n",
" {\n",
" \"id\": \"e1\",\n",
" \"type\": \"Person\",\n",
" \"name\": \"John Doe\",\n",
" \"age\": 30,\n",
" \"location\": \"New York\",\n",
" \"source\": \"source1\",\n",
" \"confidence\": 0.8,\n",
" \"metadata\": {\"timestamp\": datetime(2023, 1, 1)}\n",
" },\n",
" {\n",
" \"id\": \"e1\",\n",
" \"type\": \"Person\",\n",
" \"name\": \"John Doe\",\n",
" \"age\": 32,\n",
" \"location\": \"Boston\",\n",
" \"source\": \"source2\",\n",
" \"confidence\": 0.9,\n",
" \"metadata\": {\"timestamp\": datetime(2023, 6, 1)}\n",
" },\n",
" {\n",
" \"id\": \"e2\",\n",
" \"type\": \"Organization\",\n",
" \"name\": \"Tech Corp\",\n",
" \"founded\": 2010,\n",
" \"employees\": 100,\n",
" \"source\": \"source1\",\n",
" \"confidence\": 0.9,\n",
" \"metadata\": {\"timestamp\": datetime(2023, 1, 1)}\n",
" },\n",
" {\n",
" \"id\": \"e2\",\n",
" \"type\": \"Organization\",\n",
" \"name\": \"Tech Corp\",\n",
" \"founded\": 2012,\n",
" \"employees\": 150,\n",
" \"source\": \"source2\",\n",
" \"confidence\": 0.7,\n",
" \"metadata\": {\"timestamp\": datetime(2023, 3, 1)}\n",
" }\n",
"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Detect Conflicts\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Initialize detector\n",
"detector = ConflictDetector(track_provenance=True)\n",
"\n",
"# Detect conflicts across all properties\n",
"conflicts = detector.detect_entity_conflicts(entities)\n",
"\n",
"print(f\"Detected {len(conflicts)} conflicts:\")\n",
"for i, conflict in enumerate(conflicts, 1):\n",
" print(f\"\\nConflict {i}:\")\n",
" print(f\" ID: {conflict.conflict_id}\")\n",
" print(f\" Type: {conflict.conflict_type.value}\")\n",
" print(f\" Entity: {conflict.entity_id}\")\n",
" print(f\" Property: {conflict.property_name}\")\n",
" print(f\" Values: {conflict.conflicting_values}\")\n",
" print(f\" Severity: {conflict.severity}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Resolve Conflicts\n",
"\n",
"We can apply different strategies to resolve the conflicts:\n",
"- **Voting**: Selects the most frequent value\n",
"- **Most Recent**: Selects the value with the latest timestamp\n",
"- **Highest Confidence**: Selects the value from the source with highest confidence\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Initialize resolver\n",
"resolver = ConflictResolver()\n",
"\n",
"# Strategy 1: Voting\n",
"print(\"--- Strategy: Voting ---\")\n",
"results_voting = resolver.resolve_conflicts(conflicts, strategy=\"voting\")\n",
"for r in results_voting:\n",
" if r.resolved:\n",
" print(f\"Resolved {r.conflict_id}: {r.resolved_value} (Confidence: {r.confidence:.2f})\")\n",
"\n",
"# Strategy 2: Most Recent\n",
"print(\"\\n--- Strategy: Most Recent ---\")\n",
"results_recent = resolver.resolve_conflicts(conflicts, strategy=\"most_recent\")\n",
"for r in results_recent:\n",
" if r.resolved:\n",
" print(f\"Resolved {r.conflict_id}: {r.resolved_value}\")\n",
"\n",
"# Strategy 3: Highest Confidence\n",
"print(\"\\n--- Strategy: Highest Confidence ---\")\n",
"results_confidence = resolver.resolve_conflicts(conflicts, strategy=\"highest_confidence\")\n",
"for r in results_confidence:\n",
" if r.resolved:\n",
" print(f\"Resolved {r.conflict_id}: {r.resolved_value} (Confidence: {r.confidence:.2f})\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Track Sources\n",
"\n",
"The `ConflictDetector` tracks source provenance when `track_provenance=True`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tracker = detector.source_tracker\n",
"\n",
"for conflict in conflicts:\n",
" print(f\"\\nConflict: {conflict.conflict_id}\")\n",
" # Get detailed source info for the property\n",
" sources = tracker.get_property_sources(conflict.entity_id, conflict.property_name)\n",
" if sources:\n",
" print(f\" Entity: {conflict.entity_id}, Property: {conflict.property_name}\")\n",
" print(f\" Sources found: {len(sources.sources)}\")\n",
" for src in sources.sources:\n",
" print(f\" - {src.document} (Confidence: {src.confidence})\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Audit Trail\n",
"\n",
"The `ConflictResolver` maintains a history of all resolutions."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"history = resolver.get_resolution_history()\n",
"\n",
"print(f\"Resolution History ({len(history)} entries):\")\n",
"for entry in history:\n",
" print(f\"\\nConflict: {entry.conflict_id}\")\n",
" print(f\" Strategy: {entry.resolution_strategy}\")\n",
" print(f\" Resolved Value: {entry.resolved_value}\")\n",
" print(f\" Notes: {entry.resolution_notes}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Conflict resolution workflow:\n",
"- Conflict Detection using `ConflictDetector`\n",
"- Multiple Resolution Strategies (Voting, Most Recent, Highest Confidence)\n",
"- Source Tracking with `SourceTracker`\n",
"- Complete Audit Trail via `ConflictResolver`"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,623 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/05_Multi_Format_Export.ipynb)\n",
"\n",
"# Advanced Multi-Format Export\n",
"\n",
"## Overview\n",
"\n",
"This advanced notebook demonstrates comprehensive export capabilities of Semantica's Export Module, covering all **8 export formats** plus report generation. You'll learn to export the same knowledge graph to multiple formats simultaneously, use advanced features, and leverage the method registry system.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/export/)\n",
"\n",
"### What You'll Learn\n",
"\n",
"- Export knowledge graphs to all 8 supported formats\n",
"- Use exporter classes directly for fine-grained control\n",
"- Generate professional reports in multiple formats\n",
"- Work with RDF serialization, validation, and namespace management\n",
"- Register and use custom export methods\n",
"- Configure export settings programmatically\n",
"- Export vectors and embeddings for vector stores\n",
"- Export to graph databases using LPG format\n",
"\n",
"### Export Formats Covered\n",
"\n",
"1. **JSON/JSON-LD** - Standard JSON and JSON-LD formats\n",
"2. **RDF** - Turtle, RDF/XML, JSON-LD, N-Triples, N3\n",
"3. **CSV** - Tabular format for entities and relationships\n",
"4. **Graph Formats** - GraphML, GEXF, DOT for visualization tools\n",
"5. **OWL** - OWL/XML and Turtle for ontologies\n",
"6. **Vector** - JSON, NumPy, Binary, FAISS for vector stores\n",
"7. **LPG** - Cypher and LPG for graph databases\n",
"8. **YAML** - Semantic network and schema YAML\n",
"9. **Reports** - HTML, Markdown, JSON, Text reports\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import core modules for building knowledge graph\n",
"from semantica.kg import GraphBuilder\n",
"from semantica.embeddings import EmbeddingGenerator\n",
"from semantica.ontology import OntologyGenerator\n",
"import os\n",
"\n",
"# Create exports directory\n",
"os.makedirs(\"exports\", exist_ok=True)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Create Sample Knowledge Graph and Data\n",
"\n",
"Create a sample knowledge graph with entities, relationships, embeddings, and an ontology for comprehensive export demonstrations.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder = GraphBuilder()\n",
"\n",
"entities = [\n",
" {\"id\": \"e1\", \"type\": \"Person\", \"name\": \"Alice\", \"properties\": {\"age\": 30}},\n",
" {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Bob\", \"properties\": {\"age\": 35}},\n",
" {\"id\": \"e3\", \"type\": \"Organization\", \"name\": \"Tech Corp\", \"properties\": {\"founded\": 2010}},\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"knows\"},\n",
" {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"works_for\"},\n",
"]\n",
"\n",
"knowledge_graph = builder.build(entities + relationships)\n",
"\n",
"embedding_generator = EmbeddingGenerator()\n",
"texts = [e[\"name\"] for e in entities]\n",
"embeddings = embedding_generator.generate_embeddings(texts, data_type=\"text\")\n",
"\n",
"ontology_generator = OntologyGenerator()\n",
"ontology = ontology_generator.generate_from_graph(knowledge_graph)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Export to JSON\n",
"\n",
"Export knowledge graph to JSON format using both the class and convenience function approaches.\n",
"\n",
"**JSONExporter Features:**\n",
"- Standard JSON serialization\n",
"- JSON-LD format support with @context\n",
"- Configurable indentation\n",
"- Metadata and provenance tracking\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import JSONExporter\n",
"\n",
"# Create JSON exporter with custom settings\n",
"json_exporter = JSONExporter(indent=2, include_metadata=True)\n",
"\n",
"# Export to JSON format\n",
"json_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.json\")\n",
"\n",
"# Export to JSON-LD format\n",
"json_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.jsonld\", format=\"json-ld\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Export to RDF\n",
"\n",
"Export knowledge graph to multiple RDF formats (Turtle, RDF/XML, JSON-LD, N-Triples).\n",
"\n",
"**RDFExporter Features:**\n",
"- Multiple RDF format support (Turtle, RDF/XML, JSON-LD, N-Triples, N3)\n",
"- Namespace management\n",
"- RDF validation\n",
"- Format conversion capabilities\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import RDFExporter, RDFSerializer, RDFValidator\n",
"\n",
"# Create RDF exporter\n",
"rdf_exporter = RDFExporter()\n",
"\n",
"# Export to Turtle format (human-readable)\n",
"rdf_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.ttl\", format=\"turtle\")\n",
"\n",
"# Export to RDF/XML format\n",
"rdf_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.rdf\", format=\"rdfxml\")\n",
"\n",
"# Export to JSON-LD format\n",
"rdf_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.jsonld\", format=\"jsonld\")\n",
"\n",
"# Export to N-Triples format\n",
"rdf_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.nt\", format=\"ntriples\")\n",
"\n",
"# Using RDFSerializer for format conversion\n",
"serializer = RDFSerializer()\n",
"rdf_data = serializer.convert_kg_to_rdf(knowledge_graph)\n",
"turtle_string = serializer.serialize_to_turtle(rdf_data)\n",
"\n",
"# Using RDFValidator for validation\n",
"validator = RDFValidator()\n",
"validation_result = validator.validate_rdf_syntax(turtle_string, format=\"turtle\")\n",
"print(f\"RDF validation: {validation_result.get('is_valid', False)}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Export to CSV\n",
"\n",
"Export knowledge graph to CSV format for tabular analysis.\n",
"\n",
"**CSVExporter Features:**\n",
"- Separate files for entities and relationships\n",
"- Configurable delimiter (comma, tab, semicolon)\n",
"- Automatic header generation\n",
"- Metadata serialization\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import CSVExporter\n",
"\n",
"# Create CSV exporter with custom delimiter\n",
"csv_exporter = CSVExporter(delimiter=\",\")\n",
"\n",
"# Export complete knowledge graph\n",
"csv_exporter.export_knowledge_graph(knowledge_graph, \"exports/output\")\n",
"\n",
"# Export entities separately\n",
"entities = knowledge_graph.get(\"entities\", [])\n",
"csv_exporter.export_entities(entities, \"exports/entities.csv\")\n",
"\n",
"# Export relationships separately\n",
"relationships = knowledge_graph.get(\"relationships\", [])\n",
"csv_exporter.export_relationships(relationships, \"exports/relationships.csv\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Export to Graph Formats (GraphML, GEXF, DOT)\n",
"\n",
"Export knowledge graph to graph formats for visualization tools.\n",
"\n",
"**GraphExporter Features:**\n",
"- GraphML format (Cytoscape, yEd)\n",
"- GEXF format (Gephi)\n",
"- DOT format (Graphviz)\n",
"- Node and edge attribute mapping\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import GraphExporter\n",
"\n",
"# Create graph exporter\n",
"graph_exporter = GraphExporter()\n",
"\n",
"# Export to GraphML format (for Cytoscape, yEd)\n",
"graph_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.graphml\", format=\"graphml\")\n",
"\n",
"# Export to GEXF format (for Gephi)\n",
"graph_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.gexf\", format=\"gexf\")\n",
"\n",
"# Export to DOT format (for Graphviz)\n",
"graph_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.dot\", format=\"dot\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Export to OWL\n",
"\n",
"Export ontology to OWL format. **Note:** OWLExporter expects an ontology structure, not a knowledge graph.\n",
"\n",
"**OWLExporter Features:**\n",
"- OWL/XML format\n",
"- OWL in Turtle format\n",
"- Class hierarchy export\n",
"- Property definition export\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import OWLExporter\n",
"\n",
"# Create OWL exporter with custom URI and version\n",
"owl_exporter = OWLExporter(ontology_uri=\"https://example.org/ontology/\", version=\"1.0\")\n",
"\n",
"# Export complete ontology to OWL/XML\n",
"owl_exporter.export(ontology, \"exports/output.owl\", format=\"owl-xml\")\n",
"\n",
"# Export to OWL in Turtle format\n",
"owl_exporter.export(ontology, \"exports/output_owl.ttl\", format=\"turtle\")\n",
"\n",
"# Export only classes\n",
"classes = ontology.get(\"classes\", [])\n",
"owl_exporter.export_classes(classes, \"exports/classes.owl\")\n",
"\n",
"# Export only properties\n",
"properties = ontology.get(\"object_properties\", [])\n",
"owl_exporter.export_properties(properties, \"exports/properties.owl\", property_type=\"object\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7: Export to Vector Formats\n",
"\n",
"Export vector embeddings to various formats for vector stores.\n",
"\n",
"**VectorExporter Features:**\n",
"- JSON format\n",
"- NumPy format\n",
"- Binary format\n",
"- FAISS format\n",
"- Vector store integration (Pinecone, Weaviate, Qdrant)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import VectorExporter\n",
"\n",
"# Create vector exporter\n",
"vector_exporter = VectorExporter()\n",
"\n",
"# Export to JSON format\n",
"vector_exporter.export(embeddings, \"exports/output_vectors.json\", format=\"json\")\n",
"\n",
"# Export to NumPy format\n",
"vector_exporter.export(embeddings, \"exports/output_vectors.npy\", format=\"numpy\")\n",
"\n",
"# Export to Binary format\n",
"vector_exporter.export(embeddings, \"exports/output_vectors.bin\", format=\"binary\")\n",
"\n",
"# Export to FAISS format\n",
"vector_exporter.export(embeddings, \"exports/output_vectors.faiss\", format=\"faiss\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8: Export to LPG (Labeled Property Graph)\n",
"\n",
"Export knowledge graph to LPG format for graph databases like Neo4j and Memgraph.\n",
"\n",
"**LPGExporter Features:**\n",
"- Cypher query format\n",
"- Labeled Property Graph format\n",
"- Batch node/relationship export\n",
"- Index generation\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import LPGExporter\n",
"\n",
"# Create LPG exporter\n",
"lpg_exporter = LPGExporter()\n",
"\n",
"# Export to Cypher format (for Neo4j, Memgraph)\n",
"lpg_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.cypher\", format=\"cypher\")\n",
"\n",
"# Export to LPG format\n",
"lpg_exporter.export_knowledge_graph(knowledge_graph, \"exports/output.lpg\", format=\"lpg\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 9: Export to YAML\n",
"\n",
"Export semantic networks and schemas to YAML format.\n",
"\n",
"**YAML Exporter Features:**\n",
"- Semantic network YAML export\n",
"- Schema YAML export\n",
"- Human-readable format\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import SemanticNetworkYAMLExporter, YAMLSchemaExporter\n",
"\n",
"# Using SemanticNetworkYAMLExporter for knowledge graphs\n",
"yaml_exporter = SemanticNetworkYAMLExporter()\n",
"yaml_exporter.export(knowledge_graph, \"exports/output_network.yaml\")\n",
"\n",
"# Using YAMLSchemaExporter for ontology schemas\n",
"schema_exporter = YAMLSchemaExporter()\n",
"yaml_content = schema_exporter.export_ontology_schema(ontology)\n",
"with open(\"exports/output_schema.yaml\", \"w\") as f:\n",
" f.write(yaml_content)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 10: Generate Reports\n",
"\n",
"Generate professional reports in multiple formats using ReportGenerator.\n",
"\n",
"**ReportGenerator Features:**\n",
"- HTML reports with styling\n",
"- Markdown reports\n",
"- JSON reports\n",
"- Plain text reports\n",
"- Quality metrics aggregation\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import ReportGenerator\n",
"\n",
"# Prepare report data\n",
"report_data = {\n",
" \"title\": \"Knowledge Graph Export Report\",\n",
" \"summary\": \"Comprehensive export of knowledge graph to multiple formats\",\n",
" \"knowledge_graph\": {\n",
" \"entities\": len(knowledge_graph.get(\"entities\", [])),\n",
" \"relationships\": len(knowledge_graph.get(\"relationships\", []))\n",
" },\n",
" \"formats_exported\": [\"JSON\", \"RDF\", \"CSV\", \"GraphML\", \"GEXF\", \"OWL\", \"Vector\", \"LPG\", \"YAML\"],\n",
" \"export_timestamp\": \"2024-01-01T00:00:00Z\"\n",
"}\n",
"\n",
"# Create report generator\n",
"report_generator = ReportGenerator()\n",
"\n",
"# Generate HTML report\n",
"report_generator.generate_report(report_data, \"exports/report.html\", format=\"html\")\n",
"\n",
"# Generate Markdown report\n",
"report_generator.generate_report(report_data, \"exports/report.md\", format=\"markdown\")\n",
"\n",
"# Generate JSON report\n",
"report_generator.generate_report(report_data, \"exports/report.json\", format=\"json\")\n",
"\n",
"# Generate Text report\n",
"report_generator.generate_report(report_data, \"exports/report.txt\", format=\"text\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 11: Method Registry and Custom Methods\n",
"\n",
"Register and use custom export methods with the MethodRegistry system.\n",
"\n",
"**MethodRegistry Features:**\n",
"- Register custom export methods\n",
"- List available methods\n",
"- Get methods by name\n",
"- Unregister methods\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import MethodRegistry, method_registry, JSONExporter\n",
"\n",
"# Define a custom export method\n",
"def custom_json_export(data, file_path, **kwargs):\n",
" \"\"\"Custom JSON export with additional formatting.\"\"\"\n",
" import json\n",
" with open(file_path, 'w') as f:\n",
" json.dump(data, f, indent=4, sort_keys=True)\n",
" print(f\"Custom export completed: {file_path}\")\n",
"\n",
"# Register custom method\n",
"MethodRegistry.register(\"json\", \"custom_formatted\", custom_json_export)\n",
"\n",
"# List all available methods\n",
"all_methods = method_registry.list_all()\n",
"print(\"Available methods:\", all_methods)\n",
"\n",
"# List methods for specific task\n",
"json_methods = method_registry.list_all(\"json\")\n",
"print(\"JSON methods:\", json_methods)\n",
"\n",
"# Use registered method with JSONExporter\n",
"json_exporter = JSONExporter()\n",
"# The custom method can be used via the registry system\n",
"custom_method = method_registry.get(\"json\", \"custom_formatted\")\n",
"if custom_method:\n",
" custom_method(knowledge_graph, \"exports/custom_output.json\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 12: Configuration Management\n",
"\n",
"Configure export settings using ExportConfig.\n",
"\n",
"**ExportConfig Features:**\n",
"- Environment variable support\n",
"- Config file support\n",
"- Programmatic configuration\n",
"- Method-specific configuration\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import ExportConfig, export_config, JSONExporter\n",
"\n",
"# Get current configuration\n",
"config = export_config.get(\"default\")\n",
"print(\"Default config:\", config)\n",
"\n",
"# Set configuration programmatically\n",
"export_config.set(\"json\", {\"indent\": 4, \"include_metadata\": True})\n",
"export_config.set(\"rdf\", {\"format\": \"turtle\", \"base_uri\": \"https://example.org/\"})\n",
"\n",
"# Get method-specific configuration\n",
"json_config = export_config.get_method_config(\"json\")\n",
"print(\"JSON config:\", json_config)\n",
"\n",
"# Set method-specific configuration\n",
"export_config.set_method_config(\"csv\", {\"delimiter\": \"\\t\"})\n",
"\n",
"# Use configured settings\n",
"json_exporter = JSONExporter(**export_config.get_method_config(\"json\"))\n",
"json_exporter.export_knowledge_graph(knowledge_graph, \"exports/output_configured.json\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## Step 13: Verify All Exports\n",
"\n",
"Verify that all exported files were created successfully.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# List all exported files\n",
"export_files = [\n",
" \"exports/output.json\",\n",
" \"exports/output.jsonld\",\n",
" \"exports/output.ttl\",\n",
" \"exports/output.rdf\",\n",
" \"exports/output.nt\",\n",
" \"exports/output.csv\",\n",
" \"exports/entities.csv\",\n",
" \"exports/relationships.csv\",\n",
" \"exports/output.graphml\",\n",
" \"exports/output.gexf\",\n",
" \"exports/output.dot\",\n",
" \"exports/output.owl\",\n",
" \"exports/output_owl.ttl\",\n",
" \"exports/output_vectors.json\",\n",
" \"exports/output_vectors.npy\",\n",
" \"exports/output.cypher\",\n",
" \"exports/output_network.yaml\",\n",
" \"exports/output_schema.yaml\",\n",
" \"exports/report.html\",\n",
" \"exports/report.md\",\n",
" \"exports/report.json\",\n",
" \"exports/report.txt\"\n",
"]\n",
"\n",
"print(\"📊 Export Summary:\")\n",
"print(\"=\" * 60)\n",
"for file in export_files:\n",
" if os.path.exists(file):\n",
" size = os.path.getsize(file)\n",
" print(f\"✅ {file:50} ({size:>10,} bytes)\")\n",
" else:\n",
" print(f\"❌ {file:50} (not found)\")\n",
"\n",
"print(\"=\" * 60)\n",
"print(f\"Total files checked: {len(export_files)}\")\n",
"print(f\"Files created: {sum(1 for f in export_files if os.path.exists(f))}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,210 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/06_Multi_Source_Data_Integration.ipynb)\n",
"\n",
"# 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",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/ingest/)\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",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## Workflow: 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, ProvenanceTracker\n",
"from semantica.conflicts import ConflictDetector\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
}
@@ -0,0 +1,233 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/07_Pipeline_Orchestration.ipynb)\n",
"\n",
"# Pipeline Orchestration\n",
"\n",
"## Overview\n",
"\n",
"Build complex pipelines, execute them, handle failures, enable parallel processing, and monitor execution.\n",
"\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/pipeline/)\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"## Workflow: Build Pipelines → Execute → Handle Failures → Parallel Processing → Monitor\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.pipeline import (\n",
" PipelineBuilder,\n",
" ExecutionEngine,\n",
" FailureHandler,\n",
" ParallelismManager,\n",
" RetryPolicy,\n",
" RetryStrategy\n",
")\n",
"from semantica.ingest import FileIngestor\n",
"from semantica.parse import DocumentParser\n",
"from semantica.semantic_extract import NERExtractor\n",
"from semantica.kg import GraphBuilder\n",
"import time\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Build Complex Pipelines\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder = PipelineBuilder()\n",
"\n",
"file_ingestor = FileIngestor()\n",
"document_parser = DocumentParser()\n",
"ner_extractor = NERExtractor()\n",
"graph_builder = GraphBuilder()\n",
"\n",
"# Define handlers for each pipeline step\n",
"def ingest_handler(data, **config):\n",
" files = data.get(\"files\", [])\n",
" if files:\n",
" # Ingest first file as example\n",
" file_obj = file_ingestor.ingest_file(files[0], read_content=True)\n",
" return {**data, \"file\": file_obj}\n",
" return data\n",
"\n",
"def parse_handler(data, **config):\n",
" # If a file was ingested, try parsing; otherwise pass text through\n",
" file_obj = data.get(\"file\")\n",
" if file_obj and getattr(file_obj, \"path\", None):\n",
" parsed = document_parser.parse_document(file_obj.path)\n",
" text = parsed.get(\"text\") if isinstance(parsed, dict) else None\n",
" return {**data, \"text\": text or data.get(\"text\")}\n",
" return data\n",
"\n",
"def extract_handler(data, **config):\n",
" text = data.get(\"text\", \"\")\n",
" entities = ner_extractor.extract_entities(text)\n",
" # Normalize to dict list for graph builder\n",
" entity_dicts = [\n",
" {\"id\": f\"e{i}\", \"name\": e.text, \"type\": e.label} for i, e in enumerate(entities)\n",
" ]\n",
" return {**data, \"entities\": entity_dicts}\n",
"\n",
"def build_graph_handler(data, **config):\n",
" entities = data.get(\"entities\", [])\n",
" graph = graph_builder.build({\"entities\": entities})\n",
" return {**data, \"graph\": graph}\n",
"\n",
"# Build pipeline with proper handlers and dependencies\n",
"pipeline = (\n",
" builder\n",
" .add_step(\"ingest\", \"ingest\", handler=ingest_handler)\n",
" .add_step(\"parse\", \"parse\", dependencies=[\"ingest\"], handler=parse_handler)\n",
" .add_step(\"extract\", \"extract\", dependencies=[\"parse\"], handler=extract_handler)\n",
" .add_step(\"build_graph\", \"build_graph\", dependencies=[\"extract\"], handler=build_graph_handler)\n",
").build()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Execute Pipeline\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"engine = ExecutionEngine()\n",
"\n",
"input_data = {\n",
" \"text\": \"Alice works at Tech Corp. Bob is a friend of Alice.\",\n",
" \"files\": []\n",
"}\n",
"\n",
"start_time = time.time()\n",
"result = engine.execute_pipeline(pipeline, input_data)\n",
"execution_time = result.metrics.get(\"execution_time\", time.time() - start_time)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Handle Failures\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Configure retry policy for the 'extract' step type\n",
"engine.failure_handler.set_retry_policy(\n",
" \"extract\",\n",
" RetryPolicy(max_retries=3, backoff_factor=2.0, strategy=RetryStrategy.EXPONENTIAL)\n",
")\n",
"\n",
"result = engine.execute_pipeline(pipeline, input_data)\n",
"print(\"Pipeline executed with retry policy configured\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Parallel Processing\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"parallelism = ParallelismManager(max_workers=4)\n",
"\n",
"# Identify groups of steps that can run in parallel\n",
"groups = parallelism.identify_parallelizable_steps(pipeline)\n",
"\n",
"# Execute first parallelizable group as a demonstration\n",
"start_time = time.time()\n",
"parallel_results = []\n",
"for group in groups:\n",
" parallel_results.extend(parallelism.execute_pipeline_steps_parallel(group, input_data, max_workers=4))\n",
"parallel_time = time.time() - start_time\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Monitor Pipeline Execution\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Metrics from execution engine\n",
"metrics = result.metrics\n",
"progress = engine.get_progress(pipeline.name)\n",
"\n",
"print(f\"Duration: {metrics.get('execution_time', 0):.2f} seconds\")\n",
"print(f\"Steps Executed: {metrics.get('steps_executed', 0)}\")\n",
"print(f\"Steps Failed: {metrics.get('steps_failed', 0)}\")\n",
"print(f\"Progress: {progress.get('progress_percentage', 0):.1f}% (status: {progress.get('status')})\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Pipeline orchestration workflow:\n",
"- Complex Pipeline Built\n",
"- Pipeline Executed\n",
"- Failure Handling Configured\n",
"- Parallel Processing Enabled\n",
"- Full Monitoring and Observability\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,336 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/08_Reasoning_and_Inference.ipynb)\n",
"\n",
"# Reasoning and Inference\n",
"\n",
"## Overview\n",
"\n",
"Build knowledge graphs, define rules, perform forward/backward chaining, and generate explanations for AI reasoning using the **Semantica Reasoning Module**.\n",
"\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/reasoning/)\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"## Workflow: Build KG → Define Rules → Forward/Backward Chaining → Generate Explanations\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.kg import GraphBuilder\n",
"from semantica.reasoning import InferenceEngine, RuleManager, ExplanationGenerator\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Build Knowledge Graph\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder = GraphBuilder()\n",
"\n",
"entities = [\n",
" {\"id\": \"alice\", \"type\": \"Person\", \"name\": \"Alice\"},\n",
" {\"id\": \"bob\", \"type\": \"Person\", \"name\": \"Bob\"},\n",
" {\"id\": \"charlie\", \"type\": \"Person\", \"name\": \"Charlie\"},\n",
" {\"id\": \"sf\", \"type\": \"Location\", \"name\": \"San Francisco\"},\n",
" {\"id\": \"california\", \"type\": \"Location\", \"name\": \"California\"},\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"alice\", \"target\": \"bob\", \"type\": \"parent_of\"},\n",
" {\"source\": \"bob\", \"target\": \"charlie\", \"type\": \"parent_of\"},\n",
" {\"source\": \"sf\", \"target\": \"california\", \"type\": \"located_in\"},\n",
" {\"source\": \"alice\", \"target\": \"sf\", \"type\": \"lives_in\"},\n",
"]\n",
"\n",
"knowledge_graph = builder.build([{\"entities\": entities, \"relationships\": relationships}])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Define Rules\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Initialize Inference Engine\n",
"engine = InferenceEngine()\n",
"\n",
"# Define rules using logic syntax\n",
"rules = [\n",
" \"IF parent_of(?a, ?b) AND parent_of(?b, ?c) THEN grandparent_of(?a, ?c)\",\n",
" \"IF lives_in(?x, ?y) AND located_in(?y, ?z) THEN lives_in(?x, ?z)\"\n",
"]\n",
"\n",
"for rule in rules:\n",
" engine.add_rule(rule)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Forward Chaining\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Load facts from relationships into the engine\n",
"for rel in relationships:\n",
" # Format: predicate(subject, object)\n",
" fact_str = f\"{rel['type']}({rel['source']}, {rel['target']})\"\n",
" engine.add_fact(fact_str)\n",
"\n",
"# Perform forward chaining to derive new facts\n",
"results = engine.forward_chain()\n",
"\n",
"print(f\"Inferred {len(results)} new facts:\")\n",
"for result in results:\n",
" print(f\" - {result.conclusion} (Rule: {result.rule_used.name})\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Backward Chaining\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Define a goal to prove\n",
"goal = \"grandparent_of(alice, charlie)\"\n",
"\n",
"# Perform backward chaining\n",
"proof = engine.backward_chain(goal)\n",
"\n",
"if proof:\n",
" print(f\"Goal '{goal}' proven successfully!\")\n",
"else:\n",
" print(f\"Could not prove goal '{goal}'.\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Generate Explanations\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"generator = ExplanationGenerator()\n",
"\n",
"# Explain the last forward chaining inference\n",
"if results:\n",
" explanation = generator.generate_explanation(results[0])\n",
" print(\"Explanation for first inferred fact:\")\n",
" print(explanation.natural_language)\n",
"\n",
"# If we have a proof from backward chaining, explain it\n",
"if proof:\n",
" proof_explanation = generator.generate_explanation(proof)\n",
" print(\"\\nExplanation for backward chaining proof:\")\n",
" print(proof_explanation.natural_language)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Reasoning and inference workflow:\n",
"- Knowledge Graph Built\n",
"- Inference Rules Defined\n",
"- Facts Loaded into Engine\n",
"- Forward Chaining Performed\n",
"- Backward Chaining Performed\n",
"- Explanations Generated\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"### Deep Dive: Reasoning Module\n",
"\n",
"This section provides an in-depth guide to Semantica's reasoning capabilities. Learn rule syntax, fact formats, chaining strategies, and explanation generation with robust, reproducible examples.\n",
"\n",
"**What you'll practice**\n",
"- Defining rules with variables and predicates\n",
"- Loading facts in predicate form\n",
"- Running forward and backward chaining\n",
"- Generating human-readable explanations\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.kg import GraphBuilder\n",
"from semantica.reasoning import InferenceEngine, ExplanationGenerator\n",
"\n",
"builder = GraphBuilder()\n",
"engine = InferenceEngine()\n",
"explainer = ExplanationGenerator()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Rule Syntax\n",
"\n",
"Rules use predicate logic with variables prefixed by `?`.\n",
"\n",
"- Example: `IF parent_of(?a, ?b) AND parent_of(?b, ?c) THEN grandparent_of(?a, ?c)`\n",
"- Variables unify across predicates in the same rule\n",
"- Conclusions are added as new facts when conditions match\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"entities = [\n",
" {\"id\": \"alice\", \"type\": \"Person\", \"name\": \"Alice\"},\n",
" {\"id\": \"bob\", \"type\": \"Person\", \"name\": \"Bob\"},\n",
" {\"id\": \"charlie\", \"type\": \"Person\", \"name\": \"Charlie\"},\n",
" {\"id\": \"sf\", \"type\": \"Location\", \"name\": \"San Francisco\"},\n",
" {\"id\": \"california\", \"type\": \"Location\", \"name\": \"California\"}\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"alice\", \"target\": \"bob\", \"type\": \"parent_of\"},\n",
" {\"source\": \"bob\", \"target\": \"charlie\", \"type\": \"parent_of\"},\n",
" {\"source\": \"sf\", \"target\": \"california\", \"type\": \"located_in\"},\n",
" {\"source\": \"alice\", \"target\": \"sf\", \"type\": \"lives_in\"}\n",
"]\n",
"\n",
"knowledge_graph = builder.build([{\"entities\": entities, \"relationships\": relationships}])\n",
"print(len(knowledge_graph.get(\"entities\", [])))\n",
"print(len(knowledge_graph.get(\"relationships\", [])))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rules = [\n",
" \"IF parent_of(?a, ?b) AND parent_of(?b, ?c) THEN grandparent_of(?a, ?c)\",\n",
" \"IF lives_in(?x, ?y) AND located_in(?y, ?z) THEN lives_in(?x, ?z)\"\n",
"]\n",
"for r in rules:\n",
" engine.add_rule(r)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for rel in relationships:\n",
" fact = f\"{rel['type']}({rel['source']}, {rel['target']})\"\n",
" engine.add_fact(fact)\n",
"\n",
"derived = engine.forward_chain()\n",
"print(len(derived))\n",
"for d in derived:\n",
" print(d.conclusion)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"goals = [\n",
" \"grandparent_of(alice, charlie)\",\n",
" \"lives_in(alice, california)\"\n",
"]\n",
"for g in goals:\n",
" proof = engine.backward_chain(g)\n",
" print(g)\n",
" print(bool(proof))\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if derived:\n",
" exp = explainer.generate_explanation(derived[0])\n",
" print(exp.natural_language)\n",
"\n",
"goal = \"grandparent_of(alice, charlie)\"\n",
"proof = engine.backward_chain(goal)\n",
"if proof:\n",
" pexp = explainer.generate_explanation(proof)\n",
" print(pexp.natural_language)\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,200 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/09_Semantic_Layer_Construction.ipynb)\n",
"\n",
"# Semantic Layer Construction\n",
"\n",
"## Overview\n",
"\n",
"Build an enterprise semantic layer: construct knowledge graph, generate ontology, create semantic layer, export RDF, and store in triple store.\n",
"\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/concepts/)\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"## Workflow: Build KG → Generate Ontology → Create Semantic Layer → Export RDF → Triple Store\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.kg import GraphBuilder\n",
"from semantica.ontology import OntologyGenerator\n",
"from semantica.export import RDFExporter\n",
"from semantica.triple_store import TripleStore\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Build Knowledge Graph\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder = GraphBuilder()\n",
"\n",
"entities = [\n",
" {\"id\": \"e1\", \"type\": \"Person\", \"name\": \"Alice\", \"properties\": {\"age\": 30, \"role\": \"Engineer\"}},\n",
" {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Bob\", \"properties\": {\"age\": 35, \"role\": \"Manager\"}},\n",
" {\"id\": \"e3\", \"type\": \"Organization\", \"name\": \"Tech Corp\", \"properties\": {\"founded\": 2010}},\n",
" {\"id\": \"e4\", \"type\": \"Project\", \"name\": \"Project Alpha\", \"properties\": {\"status\": \"active\"}},\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"reports_to\"},\n",
" {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"works_for\"},\n",
" {\"source\": \"e2\", \"target\": \"e3\", \"type\": \"works_for\"},\n",
" {\"source\": \"e1\", \"target\": \"e4\", \"type\": \"works_on\"},\n",
"]\n",
"\n",
"knowledge_graph = builder.build(entities, relationships)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Generate Ontology\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"generator = OntologyGenerator()\n",
"ontology = generator.generate_from_graph(knowledge_graph)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Create Semantic Layer\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def create_mappings(kg, ontology):\n",
" mappings = {\n",
" \"entity_type_mappings\": {},\n",
" \"relationship_type_mappings\": {},\n",
" \"property_mappings\": {}\n",
" }\n",
" \n",
" entity_types = set(e.get(\"type\") for e in entities)\n",
" ontology_classes = ontology.get(\"classes\", [])\n",
" \n",
" for entity_type in entity_types:\n",
" matching_class = next((cls for cls in ontology_classes if cls.get(\"name\") == entity_type), None)\n",
" if matching_class:\n",
" mappings[\"entity_type_mappings\"][entity_type] = matching_class.get(\"uri\", entity_type)\n",
" \n",
" relationship_types = set(r.get(\"type\") for r in relationships)\n",
" ontology_properties = ontology.get(\"properties\", [])\n",
" \n",
" for rel_type in relationship_types:\n",
" matching_prop = next((prop for prop in ontology_properties if prop.get(\"name\") == rel_type), None)\n",
" if matching_prop:\n",
" mappings[\"relationship_type_mappings\"][rel_type] = matching_prop.get(\"uri\", rel_type)\n",
" \n",
" return mappings\n",
"\n",
"mappings = create_mappings(knowledge_graph, ontology)\n",
"\n",
"semantic_layer = {\n",
" \"graph\": knowledge_graph,\n",
" \"ontology\": ontology,\n",
" \"mappings\": mappings,\n",
" \"metadata\": {\n",
" \"version\": \"1.0\",\n",
" \"created_at\": \"2024-01-01\",\n",
" \"description\": \"Enterprise semantic layer\"\n",
" }\n",
"}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Export RDF\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"exporter = RDFExporter()\n",
"exporter.export(knowledge_graph, ontology, \"semantic_layer.rdf\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Store in Triple Store\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triple_store = TripleStore()\n",
"triple_store.store(knowledge_graph, ontology)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Enterprise semantic layer construction:\n",
"- Knowledge Graph Built\n",
"- Ontology Generated\n",
"- Semantic Layer Created with Mappings\n",
"- RDF Export Completed\n",
"- Triple Store Storage Completed\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,230 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/10_Temporal_Knowledge_Graphs.ipynb)\n",
"\n",
"# Temporal Knowledge Graphs\n",
"\n",
"## Overview\n",
"\n",
"This notebook demonstrates advanced temporal knowledge graph capabilities using TemporalGraphQuery, TemporalPatternDetector, TemporalVersionManager, and TemporalVisualizer.\n",
"\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/kg/)\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",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## Workflow: 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"
]
},
{
"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"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Snapshot Comparison and Version History\n",
"\n",
"Compare graph snapshots across time and visualize version history."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create multiple versions\n",
"version_manager = TemporalVersionManager()\n",
"version_2020 = version_manager.create_version(temporal_kg, timestamp=\"2020-01-01\", version_label=\"v2020\")\n",
"# Simulate changes for 2023\n",
"temporal_kg_updated = {\n",
" \"entities\": temporal_kg.get(\"entities\", []),\n",
" \"relationships\": temporal_kg.get(\"relationships\", []) + [\n",
" {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"collaborated_with\", \"valid_from\": \"2023-01-01\"}\n",
" ]\n",
"}\n",
"version_2023 = version_manager.create_version(temporal_kg_updated, timestamp=\"2023-01-01\", version_label=\"v2023\")\n",
"\n",
"# Build snapshots dict for comparison\n",
"snapshots = {\n",
" version_2020[\"timestamp\"]: version_2020,\n",
" version_2023[\"timestamp\"]: version_2023\n",
"}\n",
"\n",
"# Visualize snapshot comparison\n",
"fig_snapshots = temporal_visualizer.visualize_snapshot_comparison(snapshots, output=\"interactive\")\n",
"\n",
"# Build version history list\n",
"version_history = [\n",
" {\"version\": version_2020.get(\"label\", \"v2020\"), \"timestamp\": version_2020.get(\"timestamp\"), \"changes\": f\"Entities: {len(version_2020.get('entities', []))}, Relationships: {len(version_2020.get('relationships', []))}\"},\n",
" {\"version\": version_2023.get(\"label\", \"v2023\"), \"timestamp\": version_2023.get(\"timestamp\"), \"changes\": f\"Entities: {len(version_2023.get('entities', []))}, Relationships: {len(version_2023.get('relationships', []))}\"}\n",
"]\n",
"\n",
"# Visualize version history\n",
"fig_versions = temporal_visualizer.visualize_version_history(version_history, output=\"interactive\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,247 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/11_Advanced_Context_Engineering.ipynb)\n",
"\n",
"# Advanced Context Engineering\n",
"\n",
"## Overview\n",
"\n",
"This notebook covers advanced topics in context engineering using Semantica. We will explore custom memory management strategies, tuning hybrid retrieval, and extending the system with custom graph builders.\n",
"\n",
"### Learning Objectives\n",
"\n",
"- **Custom Memory Pruning**: Implement importance-based pruning instead of FIFO.\n",
"- **Hybrid Retrieval Tuning**: Optimize weights for vector, graph, and keyword search.\n",
"- **Custom Extensions**: Register custom graph building methods.\n",
"- **Performance Optimization**: Balance token limits and retrieval latency.\n",
"\n",
"---\n",
"\n",
"## 1. Setup\n",
"\n",
"We'll start by setting up a mock vector store and importing necessary components."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from typing import List, Dict, Any, Optional\n",
"from semantica.context import AgentMemory, AgentContext, ContextGraph, ContextRetriever, VectorStore\n",
"from semantica.context import registry\n",
"\n",
"# Mock Vector Store (same as in introduction)\n",
"class MockVectorStore(VectorStore):\n",
" def __init__(self):\n",
" self.items = {}\n",
" self.counter = 0\n",
" def add(self, texts, metadata=None, **kwargs):\n",
" ids = []\n",
" for i, text in enumerate(texts):\n",
" id_ = f\"id_{self.counter}\"\n",
" self.items[id_] = {\"text\": text, \"metadata\": metadata[i] if metadata else {}}\n",
" ids.append(id_)\n",
" self.counter += 1\n",
" return ids\n",
" def search(self, query, limit=5, **kwargs):\n",
" return [{\n",
" \"id\": k, \"content\": v[\"text\"], \"score\": 0.85, \"metadata\": v[\"metadata\"]\n",
" } for k, v in list(self.items.items())[:limit]]\n",
" def delete(self, ids, **kwargs):\n",
" return True\n",
"\n",
"vs = MockVectorStore()\n",
"kg = ContextGraph()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Custom Memory Pruning Strategy\n",
"\n",
"By default, `AgentMemory` uses a FIFO (First-In-First-Out) strategy combined with a token limit to prune short-term memory. However, you might want to keep \"important\" memories longer regardless of their age.\n",
"\n",
"Let's subclass `AgentMemory` to implement an importance-based pruning strategy."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class ImportanceAwareMemory(AgentMemory):\n",
" def _prune_short_term_memory(self):\n",
" \"\"\"\n",
" Custom pruning: Always keep items marked as 'important' in metadata,\n",
" then prune others based on token limits.\n",
" \"\"\"\n",
" if not self.short_term_memory:\n",
" return\n",
"\n",
" # Separate important items\n",
" important_items = [item for item in self.short_term_memory if item.metadata.get(\"important\")]\n",
" other_items = [item for item in self.short_term_memory if not item.metadata.get(\"important\")]\n",
" \n",
" # Calculate tokens used by important items\n",
" important_tokens = sum(self._count_tokens(item.content) for item in important_items)\n",
" \n",
" # Calculate remaining budget\n",
" remaining_tokens = max(0, self.token_limit - important_tokens)\n",
" \n",
" # Prune other items to fit remaining budget\n",
" kept_others = []\n",
" current_tokens = 0\n",
" \n",
" # Iterate in reverse (newest first) to keep recent items\n",
" for item in reversed(other_items):\n",
" item_tokens = self._count_tokens(item.content)\n",
" if current_tokens + item_tokens <= remaining_tokens:\n",
" kept_others.insert(0, item)\n",
" current_tokens += item_tokens\n",
" else:\n",
" break # Stop once we hit the limit\n",
" \n",
" # Reconstruct memory: Important items + kept recent items\n",
" # Sort by timestamp to maintain order\n",
" all_kept = sorted(important_items + kept_others, key=lambda x: x.timestamp)\n",
" self.short_term_memory = all_kept\n",
"\n",
"# Test the custom memory\n",
"memory = ImportanceAwareMemory(vector_store=vs, token_limit=100)\n",
"\n",
"# Add an old important memory\n",
"memory.store(\"IMPORTANT: User's name is Alice\", metadata={\"important\": True})\n",
"\n",
"# Fill with filler memories\n",
"for i in range(20):\n",
" memory.store(f\"Filler memory {i} \" * 5) # Consumes tokens\n",
"\n",
"print(f\"Short-term items: {len(memory.short_term_memory)}\")\n",
"print(\"First item (should be the important one):\", memory.short_term_memory[0].content)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Tuning Hybrid Retrieval\n",
"\n",
"Hybrid retrieval combines scores from vector search and graph traversal. You can tune the `hybrid_alpha` parameter to weight these components.\n",
"\n",
"- `hybrid_alpha = 0.0`: Pure Vector Search\n",
"- `hybrid_alpha = 1.0`: Pure Graph Search\n",
"- `hybrid_alpha = 0.5`: Balanced (Default)\n",
"\n",
"Additionally, `max_expansion_hops` controls how far we traverse the graph from retrieved nodes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Populate graph with some structure\n",
"kg.add_node(\"python\", \"concept\", \"Python\")\n",
"kg.add_node(\"ml\", \"concept\", \"Machine Learning\")\n",
"kg.add_edge(\"python\", \"ml\", \"used_for\")\n",
"\n",
"retriever = ContextRetriever(\n",
" memory_store=memory,\n",
" knowledge_graph=kg,\n",
" vector_store=vs,\n",
" hybrid_alpha=0.7, # Favor graph connections\n",
" max_expansion_hops=2 # Traverse deeper\n",
")\n",
"\n",
"results = retriever.retrieve(\"Python\")\n",
"for res in results:\n",
" print(f\"Source: {res.source}, Score: {res.score:.2f}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Extending with Custom Methods\n",
"\n",
"Semantica's registry system allows you to plug in custom logic. Let's register a custom graph builder that creates a star graph topology."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def star_graph_builder(center_entity, satellites, **kwargs):\n",
" \"\"\"\n",
" Builds a star graph where all satellites connect to the center.\n",
" \"\"\"\n",
" nodes = []\n",
" edges = []\n",
" \n",
" # Center node\n",
" nodes.append({\"id\": \"center\", \"label\": center_entity, \"type\": \"CENTER\"})\n",
" \n",
" for i, sat in enumerate(satellites):\n",
" sat_id = f\"sat_{i}\"\n",
" nodes.append({\"id\": sat_id, \"label\": sat, \"type\": \"SATELLITE\"})\n",
" edges.append({\"source\": \"center\", \"target\": sat_id, \"relation\": \"connects_to\"})\n",
" \n",
" return {\"nodes\": nodes, \"edges\": edges}\n",
"\n",
"# Register the method\n",
"registry.method_registry.register(\"graph\", \"star_builder\", star_graph_builder)\n",
"\n",
"# Verify registration\n",
"print(\"Available graph methods:\", registry.method_registry.list_all(\"graph\"))\n",
"\n",
"# Use it (conceptual - typically used via build_context_graph wrapper)\n",
"graph_data = star_graph_builder(\"Central Hub\", [\"Spoke 1\", \"Spoke 2\"])\n",
"print(f\"Created graph with {len(graph_data['nodes'])} nodes and {len(graph_data['edges'])} edges.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. Best Practices for Production\n",
"\n",
"1. **Token Limits**: Align `token_limit` with your LLM's context window minus the prompt template size.\n",
"2. **Vector Store**: Use a production-grade vector store (e.g., Pinecone, Weaviate, Qdrant) instead of the mock store.\n",
"3. **Asynchronous Operations**: For high-throughput systems, consider wrapping storage operations in async tasks (though the core logic is synchronous for simplicity).\n",
"4. **Entity Resolution**: Implement a robust `EntityLinker` strategy to prevent graph fragmentation (e.g., \"Alice\" vs \"Alice S.\")."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
@@ -0,0 +1,286 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/12_Unstructured_to_Ontology.ipynb)\n",
"\n",
"# Advanced: Unstructured Text to Ontology\n",
"\n",
"Welcome to the advanced guide on extracting structured ontologies from unstructured text. This notebook explores two powerful paradigms available in Semantica:\n",
"\n",
"1. **Classical NLP Pipeline**: Using Named Entity Recognition (NER) and Relation Extraction.\n",
"2. **Generative AI Pipeline**: Using Large Language Models (LLMs) for direct conceptual modeling.\n",
"\n",
"We will compare both approaches, visualize the results, and validate the generated ontologies.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/ontology/)\n",
"\n",
"## Setup and Installation\n",
"\n",
"Ensure you have Semantica installed with all dependencies."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# !pip install semantica[all]\n",
"\n",
"from semantica.utils.logging import get_logger\n",
"\n",
"logger = get_logger(\"unstructured_guide\")\n",
"print(\"Environment setup complete.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## The Input Text\n",
"\n",
"We will use a rich paragraph of text describing a technology company to test both extraction methods."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"text_corpus = \"\"\"\n",
"QuantumDynamics is a leading AI research lab founded by Dr. Elena Rostova in 2018. \n",
"The lab is headquartered in Zurich, Switzerland, and focuses on quantum computing algorithms. \n",
"Dr. Rostova serves as the Chief Scientist. \n",
"The lab has released products like the Q-1 Processor and the NeuralBridge SDK. \n",
"QuantumDynamics collaborates with major universities such as MIT and ETH Zurich.\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Approach 1: The Classical NLP Pipeline\n",
"\n",
"This approach builds the ontology from the bottom up:\n",
"1. **Extract Entities**: Identify nouns/proper nouns (e.g., \"QuantumDynamics\", \"Zurich\").\n",
"2. **Extract Relations**: Identify verbs connecting them (e.g., \"headquartered in\").\n",
"3. **Generate Ontology**: Map these triplets to Classes and Properties.\n",
"\n",
"**Pros**: Deterministic, traceable, works offline.\n",
"**Cons**: Dependent on the underlying NLP model's vocabulary and flexibility."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import NERExtractor, RelationExtractor\n",
"from semantica.ontology import OntologyGenerator, OntologyOptimizer\n",
"\n",
"# 1. Initialize Extractors\n",
"ner = NERExtractor()\n",
"re = RelationExtractor()\n",
"\n",
"# 2. Extract Entities\n",
"print(\"Extracting entities...\")\n",
"entities = ner.extract(text_corpus)\n",
"print(f\"Found {len(entities)} entities: {[e['text'] for e in entities]}\")\n",
"\n",
"# 3. Extract Relationships\n",
"print(\"Extracting relationships...\")\n",
"relationships = re.extract(text_corpus, entities)\n",
"for r in relationships:\n",
" print(f\" - {r['source']} -> {r['type']} -> {r['target']}\")\n",
"\n",
"# 4. Generate Structure\n",
"generator = OntologyGenerator()\n",
"nlp_ontology = generator.generate(entities, relationships, name=\"QuantumOntologyNLP\")\n",
"\n",
"# 5. Optimize (Clean up)\n",
"optimizer = OntologyOptimizer()\n",
"nlp_ontology = optimizer.optimize_ontology(nlp_ontology, remove_redundancy=True)\n",
"\n",
"print(f\"\\nGenerated NLP Ontology with {len(nlp_ontology['classes'])} classes and {len(nlp_ontology['properties'])} properties.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Approach 2: The Generative AI Pipeline (LLM)\n",
"\n",
"This approach uses a Large Language Model to \"read\" the text and directly propose a schema.\n",
"\n",
"**Pros**: Context-aware, can handle ambiguity, generates human-like class names.\n",
"**Cons**: Non-deterministic, requires API access.\n",
"\n",
"*Note: This step requires a configured LLM provider (e.g., OpenAI).* "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ontology import LLMOntologyGenerator\n",
"\n",
"try:\n",
" # Initialize LLM Generator (ensure OPENAI_API_KEY is set in env)\n",
" llm_gen = LLMOntologyGenerator(provider=\"openai\", model=\"gpt-4\")\n",
" \n",
" print(\"Generating ontology with LLM...\")\n",
" llm_ontology = llm_gen.generate_ontology_from_text(\n",
" text=text_corpus,\n",
" name=\"QuantumOntologyLLM\"\n",
" )\n",
" \n",
" print(f\"Generated LLM Ontology with {len(llm_ontology['classes'])} classes and {len(llm_ontology['properties'])} properties.\")\n",
" print(\"Classes detected:\", [c['name'] for c in llm_ontology['classes']])\n",
" \n",
"except Exception as e:\n",
" print(f\"Skipping LLM generation: {e}\")\n",
" llm_ontology = None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Comparing Results with Visualization\n",
"\n",
"Let's visualize both ontologies side-by-side (if available) to see the difference in structure. The NLP model tends to be more literal, while the LLM model tends to be more conceptual."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.visualization import OntologyVisualizer\n",
"\n",
"visualizer = OntologyVisualizer()\n",
"\n",
"print(\"--- NLP Approach Visualization ---\")\n",
"fig_nlp = visualizer.visualize_structure(nlp_ontology, output=\"interactive\")\n",
"if fig_nlp: fig_nlp.show()\n",
"\n",
"if llm_ontology:\n",
" print(\"--- LLM Approach Visualization ---\")\n",
" fig_llm = visualizer.visualize_structure(llm_ontology, output=\"interactive\")\n",
" if fig_llm: fig_llm.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Validation\n",
"\n",
"No matter the method, validation is crucial. We check for structural integrity and logical consistency."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ontology import OntologyValidator\n",
"\n",
"validator = OntologyValidator()\n",
"\n",
"def print_report(name, ont):\n",
" if not ont: return\n",
" res = validator.validate_ontology(ont)\n",
" print(f\"[{name}] Valid: {res.valid}, Errors: {len(res.errors)}\")\n",
" if res.metrics:\n",
" print(f\" Depth: {res.metrics.get('hierarchy_depth')}, Concepts: {res.metrics.get('class_count')}\")\n",
"\n",
"print_report(\"Classical NLP\", nlp_ontology)\n",
"print_report(\"Generative AI\", llm_ontology)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Export to OWL\n",
"\n",
"Finally, we choose the best model (or merge them using `ReuseManager`, covered in other guides) and export it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import OWLExporter\n",
"\n",
"exporter = OWLExporter()\n",
"\n",
"# Export the NLP ontology by default, or the LLM one if preferred\n",
"target_ontology = llm_ontology if llm_ontology else nlp_ontology\n",
"\n",
"output_file = \"quantum_ontology.ttl\"\n",
"exporter.export(target_ontology, output_file, format=\"turtle\")\n",
"print(f\"Successfully exported ontology to {output_file}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"You have learned to:\n",
"1. **Extract Ontologies Programmatically**: Using `NERExtractor` for reliable, data-driven modeling.\n",
"2. **Generate Ontologies with AI**: Using `LLMOntologyGenerator` for conceptual, high-level modeling.\n",
"3. **Visualize and Compare**: Using `OntologyVisualizer` to inspect the structural differences.\n",
"4. **Validate and Export**: Ensuring quality before saving to OWL standards."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
-211
View File
@@ -1,211 +0,0 @@
{
"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
}
@@ -1,179 +0,0 @@
{
"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
}
@@ -0,0 +1,371 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/advanced/Advanced_Vector_Store_and_Search.ipynb)\n",
"\n",
"# Advanced Vector Store - Made Easy\n",
"\n",
"## What You'll Learn\n",
"\n",
"This notebook shows you **practical ways** to use vector stores in real applications. Each example is simple and ready to use.\n",
"\n",
"### Topics\n",
"\n",
"1. **Choosing the Right Index** - Which one to use and when\n",
"2. **Smart Filtering** - Find exactly what you need\n",
"3. **Combining Results** - Merge searches from different sources\n",
"4. **Organizing Data** - Keep different users' data separate\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 0: Setup Embeddings\n",
"\n",
"First, let's select our embedding provider and model. Semantica supports multiple providers like Sentence Transformers and FastEmbed.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.embeddings import TextEmbedder\n",
"\n",
"# Choose provider and model\n",
"embedder = TextEmbedder(method=\"fastembed\", model_name=\"BAAI/bge-small-en-v1.5\")\n",
"dimension = embedder.get_embedding_dimension()\n",
"\n",
"print(f\"Selected model: {embedder.get_model_info()['model_name']}\")\n",
"print(f\"Embedding dimension: {dimension}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 1: Choosing the Right Index\n",
"\n",
"Think of an index like choosing a filing system:\n",
"- **Flat**: Like a small notebook - slow but perfect\n",
"- **HNSW**: Like a well-organized library - fast and accurate\n",
"- **IVF**: Like a warehouse with sections - very fast for huge collections\n",
"\n",
"### Simple Rule\n",
"- Less than 10,000 items? Use **Flat**\n",
"- Between 10,000 and 1 million? Use **HNSW** ✅ (recommended)\n",
"- More than 1 million? Use **IVF**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import FAISSAdapter\n",
"import numpy as np\n",
"\n",
"# Create some example vectors (like document embeddings)\n",
"vectors = np.random.rand(5000, 768).astype('float32')\n",
"query = np.random.rand(768).astype('float32')\n",
"\n",
"adapter = FAISSAdapter(dimension=768)\n",
"\n",
"# HNSW Index - Best for most cases\n",
"index = adapter.create_index(index_type=\"hnsw\", metric=\"L2\", m=16)\n",
"adapter.add_vectors(index, vectors, ids=[f\"doc_{i}\" for i in range(len(vectors))])\n",
"\n",
"# Search for similar vectors\n",
"distances, indices = adapter.search(index, query, k=5)\n",
"\n",
"print(\"Found 5 most similar documents:\")\n",
"for i, (dist, idx) in enumerate(zip(distances, indices), 1):\n",
" print(f\" {i}. Document {idx} (distance: {dist:.3f})\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 2: Smart Filtering with Metadata\n",
"\n",
"Imagine searching for \"similar articles\" but only from 2024 and only in the \"Technology\" category. That's what metadata filtering does!\n",
"\n",
"### Real-World Example\n",
"You're building a document search where users want:\n",
"- Similar documents (vector search)\n",
"- From specific categories (metadata filter)\n",
"- From recent years (metadata filter)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import HybridSearch, MetadataFilter\n",
"import numpy as np\n",
"\n",
"# Create sample documents with metadata\n",
"documents = [\n",
" {\"id\": 0, \"text\": \"AI in Healthcare\", \"category\": \"Technology\", \"year\": 2024},\n",
" {\"id\": 1, \"text\": \"Machine Learning Basics\", \"category\": \"Technology\", \"year\": 2023},\n",
" {\"id\": 2, \"text\": \"Business Strategy\", \"category\": \"Business\", \"year\": 2024},\n",
" {\"id\": 3, \"text\": \"Data Science Guide\", \"category\": \"Technology\", \"year\": 2024},\n",
" {\"id\": 4, \"text\": \"Marketing Tips\", \"category\": \"Business\", \"year\": 2023},\n",
"]\n",
"\n",
"# Create vectors for each document\n",
"vectors = [np.random.rand(768) for _ in documents]\n",
"metadata = [{\"category\": d[\"category\"], \"year\": d[\"year\"]} for d in documents]\n",
"vector_ids = [f\"doc_{d['id']}\" for d in documents]\n",
"\n",
"# Create search\n",
"search = HybridSearch()\n",
"query = np.random.rand(768)\n",
"\n",
"# Example 1: Find Technology articles from 2024\n",
"filter1 = MetadataFilter().eq(\"category\", \"Technology\").eq(\"year\", 2024)\n",
"results = search.search(query, vectors, metadata, vector_ids, filter=filter1, k=10)\n",
"\n",
"print(\"Technology articles from 2024:\")\n",
"for r in results:\n",
" doc_id = int(r['id'].split('_')[1])\n",
" print(f\" - {documents[doc_id]['text']}\")\n",
"\n",
"# Example 2: Find any article from 2024\n",
"filter2 = MetadataFilter().eq(\"year\", 2024)\n",
"results2 = search.search(query, vectors, metadata, vector_ids, filter=filter2, k=10)\n",
"\n",
"print(\"\\nAll articles from 2024:\")\n",
"for r in results2:\n",
" doc_id = int(r['id'].split('_')[1])\n",
" print(f\" - {documents[doc_id]['text']} ({documents[doc_id]['category']})\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 3: Combining Search Results\n",
"\n",
"Sometimes you want to search in multiple places and combine the results. Like searching both your email and documents, then showing the best matches from both.\n",
"\n",
"### When to Use This\n",
"- Searching multiple databases\n",
"- Combining different search strategies\n",
"- Giving more weight to certain sources"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import SearchRanker\n",
"\n",
"# Simulate two different searches\n",
"# Search 1: Recent documents\n",
"recent_results = [\n",
" {\"id\": \"doc_3\", \"score\": 0.95, \"source\": \"recent\"},\n",
" {\"id\": \"doc_0\", \"score\": 0.90, \"source\": \"recent\"},\n",
" {\"id\": \"doc_2\", \"score\": 0.85, \"source\": \"recent\"},\n",
"]\n",
"\n",
"# Search 2: Popular documents\n",
"popular_results = [\n",
" {\"id\": \"doc_1\", \"score\": 0.92, \"source\": \"popular\"},\n",
" {\"id\": \"doc_3\", \"score\": 0.88, \"source\": \"popular\"},\n",
" {\"id\": \"doc_4\", \"score\": 0.80, \"source\": \"popular\"},\n",
"]\n",
"\n",
"# Method 1: Fair combination (RRF)\n",
"ranker = SearchRanker(strategy=\"reciprocal_rank_fusion\")\n",
"combined = ranker.rank([recent_results, popular_results])\n",
"\n",
"print(\"Combined results (fair ranking):\")\n",
"for i, result in enumerate(combined[:3], 1):\n",
" doc_id = int(result['id'].split('_')[1])\n",
" print(f\" {i}. {documents[doc_id]['text']} (score: {result['score']:.3f})\")\n",
"\n",
"# Method 2: Prefer recent documents (70% recent, 30% popular)\n",
"weighted_ranker = SearchRanker(strategy=\"weighted_average\")\n",
"weighted_combined = weighted_ranker.rank(\n",
" [recent_results, popular_results],\n",
" weights=[0.7, 0.3]\n",
")\n",
"\n",
"print(\"\\nCombined results (prefer recent):\")\n",
"for i, result in enumerate(weighted_combined[:3], 1):\n",
" doc_id = int(result['id'].split('_')[1])\n",
" print(f\" {i}. {documents[doc_id]['text']} (score: {result['score']:.3f})\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 4: Keeping User Data Separate\n",
"\n",
"If you're building an app with multiple users or companies, you need to keep their data separate. Namespaces do this automatically.\n",
"\n",
"### Real Example\n",
"You're building a SaaS app where:\n",
"- Company A has their documents\n",
"- Company B has their documents\n",
"- They should never see each other's data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import NamespaceManager\n",
"\n",
"# Create manager\n",
"manager = NamespaceManager()\n",
"\n",
"# Create separate spaces for each company\n",
"company_a = manager.create_namespace(\"company_a\", \"Company A's documents\")\n",
"company_b = manager.create_namespace(\"company_b\", \"Company B's documents\")\n",
"\n",
"# Add documents to Company A\n",
"for i in range(10):\n",
" manager.add_vector_to_namespace(f\"company_a_doc_{i}\", \"company_a\")\n",
"\n",
"# Add documents to Company B\n",
"for i in range(15):\n",
" manager.add_vector_to_namespace(f\"company_b_doc_{i}\", \"company_b\")\n",
"\n",
"# Get each company's documents\n",
"a_docs = manager.get_namespace_vectors(\"company_a\")\n",
"b_docs = manager.get_namespace_vectors(\"company_b\")\n",
"\n",
"print(f\"Company A has {len(a_docs)} documents\")\n",
"print(f\"Company B has {len(b_docs)} documents\")\n",
"\n",
"# Set permissions (who can access what)\n",
"company_a.set_access_control(\"admin@companya.com\", [\"read\", \"write\", \"delete\"])\n",
"company_a.set_access_control(\"user@companya.com\", [\"read\"]) # Read-only\n",
"\n",
"# Check permissions\n",
"print(f\"\\nAdmin can delete: {company_a.has_permission('admin@companya.com', 'delete')}\")\n",
"print(f\"User can delete: {company_a.has_permission('user@companya.com', 'delete')}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Quick Reference Guide\n",
"\n",
"### Which Index Should I Use?\n",
"\n",
"```python\n",
"# Small dataset (< 10,000 items)\n",
"index = adapter.create_index(index_type=\"flat\", metric=\"L2\")\n",
"\n",
"# Medium dataset (10,000 - 1,000,000 items) ✅ RECOMMENDED\n",
"index = adapter.create_index(index_type=\"hnsw\", metric=\"L2\", m=16)\n",
"\n",
"# Large dataset (> 1,000,000 items)\n",
"index = adapter.create_index(index_type=\"ivf\", metric=\"L2\", nlist=100)\n",
"```\n",
"\n",
"### How Do I Filter Results?\n",
"\n",
"```python\n",
"# Single condition\n",
"filter = MetadataFilter().eq(\"category\", \"Technology\")\n",
"\n",
"# Multiple conditions (AND)\n",
"filter = MetadataFilter() \\\n",
" .eq(\"category\", \"Technology\") \\\n",
" .eq(\"year\", 2024)\n",
"\n",
"# Greater than / Less than\n",
"filter = MetadataFilter().gt(\"year\", 2020)\n",
"```\n",
"\n",
"### How Do I Combine Results?\n",
"\n",
"```python\n",
"# Fair combination\n",
"ranker = SearchRanker(strategy=\"reciprocal_rank_fusion\")\n",
"combined = ranker.rank([results1, results2])\n",
"\n",
"# Weighted combination (prefer first source)\n",
"ranker = SearchRanker(strategy=\"weighted_average\")\n",
"combined = ranker.rank([results1, results2], weights=[0.7, 0.3])\n",
"```\n",
"\n",
"### How Do I Separate User Data?\n",
"\n",
"```python\n",
"# Create namespace for each user/company\n",
"manager = NamespaceManager()\n",
"user_space = manager.create_namespace(\"user_123\", \"User 123's data\")\n",
"\n",
"# Add data to namespace\n",
"manager.add_vector_to_namespace(\"doc_1\", \"user_123\")\n",
"\n",
"# Get user's data\n",
"user_docs = manager.get_namespace_vectors(\"user_123\")\n",
"```\n",
"\n",
"---\n",
"\n",
"## Summary\n",
"\n",
"You've learned:\n",
"\n",
"1. ✅ **Index Selection**: Use HNSW for most cases\n",
"2. ✅ **Smart Filtering**: Combine vector search with metadata\n",
"3. ✅ **Result Fusion**: Merge searches from different sources\n",
"4. ✅ **Data Isolation**: Keep users' data separate\n",
"\n",
"### Next Steps\n",
"\n",
"- Try these examples with your own data\n",
"- Experiment with different filters\n",
"- Build a multi-user application\n",
"- Explore the [introduction notebook](../introduction/13_Vector_Store.ipynb) for more basics\n",
"\n",
"**Need Help?** Check our [documentation](https://semantica.readthedocs.io) or ask on [GitHub](https://github.com/Hawksight-AI/semantica)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -1,250 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Complete Visualization Suite\n",
"\n",
"## Overview\n",
"\n",
"Comprehensive visualization capabilities: visualize knowledge graphs, embeddings, quality metrics, analytics, and temporal data.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.visualization import (\n",
" KGVisualizer,\n",
" EmbeddingVisualizer,\n",
" QualityVisualizer,\n",
" AnalyticsVisualizer,\n",
" TemporalVisualizer\n",
")\n",
"from semantica.kg import GraphBuilder, GraphAnalyzer\n",
"from semantica.embeddings import EmbeddingGenerator\n",
"from semantica.kg_qa import KGQualityAssessor\n",
"import numpy as np\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Create Sample Knowledge Graph\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder = GraphBuilder()\n",
"\n",
"entities = [\n",
" {\"id\": \"e1\", \"type\": \"Person\", \"name\": \"Alice\", \"properties\": {\"age\": 30}},\n",
" {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Bob\", \"properties\": {\"age\": 35}},\n",
" {\"id\": \"e3\", \"type\": \"Organization\", \"name\": \"Tech Corp\", \"properties\": {\"founded\": 2010}},\n",
" {\"id\": \"e4\", \"type\": \"Location\", \"name\": \"San Francisco\", \"properties\": {\"country\": \"USA\"}},\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"knows\", \"properties\": {\"since\": 2020}},\n",
" {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"works_for\", \"properties\": {\"role\": \"Engineer\"}},\n",
" {\"source\": \"e3\", \"target\": \"e4\", \"type\": \"located_in\", \"properties\": {}},\n",
"]\n",
"\n",
"knowledge_graph = builder.build(entities, relationships)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Knowledge Graph Visualization\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"kg_visualizer = KGVisualizer()\n",
"kg_visualizer.visualize(knowledge_graph, layout=\"spring\", show_labels=True)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Generate Embeddings and Visualize\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"embedding_generator = EmbeddingGenerator()\n",
"texts = [entity.get(\"name\", \"\") for entity in entities]\n",
"embeddings = embedding_generator.generate(texts)\n",
"\n",
"labels = [entity.get(\"type\", \"Unknown\") for entity in entities]\n",
"\n",
"embedding_visualizer = EmbeddingVisualizer()\n",
"embedding_visualizer.visualize_tsne(embeddings, labels, title=\"Entity Embeddings Visualization\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Quality Metrics Visualization\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"quality_assessor = KGQualityAssessor()\n",
"quality_metrics = quality_assessor.assess(knowledge_graph)\n",
"\n",
"quality_visualizer = QualityVisualizer()\n",
"quality_visualizer.visualize_metrics(quality_metrics, title=\"Knowledge Graph Quality Metrics\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Graph Analytics Visualization\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"graph_analyzer = GraphAnalyzer()\n",
"\n",
"centrality_results = graph_analyzer.calculate_centrality(\n",
" knowledge_graph, \n",
" centrality_type=\"degree\"\n",
")\n",
"\n",
"centrality_scores = {}\n",
"if centrality_results and \"centrality_measures\" in centrality_results:\n",
" degree_centrality = centrality_results[\"centrality_measures\"].get(\"degree\", {})\n",
" if isinstance(degree_centrality, dict) and \"centrality\" in degree_centrality:\n",
" centrality_scores = degree_centrality[\"centrality\"]\n",
" elif isinstance(degree_centrality, dict):\n",
" centrality_scores = degree_centrality\n",
"\n",
"communities_result = graph_analyzer.detect_communities(\n",
" knowledge_graph, \n",
" algorithm=\"louvain\"\n",
")\n",
"\n",
"communities = []\n",
"community_dict = {}\n",
"if communities_result and \"communities\" in communities_result:\n",
" communities_data = communities_result[\"communities\"]\n",
" if isinstance(communities_data, list):\n",
" communities = communities_data\n",
" for idx, community in enumerate(communities):\n",
" if isinstance(community, list):\n",
" for node in community:\n",
" community_dict[node] = idx\n",
" elif isinstance(community, dict) and \"nodes\" in community:\n",
" for node in community[\"nodes\"]:\n",
" community_dict[node] = idx\n",
"\n",
"analytics_visualizer = AnalyticsVisualizer()\n",
"analytics_visualizer.visualize_centrality(centrality_scores, title=\"Node Centrality Scores\")\n",
"\n",
"if community_dict:\n",
" analytics_visualizer.visualize_communities(\n",
" knowledge_graph, \n",
" community_dict, \n",
" title=\"Community Detection\"\n",
" )\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Temporal Data Visualization\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"temporal_kg = {\n",
" \"entities\": entities,\n",
" \"relationships\": relationships,\n",
" \"timestamps\": {\n",
" \"e1\": [2020, 2021, 2022],\n",
" \"e2\": [2020, 2021],\n",
" \"e3\": [2010, 2015, 2020, 2022],\n",
" }\n",
"}\n",
"\n",
"entity_history = {\n",
" \"e1\": [\n",
" {\"timestamp\": 2020, \"properties\": {\"age\": 28}},\n",
" {\"timestamp\": 2021, \"properties\": {\"age\": 29}},\n",
" {\"timestamp\": 2022, \"properties\": {\"age\": 30}},\n",
" ]\n",
"}\n",
"\n",
"temporal_visualizer = TemporalVisualizer()\n",
"temporal_visualizer.visualize_timeline(temporal_kg, title=\"Temporal Knowledge Graph Timeline\")\n",
"temporal_visualizer.visualize_evolution(entity_history, entity_id=\"e1\", title=\"Entity Evolution\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"All visualization types demonstrated:\n",
"- Knowledge Graph Visualization\n",
"- Embedding Visualization (t-SNE)\n",
"- Quality Metrics Visualization\n",
"- Graph Analytics Visualization (Centrality & Communities)\n",
"- Temporal Data Visualization (Timeline & Evolution)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Complete Visualization Suite\")\n",
"print(\"All visualizations generated successfully\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -1,313 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Conflict Resolution Strategies\n",
"\n",
"## Overview\n",
"\n",
"Detect conflicts in knowledge graphs, apply multiple resolution strategies, track sources, and maintain audit trails.\n",
"\n",
"## Workflow: Detect Conflicts → Multiple Resolution Strategies → Track Sources → Audit\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.kg import GraphBuilder\n",
"from semantica.kg_qa import ConsistencyChecker\n",
"from datetime import datetime\n",
"import json\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Create Knowledge Graph with Conflicting Data\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder = GraphBuilder()\n",
"\n",
"entities = [\n",
" {\n",
" \"id\": \"e1\",\n",
" \"type\": \"Person\",\n",
" \"name\": \"John Doe\",\n",
" \"properties\": {\"age\": 30, \"location\": \"New York\"},\n",
" \"source\": \"source1\",\n",
" \"timestamp\": datetime(2023, 1, 1)\n",
" },\n",
" {\n",
" \"id\": \"e1\",\n",
" \"type\": \"Person\",\n",
" \"name\": \"John Doe\",\n",
" \"properties\": {\"age\": 32, \"location\": \"Boston\"},\n",
" \"source\": \"source2\",\n",
" \"timestamp\": datetime(2023, 6, 1)\n",
" },\n",
" {\n",
" \"id\": \"e2\",\n",
" \"type\": \"Organization\",\n",
" \"name\": \"Tech Corp\",\n",
" \"properties\": {\"founded\": 2010, \"employees\": 100},\n",
" \"source\": \"source1\",\n",
" \"timestamp\": datetime(2023, 1, 1)\n",
" },\n",
" {\n",
" \"id\": \"e2\",\n",
" \"type\": \"Organization\",\n",
" \"name\": \"Tech Corp\",\n",
" \"properties\": {\"founded\": 2012, \"employees\": 150},\n",
" \"source\": \"source2\",\n",
" \"timestamp\": datetime(2023, 3, 1)\n",
" },\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"works_for\", \"source\": \"source1\"},\n",
" {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"founder_of\", \"source\": \"source2\"},\n",
"]\n",
"\n",
"knowledge_graph = builder.build(entities, relationships)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Detect Conflicts\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"consistency_checker = ConsistencyChecker()\n",
"conflicts = consistency_checker.check_conflicts(knowledge_graph)\n",
"\n",
"for i, conflict in enumerate(conflicts, 1):\n",
" print(f\"Conflict {i}:\")\n",
" print(f\" Entity/Relationship: {conflict.get('entity_id', conflict.get('relationship_id'))}\")\n",
" print(f\" Type: {conflict.get('type')}\")\n",
" print(f\" Conflicting values: {conflict.get('values')}\")\n",
" print(f\" Sources: {conflict.get('sources')}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Multiple Resolution Strategies\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class ConflictResolver:\n",
" def __init__(self):\n",
" self.audit_trail = []\n",
" \n",
" def resolve(self, conflicts, strategy=\"most_recent\"):\n",
" resolved = []\n",
" \n",
" for conflict in conflicts:\n",
" if strategy == \"most_recent\":\n",
" values = conflict.get('values', [])\n",
" timestamps = conflict.get('timestamps', [])\n",
" if timestamps:\n",
" most_recent_idx = timestamps.index(max(timestamps))\n",
" resolved_value = values[most_recent_idx]\n",
" else:\n",
" resolved_value = values[-1] if values else None\n",
" \n",
" elif strategy == \"authoritative\":\n",
" sources = conflict.get('sources', [])\n",
" authoritative_sources = [\"source1\", \"official_db\", \"verified\"]\n",
" resolved_value = None\n",
" for auth_source in authoritative_sources:\n",
" if auth_source in sources:\n",
" idx = sources.index(auth_source)\n",
" resolved_value = conflict.get('values', [])[idx]\n",
" break\n",
" if resolved_value is None:\n",
" resolved_value = conflict.get('values', [])[0] if conflict.get('values') else None\n",
" \n",
" elif strategy == \"merge\":\n",
" values = conflict.get('values', [])\n",
" if isinstance(values[0], dict):\n",
" merged = {}\n",
" for val in values:\n",
" merged.update(val)\n",
" resolved_value = merged\n",
" elif isinstance(values[0], (int, float)):\n",
" resolved_value = sum(values) / len(values)\n",
" else:\n",
" resolved_value = \", \".join(set(str(v) for v in values))\n",
" else:\n",
" resolved_value = conflict.get('values', [])[0] if conflict.get('values') else None\n",
" \n",
" resolved.append({\n",
" 'conflict_id': conflict.get('entity_id', conflict.get('relationship_id')),\n",
" 'resolved_value': resolved_value,\n",
" 'strategy': strategy,\n",
" 'timestamp': datetime.now()\n",
" })\n",
" \n",
" self.audit_trail.append({\n",
" 'conflict': conflict,\n",
" 'resolution': resolved[-1],\n",
" 'resolved_at': datetime.now()\n",
" })\n",
" \n",
" return resolved\n",
"\n",
"resolver = ConflictResolver()\n",
"\n",
"resolved_1 = resolver.resolve(conflicts, strategy=\"most_recent\")\n",
"print(\"Strategy 1: Most Recent Wins\")\n",
"for r in resolved_1:\n",
" print(f\" Resolved: {r['conflict_id']} = {r['resolved_value']}\")\n",
"\n",
"resolver2 = ConflictResolver()\n",
"resolved_2 = resolver2.resolve(conflicts, strategy=\"authoritative\")\n",
"print(\"\\nStrategy 2: Most Authoritative Source Wins\")\n",
"for r in resolved_2:\n",
" print(f\" Resolved: {r['conflict_id']} = {r['resolved_value']}\")\n",
"\n",
"resolver3 = ConflictResolver()\n",
"resolved_3 = resolver3.resolve(conflicts, strategy=\"merge\")\n",
"print(\"\\nStrategy 3: Merge Conflicting Information\")\n",
"for r in resolved_3:\n",
" print(f\" Resolved: {r['conflict_id']} = {r['resolved_value']}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Track Sources\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class SourceTracker:\n",
" def __init__(self):\n",
" self.source_map = {}\n",
" \n",
" def track_sources(self, conflicts):\n",
" for conflict in conflicts:\n",
" conflict_id = conflict.get('entity_id', conflict.get('relationship_id'))\n",
" sources = conflict.get('sources', [])\n",
" timestamps = conflict.get('timestamps', [])\n",
" \n",
" self.source_map[conflict_id] = {\n",
" 'sources': sources,\n",
" 'timestamps': timestamps,\n",
" 'values': conflict.get('values', [])\n",
" }\n",
" \n",
" def get_sources(self, conflict):\n",
" conflict_id = conflict.get('entity_id', conflict.get('relationship_id'))\n",
" return self.source_map.get(conflict_id, {})\n",
"\n",
"tracker = SourceTracker()\n",
"tracker.track_sources(conflicts)\n",
"\n",
"for conflict in conflicts:\n",
" sources = tracker.get_sources(conflict)\n",
" conflict_id = conflict.get('entity_id', conflict.get('relationship_id'))\n",
" print(f\"Conflict: {conflict_id}\")\n",
" print(f\" Sources: {sources.get('sources', [])}\")\n",
" print(f\" Timestamps: {sources.get('timestamps', [])}\")\n",
" print(f\" Values: {sources.get('values', [])}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Audit Trail\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"audit_log = resolver.get_audit_trail() if hasattr(resolver, 'get_audit_trail') else resolver.audit_trail\n",
"\n",
"for i, entry in enumerate(audit_log, 1):\n",
" print(f\"Entry {i}:\")\n",
" print(f\" Conflict ID: {entry['conflict'].get('entity_id', entry['conflict'].get('relationship_id'))}\")\n",
" print(f\" Resolution Strategy: {entry['resolution']['strategy']}\")\n",
" print(f\" Resolved Value: {entry['resolution']['resolved_value']}\")\n",
" print(f\" Resolved At: {entry['resolved_at']}\")\n",
"\n",
"audit_export = []\n",
"for entry in audit_log:\n",
" audit_export.append({\n",
" 'conflict_id': entry['conflict'].get('entity_id', entry['conflict'].get('relationship_id')),\n",
" 'conflict_type': entry['conflict'].get('type'),\n",
" 'original_values': entry['conflict'].get('values'),\n",
" 'sources': entry['conflict'].get('sources'),\n",
" 'resolution_strategy': entry['resolution']['strategy'],\n",
" 'resolved_value': str(entry['resolution']['resolved_value']),\n",
" 'resolved_at': entry['resolved_at'].isoformat()\n",
" })\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Conflict resolution workflow:\n",
"- Conflict Detection\n",
"- Multiple Resolution Strategies (Most Recent, Authoritative, Merge)\n",
"- Source Tracking\n",
"- Complete Audit Trail\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(f\"Detected {len(conflicts)} conflicts\")\n",
"print(f\"Applied 3 resolution strategies\")\n",
"print(f\"Maintained audit trail with {len(audit_log)} entries\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
-221
View File
@@ -1,221 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Multi-Format Export\n",
"\n",
"## Overview\n",
"\n",
"Export knowledge graphs and data to multiple formats: JSON, RDF, CSV, Graph formats, OWL, and Vector formats.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import (\n",
" JSONExporter,\n",
" RDFExporter,\n",
" CSVExporter,\n",
" GraphExporter,\n",
" OWLExporter,\n",
" VectorExporter\n",
")\n",
"from semantica.kg import GraphBuilder\n",
"from semantica.embeddings import EmbeddingGenerator\n",
"from semantica.ontology import OntologyGenerator\n",
"import os\n",
"\n",
"os.makedirs(\"exports\", exist_ok=True)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Create Sample Knowledge Graph and Data\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder = GraphBuilder()\n",
"\n",
"entities = [\n",
" {\"id\": \"e1\", \"type\": \"Person\", \"name\": \"Alice\", \"properties\": {\"age\": 30}},\n",
" {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Bob\", \"properties\": {\"age\": 35}},\n",
" {\"id\": \"e3\", \"type\": \"Organization\", \"name\": \"Tech Corp\", \"properties\": {\"founded\": 2010}},\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"knows\"},\n",
" {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"works_for\"},\n",
"]\n",
"\n",
"knowledge_graph = builder.build(entities, relationships)\n",
"\n",
"embedding_generator = EmbeddingGenerator()\n",
"texts = [e[\"name\"] for e in entities]\n",
"embeddings = embedding_generator.generate(texts)\n",
"\n",
"ontology_generator = OntologyGenerator()\n",
"ontology = ontology_generator.generate_from_graph(knowledge_graph)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Export to JSON\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"json_exporter = JSONExporter()\n",
"json_exporter.export(knowledge_graph, \"exports/output.json\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Export to RDF\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"rdf_exporter = RDFExporter()\n",
"rdf_exporter.export(knowledge_graph, \"exports/output.rdf\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Export to CSV\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"csv_exporter = CSVExporter()\n",
"csv_exporter.export(knowledge_graph, \"exports/output.csv\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Export to Graph Formats (GraphML, GEXF)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"graph_exporter = GraphExporter()\n",
"graph_exporter.export(knowledge_graph, \"exports/output.graphml\", format=\"graphml\")\n",
"graph_exporter.export(knowledge_graph, \"exports/output.gexf\", format=\"gexf\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Export to OWL\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"owl_exporter = OWLExporter()\n",
"owl_exporter.export(ontology, \"exports/output.owl\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7: Export to Vector Formats\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"vector_exporter = VectorExporter()\n",
"vector_exporter.export(embeddings, \"exports/output.vectors\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Export formats:\n",
"- JSON\n",
"- RDF\n",
"- CSV\n",
"- GraphML\n",
"- GEXF\n",
"- OWL\n",
"- Vector format\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"export_files = [\n",
" \"exports/output.json\",\n",
" \"exports/output.rdf\",\n",
" \"exports/output.csv\",\n",
" \"exports/output.graphml\",\n",
" \"exports/output.gexf\",\n",
" \"exports/output.owl\",\n",
" \"exports/output.vectors\"\n",
"]\n",
"\n",
"for file in export_files:\n",
" if os.path.exists(file):\n",
" size = os.path.getsize(file)\n",
" print(f\"{file} ({size} bytes)\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -1,194 +0,0 @@
{
"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
}
@@ -1,195 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Pipeline Orchestration\n",
"\n",
"## Overview\n",
"\n",
"Build complex pipelines, execute them, handle failures, enable parallel processing, and monitor execution.\n",
"\n",
"## Workflow: Build Pipelines → Execute → Handle Failures → Parallel Processing → Monitor\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.pipeline import (\n",
" PipelineBuilder,\n",
" ExecutionEngine,\n",
" FailureHandler,\n",
" ParallelismManager\n",
")\n",
"from semantica.ingest import FileIngestor\n",
"from semantica.parse import DocumentParser\n",
"from semantica.semantic_extract import NERExtractor\n",
"from semantica.kg import GraphBuilder\n",
"import time\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Build Complex Pipelines\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder = PipelineBuilder()\n",
"\n",
"file_ingestor = FileIngestor()\n",
"document_parser = DocumentParser()\n",
"ner_extractor = NERExtractor()\n",
"graph_builder = GraphBuilder()\n",
"\n",
"pipeline = builder.add_step(\"ingest\", file_ingestor) \\\n",
" .add_step(\"parse\", document_parser) \\\n",
" .add_step(\"extract\", ner_extractor) \\\n",
" .add_step(\"build_graph\", graph_builder) \\\n",
" .build()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Execute Pipeline\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"engine = ExecutionEngine()\n",
"\n",
"input_data = {\n",
" \"text\": \"Alice works at Tech Corp. Bob is a friend of Alice.\",\n",
" \"files\": []\n",
"}\n",
"\n",
"start_time = time.time()\n",
"results = engine.execute(pipeline, input_data)\n",
"execution_time = time.time() - start_time\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Handle Failures\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"failure_handler = FailureHandler()\n",
"\n",
"pipeline_with_retry = failure_handler.configure_retry(pipeline, max_retries=3)\n",
"\n",
"pipeline_with_error_handling = failure_handler.configure_error_handling(\n",
" pipeline_with_retry, \n",
" on_error=\"skip\"\n",
")\n",
"\n",
"try:\n",
" results = engine.execute(pipeline_with_error_handling, input_data)\n",
"except Exception as e:\n",
" print(f\"Error handled gracefully: {e}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Parallel Processing\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"parallelism = ParallelismManager()\n",
"\n",
"parallel_pipeline = parallelism.enable_parallel(pipeline, max_workers=4)\n",
"\n",
"start_time = time.time()\n",
"results_parallel = engine.execute(parallel_pipeline, input_data)\n",
"parallel_time = time.time() - start_time\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Monitor Pipeline Execution\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"metrics = engine.get_metrics() if hasattr(engine, 'get_metrics') else {\n",
" 'duration': execution_time,\n",
" 'items_processed': 1,\n",
" 'steps_completed': 4,\n",
" 'errors': 0\n",
"}\n",
"\n",
"print(f\"Duration: {metrics.get('duration', 0):.2f} seconds\")\n",
"print(f\"Items Processed: {metrics.get('items_processed', 0)}\")\n",
"print(f\"Steps Completed: {metrics.get('steps_completed', 0)}\")\n",
"print(f\"Errors: {metrics.get('errors', 0)}\")\n",
"print(f\"Success Rate: {(1 - metrics.get('errors', 0) / max(metrics.get('items_processed', 1), 1)) * 100:.1f}%\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Pipeline orchestration workflow:\n",
"- Complex Pipeline Built\n",
"- Pipeline Executed\n",
"- Failure Handling Configured\n",
"- Parallel Processing Enabled\n",
"- Full Monitoring and Observability\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Pipeline Orchestration Complete\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -1,272 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Reasoning and Inference\n",
"\n",
"## Overview\n",
"\n",
"Build knowledge graphs, define rules, perform forward/backward chaining, and generate explanations for AI reasoning.\n",
"\n",
"## Workflow: Build KG → Define Rules → Forward/Backward Chaining → Generate Explanations\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.kg import GraphBuilder\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Build Knowledge Graph\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder = GraphBuilder()\n",
"\n",
"entities = [\n",
" {\"id\": \"alice\", \"type\": \"Person\", \"name\": \"Alice\"},\n",
" {\"id\": \"bob\", \"type\": \"Person\", \"name\": \"Bob\"},\n",
" {\"id\": \"charlie\", \"type\": \"Person\", \"name\": \"Charlie\"},\n",
" {\"id\": \"sf\", \"type\": \"Location\", \"name\": \"San Francisco\"},\n",
" {\"id\": \"california\", \"type\": \"Location\", \"name\": \"California\"},\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"alice\", \"target\": \"bob\", \"type\": \"parent_of\"},\n",
" {\"source\": \"bob\", \"target\": \"charlie\", \"type\": \"parent_of\"},\n",
" {\"source\": \"sf\", \"target\": \"california\", \"type\": \"located_in\"},\n",
" {\"source\": \"alice\", \"target\": \"sf\", \"type\": \"lives_in\"},\n",
"]\n",
"\n",
"knowledge_graph = builder.build(entities, relationships)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Define Rules\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class RuleManager:\n",
" def __init__(self):\n",
" self.rules = []\n",
" \n",
" def add_rules(self, rules):\n",
" self.rules.extend(rules)\n",
"\n",
"rule_manager = RuleManager()\n",
"\n",
"rules = [\n",
" \"IF A is parent_of B AND B is parent_of C THEN A is grandparent_of C\",\n",
" \"IF X is located_in Y AND Y is part_of Z THEN X is located_in Z\",\n",
" \"IF X lives_in Y AND Y is located_in Z THEN X lives_in Z\"\n",
"]\n",
"\n",
"rule_manager.add_rules(rules)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Forward Chaining\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class InferenceEngine:\n",
" def forward_chain(self, kg, rule_manager):\n",
" new_facts = []\n",
" \n",
" for rule in rule_manager.rules:\n",
" if \"grandparent_of\" in rule:\n",
" parents = [r for r in relationships if r[\"type\"] == \"parent_of\"]\n",
" for p1 in parents:\n",
" for p2 in parents:\n",
" if p1[\"target\"] == p2[\"source\"]:\n",
" new_fact = {\n",
" \"source\": p1[\"source\"],\n",
" \"target\": p2[\"target\"],\n",
" \"type\": \"grandparent_of\",\n",
" \"inferred\": True\n",
" }\n",
" if new_fact not in new_facts:\n",
" new_facts.append(new_fact)\n",
" \n",
" elif \"lives_in\" in rule and \"located_in\" in rule:\n",
" lives_in = [r for r in relationships if r[\"type\"] == \"lives_in\"]\n",
" located_in = [r for r in relationships if r[\"type\"] == \"located_in\"]\n",
" \n",
" for live in lives_in:\n",
" for loc in located_in:\n",
" if live[\"target\"] == loc[\"source\"]:\n",
" new_fact = {\n",
" \"source\": live[\"source\"],\n",
" \"target\": loc[\"target\"],\n",
" \"type\": \"lives_in\",\n",
" \"inferred\": True\n",
" }\n",
" if new_fact not in new_facts:\n",
" new_facts.append(new_fact)\n",
" \n",
" return new_facts\n",
"\n",
"inference_engine = InferenceEngine()\n",
"new_facts = inference_engine.forward_chain(knowledge_graph, rule_manager)\n",
"\n",
"for fact in new_facts:\n",
" print(f\"{fact['source']} {fact['type']} {fact['target']} (inferred)\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Backward Chaining\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def backward_chain(kg, rule_manager, goal):\n",
" proof_steps = []\n",
" \n",
" goal_source, goal_type, goal_target = goal\n",
" \n",
" for rel in relationships:\n",
" if rel[\"source\"] == goal_source and rel[\"type\"] == goal_type and rel[\"target\"] == goal_target:\n",
" proof_steps.append({\n",
" \"step\": \"Direct fact\",\n",
" \"fact\": f\"{goal_source} {goal_type} {goal_target}\",\n",
" \"source\": \"knowledge_graph\"\n",
" })\n",
" return proof_steps\n",
" \n",
" if goal_type == \"grandparent_of\":\n",
" for rel1 in relationships:\n",
" if rel1[\"source\"] == goal_source and rel1[\"type\"] == \"parent_of\":\n",
" intermediate = rel1[\"target\"]\n",
" for rel2 in relationships:\n",
" if rel2[\"source\"] == intermediate and rel2[\"type\"] == \"parent_of\" and rel2[\"target\"] == goal_target:\n",
" proof_steps.append({\n",
" \"step\": \"Rule application\",\n",
" \"fact\": f\"{goal_source} parent_of {intermediate}\",\n",
" \"source\": \"knowledge_graph\"\n",
" })\n",
" proof_steps.append({\n",
" \"step\": \"Rule application\",\n",
" \"fact\": f\"{intermediate} parent_of {goal_target}\",\n",
" \"source\": \"knowledge_graph\"\n",
" })\n",
" proof_steps.append({\n",
" \"step\": \"Inference\",\n",
" \"fact\": f\"{goal_source} grandparent_of {goal_target}\",\n",
" \"source\": \"inference_rule\"\n",
" })\n",
" return proof_steps\n",
" \n",
" return proof_steps\n",
"\n",
"goal = (\"alice\", \"grandparent_of\", \"charlie\")\n",
"proof = backward_chain(knowledge_graph, rule_manager, goal)\n",
"\n",
"for i, step in enumerate(proof, 1):\n",
" print(f\"Step {i}: {step['step']} - {step['fact']}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Generate Explanations\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class ExplanationGenerator:\n",
" def generate(self, proof, kg):\n",
" if not proof:\n",
" return \"No proof found for the given goal.\"\n",
" \n",
" explanation_parts = []\n",
" explanation_parts.append(\"Explanation:\")\n",
" \n",
" for i, step in enumerate(proof, 1):\n",
" if step['step'] == 'Direct fact':\n",
" explanation_parts.append(f\"{i}. We know that {step['fact']} from the knowledge graph.\")\n",
" elif step['step'] == 'Rule application':\n",
" explanation_parts.append(f\"{i}. From the knowledge graph: {step['fact']}.\")\n",
" elif step['step'] == 'Inference':\n",
" explanation_parts.append(f\"{i}. Therefore, by applying the inference rule: {step['fact']}.\")\n",
" \n",
" return \"\\n\".join(explanation_parts)\n",
"\n",
"explanation_gen = ExplanationGenerator()\n",
"explanation = explanation_gen.generate(proof, knowledge_graph)\n",
"print(explanation)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Reasoning and inference workflow:\n",
"- Knowledge Graph Built\n",
"- Inference Rules Defined\n",
"- Forward Chaining Performed\n",
"- Backward Chaining Performed\n",
"- Explanations Generated\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Reasoning and Inference Complete\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -1,194 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Semantic Layer Construction\n",
"\n",
"## Overview\n",
"\n",
"Build an enterprise semantic layer: construct knowledge graph, generate ontology, create semantic layer, export RDF, and store in triple store.\n",
"\n",
"## Workflow: Build KG → Generate Ontology → Create Semantic Layer → Export RDF → Triple Store\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.kg import GraphBuilder\n",
"from semantica.ontology import OntologyGenerator\n",
"from semantica.export import RDFExporter\n",
"from semantica.triple_store import TripleStore\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Build Knowledge Graph\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"builder = GraphBuilder()\n",
"\n",
"entities = [\n",
" {\"id\": \"e1\", \"type\": \"Person\", \"name\": \"Alice\", \"properties\": {\"age\": 30, \"role\": \"Engineer\"}},\n",
" {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Bob\", \"properties\": {\"age\": 35, \"role\": \"Manager\"}},\n",
" {\"id\": \"e3\", \"type\": \"Organization\", \"name\": \"Tech Corp\", \"properties\": {\"founded\": 2010}},\n",
" {\"id\": \"e4\", \"type\": \"Project\", \"name\": \"Project Alpha\", \"properties\": {\"status\": \"active\"}},\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"e1\", \"target\": \"e2\", \"type\": \"reports_to\"},\n",
" {\"source\": \"e1\", \"target\": \"e3\", \"type\": \"works_for\"},\n",
" {\"source\": \"e2\", \"target\": \"e3\", \"type\": \"works_for\"},\n",
" {\"source\": \"e1\", \"target\": \"e4\", \"type\": \"works_on\"},\n",
"]\n",
"\n",
"knowledge_graph = builder.build(entities, relationships)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Generate Ontology\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"generator = OntologyGenerator()\n",
"ontology = generator.generate_from_graph(knowledge_graph)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Create Semantic Layer\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def create_mappings(kg, ontology):\n",
" mappings = {\n",
" \"entity_type_mappings\": {},\n",
" \"relationship_type_mappings\": {},\n",
" \"property_mappings\": {}\n",
" }\n",
" \n",
" entity_types = set(e.get(\"type\") for e in entities)\n",
" ontology_classes = ontology.get(\"classes\", [])\n",
" \n",
" for entity_type in entity_types:\n",
" matching_class = next((cls for cls in ontology_classes if cls.get(\"name\") == entity_type), None)\n",
" if matching_class:\n",
" mappings[\"entity_type_mappings\"][entity_type] = matching_class.get(\"uri\", entity_type)\n",
" \n",
" relationship_types = set(r.get(\"type\") for r in relationships)\n",
" ontology_properties = ontology.get(\"properties\", [])\n",
" \n",
" for rel_type in relationship_types:\n",
" matching_prop = next((prop for prop in ontology_properties if prop.get(\"name\") == rel_type), None)\n",
" if matching_prop:\n",
" mappings[\"relationship_type_mappings\"][rel_type] = matching_prop.get(\"uri\", rel_type)\n",
" \n",
" return mappings\n",
"\n",
"mappings = create_mappings(knowledge_graph, ontology)\n",
"\n",
"semantic_layer = {\n",
" \"graph\": knowledge_graph,\n",
" \"ontology\": ontology,\n",
" \"mappings\": mappings,\n",
" \"metadata\": {\n",
" \"version\": \"1.0\",\n",
" \"created_at\": \"2024-01-01\",\n",
" \"description\": \"Enterprise semantic layer\"\n",
" }\n",
"}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Export RDF\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"exporter = RDFExporter()\n",
"exporter.export(knowledge_graph, ontology, \"semantic_layer.rdf\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Store in Triple Store\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"triple_store = TripleStore()\n",
"triple_store.store(knowledge_graph, ontology)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Enterprise semantic layer construction:\n",
"- Knowledge Graph Built\n",
"- Ontology Generated\n",
"- Semantic Layer Created with Mappings\n",
"- RDF Export Completed\n",
"- Triple Store Storage Completed\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Semantic Layer Construction Complete\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -1,171 +0,0 @@
{
"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
}
@@ -1,260 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Text Chunking Strategies\n",
"\n",
"## Overview\n",
"\n",
"Explore different text chunking strategies: semantic, structural, sliding window, and table chunking for optimal document processing.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.parse import TextSplitter\n",
"import re\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Prepare Sample Document\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"document = \"\"\"\n",
"# Introduction to Knowledge Graphs\n",
"\n",
"Knowledge graphs are powerful data structures that represent information as entities and their relationships. \n",
"They enable semantic understanding and reasoning over complex data.\n",
"\n",
"## What are Knowledge Graphs?\n",
"\n",
"A knowledge graph is a graph-based data model used to represent knowledge. It consists of nodes (entities) \n",
"and edges (relationships) that connect these entities. Knowledge graphs are widely used in search engines, \n",
"recommendation systems, and AI applications.\n",
"\n",
"## Applications\n",
"\n",
"Knowledge graphs have numerous applications:\n",
"- Search engines use them to understand user queries\n",
"- Recommendation systems leverage them for personalized suggestions\n",
"- AI systems use them for reasoning and inference\n",
"\n",
"## Conclusion\n",
"\n",
"In summary, knowledge graphs provide a flexible and powerful way to represent and reason about complex information.\n",
"\"\"\"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Semantic Chunking\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class SemanticChunker:\n",
" def chunk(self, document, chunk_size=500):\n",
" paragraphs = [p.strip() for p in document.split('\\n\\n') if p.strip()]\n",
" \n",
" chunks = []\n",
" current_chunk = \"\"\n",
" \n",
" for para in paragraphs:\n",
" if len(current_chunk) + len(para) <= chunk_size:\n",
" current_chunk += para + \"\\n\\n\"\n",
" else:\n",
" if current_chunk:\n",
" chunks.append(current_chunk.strip())\n",
" current_chunk = para + \"\\n\\n\"\n",
" \n",
" if current_chunk:\n",
" chunks.append(current_chunk.strip())\n",
" \n",
" return chunks\n",
"\n",
"semantic_chunker = SemanticChunker()\n",
"semantic_chunks = semantic_chunker.chunk(document, chunk_size=500)\n",
"\n",
"for i, chunk in enumerate(semantic_chunks, 1):\n",
" print(f\"Chunk {i}: {len(chunk)} characters\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Structural Chunking\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class StructuralChunker:\n",
" def chunk(self, document):\n",
" chunks = []\n",
" current_section = \"\"\n",
" current_header = \"\"\n",
" \n",
" lines = document.split('\\n')\n",
" \n",
" for line in lines:\n",
" if line.startswith('#'):\n",
" if current_section:\n",
" chunks.append({\n",
" 'header': current_header,\n",
" 'content': current_section.strip()\n",
" })\n",
" current_header = line.strip()\n",
" current_section = \"\"\n",
" else:\n",
" current_section += line + \"\\n\"\n",
" \n",
" if current_section:\n",
" chunks.append({\n",
" 'header': current_header,\n",
" 'content': current_section.strip()\n",
" })\n",
" \n",
" return chunks\n",
"\n",
"structural_chunker = StructuralChunker()\n",
"structural_chunks = structural_chunker.chunk(document)\n",
"\n",
"for i, chunk in enumerate(structural_chunks, 1):\n",
" header = chunk['header'][:50] if chunk['header'] else \"No header\"\n",
" print(f\"Chunk {i}: {header}... ({len(chunk['content'])} chars)\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Sliding Window Chunking\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class SlidingWindowChunker:\n",
" def chunk(self, document, window_size=200, overlap=50):\n",
" words = document.split()\n",
" chunks = []\n",
" \n",
" start = 0\n",
" while start < len(words):\n",
" end = min(start + window_size, len(words))\n",
" chunk_words = words[start:end]\n",
" chunks.append(' '.join(chunk_words))\n",
" \n",
" start += window_size - overlap\n",
" \n",
" return chunks\n",
"\n",
"sliding_chunker = SlidingWindowChunker()\n",
"sliding_chunks = sliding_chunker.chunk(document, window_size=200, overlap=50)\n",
"\n",
"for i, chunk in enumerate(sliding_chunks[:3], 1):\n",
" print(f\"Chunk {i}: {len(chunk)} characters\")\n",
"if len(sliding_chunks) > 3:\n",
" print(f\"... and {len(sliding_chunks) - 3} more chunks\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Table Chunking\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class TableChunker:\n",
" def chunk(self, table_data):\n",
" if isinstance(table_data, str):\n",
" rows = [row.strip() for row in table_data.split('\\n') if row.strip()]\n",
" chunks = []\n",
" for row in rows:\n",
" if '|' in row:\n",
" chunks.append(row)\n",
" return chunks\n",
" elif isinstance(table_data, list):\n",
" return [str(row) for row in table_data]\n",
" else:\n",
" return [str(table_data)]\n",
"\n",
"table_data = \"\"\"\n",
"| Name | Age | Role |\n",
"|------|-----|------|\n",
"| Alice | 30 | Engineer |\n",
"| Bob | 35 | Manager |\n",
"| Charlie | 28 | Developer |\n",
"\"\"\"\n",
"\n",
"table_chunker = TableChunker()\n",
"table_chunks = table_chunker.chunk(table_data)\n",
"\n",
"for i, chunk in enumerate(table_chunks, 1):\n",
" print(f\"Chunk {i}: {chunk[:50]}...\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Chunking strategies:\n",
"- Semantic Chunking (by meaning/paragraphs)\n",
"- Structural Chunking (by document structure)\n",
"- Sliding Window Chunking (with overlap)\n",
"- Table Chunking (for structured data)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Text Chunking Strategies Complete\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -1,166 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Unstructured to Ontology\n",
"\n",
"## Overview\n",
"\n",
"Transform unstructured text into a formal ontology: extract concepts, generate ontology, validate, and export to OWL.\n",
"\n",
"## Workflow: Unstructured Text → Extract Concepts → Generate Ontology → Validate → Export OWL\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import NERExtractor, RelationExtractor\n",
"from semantica.ontology import OntologyGenerator, OntologyValidator\n",
"from semantica.export import OWLExporter\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Extract Concepts from Unstructured Text\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"unstructured_text = \"\"\"\n",
"Apple Inc. is a technology company founded by Steve Jobs in 1976. \n",
"The company is headquartered in Cupertino, California. \n",
"Tim Cook is the current CEO of Apple. \n",
"Apple develops products like iPhone, iPad, and MacBook.\n",
"The company has offices in multiple countries including the United States, China, and Japan.\n",
"\"\"\"\n",
"\n",
"extractor = NERExtractor()\n",
"entities = extractor.extract(unstructured_text)\n",
"\n",
"relation_extractor = RelationExtractor()\n",
"relationships = relation_extractor.extract(unstructured_text, entities)\n",
"\n",
"for entity in entities[:5]:\n",
" print(f\"{entity.get('text', entity)} ({entity.get('type', 'Unknown')})\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Generate Ontology\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"generator = OntologyGenerator()\n",
"ontology = generator.generate(entities, relationships)\n",
"\n",
"if ontology.get('classes'):\n",
" for cls in ontology.get('classes', [])[:5]:\n",
" print(f\"{cls.get('name', cls)}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Validate Ontology\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"validator = OntologyValidator()\n",
"validation_result = validator.validate_ontology(ontology)\n",
"\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",
"\n",
"if validation_result.errors:\n",
" print(\"\\nErrors:\")\n",
" for error in validation_result.errors:\n",
" print(f\" - {error}\")\n",
"\n",
"if validation_result.warnings:\n",
" print(\"\\nWarnings:\")\n",
" for warning in validation_result.warnings:\n",
" print(f\" - {warning}\")\n",
"\n",
"if validation_result.metrics:\n",
" print(\"\\nMetrics:\")\n",
" print(f\" Classes: {validation_result.metrics.get('class_count', 0)}\")\n",
" print(f\" Properties: {validation_result.metrics.get('property_count', 0)}\")\n",
" print(f\" Object Properties: {validation_result.metrics.get('object_property_count', 0)}\")\n",
" print(f\" Data Properties: {validation_result.metrics.get('data_property_count', 0)}\")\n",
" print(f\" Hierarchy Depth: {validation_result.metrics.get('hierarchy_depth', 0)}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Export to OWL\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"exporter = OWLExporter()\n",
"exporter.export(ontology, \"output.owl\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"Unstructured to ontology transformation:\n",
"- Concepts Extracted from Text\n",
"- Ontology Generated\n",
"- Ontology Validated\n",
"- OWL Export Completed\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Unstructured to Ontology Complete\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,656 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/01_Welcome_to_Semantica.ipynb)\n",
"\n",
"# Welcome to Semantica\n",
"\n",
"## Overview\n",
"\n",
"This notebook introduces you to the **Semantica framework** - a comprehensive knowledge graph and semantic processing framework for building production-ready semantic AI applications.\n",
"\n",
"**Documentation**: [Getting Started](https://semantica.readthedocs.io/getting-started/) • [Concepts](https://semantica.readthedocs.io/concepts/) • [API Reference](https://semantica.readthedocs.io/reference/)\n",
"\n",
"### What You'll Learn\n",
"\n",
"- What Semantica is and why it's useful\n",
"- How to install and configure the framework\n",
"- Understanding the framework architecture\n",
"- Key concepts and terminology\n",
"- Next steps for getting started\n",
"\n",
"## What is Semantica?\n",
"\n",
"**Semantica** is a production-ready framework for:\n",
"\n",
"- **Building Knowledge Graphs**: Transform unstructured data into structured knowledge graphs\n",
"- **Semantic Processing**: Extract entities, relationships, and meaning from text\n",
"- **GraphRAG**: Graph-based retrieval augmented generation\n",
"- **Temporal Analysis**: Time-aware knowledge graphs\n",
"- **Text Processing**: Handle text and structured data\n",
"- **Enterprise Features**: Quality assurance, conflict resolution, ontology generation\n",
"\n",
"### Use Cases\n",
"\n",
"- Threat intelligence and cybersecurity\n",
"- Healthcare and medical research\n",
"- Financial analysis and fraud detection\n",
"- Supply chain optimization\n",
"- Research and knowledge management\n",
"- Multi-agent AI systems\n",
"\n",
"\n",
"## Installation & Setup\n",
"\n",
"### Prerequisites\n",
"\n",
"Before installing Semantica, ensure you have:\n",
"- Python 3.8 or higher\n",
"- pip package manager\n",
"- (Optional) Virtual environment for isolation\n",
"\n",
"### Installation Methods\n",
"\n",
"```bash\n",
"# Method 1: Install from PyPI (Recommended)\n",
"pip install semantica\n",
"\n",
"# Or install with all optional dependencies:\n",
"pip install semantica[all]\n",
"\n",
"# Method 2: Install from source (development version)\n",
"git clone https://github.com/Hawksight-AI/semantica.git\n",
"cd semantica\n",
"pip install -e .\n",
"\n",
"# Or with all optional dependencies:\n",
"pip install -e \".[all]\"\n",
"\n",
"# Verify installation\n",
"import semantica\n",
"print(semantica.__version__)\n",
"```\n",
"\n",
"### Configuration\n",
"\n",
"```bash\n",
"# Set up environment variables for API keys and configuration\n",
"# export SEMANTICA_API_KEY=your_openai_key\n",
"# export SEMANTICA_EMBEDDING_PROVIDER=openai\n",
"# export SEMANTICA_MODEL_NAME=gpt-4\n",
"\n",
"# Or use a config file (config.yaml):\n",
"# api_keys:\n",
"# openai: your_key_here\n",
"# anthropic: your_key_here\n",
"# embedding:\n",
"# provider: openai\n",
"# model: text-embedding-3-large\n",
"# dimensions: 3072\n",
"# knowledge_graph:\n",
"# backend: networkx # or neo4j, arangodb\n",
"# temporal: true\n",
"```\n",
"\n",
"---\n",
"\n",
"## Framework Architecture Overview\n",
"\n",
"Semantica is organized into modular components, each handling a specific aspect of semantic processing:\n",
"\n",
"### 1. INGEST MODULE - Data Ingestion\n",
"**Purpose**: Ingest data from various sources\n",
"**Components**:\n",
"- `FileIngestor`: Read files (PDF, DOCX, HTML, JSON, CSV, etc.)\n",
"- `WebIngestor`: Scrape and ingest web pages\n",
"- `FeedIngestor`: Process RSS/Atom feeds\n",
"- `StreamIngestor`: Real-time data streaming (Kafka, RabbitMQ, Kinesis, Pulsar)\n",
"- `DBIngestor`: Database queries and ingestion (PostgreSQL, MySQL, SQLite, Oracle, SQL Server)\n",
"- `EmailIngestor`: Process email messages (IMAP, POP3)\n",
"- `RepoIngestor`: Git repository analysis\n",
"- `MCPIngestor`: Model Context Protocol server integration\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.ingest import FileIngestor, WebIngestor, FeedIngestor, StreamIngestor, DBIngestor, EmailIngestor, RepoIngestor, MCPIngestor\n",
"file_ingestor = FileIngestor()\n",
"web_ingestor = WebIngestor()\n",
"documents = file_ingestor.ingest(\"data/\")\n",
"web_docs = web_ingestor.ingest(\"https://example.com\")\n",
"```\n",
"\n",
"### 2. PARSE MODULE - Document Parsing\n",
"**Purpose**: Parse and extract content from various formats\n",
"**Components**:\n",
"- `DocumentParser`: Main parser orchestrator\n",
"- `PDFParser`: Extract text, tables, images from PDFs\n",
"- `DOCXParser`: Parse Word documents\n",
"- `HTMLParser`: Extract content from HTML\n",
"- `JSONParser`: Parse structured JSON data\n",
"- `ExcelParser`: Process spreadsheets\n",
"- `ImageParser`: OCR and image analysis\n",
"- `CodeParser`: Parse source code files\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.parse import DocumentParser\n",
"parser = DocumentParser()\n",
"parsed_docs = parser.parse(documents)\n",
"```\n",
"\n",
"### 3. NORMALIZE MODULE - Text Normalization\n",
"**Purpose**: Clean and normalize text for processing\n",
"**Components**:\n",
"- `TextNormalizer`: Main normalization orchestrator\n",
"- `TextCleaner`: Remove noise, fix encoding\n",
"- `DataCleaner`: Clean structured data\n",
"- `EntityNormalizer`: Normalize entity names\n",
"- `DateNormalizer`: Standardize date formats\n",
"- `NumberNormalizer`: Normalize numeric values\n",
"- `LanguageDetector`: Detect document language\n",
"- `EncodingHandler`: Handle character encoding\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.normalize import TextNormalizer\n",
"normalizer = TextNormalizer()\n",
"normalized = normalizer.normalize(parsed_docs)\n",
"```\n",
"\n",
"### 4. SEMANTIC_EXTRACT MODULE - Entity & Relationship Extraction\n",
"**Purpose**: Extract entities, relationships, and semantic information\n",
"**Components**:\n",
"- `NERExtractor`: Named Entity Recognition\n",
"- `RelationExtractor`: Extract relationships between entities\n",
"- `SemanticAnalyzer`: Deep semantic analysis\n",
"- `SemanticNetworkExtractor`: Extract semantic networks\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.semantic_extract import NERExtractor, RelationExtractor\n",
"extractor = NERExtractor()\n",
"entities = extractor.extract(normalized_docs)\n",
"relation_extractor = RelationExtractor()\n",
"relationships = relation_extractor.extract(normalized_docs, entities)\n",
"```\n",
"\n",
"### 5. KG MODULE - Knowledge Graph Construction\n",
"**Purpose**: Build and manage knowledge graphs\n",
"**Components**:\n",
"- `GraphBuilder`: Construct knowledge graphs from entities/relationships\n",
"- `GraphAnalyzer`: Analyze graph structure and properties\n",
"- `GraphValidator`: Validate graph quality and consistency\n",
"- `EntityResolver`: Resolve entity conflicts and duplicates\n",
"- `ConflictDetector`: Detect conflicting information\n",
"- `CentralityCalculator`: Calculate node importance metrics\n",
"- `CommunityDetector`: Detect communities in graphs\n",
"- `ConnectivityAnalyzer`: Analyze graph connectivity\n",
"- `TemporalQuery`: Query temporal knowledge graphs\n",
"- `Deduplicator`: Remove duplicate entities/relationships\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.kg import GraphBuilder, GraphAnalyzer\n",
"builder = GraphBuilder()\n",
"kg = builder.build(entities, relationships)\n",
"analyzer = GraphAnalyzer()\n",
"metrics = analyzer.analyze(kg)\n",
"```\n",
"\n",
"### 6. EMBEDDINGS MODULE - Embedding Generation\n",
"**Purpose**: Generate vector embeddings for text data\n",
"**Components**:\n",
"- `EmbeddingGenerator`: Main embedding orchestrator\n",
"- `TextEmbedder`: Generate text embeddings\n",
"- `VectorEmbeddingManager`: Manage embeddings for vector databases\n",
"- `GraphEmbeddingManager`: Manage embeddings for graph databases\n",
"- `ProviderAdapters`: Support for OpenAI, BGE, FastEmbed, Llama, etc.\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.embeddings import EmbeddingGenerator\n",
"generator = EmbeddingGenerator()\n",
"embeddings = generator.generate_embeddings(documents, data_type=\"text\")\n",
"```\n",
"\n",
"### 7. VECTOR_STORE MODULE - Vector Database Operations\n",
"**Purpose**: Store and search vector embeddings\n",
"**Components**:\n",
"- `VectorStore`: Main vector store interface\n",
"- `FAISSAdapter`: FAISS integration\n",
"- `HybridSearch`: Combine vector and keyword search\n",
"- `VectorRetriever`: Retrieve relevant vectors\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.vector_store import VectorStore, HybridSearch\n",
"vector_store = VectorStore()\n",
"vector_store.store(embeddings, documents, metadata)\n",
"hybrid_search = HybridSearch(vector_store)\n",
"results = hybrid_search.search(query, top_k=10)\n",
"```\n",
"\n",
"### 8. GRAPH_STORE MODULE - Persistent Graph Database Operations\n",
"**Purpose**: Store and query property graphs in Neo4j or FalkorDB\n",
"**Components**:\n",
"- `GraphStore`: Main graph store interface\n",
"- `Neo4jAdapter`: Neo4j integration (enterprise features)\n",
"- `FalkorDBAdapter`: FalkorDB integration (Redis-based, ultra-fast)\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.graph_store import GraphStore\n",
"store = GraphStore(backend=\"neo4j\", uri=\"bolt://localhost:7687\", user=\"neo4j\", password=\"password\")\n",
"store.connect()\n",
"node1 = store.create_node(\n",
" labels=[\"Person\"],\n",
" properties={\"name\": \"John\", \"age\": 30}\n",
")\n",
"node2 = store.create_node(\n",
" labels=[\"Person\"],\n",
" properties={\"name\": \"Jane\", \"age\": 28}\n",
")\n",
"store.create_relationship(\n",
" start_node_id=node1[\"id\"],\n",
" end_node_id=node2[\"id\"],\n",
" rel_type=\"KNOWS\",\n",
" properties={\"since\": 2020}\n",
")\n",
"results = store.execute_query(\"MATCH (p:Person) RETURN p.name\")\n",
"store.close()\n",
"```\n",
"\n",
"### 9. REASONING MODULE - Inference and Reasoning\n",
"**Purpose**: Perform logical inference and reasoning\n",
"**Components**:\n",
"- `InferenceEngine`: Main inference orchestrator\n",
"- `RuleManager`: Manage inference rules\n",
"- `DeductiveReasoner`: Deductive reasoning\n",
"- `AbductiveReasoner`: Abductive reasoning\n",
"- `ExplanationGenerator`: Generate explanations for inferences\n",
"- `RETEEngine`: RETE algorithm for rule matching\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.reasoning import InferenceEngine, RuleManager\n",
"inference_engine = InferenceEngine()\n",
"rule_manager = RuleManager()\n",
"new_facts = inference_engine.forward_chain()\n",
"```\n",
"\n",
"### 10. ONTOLOGY MODULE - Ontology Generation\n",
"**Purpose**: Generate and manage ontologies\n",
"**Components**:\n",
"- `OntologyGenerator`: Generate ontologies from knowledge graphs\n",
"- `OntologyValidator`: Validate ontology structure\n",
"- `OWLGenerator`: Generate OWL format ontologies\n",
"- `PropertyGenerator`: Generate ontology properties\n",
"- `ClassInferrer`: Infer ontology classes\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.ontology import OntologyGenerator\n",
"generator = OntologyGenerator()\n",
"ontology = generator.generate_from_graph(kg)\n",
"```\n",
"\n",
"### 11. EXPORT MODULE - Data Export\n",
"**Purpose**: Export data in various formats\n",
"**Components**:\n",
"- `JSONExporter`: Export to JSON\n",
"- `RDFExporter`: Export to RDF/XML\n",
"- `CSVExporter`: Export to CSV\n",
"- `GraphExporter`: Export to graph formats (GraphML, GEXF)\n",
"- `OWLExporter`: Export to OWL\n",
"- `VectorExporter`: Export vectors\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.export import JSONExporter, RDFExporter\n",
"json_exporter = JSONExporter()\n",
"json_exporter.export(kg, \"output.json\")\n",
"```\n",
"\n",
"### 12. VISUALIZATION MODULE - Graph Visualization\n",
"**Purpose**: Visualize knowledge graphs and analytics\n",
"**Components**:\n",
"- `KGVisualizer`: Visualize knowledge graphs\n",
"- `EmbeddingVisualizer`: Visualize embeddings (t-SNE, PCA, UMAP)\n",
"- `QualityVisualizer`: Visualize quality metrics\n",
"- `AnalyticsVisualizer`: Visualize graph analytics\n",
"- `TemporalVisualizer`: Visualize temporal data\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.visualization import KGVisualizer\n",
"visualizer = KGVisualizer()\n",
"visualizer.visualize(kg)\n",
"```\n",
"\n",
"### 13. PIPELINE MODULE - Pipeline Orchestration\n",
"**Purpose**: Build and execute processing pipelines\n",
"**Components**:\n",
"- `PipelineBuilder`: Build complex pipelines\n",
"- `ExecutionEngine`: Execute pipelines\n",
"- `FailureHandler`: Handle pipeline failures\n",
"- `ParallelismManager`: Enable parallel processing\n",
"- `ResourceScheduler`: Schedule resources\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.pipeline import PipelineBuilder\n",
"builder = PipelineBuilder()\n",
"pipeline = builder.add_step(\"ingest\", FileIngestor()) \\\\\n",
" .add_step(\"parse\", DocumentParser()) \\\\\n",
" .build()\n",
"```\n",
"\n",
"### 14. CORE MODULE - Framework Orchestration\n",
"**Purpose**: Framework initialization, configuration, lifecycle management, and plugin system\n",
"**Components**:\n",
"- `Semantica`: Main framework class coordinating all components\n",
"- `ConfigManager`: Configuration loading, validation, and management\n",
"- `Config`: Configuration data class with validation\n",
"- `LifecycleManager`: System lifecycle management with hooks and health monitoring\n",
"- `PluginRegistry`: Dynamic plugin discovery and loading\n",
"- `MethodRegistry`: Registry for custom orchestration methods\n",
"\n",
"**Example**:\n",
"```python\n",
"from semantica.core import Semantica, ConfigManager\n",
"\n",
"# Load configuration\n",
"config_manager = ConfigManager()\n",
"config = config_manager.load_from_file(\"config.yaml\")\n",
"\n",
"# Initialize framework\n",
"framework = Semantica(config=config)\n",
"framework.initialize()\n",
"\n",
"# Build knowledge base\n",
"result = framework.build_knowledge_base(\n",
" sources=[\"doc1.pdf\", \"doc2.docx\"],\n",
" embeddings=True,\n",
" graph=True\n",
")\n",
"\n",
"# Shutdown gracefully\n",
"framework.shutdown()\n",
"```\n",
"\n",
"---\n",
"\n",
"## Key Concepts Explained\n",
"\n",
"Understanding these concepts is crucial for working with Semantica:\n",
"\n",
"### 1. KNOWLEDGE GRAPHS\n",
"**Definition**: A knowledge graph is a structured representation of entities (nodes) and their relationships (edges) with properties and attributes.\n",
"\n",
"**Structure**:\n",
"- **Nodes**: Represent entities (people, places, concepts, events)\n",
"- **Edges**: Represent relationships (works_for, located_in, causes)\n",
"- **Properties**: Attributes of entities and relationships\n",
"- **Metadata**: Additional information (sources, timestamps, confidence)\n",
"\n",
"**Example**:\n",
"- Entity: \"John Doe\" (Person)\n",
"- Relationship: \"works_for\" -> \"Acme Corp\" (Organization)\n",
"- Properties: `{start_date: \"2020-01-01\", role: \"Engineer\"}`\n",
"\n",
"**Benefits**:\n",
"- Structured representation of unstructured data\n",
"- Enables complex queries and reasoning\n",
"- Supports temporal tracking\n",
"- Facilitates knowledge discovery\n",
"\n",
"### 2. ENTITY EXTRACTION (NER - Named Entity Recognition)\n",
"**Definition**: The process of identifying and classifying named entities in text into predefined categories.\n",
"\n",
"**Entity Types**:\n",
"- **Person**: Names of people\n",
"- **Organization**: Companies, institutions\n",
"- **Location**: Places, geographic entities\n",
"- **Date/Time**: Temporal expressions\n",
"- **Money**: Monetary values\n",
"- **Product**: Products and services\n",
"- **Event**: Events and occurrences\n",
"- **Custom**: Domain-specific entities\n",
"\n",
"**Example**:\n",
"Text: \"Apple Inc. was founded by Steve Jobs in Cupertino, California.\"\n",
"Entities:\n",
"- \"Apple Inc.\" -> Organization\n",
"- \"Steve Jobs\" -> Person\n",
"- \"Cupertino, California\" -> Location\n",
"\n",
"**Methods**:\n",
"- Rule-based: Pattern matching\n",
"- Machine Learning: Trained models (spaCy, transformers)\n",
"- LLM-based: Using large language models\n",
"\n",
"### 3. RELATIONSHIP EXTRACTION\n",
"**Definition**: Identifying and extracting relationships between entities in text.\n",
"\n",
"**Relationship Types**:\n",
"- **Semantic**: \"works_for\", \"located_in\", \"causes\"\n",
"- **Temporal**: \"before\", \"after\", \"during\"\n",
"- **Causal**: \"causes\", \"results_in\", \"prevents\"\n",
"- **Hierarchical**: \"part_of\", \"subclass_of\", \"instance_of\"\n",
"\n",
"**Example**:\n",
"Text: \"John works for Acme Corp in New York.\"\n",
"Relationships:\n",
"- (John, works_for, Acme Corp)\n",
"- (Acme Corp, located_in, New York)\n",
"\n",
"**Methods**:\n",
"- Pattern matching\n",
"- Dependency parsing\n",
"- Machine learning models\n",
"- LLM-based extraction\n",
"\n",
"### 4. EMBEDDINGS\n",
"**Definition**: Dense vector representations of text, images, or other data that capture semantic meaning in a continuous vector space.\n",
"\n",
"**Properties**:\n",
"- Similar entities have similar embeddings (close in vector space)\n",
"- Enable semantic search and similarity calculations\n",
"- Fixed or variable dimensions (typically 128-4096)\n",
"\n",
"**Example**:\n",
"Text: \"machine learning\"\n",
"Embedding: `[0.123, -0.456, 0.789, ..., 0.234]` (vector of 1536 dimensions)\n",
"\n",
"**Use Cases**:\n",
"- Semantic search\n",
"- Clustering and classification\n",
"- Recommendation systems\n",
"- Anomaly detection\n",
"\n",
"### 5. TEMPORAL GRAPHS\n",
"**Definition**: Knowledge graphs that track changes over time, allowing queries about the state of the graph at specific time points.\n",
"\n",
"**Features**:\n",
"- Timestamps on entities and relationships\n",
"- Version history\n",
"- Time-point queries\n",
"- Temporal pattern detection\n",
"\n",
"**Example**:\n",
"- Entity: \"Company X\"\n",
"- Relationship: (Company X, has_CEO, Person Y)\n",
"- Temporal: `valid_from=\"2020-01-01\", valid_to=\"2023-12-31\"`\n",
"\n",
"**Use Cases**:\n",
"- Tracking organizational changes\n",
"- Monitoring system evolution\n",
"- Analyzing trends over time\n",
"- Historical analysis\n",
"\n",
"### 6. GraphRAG (Graph-based Retrieval Augmented Generation)\n",
"**Definition**: An advanced RAG approach that combines vector search with knowledge graph traversal to provide more accurate and contextually relevant information to LLMs.\n",
"\n",
"**Components**:\n",
"- **Vector Store**: For semantic similarity search\n",
"- **Knowledge Graph**: For structured relationship traversal\n",
"- **Hybrid Search**: Combines both approaches\n",
"- **LLM Integration**: Uses retrieved context for generation\n",
"\n",
"**Advantages over Traditional RAG**:\n",
"- Better handling of complex queries\n",
"- Relationship-aware retrieval\n",
"- Reduced hallucinations\n",
"- More accurate answers\n",
"\n",
"**Example Workflow**:\n",
"1. Query: \"Who worked with John at Acme Corp?\"\n",
"2. Vector search finds relevant documents\n",
"3. Knowledge graph traversal finds relationships\n",
"4. Combined context sent to LLM\n",
"5. LLM generates accurate answer using both sources\n",
"\n",
"### 7. ONTOLOGY\n",
"**Definition**: A formal specification of concepts, relationships, and constraints in a domain, typically expressed in OWL (Web Ontology Language).\n",
"\n",
"**Components**:\n",
"- **Classes**: Categories of entities\n",
"- **Properties**: Relationships and attributes\n",
"- **Individuals**: Specific instances\n",
"- **Axioms**: Rules and constraints\n",
"\n",
"**Example**:\n",
"- Class: Person\n",
"- SubClass: Employee, Customer\n",
"- Property: worksFor (domain: Person, range: Organization)\n",
"\n",
"**Use Cases**:\n",
"- Standardize domain knowledge\n",
"- Enable reasoning\n",
"- Facilitate data integration\n",
"- Support semantic web\n",
"\n",
"### 8. QUALITY ASSURANCE\n",
"**Definition**: Processes and metrics to ensure knowledge graph quality, including completeness, consistency, and accuracy.\n",
"\n",
"**Metrics**:\n",
"- **Completeness**: Percentage of entities with required properties\n",
"- **Consistency**: Absence of contradictions\n",
"- **Accuracy**: Correctness of extracted information\n",
"- **Coverage**: Breadth of domain coverage\n",
"\n",
"**Methods**:\n",
"- Validation rules\n",
"- Automated quality checks\n",
"- Conflict detection\n",
"- Source verification\n",
"---\n",
"\n",
"## Next Steps\n",
"\n",
"Now that you understand the basics, here are recommended next steps:\n",
"\n",
"1. **Your First Knowledge Graph** (`01_Your_First_Knowledge_Graph.ipynb`)\n",
" - Build your first knowledge graph from a document\n",
" - Learn the basic workflow\n",
"\n",
"2. **Configuration Basics** (`02_Configuration_Basics.ipynb`)\n",
" - Set up configuration files\n",
" - Configure API keys and providers\n",
"\n",
"3. **Core Workflows** (`01_core_workflows/`)\n",
" - Learn common patterns and workflows\n",
" - Start with \"From Unstructured to Structured\"\n",
"\n",
"4. **Use Cases** (`03_use_cases/`)\n",
" - Explore domain-specific applications\n",
" - Find examples relevant to your domain\n",
"\n",
"---\n",
"\n",
"## Best Practices\n",
"\n",
"### 1. START SMALL\n",
"- Begin with simple documents\n",
"- Validate each step before moving forward\n",
"- Build incrementally\n",
"\n",
"### 2. CONFIGURE PROPERLY\n",
"- Use environment variables for sensitive data\n",
"- Set up proper logging\n",
"- Configure appropriate model sizes\n",
"\n",
"### 3. VALIDATE DATA\n",
"- Always validate extracted entities\n",
"- Check relationship quality\n",
"- Use quality assurance tools\n",
"\n",
"### 4. HANDLE ERRORS\n",
"- Implement error handling\n",
"- Use retry mechanisms\n",
"- Log errors for debugging\n",
"\n",
"### 5. OPTIMIZE PERFORMANCE\n",
"- Use batch processing for large datasets\n",
"- Enable parallel processing where possible\n",
"- Cache embeddings and results\n",
"\n",
"### 6. DOCUMENT YOUR WORKFLOWS\n",
"- Document data sources\n",
"- Track processing steps\n",
"- Maintain metadata\n",
"\n",
"---\n",
"\n",
"## Troubleshooting\n",
"\n",
"Common issues and solutions:\n",
"\n",
"### Issue 1: Import Errors\n",
"**Solution**:\n",
"- Ensure Semantica is properly installed\n",
"- Check Python version (3.8+)\n",
"- Verify virtual environment is activated\n",
"- Install missing dependencies: `pip install -r requirements.txt`\n",
"\n",
"### Issue 2: API Key Errors\n",
"**Solution**:\n",
"- Set environment variables: `export SEMANTICA_API_KEY=your_key`\n",
"- Check config file for correct key format\n",
"- Verify API key is valid and has sufficient credits\n",
"\n",
"### Issue 3: Memory Issues\n",
"**Solution**:\n",
"- Process documents in batches\n",
"- Use smaller embedding models\n",
"- Enable garbage collection\n",
"- Consider using streaming for large datasets\n",
"\n",
"### Issue 4: Low Quality Extractions\n",
"**Solution**:\n",
"- Preprocess and normalize text\n",
"- Use domain-specific models\n",
"- Adjust extraction parameters\n",
"- Validate and clean extracted entities\n",
"\n",
"### Issue 5: Slow Processing\n",
"**Solution**:\n",
"- Enable parallel processing\n",
"- Use GPU acceleration if available\n",
"- Cache intermediate results\n",
"- Optimize batch sizes\n",
"\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,430 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/02_Data_Ingestion.ipynb)\n",
"\n",
"# Data Ingestion - Comprehensive Guide\n",
"\n",
"## Overview\n",
"\n",
"This notebook provides a comprehensive guide to Semantica's data ingestion capabilities. It covers all submodules, classes, and helper functions available in the `semantica.ingest` module.\n",
"\n",
"**Documentation**: [Ingest API Reference](https://semantica.readthedocs.io/reference/ingest/)\n",
"\n",
"### Table of Contents\n",
"\n",
"1. **Unified Ingestion**: `ingest` function\n",
"2. **File Ingestion**: `FileIngestor`, `FileTypeDetector`, `CloudStorageIngestor`\n",
"3. **Web Ingestion**: `WebIngestor`, `ContentExtractor`, `SitemapCrawler`, `RobotsChecker`\n",
"4. **Feed Ingestion**: `FeedIngestor`, `FeedMonitor`\n",
"5. **Stream Ingestion**: `StreamIngestor`, `StreamMonitor`\n",
"6. **Repository Ingestion**: `RepoIngestor`, `CodeExtractor`, `GitAnalyzer`\n",
"7. **Email Ingestion**: `EmailIngestor`, `AttachmentProcessor`\n",
"8. **Database Ingestion**: `DBIngestor`, `DatabaseConnector`\n",
"9. **MCP Ingestion**: `MCPIngestor`\n",
"10. **Configuration**: `IngestConfig`\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica with all dependencies:\n",
"\n",
"```bash\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## 1. Unified Ingestion\n",
"\n",
"The `ingest` function is the main entry point for quick data loading. It automatically detects the source type.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ingest import ingest\n",
"import tempfile\n",
"import os\n",
"import json\n",
"\n",
"# Setup temporary directory for examples\n",
"temp_dir = tempfile.mkdtemp()\n",
"sample_file = os.path.join(temp_dir, \"sample.txt\")\n",
"with open(sample_file, 'w') as f:\n",
" f.write(\"Semantica Unified Ingestion Example\")\n",
"\n",
"# Auto-detect file source\n",
"result = ingest(sample_file)\n",
"print(f\"Ingested: {result.name} (Type: {result.file_type})\")\n",
"\n",
"# Explicit source type\n",
"result_explicit = ingest(sample_file, source_type=\"file\")\n",
"print(f\"Explicit Ingest: {result_explicit.name}\")\n",
"\n",
"# Ingest web URL (auto-detected)\n",
"# Note: This will fail if no internet connection\n",
"try:\n",
" result_web = ingest(\"https://example.com\")\n",
" print(f\"Ingested Web: {result_web.title}\")\n",
"except Exception as e:\n",
" print(f\"Web ingestion skipped: {e}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. File Ingestion\n",
"\n",
"Detailed control over file processing using `FileIngestor` and helper classes.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ingest import FileIngestor, FileTypeDetector, CloudStorageIngestor\n",
"\n",
"# --- FileTypeDetector ---\n",
"detector = FileTypeDetector()\n",
"detected_type = detector.detect_type(sample_file)\n",
"print(f\"Detected Type: {detected_type}\")\n",
"\n",
"# --- FileIngestor ---\n",
"file_ingestor = FileIngestor()\n",
"\n",
"# Ingest Directory\n",
"subdir = os.path.join(temp_dir, \"docs\")\n",
"os.makedirs(subdir, exist_ok=True)\n",
"with open(os.path.join(subdir, \"note.md\"), 'w') as f:\n",
" f.write(\"# Note\\nThis is a markdown file.\")\n",
"\n",
"files = file_ingestor.ingest_directory(temp_dir, recursive=True)\n",
"print(f\"Ingested {len(files)} files from directory\")\n",
"\n",
"# --- CloudStorageIngestor (Mock Config) ---\n",
"s3_config = {\n",
" \"aws_access_key_id\": \"mock_key\",\n",
" \"aws_secret_access_key\": \"mock_secret\",\n",
" \"region_name\": \"us-east-1\"\n",
"}\n",
"cloud_ingestor = CloudStorageIngestor(provider=\"s3\", **s3_config)\n",
"\n",
"# Example call (will raise error without real credentials)\n",
"try:\n",
" result = cloud_ingestor.ingest(\"s3://my-bucket/data.csv\")\n",
" print(f\"Cloud Ingest: {result.name}\")\n",
"except Exception as e:\n",
" print(f\"Cloud ingestion skipped (Mock Config): {e}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Web Ingestion\n",
"\n",
"Scraping and crawling with `WebIngestor`, `ContentExtractor`, and `SitemapCrawler`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ingest import WebIngestor, ContentExtractor, SitemapCrawler, RobotsChecker\n",
"\n",
"# --- ContentExtractor ---\n",
"extractor = ContentExtractor()\n",
"html_content = \"<html><body><h1>Hello World</h1><p>This is a test.</p><a href='/link'>Link</a></body></html>\"\n",
"text = extractor.extract_text(html_content)\n",
"links = extractor.extract_links(html_content, base_url=\"https://example.com\")\n",
"print(f\"Extracted Text: {text}\")\n",
"print(f\"Extracted Links: {links}\")\n",
"\n",
"# --- RobotsChecker ---\n",
"checker = RobotsChecker()\n",
"can_fetch = checker.can_fetch(\"https://www.google.com/search\", \"MyBot\")\n",
"print(f\"Can fetch google search? {can_fetch}\")\n",
"\n",
"# --- WebIngestor ---\n",
"web_ingestor = WebIngestor(delay=1.0)\n",
"try:\n",
" web_content = web_ingestor.ingest_url(\"https://example.com\")\n",
" print(f\"Web Content Title: {web_content.title}\")\n",
"except Exception as e:\n",
" print(f\"Web ingest failed: {e}\")\n",
"\n",
"# --- SitemapCrawler ---\n",
"crawler = SitemapCrawler()\n",
"try:\n",
" urls = crawler.parse_sitemap(\"https://www.google.com/sitemap.xml\")\n",
" print(f\"Found {len(urls)} URLs in sitemap\")\n",
"except Exception as e:\n",
" print(f\"Sitemap crawl failed: {e}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Feed Ingestion\n",
"\n",
"Consuming RSS/Atom feeds with `FeedIngestor` and monitoring with `FeedMonitor`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ingest import FeedIngestor, FeedMonitor\n",
"import time\n",
"\n",
"# --- FeedIngestor ---\n",
"feed_ingestor = FeedIngestor()\n",
"try:\n",
" feed_data = feed_ingestor.ingest_feed(\"https://feeds.feedburner.com/oreilly/radar\")\n",
" print(f\"Feed Title: {feed_data.title}\")\n",
"except Exception as e:\n",
" print(f\"Feed ingest failed: {e}\")\n",
"\n",
"# --- FeedMonitor ---\n",
"def feed_callback(feed_data):\n",
" print(f\"Feed Updated: {feed_data.title} with {len(feed_data.items)} items\")\n",
"\n",
"monitor = FeedMonitor(check_interval=5)\n",
"try:\n",
" monitor.monitor(\"https://feeds.feedburner.com/oreilly/radar\", callback=feed_callback)\n",
" time.sleep(2) # Let it run briefly\n",
" monitor.stop()\n",
"except Exception as e:\n",
" print(f\"Feed monitor failed: {e}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. Stream Ingestion\n",
"\n",
"Real-time processing with `StreamIngestor` and `StreamMonitor`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ingest import StreamIngestor, StreamMonitor\n",
"\n",
"stream_ingestor = StreamIngestor()\n",
"\n",
"# --- Kafka Processor ---\n",
"kafka_config = {\"bootstrap_servers\": [\"localhost:9092\"]}\n",
"kafka_processor = stream_ingestor.ingest_kafka(\"my-topic\", **kafka_config)\n",
"\n",
"# --- RabbitMQ Processor ---\n",
"rabbitmq_processor = stream_ingestor.ingest_rabbitmq(\"my-queue\", \"amqp://guest:guest@localhost:5672/\")\n",
"\n",
"# --- Stream Monitor ---\n",
"monitor = stream_ingestor.monitor\n",
"health = monitor.check_health()\n",
"print(f\"Stream Health: {health['overall']}\")\n",
"print(f\"Processors: {list(health['processors'].keys())}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6. Repository Ingestion\n",
"\n",
"Analyzing codebases with `RepoIngestor`, `CodeExtractor`, and `GitAnalyzer`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ingest import RepoIngestor, CodeExtractor, GitAnalyzer\n",
"\n",
"# --- CodeExtractor ---\n",
"code_extractor = CodeExtractor()\n",
"py_code = \"class MyClass:\\n def my_method(self):\\n pass\"\n",
"structure = code_extractor.extract_structure(py_code, language=\"python\")\n",
"print(f\"Classes: {structure.get('classes')}\")\n",
"print(f\"Functions: {structure.get('functions')}\")\n",
"\n",
"# --- RepoIngestor ---\n",
"repo_ingestor = RepoIngestor()\n",
"try:\n",
" repo_data = repo_ingestor.ingest_repository(\"https://github.com/Hawksight-AI/semantica.git\")\n",
" print(f\"Repo Name: {repo_data['name']}\")\n",
"except Exception as e:\n",
" print(f\"Repo ingest failed: {e}\")\n",
"\n",
"# --- GitAnalyzer ---\n",
"try:\n",
" analyzer = GitAnalyzer(\".\")\n",
" stats = analyzer.get_statistics()\n",
" print(f\"Commits in current repo: {stats.get('total_commits', 'N/A')}\")\n",
"except Exception as e:\n",
" print(f\"Git analysis failed: {e}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 7. Email Ingestion\n",
"\n",
"Processing emails with `EmailIngestor` and `AttachmentProcessor`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ingest import EmailIngestor, AttachmentProcessor\n",
"\n",
"# --- AttachmentProcessor ---\n",
"att_processor = AttachmentProcessor()\n",
"dummy_content = b\"PDF Content\"\n",
"saved_path = att_processor.save_attachment(dummy_content, \"doc.pdf\", temp_dir)\n",
"print(f\"Saved attachment to: {saved_path}\")\n",
"\n",
"# --- EmailIngestor ---\n",
"email_ingestor = EmailIngestor()\n",
"try:\n",
" email_ingestor.connect_imap(\"imap.gmail.com\", \"user\", \"pass\")\n",
" emails = email_ingestor.ingest_mailbox(\"INBOX\", max_emails=5)\n",
" print(f\"Fetched {len(emails)} emails\")\n",
"except Exception as e:\n",
" print(f\"Email ingest failed (Auth required): {e}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 8. Database Ingestion\n",
"\n",
"Connecting to SQL databases with `DBIngestor` and `DatabaseConnector`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ingest import DBIngestor, DatabaseConnector\n",
"import sqlite3\n",
"\n",
"# Setup SQLite DB\n",
"db_path = os.path.join(temp_dir, \"test.db\")\n",
"conn = sqlite3.connect(db_path)\n",
"conn.execute(\"CREATE TABLE items (id INT, name TEXT)\")\n",
"conn.execute(\"INSERT INTO items VALUES (1, 'Item 1'), (2, 'Item 2')\")\n",
"conn.commit()\n",
"conn.close()\n",
"\n",
"# --- DatabaseConnector ---\n",
"connector = DatabaseConnector()\n",
"engine = connector.create_engine(f\"sqlite:///{db_path}\")\n",
"print(f\"Connected to DB: {engine.name}\")\n",
"\n",
"# --- DBIngestor ---\n",
"db_ingestor = DBIngestor()\n",
"table_data = db_ingestor.ingest_database(f\"sqlite:///{db_path}\", table=\"items\")\n",
"print(f\"Table: {table_data.table_name}\")\n",
"print(f\"Rows: {table_data.row_count}\")\n",
"for row in table_data.rows:\n",
" print(f\" - {row}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 9. MCP Ingestion\n",
"\n",
"Integrating with Model Context Protocol servers using `MCPIngestor`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ingest import MCPIngestor\n",
"\n",
"mcp_ingestor = MCPIngestor()\n",
"\n",
"try:\n",
" # Connect\n",
" mcp_ingestor.connect(\"weather_server\", url=\"http://localhost:8000/mcp\")\n",
"\n",
" # Ingest Resources\n",
" resources = mcp_ingestor.ingest_resources(\"weather_server\")\n",
" print(f\"Resources: {len(resources)}\")\n",
"\n",
" # Call Tool\n",
" result = mcp_ingestor.ingest_tool_output(\"weather_server\", \"get_forecast\", {\"city\": \"NYC\"})\n",
" print(f\"Tool Result: {result.content}\")\n",
"except Exception as e:\n",
" print(f\"MCP ingest failed (Server required): {e}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 10. Configuration\n",
"\n",
"Managing ingestion settings with `IngestConfig`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ingest import IngestConfig, ingest_config\n",
"\n",
"# Global config\n",
"print(f\"Default Source Type: {ingest_config.get('default_source_type')}\")\n",
"\n",
"# Custom config instance\n",
"config = IngestConfig()\n",
"config.set(\"max_file_size\", 1024 * 1024) # 1MB\n",
"print(f\"Max File Size: {config.get('max_file_size')} bytes\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,260 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/04_Document_Parsing.ipynb)\n",
"\n",
"# 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",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/parse/)\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",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## 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",
"text[:50], 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 = \"\"\"<?xml version=\"1.0\"?>\n",
"<companies>\n",
" <company name=\"Apple Inc.\" ceo=\"Tim Cook\"/>\n",
" <company name=\"Microsoft Corporation\" ceo=\"Satya Nadella\"/>\n",
"</companies>\"\"\"\n",
"\n",
"with open(xml_file, 'w') as f:\n",
" f.write(xml_content)\n",
"\n",
"xml_data = xml_parser.parse(xml_file)\n",
"\n",
"print(f\"Parsed XML with {len(xml_data.elements)} elements\")\n",
"print(f\"Root element: {xml_data.root.tag if xml_data.root else 'None'}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: HTML Parser\n",
"\n",
"Parse HTML files to extract content and structure.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.parse import HTMLParser\n",
"\n",
"html_parser = HTMLParser()\n",
"html_file = os.path.join(temp_dir, \"page.html\")\n",
"\n",
"html_content = \"\"\"<html>\n",
"<head><title>Sample Page</title></head>\n",
"<body>\n",
" <h1>Technology Companies</h1>\n",
" <p>Apple Inc. is a technology company.</p>\n",
"</body>\n",
"</html>\"\"\"\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_data(json_file, data_format=\"json\")\n",
"parsed_csv = structured_parser.parse_data(csv_file, data_format=\"csv\")\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
}
@@ -0,0 +1,234 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/05_Data_Normalization.ipynb)\n",
"\n",
"# 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",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/normalize/)\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",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## 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",
"sample_text, normalized, 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",
" normalized = date_normalizer.normalize_date(date_str)\n",
" print(f\"{date_str} -> {normalized}\")\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",
" normalized = number_normalizer.normalize_number(num_str)\n",
" print(f\"{num_str} -> {normalized}\")\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(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(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
}
@@ -0,0 +1,641 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/05_Entity_Extraction.ipynb)\n",
"\n",
"# Entity Extraction - Comprehensive Guide\n",
"\n",
"## Overview\n",
"\n",
"This notebook provides a **comprehensive guide** to extracting named entities from text using Semantica's powerful NER (Named Entity Recognition) modules. You'll learn to use multiple extractors, methods, and advanced features to identify and classify entities in text.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/semantic_extract/)\n",
"\n",
"### Learning Objectives\n",
"\n",
"By the end of this notebook, you will be able to:\n",
"\n",
"- Extract entities using `NERExtractor` and `NamedEntityRecognizer`\n",
"- Understand different extraction methods (pattern, regex, ML, HuggingFace, LLM)\n",
"- Use `EntityClassifier` to classify and group entities\n",
"- Apply `EntityConfidenceScorer` to assess extraction quality\n",
"- Create custom entity patterns with `CustomEntityDetector`\n",
"- Configure extraction parameters for optimal results\n",
"- Process multiple documents efficiently\n",
"- Handle edge cases and errors gracefully\n",
"\n",
"### What You'll Learn\n",
"\n",
"| Component | Purpose | When to Use |\n",
"|-----------|---------|-------------|\n",
"| `NERExtractor` | Core entity extraction | Quick, simple extraction |\n",
"| `NamedEntityRecognizer` | Advanced NER with configuration | Fine-tuned control needed |\n",
"| `EntityClassifier` | Classify and group entities | Organizing extracted entities |\n",
"| `EntityConfidenceScorer` | Score entity confidence | Quality assessment |\n",
"| `CustomEntityDetector` | Domain-specific entities | Custom patterns needed |\n",
"\n",
"---\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Basic Entity Extraction with NERExtractor\n",
"\n",
"Let's start with the simplest approach using `NERExtractor`. This class provides a straightforward interface for extracting named entities from text.\n",
"\n",
"### What is NERExtractor?\n",
"\n",
"`NERExtractor` is the core entity extraction class that:\n",
"- Identifies named entities (people, organizations, locations, dates, etc.)\n",
"- Returns entity objects with text, type, position, and confidence\n",
"- Supports multiple extraction methods\n",
"- Works out-of-the-box with sensible defaults"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import NERExtractor\n",
"\n",
"# Initialize the extractor\n",
"ner_extractor = NERExtractor()\n",
"\n",
"# Sample text with various entity types\n",
"text = \"\"\"\n",
"Apple Inc. is a technology company founded by Steve Jobs, Steve Wozniak, and Ronald Wayne \n",
"in Cupertino, California on April 1, 1976. The company's current CEO is Tim Cook, who took \n",
"over from Steve Jobs in August 2011. Apple is headquartered at One Apple Park Way in Cupertino.\n",
"\"\"\"\n",
"\n",
"# Extract entities\n",
"entities = ner_extractor.extract(text)\n",
"\n",
"print(f\" Extracted {len(entities)} entities:\\n\")\n",
"print(\"-\" * 80)\n",
"\n",
"for i, entity in enumerate(entities, 1):\n",
" # Handle both dict and object formats\n",
" entity_text = entity.get('text', entity.get('entity', '')) if isinstance(entity, dict) else entity.text\n",
" entity_type = entity.get('type', entity.get('label', 'Unknown')) if isinstance(entity, dict) else entity.label\n",
" confidence = entity.get('confidence', 1.0) if isinstance(entity, dict) else getattr(entity, 'confidence', 1.0)\n",
" \n",
" print(f\"{i:2d}. {entity_text:30s} | Type: {entity_type:12s} | Confidence: {confidence:.2f}\")\n",
"\n",
"print(\"-\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Understanding Entity Objects\n",
"\n",
"Each extracted entity contains:\n",
"\n",
"| Attribute | Description | Example |\n",
"|-----------|-------------|----------|\n",
"| `text` | The entity text | \"Apple Inc.\" |\n",
"| `label/type` | Entity category | \"ORG\" (Organization) |\n",
"| `start_char` | Starting position | 0 |\n",
"| `end_char` | Ending position | 10 |\n",
"| `confidence` | Extraction confidence (0-1) | 0.95 |\n",
"| `metadata` | Additional information | {\"method\": \"ml\"} |\n",
"\n",
"### Common Entity Types\n",
"\n",
"- **PERSON**: People, including fictional characters\n",
"- **ORG**: Companies, agencies, institutions\n",
"- **GPE**: Countries, cities, states (Geo-Political Entities)\n",
"- **LOC**: Non-GPE locations, mountain ranges, bodies of water\n",
"- **DATE**: Absolute or relative dates or periods\n",
"- **TIME**: Times smaller than a day\n",
"- **MONEY**: Monetary values\n",
"- **PERCENT**: Percentage values"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Visualizing Entities in Context\n",
"\n",
"Let's create a simple visualization to see entities highlighted in the original text."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def highlight_entities(text, entities):\n",
" \"\"\"\n",
" Create a simple text visualization with entity markers.\n",
" \"\"\"\n",
" # Group entities by type\n",
" entity_types = {}\n",
" for entity in entities:\n",
" entity_text = entity.get('text', entity.get('entity', '')) if isinstance(entity, dict) else entity.text\n",
" entity_type = entity.get('type', entity.get('label', 'Unknown')) if isinstance(entity, dict) else entity.label\n",
" \n",
" if entity_type not in entity_types:\n",
" entity_types[entity_type] = []\n",
" entity_types[entity_type].append(entity_text)\n",
" \n",
" print(\"\\n Entity Visualization:\\n\")\n",
" print(\"=\" * 80)\n",
" \n",
" for entity_type, entity_list in sorted(entity_types.items()):\n",
" unique_entities = list(set(entity_list))\n",
" print(f\"\\n{entity_type}:\")\n",
" for ent in unique_entities:\n",
" print(f\" • {ent}\")\n",
" \n",
" print(\"\\n\" + \"=\" * 80)\n",
"\n",
"# Visualize the extracted entities\n",
"highlight_entities(text, entities)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Different Extraction Methods\n",
"\n",
"Semantica supports multiple extraction methods, each with different strengths:\n",
"\n",
"### Method Comparison\n",
"\n",
"| Method | Speed | Accuracy | Use Case | Requires |\n",
"|--------|-------|----------|----------|----------|\n",
"| **pattern** | | ⭐⭐ | Simple, predictable patterns | Nothing |\n",
"| **regex** | | ⭐⭐⭐ | Custom patterns, IDs, codes | Regex knowledge |\n",
"| **ml** (spaCy) | | ⭐⭐⭐⭐ | General text, multiple languages | spaCy model |\n",
"| **huggingface** | | ⭐⭐⭐⭐⭐ | Domain-specific, fine-tuned | HF model |\n",
"| **llm** | | ⭐⭐⭐⭐⭐ | Complex, custom types | API key |\n",
"\n",
"Let's try different methods:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract.methods import get_entity_method\n",
"\n",
"sample_text = \"Apple Inc. was founded by Steve Jobs in Cupertino, California in 1976.\"\n",
"\n",
"print(\" Comparing Extraction Methods:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"# Try different methods\n",
"methods_to_try = [\"pattern\", \"regex\", \"ml\"]\n",
"\n",
"for method_name in methods_to_try:\n",
" try:\n",
" print(f\"\\n Method: {method_name.upper()}\")\n",
" print(\"-\" * 40)\n",
" \n",
" method = get_entity_method(method_name)\n",
" entities = method(sample_text)\n",
" \n",
" print(f\"Found {len(entities)} entities:\")\n",
" for entity in entities[:5]: # Show first 5\n",
" entity_text = entity.get('text', entity.get('entity', '')) if isinstance(entity, dict) else entity.text\n",
" entity_type = entity.get('type', entity.get('label', 'Unknown')) if isinstance(entity, dict) else entity.label\n",
" print(f\" • {entity_text} ({entity_type})\")\n",
" \n",
" except Exception as e:\n",
" print(f\" Method '{method_name}' not available: {str(e)[:50]}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Advanced Entity Recognition with NamedEntityRecognizer\n",
"\n",
"`NamedEntityRecognizer` provides more control over the extraction process through configuration parameters.\n",
"\n",
"### Key Parameters:\n",
"\n",
"- **`methods`**: List of extraction methods to use (e.g., `[\"spacy\", \"rule-based\"]`)\n",
"- **`confidence_threshold`**: Minimum confidence score (0.0-1.0, default: 0.5)\n",
"- **`merge_overlapping`**: Whether to merge overlapping entities (default: True)\n",
"- **`include_standard_types`**: Include standard entity types (PERSON, ORG, LOC, etc.)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import NamedEntityRecognizer\n",
"\n",
"# Create recognizer with custom configuration\n",
"ner = NamedEntityRecognizer(\n",
" methods=[\"spacy\"], # Use spaCy for ML-based extraction\n",
" confidence_threshold=0.7, # Only keep high-confidence entities\n",
" merge_overlapping=True, # Merge overlapping entity mentions\n",
" include_standard_types=True # Include standard entity types\n",
")\n",
"\n",
"# Sample texts for batch processing\n",
"texts = [\n",
" \"Tim Cook is the CEO of Apple Inc., based in Cupertino.\",\n",
" \"Microsoft Corporation, founded by Bill Gates, is headquartered in Redmond, Washington.\",\n",
" \"Amazon was founded by Jeff Bezos in Seattle in 1994.\",\n",
" \"Google was started by Larry Page and Sergey Brin at Stanford University.\"\n",
"]\n",
"\n",
"print(\" Advanced Entity Recognition Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"all_entities = []\n",
"for i, text in enumerate(texts, 1):\n",
" entities = ner.extract_entities(text)\n",
" all_entities.extend(entities)\n",
" \n",
" print(f\"\\n Text {i}: {text[:60]}...\")\n",
" print(f\" Found {len(entities)} entities:\")\n",
" \n",
" for entity in entities:\n",
" entity_text = entity.get('text', entity.get('entity', '')) if isinstance(entity, dict) else entity.text\n",
" entity_type = entity.get('type', entity.get('label', 'Unknown')) if isinstance(entity, dict) else entity.label\n",
" confidence = entity.get('confidence', 1.0) if isinstance(entity, dict) else getattr(entity, 'confidence', 1.0)\n",
" print(f\" • {entity_text:25s} | {entity_type:10s} | Confidence: {confidence:.2f}\")\n",
"\n",
"print(f\"\\n Total entities extracted: {len(all_entities)}\")\n",
"print(\"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Entity Classification\n",
"\n",
"Use `EntityClassifier` to classify and group entities by type, and disambiguate similar entities.\n",
"\n",
"### What is EntityClassifier?\n",
"\n",
"The `EntityClassifier` helps you:\n",
"- **Classify entities** by their type (normalize variations like \"ORG\" vs \"ORGANIZATION\")\n",
"- **Group entities** by category for analysis\n",
"- **Disambiguate entities** when multiple candidates exist\n",
"- **Standardize entity types** across different extraction methods"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import EntityClassifier\n",
"\n",
"# Initialize classifier\n",
"classifier = EntityClassifier()\n",
"\n",
"# Classify the entities we extracted earlier\n",
"classified = classifier.classify_entities(all_entities)\n",
"\n",
"print(\" Entity Classification Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for entity_type, entity_list in sorted(classified.items()):\n",
" print(f\"\\n{entity_type} ({len(entity_list)} entities):\")\n",
" print(\"-\" * 40)\n",
" \n",
" # Get unique entity texts\n",
" unique_entities = set()\n",
" for entity in entity_list:\n",
" entity_text = entity.get('text', entity.get('entity', '')) if isinstance(entity, dict) else entity.text\n",
" unique_entities.add(entity_text)\n",
" \n",
" for entity_text in sorted(unique_entities):\n",
" print(f\" • {entity_text}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Confidence Scoring\n",
"\n",
"Use `EntityConfidenceScorer` to assess and improve the confidence scores of extracted entities.\n",
"\n",
"### Why Confidence Scoring?\n",
"\n",
"Confidence scores help you:\n",
"- **Filter low-quality extractions** (e.g., only keep entities with confidence > 0.8)\n",
"- **Prioritize entities** for manual review or validation\n",
"- **Assess extraction quality** across different methods or texts\n",
"- **Make informed decisions** about which entities to use"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import EntityConfidenceScorer\n",
"\n",
"# Initialize confidence scorer\n",
"scorer = EntityConfidenceScorer()\n",
"\n",
"# Score the entities\n",
"scored_entities = scorer.score_entities(all_entities)\n",
"\n",
"print(\" Entity Confidence Scoring:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"# Group by confidence levels\n",
"high_confidence = []\n",
"medium_confidence = []\n",
"low_confidence = []\n",
"\n",
"for entity in scored_entities:\n",
" confidence = entity.get('confidence', 1.0) if isinstance(entity, dict) else getattr(entity, 'confidence', 1.0)\n",
" \n",
" if confidence >= 0.8:\n",
" high_confidence.append(entity)\n",
" elif confidence >= 0.5:\n",
" medium_confidence.append(entity)\n",
" else:\n",
" low_confidence.append(entity)\n",
"\n",
"print(f\" High Confidence (≥0.8): {len(high_confidence)} entities\")\n",
"print(f\" Medium Confidence (0.5-0.8): {len(medium_confidence)} entities\")\n",
"print(f\" Low Confidence (<0.5): {len(low_confidence)} entities\")\n",
"\n",
"print(\"\\n Confidence Distribution:\")\n",
"print(\"-\" * 40)\n",
"\n",
"# Show some examples from each category\n",
"if high_confidence:\n",
" print(\"\\nHigh Confidence Examples:\")\n",
" for entity in high_confidence[:3]:\n",
" entity_text = entity.get('text', entity.get('entity', '')) if isinstance(entity, dict) else entity.text\n",
" entity_type = entity.get('type', entity.get('label', 'Unknown')) if isinstance(entity, dict) else entity.label\n",
" confidence = entity.get('confidence', 1.0) if isinstance(entity, dict) else getattr(entity, 'confidence', 1.0)\n",
" print(f\" • {entity_text} ({entity_type}) - {confidence:.2f}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7: Custom Entity Detection\n",
"\n",
"Use `CustomEntityDetector` to define domain-specific entity patterns.\n",
"\n",
"### When to Use Custom Patterns?\n",
"\n",
"Custom patterns are useful for:\n",
"- **Domain-specific entities** (e.g., product codes, invoice numbers)\n",
"- **Structured identifiers** (e.g., email addresses, phone numbers)\n",
"- **Industry-specific terms** (e.g., medical codes, legal citations)\n",
"- **Custom formats** not recognized by standard NER"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import CustomEntityDetector\n",
"import re\n",
"\n",
"# Define custom patterns\n",
"custom_patterns = {\n",
" \"EMAIL\": r'\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Z|a-z]{2,}\\b',\n",
" \"PHONE\": r'\\b\\d{3}[-.]?\\d{3}[-.]?\\d{4}\\b',\n",
" \"PRODUCT_CODE\": r'\\b[A-Z]{2,3}-\\d{4,6}\\b',\n",
" \"URL\": r'https?://[^\\s]+'\n",
"}\n",
"\n",
"# Initialize custom detector\n",
"custom_detector = CustomEntityDetector(patterns=custom_patterns)\n",
"\n",
"# Sample text with custom entities\n",
"custom_text = \"\"\"\n",
"For support, contact support@apple.com or call 1-800-692-7753.\n",
"Order product SKU-12345 from https://store.apple.com.\n",
"Technical inquiries: tech@apple.com or visit our website.\n",
"\"\"\"\n",
"\n",
"print(\" Custom Entity Detection:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for entity_type in custom_patterns.keys():\n",
" entities = custom_detector.detect_custom_entities(custom_text, entity_type)\n",
" \n",
" if entities:\n",
" print(f\"\\n{entity_type}:\")\n",
" for entity in entities:\n",
" entity_text = entity.get('text', entity.get('entity', '')) if isinstance(entity, dict) else entity.text\n",
" print(f\" • {entity_text}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8: Batch Processing\n",
"\n",
"Process multiple documents efficiently using batch processing capabilities.\n",
"\n",
"### Benefits of Batch Processing:\n",
"\n",
"- **Performance**: Process multiple documents in one call\n",
"- **Consistency**: Same configuration applied to all documents\n",
"- **Efficiency**: Reduced overhead from initialization\n",
"- **Scalability**: Handle large document collections"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Sample document collection\n",
"documents = [\n",
" \"Apple Inc. released the iPhone 15 in September 2023.\",\n",
" \"Microsoft announced Azure AI updates at Build 2023 in Seattle.\",\n",
" \"Google's Sundar Pichai spoke at I/O 2023 in Mountain View, California.\",\n",
" \"Tesla's Elon Musk unveiled the Cybertruck in Austin, Texas.\",\n",
" \"Amazon Web Services launched new features in Northern Virginia.\"\n",
"]\n",
"\n",
"print(\" Batch Processing Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"# Process all documents\n",
"batch_results = ner.process_batch(documents)\n",
"\n",
"# Analyze results\n",
"total_entities = 0\n",
"entity_type_counts = {}\n",
"\n",
"for i, (doc, entities) in enumerate(zip(documents, batch_results), 1):\n",
" total_entities += len(entities)\n",
" \n",
" print(f\"\\n Document {i}:\")\n",
" print(f\" Text: {doc[:50]}...\")\n",
" print(f\" Entities: {len(entities)}\")\n",
" \n",
" for entity in entities:\n",
" entity_type = entity.get('type', entity.get('label', 'Unknown')) if isinstance(entity, dict) else entity.label\n",
" entity_type_counts[entity_type] = entity_type_counts.get(entity_type, 0) + 1\n",
"\n",
"print(f\"\\n Batch Processing Summary:\")\n",
"print(\"-\" * 40)\n",
"print(f\"Documents processed: {len(documents)}\")\n",
"print(f\"Total entities: {total_entities}\")\n",
"print(f\"Average per document: {total_entities/len(documents):.1f}\")\n",
"\n",
"print(\"\\n Entity Type Distribution:\")\n",
"for entity_type, count in sorted(entity_type_counts.items(), key=lambda x: x[1], reverse=True):\n",
" print(f\" {entity_type}: {count}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 9: Best Practices & Tips\n",
"\n",
"### Choosing the Right Method\n",
"\n",
"1. **Start with ML (spaCy)** for general text\n",
"2. **Use patterns/regex** for structured data (IDs, codes)\n",
"3. **Try HuggingFace** for domain-specific needs\n",
"4. **Consider LLM** for complex, custom entity types\n",
"\n",
"### Optimizing Performance\n",
"\n",
"- **Set appropriate confidence thresholds** (0.7-0.8 for production)\n",
"- **Use batch processing** for multiple documents\n",
"- **Enable merge_overlapping** to reduce duplicates\n",
"- **Cache extractors** instead of recreating them\n",
"\n",
"### Common Pitfalls to Avoid\n",
"\n",
"- **Don't** use very low confidence thresholds (< 0.5)\n",
"- **Don't** process one document at a time in loops\n",
"- **Don't** ignore entity metadata (contains useful info)\n",
"- **Don't** forget to handle extraction errors\n",
"\n",
"### When to Use Each Class\n",
"\n",
"| Use Case | Recommended Class |\n",
"|----------|-------------------|\n",
"| Quick extraction | `NERExtractor` |\n",
"| Fine-tuned control | `NamedEntityRecognizer` |\n",
"| Grouping entities | `EntityClassifier` |\n",
"| Quality assessment | `EntityConfidenceScorer` |\n",
"| Domain-specific | `CustomEntityDetector` |"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"### What You've Learned\n",
"\n",
"In this notebook, you've learned how to:\n",
"\n",
" **Extract entities** using `NERExtractor` and `NamedEntityRecognizer` \n",
" **Compare different extraction methods** (pattern, regex, ML, HuggingFace, LLM) \n",
" **Classify and group entities** with `EntityClassifier` \n",
" **Score entity confidence** using `EntityConfidenceScorer` \n",
" **Create custom patterns** with `CustomEntityDetector` \n",
" **Process documents in batch** for efficiency \n",
" **Apply best practices** for production use \n",
"\n",
"### Key Takeaways\n",
"\n",
"1. **Multiple methods available**: Choose based on your needs (speed vs accuracy)\n",
"2. **Configuration matters**: Tune parameters for optimal results\n",
"3. **Confidence is key**: Use thresholds to filter low-quality extractions\n",
"4. **Custom patterns work**: For domain-specific entities\n",
"5. **Batch processing scales**: Process multiple documents efficiently\n",
"\n",
"### Next Steps\n",
"\n",
" **Next Notebook**: [06_Relation_Extraction.ipynb](./06_Relation_Extraction.ipynb) \n",
"Learn how to extract relationships between the entities you've identified!\n",
"\n",
" **Further Reading**:\n",
"- [Semantic Extract API Reference](https://semantica.readthedocs.io/reference/semantic_extract/)\n",
"- [Advanced Extraction Techniques](../advanced/01_Advanced_Extraction.ipynb)\n",
"- [Building Knowledge Graphs](./07_Building_Knowledge_Graphs.ipynb)\n",
"\n",
"---\n",
"\n",
"**Questions or Issues?** Check out our [GitHub repository](https://github.com/Hawksight-AI/semantica) or [documentation](https://semantica.readthedocs.io)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,693 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/06_Relation_Extraction.ipynb)\n",
"\n",
"# Relation Extraction - Comprehensive Guide\n",
"\n",
"## Overview\n",
"\n",
"This notebook provides a **comprehensive guide** to extracting relationships between entities and building RDF triples using Semantica's relation extraction modules. You'll learn to identify connections, extract structured triples, and prepare data for knowledge graphs.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/semantic_extract/)\n",
"\n",
"### Learning Objectives\n",
"\n",
"By the end of this notebook, you will be able to:\n",
"\n",
"- Extract relationships using `RelationExtractor`\n",
"- Understand different extraction methods (pattern, dependency, co-occurrence, HuggingFace, LLM)\n",
"- Configure extraction parameters for optimal results\n",
"- Extract RDF triples with `TripleExtractor`\n",
"- Validate triples using `TripleValidator`\n",
"- Serialize triples to RDF formats with `RDFSerializer`\n",
"- Assess triple quality with `TripleQualityChecker`\n",
"- Build complete entity → relation → triple pipelines\n",
"\n",
"### What You'll Learn\n",
"\n",
"| Component | Purpose | When to Use |\n",
"|-----------|---------|-------------|\n",
"| `RelationExtractor` | Extract entity relationships | Finding connections |\n",
"| `TripleExtractor` | Extract RDF triples | Building knowledge graphs |\n",
"| `TripleValidator` | Validate triple quality | Quality assurance |\n",
"| `RDFSerializer` | Serialize to RDF formats | Data export |\n",
"| `TripleQualityChecker` | Assess triple quality | Quality metrics |\n",
"\n",
"---\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Basic Relation Extraction\n",
"\n",
"Let's start by extracting relationships between entities using `RelationExtractor`.\n",
"\n",
"### What is RelationExtractor?\n",
"\n",
"`RelationExtractor` identifies relationships between entities:\n",
"- Finds connections like \"founded_by\", \"located_in\", \"works_for\"\n",
"- Returns Relation objects with subject, predicate, object\n",
"- Supports multiple extraction methods\n",
"- Provides confidence scores for each relation\n",
"\n",
"### Understanding Relations\n",
"\n",
"A relation has three parts:\n",
"- **Subject**: The source entity (e.g., \"Apple Inc.\")\n",
"- **Predicate**: The relationship type (e.g., \"founded_by\")\n",
"- **Object**: The target entity (e.g., \"Steve Jobs\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import RelationExtractor, NERExtractor\n",
"\n",
"# Initialize extractors\n",
"ner_extractor = NERExtractor()\n",
"relation_extractor = RelationExtractor()\n",
"\n",
"# Sample text with clear relationships\n",
"text = \"\"\"\n",
"Apple Inc. was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in 1976.\n",
"The company is headquartered in Cupertino, California. Tim Cook is the current CEO\n",
"of Apple Inc. and took over from Steve Jobs in August 2011.\n",
"\"\"\"\n",
"\n",
"# First, extract entities\n",
"entities = ner_extractor.extract(text)\n",
"print(f\" Extracted {len(entities)} entities\\n\")\n",
"\n",
"# Then, extract relationships\n",
"relationships = relation_extractor.extract(text, entities)\n",
"\n",
"print(f\" Extracted {len(relationships)} relationships:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, rel in enumerate(relationships, 1):\n",
" # Handle both dict and object formats\n",
" source = rel.get('source', rel.get('subject', '')) if isinstance(rel, dict) else getattr(rel, 'subject', '')\n",
" target = rel.get('target', rel.get('object', '')) if isinstance(rel, dict) else getattr(rel, 'object', '')\n",
" rel_type = rel.get('type', rel.get('predicate', 'related_to')) if isinstance(rel, dict) else getattr(rel, 'predicate', 'related_to')\n",
" confidence = rel.get('confidence', 1.0) if isinstance(rel, dict) else getattr(rel, 'confidence', 1.0)\n",
" \n",
" # Get source and target text\n",
" if isinstance(source, dict):\n",
" source_text = source.get('text', source.get('entity', str(source)))\n",
" else:\n",
" source_text = getattr(source, 'text', str(source))\n",
" \n",
" if isinstance(target, dict):\n",
" target_text = target.get('text', target.get('entity', str(target)))\n",
" else:\n",
" target_text = getattr(target, 'text', str(target))\n",
" \n",
" print(f\"{i:2d}. {source_text:20s} --[{rel_type:15s}]--> {target_text:20s} (conf: {confidence:.2f})\")\n",
"\n",
"print(\"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Understanding Relation Objects\n",
"\n",
"Each extracted relation contains:\n",
"\n",
"| Attribute | Description | Example |\n",
"|-----------|-------------|----------|\n",
"| `subject` | Source entity | Entity(\"Apple Inc.\") |\n",
"| `predicate` | Relationship type | \"founded_by\" |\n",
"| `object` | Target entity | Entity(\"Steve Jobs\") |\n",
"| `confidence` | Extraction confidence (0-1) | 0.85 |\n",
"| `context` | Surrounding text | \"Apple Inc. was founded by Steve Jobs\" |\n",
"| `metadata` | Additional info | {\"method\": \"pattern\"} |\n",
"\n",
"### Common Relation Types\n",
"\n",
"- **founded_by**: Organization founded by person\n",
"- **located_in**: Entity located in place\n",
"- **works_for**: Person works for organization\n",
"- **born_in**: Person born in location\n",
"- **part_of**: Entity is part of another\n",
"- **related_to**: Generic relationship"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Different Extraction Methods\n",
"\n",
"Semantica supports multiple relation extraction methods:\n",
"\n",
"### Method Comparison\n",
"\n",
"| Method | Speed | Accuracy | Use Case | Requires |\n",
"|--------|-------|----------|----------|----------|\n",
"| **pattern** | | ⭐⭐⭐ | Common relations | Nothing |\n",
"| **dependency** | | ⭐⭐⭐⭐ | Grammatical relations | spaCy |\n",
"| **cooccurrence** | | ⭐⭐ | Proximity-based | Nothing |\n",
"| **huggingface** | | ⭐⭐⭐⭐⭐ | Domain-specific | HF model |\n",
"| **llm** | | ⭐⭐⭐⭐⭐ | Complex, custom | API key |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract.methods import get_relation_method\n",
"\n",
"sample_text = \"Apple Inc. was founded by Steve Jobs in Cupertino, California.\"\n",
"sample_entities = ner_extractor.extract(sample_text)\n",
"\n",
"print(\" Comparing Relation Extraction Methods:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"# Try different methods\n",
"methods_to_try = [\"pattern\", \"dependency\", \"cooccurrence\"]\n",
"\n",
"for method_name in methods_to_try:\n",
" try:\n",
" print(f\"\\n Method: {method_name.upper()}\")\n",
" print(\"-\" * 40)\n",
" \n",
" method = get_relation_method(method_name)\n",
" relations = method(sample_text, sample_entities)\n",
" \n",
" print(f\"Found {len(relations)} relations:\")\n",
" for rel in relations[:3]: # Show first 3\n",
" source = rel.get('source', rel.get('subject', '')) if isinstance(rel, dict) else getattr(rel, 'subject', '')\n",
" target = rel.get('target', rel.get('object', '')) if isinstance(rel, dict) else getattr(rel, 'object', '')\n",
" rel_type = rel.get('type', rel.get('predicate', 'related_to')) if isinstance(rel, dict) else getattr(rel, 'predicate', 'related_to')\n",
" \n",
" # Get text representations\n",
" source_text = source.get('text', str(source)) if isinstance(source, dict) else getattr(source, 'text', str(source))\n",
" target_text = target.get('text', str(target)) if isinstance(target, dict) else getattr(target, 'text', str(target))\n",
" \n",
" print(f\" • {source_text} --[{rel_type}]--> {target_text}\")\n",
" \n",
" except Exception as e:\n",
" print(f\" Method '{method_name}' not available: {str(e)[:50]}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Advanced Relation Extraction with Configuration\n",
"\n",
"`RelationExtractor` provides powerful configuration options:\n",
"\n",
"### Key Parameters:\n",
"\n",
"- **`relation_types`**: Specific relation types to extract (e.g., `[\"founded\", \"works_at\"]`)\n",
"- **`bidirectional`**: Extract bidirectional relations (default: False)\n",
"- **`confidence_threshold`**: Minimum confidence score (0.0-1.0, default: 0.6)\n",
"- **`max_distance`**: Maximum token distance between entities (default: 50)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create extractor with custom configuration\n",
"advanced_extractor = RelationExtractor(\n",
" relation_types=[\"founded_by\", \"located_in\", \"works_for\"], # Only extract these types\n",
" confidence_threshold=0.7, # Higher threshold for quality\n",
" bidirectional=False, # One-way relations only\n",
" max_distance=50 # Max 50 tokens between entities\n",
")\n",
"\n",
"# Sample texts\n",
"texts = [\n",
" \"Microsoft was founded by Bill Gates and Paul Allen in Albuquerque, New Mexico.\",\n",
" \"Satya Nadella works for Microsoft as the CEO.\",\n",
" \"Google is located in Mountain View, California.\",\n",
" \"Amazon was founded by Jeff Bezos in Seattle, Washington.\"\n",
"]\n",
"\n",
"print(\" Advanced Relation Extraction:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, text in enumerate(texts, 1):\n",
" entities = ner_extractor.extract(text)\n",
" relations = advanced_extractor.extract(text, entities)\n",
" \n",
" print(f\"\\n Text {i}: {text}\")\n",
" print(f\" Relations found: {len(relations)}\")\n",
" \n",
" for rel in relations:\n",
" source = rel.get('source', rel.get('subject', '')) if isinstance(rel, dict) else getattr(rel, 'subject', '')\n",
" target = rel.get('target', rel.get('object', '')) if isinstance(rel, dict) else getattr(rel, 'object', '')\n",
" rel_type = rel.get('type', rel.get('predicate', 'related_to')) if isinstance(rel, dict) else getattr(rel, 'predicate', 'related_to')\n",
" confidence = rel.get('confidence', 1.0) if isinstance(rel, dict) else getattr(rel, 'confidence', 1.0)\n",
" \n",
" source_text = source.get('text', str(source)) if isinstance(source, dict) else getattr(source, 'text', str(source))\n",
" target_text = target.get('text', str(target)) if isinstance(target, dict) else getattr(target, 'text', str(target))\n",
" \n",
" print(f\" • {source_text} --[{rel_type}]--> {target_text} (conf: {confidence:.2f})\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Relation Classification\n",
"\n",
"Group and classify extracted relations by their predicate type."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Extract relations from all texts\n",
"all_relations = []\n",
"for text in texts:\n",
" entities = ner_extractor.extract(text)\n",
" relations = advanced_extractor.extract(text, entities)\n",
" all_relations.extend(relations)\n",
"\n",
"# Classify relations\n",
"classified_relations = advanced_extractor.classify_relations(all_relations)\n",
"\n",
"print(\" Relation Classification:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for rel_type, rel_list in sorted(classified_relations.items()):\n",
" print(f\"\\n{rel_type.upper()} ({len(rel_list)} relations):\")\n",
" print(\"-\" * 40)\n",
" \n",
" for rel in rel_list:\n",
" source = rel.get('source', rel.get('subject', '')) if isinstance(rel, dict) else getattr(rel, 'subject', '')\n",
" target = rel.get('target', rel.get('object', '')) if isinstance(rel, dict) else getattr(rel, 'object', '')\n",
" \n",
" source_text = source.get('text', str(source)) if isinstance(source, dict) else getattr(source, 'text', str(source))\n",
" target_text = target.get('text', str(target)) if isinstance(target, dict) else getattr(target, 'text', str(target))\n",
" \n",
" print(f\" • {source_text} → {target_text}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Triple Extraction\n",
"\n",
"Extract RDF triples using `TripleExtractor`. Triples are the foundation of knowledge graphs.\n",
"\n",
"### What are RDF Triples?\n",
"\n",
"RDF (Resource Description Framework) triples are statements with three parts:\n",
"- **Subject**: What we're talking about\n",
"- **Predicate**: The property or relationship\n",
"- **Object**: The value or target\n",
"\n",
"Example: `(Apple Inc., founded_by, Steve Jobs)`\n",
"\n",
"### Why Use Triples?\n",
"\n",
"- **Standardized format** for knowledge representation\n",
"- **Compatible** with RDF databases and semantic web\n",
"- **Queryable** using SPARQL\n",
"- **Interoperable** across systems"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import TripleExtractor\n",
"\n",
"# Initialize triple extractor\n",
"triple_extractor = TripleExtractor(\n",
" include_temporal=True, # Include temporal information\n",
" include_provenance=True # Track source sentences\n",
")\n",
"\n",
"# Sample text\n",
"triple_text = \"\"\"\n",
"Apple Inc. was founded by Steve Jobs in 1976. The company is based in Cupertino, California.\n",
"Tim Cook became CEO in 2011. Apple develops the iPhone and MacBook products.\n",
"\"\"\"\n",
"\n",
"# Extract triples\n",
"triples = triple_extractor.extract_triples(triple_text)\n",
"\n",
"print(f\" Extracted {len(triples)} RDF Triples:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, triple in enumerate(triples, 1):\n",
" subject = triple.get('subject', '') if isinstance(triple, dict) else triple.subject\n",
" predicate = triple.get('predicate', '') if isinstance(triple, dict) else triple.predicate\n",
" obj = triple.get('object', '') if isinstance(triple, dict) else triple.object\n",
" confidence = triple.get('confidence', 1.0) if isinstance(triple, dict) else getattr(triple, 'confidence', 1.0)\n",
" \n",
" print(f\"{i:2d}. ({subject}, {predicate}, {obj})\")\n",
" print(f\" Confidence: {confidence:.2f}\")\n",
" \n",
" # Show temporal info if available\n",
" metadata = triple.get('metadata', {}) if isinstance(triple, dict) else getattr(triple, 'metadata', {})\n",
" if metadata.get('temporal'):\n",
" print(f\" Temporal: {metadata['temporal']}\")\n",
" print()\n",
"\n",
"print(\"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Triple Validation\n",
"\n",
"Validate extracted triples using `TripleValidator` and assess quality with `TripleQualityChecker`.\n",
"\n",
"### Why Validate Triples?\n",
"\n",
"- **Ensure completeness**: All parts (subject, predicate, object) present\n",
"- **Check confidence**: Filter low-quality extractions\n",
"- **Verify consistency**: No contradictory statements\n",
"- **Assess quality**: Overall extraction quality metrics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import TripleValidator, TripleQualityChecker\n",
"\n",
"# Initialize validator and quality checker\n",
"validator = TripleValidator()\n",
"quality_checker = TripleQualityChecker()\n",
"\n",
"print(\" Triple Validation:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"# Validate triples\n",
"valid_triples = validator.validate_triples(triples, min_confidence=0.5)\n",
"\n",
"print(f\"\\n Validation Results:\")\n",
"print(f\" Total triples: {len(triples)}\")\n",
"print(f\" Valid triples: {len(valid_triples)}\")\n",
"print(f\" Filtered out: {len(triples) - len(valid_triples)}\")\n",
"\n",
"# Check quality\n",
"quality_scores = quality_checker.calculate_quality_scores(valid_triples)\n",
"\n",
"print(f\"\\n Quality Metrics:\")\n",
"print(\"-\" * 40)\n",
"for metric, value in quality_scores.items():\n",
" if isinstance(value, float):\n",
" print(f\" {metric}: {value:.2f}\")\n",
" else:\n",
" print(f\" {metric}: {value}\")\n",
"\n",
"# Check consistency\n",
"consistency_report = validator.check_triple_consistency(valid_triples)\n",
"\n",
"print(f\"\\n Consistency Check:\")\n",
"print(f\" Consistent: {consistency_report.get('consistent', True)}\")\n",
"print(f\" Issues found: {len(consistency_report.get('issues', []))}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7: RDF Serialization\n",
"\n",
"Serialize triples to various RDF formats using `RDFSerializer`.\n",
"\n",
"### Supported Formats:\n",
"\n",
"| Format | Extension | Use Case |\n",
"|--------|-----------|----------|\n",
"| **Turtle** | .ttl | Human-readable, compact |\n",
"| **N-Triples** | .nt | Simple, line-based |\n",
"| **JSON-LD** | .jsonld | Web-friendly, JSON-based |\n",
"| **RDF/XML** | .rdf | XML-based, verbose |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import RDFSerializer\n",
"\n",
"# Initialize serializer\n",
"serializer = RDFSerializer()\n",
"\n",
"print(\" RDF Serialization Examples:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"# Serialize to different formats\n",
"formats = [\"turtle\", \"ntriples\", \"jsonld\"]\n",
"\n",
"for fmt in formats:\n",
" print(f\"\\n {fmt.upper()} Format:\")\n",
" print(\"-\" * 40)\n",
" \n",
" try:\n",
" serialized = serializer.serialize_to_rdf(valid_triples[:3], format=fmt) # Show first 3\n",
" \n",
" # Show preview (first 300 chars)\n",
" preview = serialized[:300] + \"...\" if len(serialized) > 300 else serialized\n",
" print(preview)\n",
" \n",
" except Exception as e:\n",
" print(f\"Error: {str(e)[:50]}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8: Complete Extraction Pipeline\n",
"\n",
"Let's build a complete pipeline: **Entities → Relations → Triples**\n",
"\n",
"This demonstrates the full workflow for knowledge graph construction."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def extract_knowledge(text):\n",
" \"\"\"\n",
" Complete knowledge extraction pipeline.\n",
" \n",
" Args:\n",
" text: Input text\n",
" \n",
" Returns:\n",
" dict: Extracted entities, relations, and triples\n",
" \"\"\"\n",
" # Step 1: Extract entities\n",
" entities = ner_extractor.extract(text)\n",
" \n",
" # Step 2: Extract relations\n",
" relations = relation_extractor.extract(text, entities)\n",
" \n",
" # Step 3: Extract triples\n",
" triples = triple_extractor.extract_triples(text, entities=entities, relationships=relations)\n",
" \n",
" # Step 4: Validate triples\n",
" valid_triples = validator.validate_triples(triples)\n",
" \n",
" return {\n",
" 'entities': entities,\n",
" 'relations': relations,\n",
" 'triples': valid_triples\n",
" }\n",
"\n",
"# Sample knowledge-rich text\n",
"knowledge_text = \"\"\"\n",
"Tesla Inc. was founded by Elon Musk, JB Straubel, Martin Eberhard, Marc Tarpenning, \n",
"and Ian Wright in 2003. The company is headquartered in Austin, Texas. Tesla produces \n",
"electric vehicles including the Model S, Model 3, Model X, and Model Y. Elon Musk serves \n",
"as CEO and has been instrumental in the company's growth.\n",
"\"\"\"\n",
"\n",
"print(\" Complete Extraction Pipeline:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"# Run pipeline\n",
"result = extract_knowledge(knowledge_text)\n",
"\n",
"print(f\"\\n Extraction Results:\")\n",
"print(\"-\" * 40)\n",
"print(f\"Entities extracted: {len(result['entities'])}\")\n",
"print(f\"Relations extracted: {len(result['relations'])}\")\n",
"print(f\"Triples extracted: {len(result['triples'])}\")\n",
"\n",
"print(f\"\\n Sample Triples:\")\n",
"for i, triple in enumerate(result['triples'][:5], 1):\n",
" subject = triple.get('subject', '') if isinstance(triple, dict) else triple.subject\n",
" predicate = triple.get('predicate', '') if isinstance(triple, dict) else triple.predicate\n",
" obj = triple.get('object', '') if isinstance(triple, dict) else triple.object\n",
" print(f\" {i}. ({subject}, {predicate}, {obj})\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 9: Best Practices & Tips\n",
"\n",
"### Choosing the Right Method\n",
"\n",
"1. **Start with pattern-based** for common relations\n",
"2. **Use dependency parsing** for grammatical accuracy\n",
"3. **Try co-occurrence** for exploratory analysis\n",
"4. **Consider LLM** for complex, domain-specific relations\n",
"\n",
"### Optimizing Extraction\n",
"\n",
"- **Set confidence thresholds** (0.6-0.7 for production)\n",
"- **Specify relation_types** to focus extraction\n",
"- **Adjust max_distance** based on text structure\n",
"- **Validate triples** before using in knowledge graphs\n",
"\n",
"### Common Pitfalls to Avoid\n",
"\n",
"- **Don't** skip entity extraction (relations need entities)\n",
"- **Don't** use very low confidence thresholds\n",
"- **Don't** ignore relation validation\n",
"- **Don't** forget to serialize triples for storage\n",
"\n",
"### When to Use Each Component\n",
"\n",
"| Use Case | Recommended Component |\n",
"|----------|----------------------|\n",
"| Find entity connections | `RelationExtractor` |\n",
"| Build knowledge graphs | `TripleExtractor` |\n",
"| Quality assurance | `TripleValidator` |\n",
"| Export to RDF | `RDFSerializer` |\n",
"| Assess extraction quality | `TripleQualityChecker` |\n",
"\n",
"### Performance Tips\n",
"\n",
"1. **Extract entities once**, reuse for relations and triples\n",
"2. **Batch process** multiple documents together\n",
"3. **Cache extractors** instead of recreating\n",
"4. **Filter early** with confidence thresholds\n",
"5. **Validate incrementally** rather than all at once"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"### What You've Learned\n",
"\n",
"In this notebook, you've learned how to:\n",
"\n",
" **Extract relationships** using `RelationExtractor` \n",
" **Compare extraction methods** (pattern, dependency, co-occurrence, HuggingFace, LLM) \n",
" **Configure extraction parameters** for optimal results \n",
" **Extract RDF triples** with `TripleExtractor` \n",
" **Validate triples** using `TripleValidator` \n",
" **Serialize to RDF formats** with `RDFSerializer` \n",
" **Assess quality** with `TripleQualityChecker` \n",
" **Build complete pipelines** from entities to triples \n",
"\n",
"### Key Takeaways\n",
"\n",
"1. **Relations connect entities**: They form the backbone of knowledge graphs\n",
"2. **Multiple methods available**: Choose based on accuracy vs speed needs\n",
"3. **Configuration is powerful**: Tune parameters for your domain\n",
"4. **Triples are standardized**: Use RDF for interoperability\n",
"5. **Validation is essential**: Ensure quality before using triples\n",
"6. **Pipelines are efficient**: Extract entities → relations → triples in sequence\n",
"\n",
"### Next Steps\n",
"\n",
" **Next Notebook**: [07_Building_Knowledge_Graphs.ipynb](./07_Building_Knowledge_Graphs.ipynb) \n",
"Learn how to build complete knowledge graphs from your extracted triples!\n",
"\n",
" **Further Reading**:\n",
"- [Semantic Extract API Reference](https://semantica.readthedocs.io/reference/semantic_extract/)\n",
"- [Knowledge Graph Module](https://semantica.readthedocs.io/reference/kg/)\n",
"- [Advanced Graph Analytics](../advanced/02_Advanced_Graph_Analytics.ipynb)\n",
"\n",
"---\n",
"\n",
"**Questions or Issues?** Check out our [GitHub repository](https://github.com/Hawksight-AI/semantica) or [documentation](https://semantica.readthedocs.io)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,191 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/08_Building_Knowledge_Graphs.ipynb)\n",
"\n",
"# 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`, and `GraphValidator`.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/kg/)\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",
"**Note**: For deduplication, use the `semantica.deduplication` module.\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## 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.deduplication import DuplicateDetector, EntityMerger, MergeStrategy\n",
"\n",
"# Detect duplicates\n",
"detector = DuplicateDetector(similarity_threshold=0.8)\n",
"duplicate_groups = detector.detect_duplicate_groups(knowledge_graph.get('entities', []))\n",
"\n",
"# Merge duplicates\n",
"merger = EntityMerger()\n",
"merge_operations = merger.merge_duplicates(\n",
" knowledge_graph.get('entities', []),\n",
" strategy=MergeStrategy.KEEP_MOST_COMPLETE\n",
")\n",
"\n",
"deduplicated_entities = [op.merged_entity for op in merge_operations]\n",
"\n",
"print(f\"Original entities: {len(knowledge_graph.get('entities', []))}\")\n",
"print(f\"Deduplicated entities: {len(deduplicated_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",
"- **Deduplication**: Use `semantica.deduplication` module for removing 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
}
@@ -0,0 +1,289 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/09_Your_First_Knowledge_Graph.ipynb)\n",
"\n",
"# 🚀 Your First Knowledge Graph\n",
"\n",
"## Overview\n",
"\n",
"This notebook walks you through creating your first knowledge graph from a simple document. You'll learn the complete end-to-end workflow from ingesting a file to visualizing the resulting knowledge graph.\n",
"\n",
"> [!TIP]\n",
"> This is the perfect starting point if you are new to Semantica. No prior knowledge of knowledge graphs is required!\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/kg/)\n",
"\n",
"### 🎯 Learning Objectives\n",
"\n",
"- **Understand the Workflow**: Learn the `File → Parse → Extract → Graph` pipeline\n",
"- **Ingest Data**: Load documents using `FileIngestor`\n",
"- **Parse Content**: Extract text using `DocumentParser`\n",
"- **Extract Knowledge**: Identify entities using `NERExtractor`\n",
"- **Build Graph**: Construct a graph using `GraphBuilder`\n",
"- **Visualize**: See your graph come to life with `KGVisualizer`\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## 🔄 Simple End-to-End Workflow\n",
"\n",
"The complete workflow consists of four main steps:\n",
"\n",
"1. **📥 Ingest** - Load data from files or other sources\n",
"2. **📄 Parse** - Extract and structure content from documents\n",
"3. **⛏️ Extract** - Identify entities and relationships\n",
"4. **🕸️ Build Graph** - Construct the knowledge graph\n",
"\n",
"Each step is demonstrated in the code cells below.\n",
"\n",
"> [!TIP]\n",
"> **Alternative: Using Semantica Framework**\n",
"> \n",
"> For a simpler, high-level approach, you can use the `Semantica` framework class which orchestrates all these steps:\n",
"> \n",
"> ```python\n",
"> from semantica.core import Semantica\n",
"> \n",
"> framework = Semantica()\n",
"> framework.initialize()\n",
"> \n",
"> result = framework.build_knowledge_base(\n",
"> sources=[\"sample_document.txt\"],\n",
"> embeddings=True,\n",
"> graph=True\n",
"> )\n",
"> \n",
"> framework.shutdown()\n",
"> ```\n",
"> \n",
"> This notebook shows the step-by-step approach for learning. See [Core Module Usage Guide](../../../semantica/core/core_usage.md) for more details.\n",
"\n",
"---\n",
"\n",
"## 📂 Step 1: Ingest a File\n",
"\n",
"In this step, we'll use `FileIngestor` to load a document. The ingestor supports various file formats including PDF, DOCX, TXT, and more.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ingest import FileIngestor\n",
"from pathlib import Path\n",
"\n",
"# Initialize the ingestor\n",
"ingestor = FileIngestor()\n",
"\n",
"# Create a sample document for demonstration\n",
"sample_text = \"\"\"\n",
"Apple Inc. is a technology company founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in 1976.\n",
"The company is headquartered in Cupertino, California.\n",
"Tim Cook is the current CEO of Apple Inc.\n",
"Apple designs and manufactures consumer electronics, software, and online services.\n",
"\"\"\"\n",
"\n",
"sample_file = Path(\"sample_document.txt\")\n",
"sample_file.write_text(sample_text)\n",
"\n",
"print(f\"File: {sample_file}\")\n",
"print(f\"Content length: {len(sample_text)} characters\")\n",
"\n",
"# Ingest the file\n",
"file_object = ingestor.ingest_file(sample_file, read_content=True)\n",
"print(f\" File name: {file_object.name}\")\n",
"print(f\" File type: {file_object.file_type}\")\n",
"print(f\" Content available: {file_object.content is not None}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 📄 Step 2: Parse the Document\n",
"\n",
"After ingesting the file, we need to parse it to extract the text content. The `DocumentParser` handles various file formats and extracts structured content.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.parse import DocumentParser\n",
"\n",
"parser = DocumentParser()\n",
"\n",
"# Parse the document to extract text\n",
"parsed_content = parser.parse_document(str(sample_file))\n",
"print(f\" Parsed content length: {len(parsed_content) if parsed_content else 0} characters\")\n",
"print(f\" Preview: {parsed_content[:200] if parsed_content else 'N/A'}...\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ⛏️ Step 3: Extract Entities\n",
"\n",
"Now we'll extract entities from the parsed text using Named Entity Recognition (NER). This identifies people, organizations, locations, dates, and other entities in the text.\n",
"\n",
"> [!NOTE]\n",
"> In a real scenario, you would use `NERExtractor` with an LLM or model backend. Here we simulate the output for demonstration purposes.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.semantic_extract import NamedEntityRecognizer, NERExtractor\n",
"\n",
"ner = NamedEntityRecognizer()\n",
"extractor = NERExtractor()\n",
"\n",
"print(f\"\\nText: {parsed_content[:100]}...\")\n",
"\n",
"# Simulated extraction results\n",
"expected_entities = [\n",
" {\"text\": \"Apple Inc.\", \"type\": \"Organization\", \"start\": 0, \"end\": 10},\n",
" {\"text\": \"Steve Jobs\", \"type\": \"Person\", \"start\": 50, \"end\": 60},\n",
" {\"text\": \"Steve Wozniak\", \"type\": \"Person\", \"start\": 62, \"end\": 75},\n",
" {\"text\": \"Ronald Wayne\", \"type\": \"Person\", \"start\": 81, \"end\": 93},\n",
" {\"text\": \"1976\", \"type\": \"Date\", \"start\": 97, \"end\": 101},\n",
" {\"text\": \"Cupertino, California\", \"type\": \"Location\", \"start\": 130, \"end\": 151},\n",
" {\"text\": \"Tim Cook\", \"type\": \"Person\", \"start\": 153, \"end\": 161},\n",
"]\n",
"\n",
"for entity in expected_entities:\n",
" print(f\" - {entity['text']} ({entity['type']})\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 🕸️ Step 4: Build the Knowledge Graph\n",
"\n",
"Using the extracted entities and relationships, we'll construct a knowledge graph. The graph represents entities as nodes and relationships as edges.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.kg import GraphBuilder\n",
"import networkx as nx\n",
"\n",
"builder = GraphBuilder()\n",
"\n",
"# Prepare data for graph construction\n",
"entities_data = [\n",
" {\"id\": f\"entity_{i}\", \"name\": entity[\"text\"], \"type\": entity[\"type\"]}\n",
" for i, entity in enumerate(expected_entities)\n",
"]\n",
"\n",
"relationships_data = [\n",
" {\"source\": \"entity_0\", \"target\": \"entity_1\", \"type\": \"founded_by\"},\n",
" {\"source\": \"entity_0\", \"target\": \"entity_2\", \"type\": \"founded_by\"},\n",
" {\"source\": \"entity_0\", \"target\": \"entity_3\", \"type\": \"founded_by\"},\n",
" {\"source\": \"entity_0\", \"target\": \"entity_4\", \"type\": \"founded_in\"},\n",
" {\"source\": \"entity_0\", \"target\": \"entity_5\", \"type\": \"located_in\"},\n",
" {\"source\": \"entity_6\", \"target\": \"entity_0\", \"type\": \"ceo_of\"},\n",
"]\n",
"\n",
"# Build the graph using NetworkX\n",
"kg = nx.DiGraph()\n",
"\n",
"for entity in entities_data:\n",
" kg.add_node(entity[\"id\"], name=entity[\"name\"], type=entity[\"type\"])\n",
"\n",
"for rel in relationships_data:\n",
" source_name = entities_data[int(rel[\"source\"].split(\"_\")[1])][\"name\"]\n",
" target_name = entities_data[int(rel[\"target\"].split(\"_\")[1])][\"name\"]\n",
" kg.add_edge(rel[\"source\"], rel[\"target\"], type=rel[\"type\"])\n",
"\n",
"print(f\" Nodes (entities): {len(kg.nodes)}\")\n",
"print(f\" Edges (relationships): {len(kg.edges)}\")\n",
"\n",
"for node_id in kg.nodes():\n",
" node_data = kg.nodes[node_id]\n",
" print(f\" Node: {node_data['name']} ({node_data['type']})\")\n",
"\n",
"for source, target, data in kg.edges(data=True):\n",
" source_name = kg.nodes[source]['name']\n",
" target_name = kg.nodes[target]['name']\n",
" print(f\" {source_name} --[{data['type']}]--> {target_name}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 📊 Step 5: Visualize and Analyze\n",
"\n",
"Finally, we'll visualize the knowledge graph and analyze its structure. This helps you understand the relationships and entities in your data.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.visualization import KGVisualizer\n",
"\n",
"visualizer = KGVisualizer()\n",
"\n",
"print(f\" Total entities: {len(kg.nodes)}\")\n",
"print(f\" Total relationships: {len(kg.edges)}\")\n",
"\n",
"entity_types = {}\n",
"for node_id in kg.nodes():\n",
" entity_type = kg.nodes[node_id]['type']\n",
" entity_types[entity_type] = entity_types.get(entity_type, 0) + 1\n",
"\n",
"for etype, count in entity_types.items():\n",
" print(f\" - {etype}: {count}\")\n",
"\n",
"rel_types = {}\n",
"for _, _, data in kg.edges(data=True):\n",
" rel_type = data.get('type', 'unknown')\n",
" rel_types[rel_type] = rel_types.get(rel_type, 0) + 1\n",
"\n",
"for rtype, count in rel_types.items():\n",
" print(f\" - {rtype}: {count}\")\n",
"\n",
"# Cleanup\n",
"if sample_file.exists():\n",
" sample_file.unlink()\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
+601
View File
@@ -0,0 +1,601 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Graph Store Module\n",
"\n",
"## Overview\n",
"\n",
"The Graph Store module provides a unified interface for working with property graph databases. It supports multiple backends (Neo4j, FalkorDB) and offers comprehensive features for storing, querying, and analyzing graph data.\n",
"\n",
"### Key Features\n",
"\n",
"- **Multi-Backend Support**: Neo4j (Enterprise), FalkorDB (Redis-based)\n",
"- **Full CRUD Operations**: Create, read, update, delete nodes and relationships\n",
"- **Cypher Query Language**: Execute complex graph queries with OpenCypher support\n",
"- **Graph Analytics**: Built-in algorithms for centrality, community detection, path finding\n",
"- **Batch Operations**: Optimized bulk data loading with progress tracking\n",
"- **Transaction Support**: ACID transactions with rollback capabilities\n",
"- **Index Management**: Create and manage indexes for performance optimization\n",
"- **Convenience Functions**: Simple function-based API for common operations\n",
"\n",
"### Learning Objectives\n",
"\n",
"By the end of this notebook, you will be able to:\n",
"\n",
"1. Initialize and configure GraphStore with different backends\n",
"2. Perform CRUD operations on nodes and relationships\n",
"3. Execute Cypher queries for complex graph operations\n",
"4. Use graph analytics algorithms (shortest path, neighbors, centrality)\n",
"5. Update and delete graph data\n",
"6. Use batch operations for efficient data loading\n",
"7. Work with convenience functions and configuration management\n",
"8. Choose the right backend for your use case\n",
"\n",
"---\n",
"\n",
"## Installation\n",
"\n",
"### Core Installation\n",
"\n",
"```bash\n",
"# Install Semantica\n",
"pip install semantica\n",
"\n",
"# Or install with all optional dependencies\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"### Backend-Specific Dependencies\n",
"\n",
"```bash\n",
"# For Neo4j (requires Neo4j server)\n",
"pip install neo4j\n",
"\n",
"# For FalkorDB (requires Redis/FalkorDB server)\n",
"pip install falkordb\n",
"```\n",
"\n",
"### Docker Setup (Optional)\n",
"\n",
"For FalkorDB, you can run it in Docker:\n",
"\n",
"```bash\n",
"docker run -p 6379:6379 -p 3000:3000 -it --rm \\\n",
" -v ./data:/var/lib/falkordb/data \\\n",
" falkordb/falkordb\n",
"```\n",
"\n",
"---\n",
"\n",
"## Backend Comparison\n",
"\n",
"| Backend | Best For | Deployment | Features |\n",
"|---------|----------|------------|----------|\n",
"| **Neo4j** | Enterprise applications, production systems | Server/Cloud | Full Cypher, APOC procedures, multi-database |\n",
"| **FalkorDB** | LLM applications, real-time systems, high performance | Redis-based | Ultra-fast, sparse matrix operations |\n",
"\n",
"**Recommendation**: Use **Neo4j** for enterprise production systems or **FalkorDB** for high-performance real-time applications.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Initialize Graph Store\n",
"\n",
"Initialize a `GraphStore` instance with your preferred backend. For this tutorial, we'll use **Neo4j** (requires a running server).\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.graph_store import GraphStore\n",
"\n",
"# Option 1: Neo4j (requires Neo4j server running)\n",
"store = GraphStore(\n",
" backend=\"neo4j\",\n",
" uri=\"bolt://localhost:7687\",\n",
" user=\"neo4j\",\n",
" password=\"password\"\n",
")\n",
"\n",
"# Option 2: FalkorDB (requires Redis/FalkorDB server)\n",
"# store = GraphStore(\n",
"# backend=\"falkordb\",\n",
"# host=\"localhost\",\n",
"# port=6379,\n",
"# graph_name=\"demo_graph\"\n",
"# )\n",
"\n",
"# Connect to the database\n",
"store.connect()\n",
"print(\"Connected to graph database successfully!\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Node Operations\n",
"\n",
"### Creating Nodes\n",
"\n",
"Nodes represent entities in your graph. Each node can have:\n",
"- **Labels**: Categories/types (e.g., `Person`, `Company`, `Location`)\n",
"- **Properties**: Key-value pairs (e.g., `{\"name\": \"Alice\", \"age\": 30}`)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create individual nodes with labels and properties\n",
"apple = store.create_node(\n",
" labels=[\"Company\"],\n",
" properties={\"name\": \"Apple Inc.\", \"founded\": 1976, \"industry\": \"Technology\"}\n",
")\n",
"print(f\"Created company node: {apple.get('properties', {}).get('name')} (ID: {apple.get('id')})\")\n",
"\n",
"tim_cook = store.create_node(\n",
" labels=[\"Person\"],\n",
" properties={\"name\": \"Tim Cook\", \"title\": \"CEO\", \"age\": 63}\n",
")\n",
"print(f\"Created person node: {tim_cook.get('properties', {}).get('name')} (ID: {tim_cook.get('id')})\")\n",
"\n",
"cupertino = store.create_node(\n",
" labels=[\"Location\"],\n",
" properties={\"name\": \"Cupertino\", \"state\": \"California\", \"country\": \"USA\"}\n",
")\n",
"print(f\"Created location node: {cupertino.get('properties', {}).get('name')} (ID: {cupertino.get('id')})\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create multiple nodes in batch (more efficient for large datasets)\n",
"other_companies = store.create_nodes([\n",
" {\"labels\": [\"Company\"], \"properties\": {\"name\": \"Microsoft\", \"founded\": 1975}},\n",
" {\"labels\": [\"Company\"], \"properties\": {\"name\": \"Google\", \"founded\": 1998}},\n",
" {\"labels\": [\"Company\"], \"properties\": {\"name\": \"Amazon\", \"founded\": 1994}},\n",
"])\n",
"print(f\"Created {len(other_companies)} company nodes in batch\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Relationship Operations\n",
"\n",
"### Creating Relationships\n",
"\n",
"Relationships connect nodes and represent connections between entities. Each relationship has:\n",
"- **Type**: The relationship type (e.g., `CEO_OF`, `LOCATED_IN`, `KNOWS`)\n",
"- **Properties**: Key-value pairs (e.g., `{\"since\": 2011}`)\n",
"- **Direction**: From `start_node_id` to `end_node_id`\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create relationships between nodes\n",
"ceo_rel = store.create_relationship(\n",
" start_node_id=tim_cook[\"id\"],\n",
" end_node_id=apple[\"id\"],\n",
" rel_type=\"CEO_OF\",\n",
" properties={\"since\": 2011}\n",
")\n",
"print(f\"Created relationship: {ceo_rel.get('type')} (ID: {ceo_rel.get('id')})\")\n",
"\n",
"location_rel = store.create_relationship(\n",
" start_node_id=apple[\"id\"],\n",
" end_node_id=cupertino[\"id\"],\n",
" rel_type=\"HEADQUARTERED_IN\",\n",
" properties={\"since\": 1977}\n",
")\n",
"print(f\"Created relationship: {location_rel.get('type')} (ID: {location_rel.get('id')})\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Querying Nodes and Relationships\n",
"\n",
"### Retrieving Nodes\n",
"\n",
"You can query nodes by labels, properties, or node IDs.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get nodes by label\n",
"companies = store.get_nodes(labels=[\"Company\"], limit=10)\n",
"print(f\"Found {len(companies)} companies:\")\n",
"for company in companies:\n",
" name = company.get('properties', {}).get('name', 'Unknown')\n",
" founded = company.get('properties', {}).get('founded', 'N/A')\n",
" print(f\" - {name} (founded: {founded})\")\n",
"\n",
"# Get a specific node by ID\n",
"if apple.get('id'):\n",
" node = store.get_node(node_id=apple[\"id\"])\n",
" print(f\"\\nRetrieved node by ID: {node.get('properties', {}).get('name')}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get relationships for a node\n",
"relationships = store.get_relationships(node_id=apple[\"id\"], direction=\"both\")\n",
"print(f\"Found {len(relationships)} relationships for Apple:\")\n",
"for rel in relationships:\n",
" rel_type = rel.get('type', 'Unknown')\n",
" props = rel.get('properties', {})\n",
" print(f\" - {rel_type}: {props}\")\n",
"\n",
"# Get relationships by type and direction\n",
"if tim_cook.get('id'):\n",
" outgoing = store.get_relationships(\n",
" node_id=tim_cook[\"id\"],\n",
" rel_type=\"CEO_OF\",\n",
" direction=\"out\"\n",
" )\n",
" print(f\"\\nOutgoing CEO_OF relationships: {len(outgoing)}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Cypher Query Execution\n",
"\n",
"### Executing Cypher Queries\n",
"\n",
"Cypher is a powerful graph query language that allows you to express complex graph patterns and operations. The Graph Store module supports **OpenCypher** syntax across all backends.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Execute a Cypher query to find CEO relationships\n",
"results = store.execute_query(\"\"\"\n",
" MATCH (p:Person)-[r:CEO_OF]->(c:Company)\n",
" RETURN p.name as person, c.name as company, r.since as since\n",
"\"\"\")\n",
"\n",
"print(\"CEO Relationships:\")\n",
"for record in results.get(\"records\", []):\n",
" person = record.get('person', 'Unknown')\n",
" company = record.get('company', 'Unknown')\n",
" since = record.get('since', 'N/A')\n",
" print(f\" - {person} is CEO of {company} since {since}\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Parameterized query (safer and more efficient)\n",
"results = store.execute_query(\n",
" \"MATCH (c:Company) WHERE c.founded > $year RETURN c.name, c.founded ORDER BY c.founded\",\n",
" parameters={\"year\": 1990}\n",
")\n",
"\n",
"print(\"Companies founded after 1990:\")\n",
"for record in results.get(\"records\", []):\n",
" name = record.get('c.name', 'Unknown')\n",
" founded = record.get('c.founded', 'N/A')\n",
" print(f\" - {name} (founded: {founded})\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Graph Analytics\n",
"\n",
"### Built-in Analytics Algorithms\n",
"\n",
"The Graph Store module provides several graph analytics algorithms for analyzing your graph structure.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get neighbors of a node (traverse the graph)\n",
"if apple.get('id'):\n",
" neighbors = store.get_neighbors(\n",
" node_id=apple[\"id\"],\n",
" direction=\"both\",\n",
" depth=2\n",
" )\n",
" \n",
" print(f\"Found {len(neighbors)} neighbors (up to depth 2) for Apple:\")\n",
" for neighbor in neighbors:\n",
" name = neighbor.get('properties', {}).get('name', 'Unknown')\n",
" labels = neighbor.get('labels', [])\n",
" print(f\" - {name} ({', '.join(labels)})\")\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Find shortest path between two nodes\n",
"if tim_cook.get('id') and cupertino.get('id'):\n",
" path = store.shortest_path(\n",
" start_node_id=tim_cook[\"id\"],\n",
" end_node_id=cupertino[\"id\"],\n",
" max_depth=5\n",
" )\n",
" \n",
" if path:\n",
" print(f\"Shortest path found:\")\n",
" print(f\" - Path length: {path.get('length')}\")\n",
" print(f\" - Nodes in path: {len(path.get('nodes', []))}\")\n",
" print(f\" - Relationships: {len(path.get('relationships', []))}\")\n",
" else:\n",
" print(\"No path found between the nodes\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7: Update and Delete Operations\n",
"\n",
"### Updating Nodes\n",
"\n",
"You can update node properties using the `update_node` method.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Update node properties (merge mode - adds/updates properties)\n",
"if tim_cook.get('id'):\n",
" updated = store.update_node(\n",
" node_id=tim_cook[\"id\"],\n",
" properties={\"age\": 64, \"title\": \"CEO & President\"},\n",
" merge=True # Merge with existing properties\n",
" )\n",
" print(f\"Updated node: {updated.get('properties', {}).get('name')}\")\n",
" print(f\" New age: {updated.get('properties', {}).get('age')}\")\n",
" print(f\" New title: {updated.get('properties', {}).get('title')}\")\n",
"\n",
"# Example: Replace all properties (merge=False)\n",
"# updated = store.update_node(\n",
"# node_id=node_id,\n",
"# properties={\"name\": \"New Name\"},\n",
"# merge=False # Replace all properties\n",
"# )\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8: Delete Operations\n",
"\n",
"### Deleting Nodes and Relationships\n",
"\n",
"You can delete nodes and relationships when needed.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Delete a relationship\n",
"if location_rel.get('id'):\n",
" deleted = store.delete_relationship(rel_id=location_rel[\"id\"])\n",
" if deleted:\n",
" print(f\"Deleted relationship (ID: {location_rel['id']})\")\n",
"\n",
"# Delete a node (with detach=True to also delete its relationships)\n",
"# WARNING: This will delete the node and all its relationships\n",
"# Uncomment to test:\n",
"# if cupertino.get('id'):\n",
"# deleted = store.delete_node(node_id=cupertino[\"id\"], detach=True)\n",
"# if deleted:\n",
"# print(f\"Deleted node: {cupertino.get('properties', {}).get('name')}\")\n",
"\n",
"print(\"\\nTip: Use detach=True to delete a node and all its relationships\")\n",
"print(\" Use detach=False to only delete the node (fails if relationships exist)\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 9: Graph Statistics\n",
"\n",
"Get comprehensive statistics about your graph.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get comprehensive graph statistics\n",
"stats = store.get_stats()\n",
"\n",
"print(\"Graph Statistics:\")\n",
"print(f\" Total nodes: {stats.get('node_count', 'N/A')}\")\n",
"print(f\" Total relationships: {stats.get('relationship_count', 'N/A')}\")\n",
"print(f\"\\nNode labels:\")\n",
"for label, count in stats.get('label_counts', {}).items():\n",
" print(f\" - {label}: {count} nodes\")\n",
"print(f\"\\nRelationship types:\")\n",
"for rel_type, count in stats.get('relationship_type_counts', {}).items():\n",
" print(f\" - {rel_type}: {count} relationships\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 10: Convenience Functions\n",
"\n",
"The Graph Store module provides convenience functions for simpler, function-based operations.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Using convenience functions (alternative to class methods)\n",
"from semantica.graph_store import (\n",
" create_node,\n",
" create_relationship,\n",
" get_nodes,\n",
" execute_query,\n",
" shortest_path\n",
")\n",
"\n",
"# These functions work with a default store instance\n",
"# For this example, we'll continue using the store instance we created\n",
"\n",
"# Example: Using convenience functions\n",
"# node = create_node(\n",
"# labels=[\"Person\"],\n",
"# properties={\"name\": \"Alice\", \"age\": 30}\n",
"# )\n",
"\n",
"print(\"Convenience functions available:\")\n",
"print(\" - create_node, create_nodes\")\n",
"print(\" - create_relationship, create_relationships\")\n",
"print(\" - get_nodes, get_relationships\")\n",
"print(\" - update_node, delete_node\")\n",
"print(\" - execute_query, shortest_path, get_neighbors\")\n",
"print(\" - run_analytics\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 11: Index Management\n",
"\n",
"Create indexes to improve query performance, especially for large graphs.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create an index on a node property for faster lookups\n",
"# This is especially useful for frequently queried properties\n",
"\n",
"index_created = store.create_index(\n",
" label=\"Company\",\n",
" property_name=\"name\",\n",
" index_type=\"btree\" # Default index type\n",
")\n",
"\n",
"if index_created:\n",
" print(\"Created index on Company.name for faster queries\")\n",
"else:\n",
" print(\"Index may already exist or not be supported by this backend\")\n",
"\n",
"# Note: Index creation support varies by backend\n",
"# Neo4j: Full support for various index types\n",
"# FalkorDB: Limited index support\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 12: Clean Up\n",
"\n",
"Always close the connection when you're done to free up resources.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Close the connection\n",
"store.close()\n",
"print(\"Connection closed successfully\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"This notebook covered the Graph Store module, a unified interface for property graph databases supporting Neo4j and FalkorDB.\n",
"\n",
"### What You Learned\n",
"\n",
"- **CRUD Operations**: Create, read, update, and delete nodes and relationships\n",
"- **Cypher Queries**: Execute complex graph queries with OpenCypher syntax\n",
"- **Graph Analytics**: Shortest path, neighbor traversal, and centrality algorithms\n",
"- **Batch Operations**: Efficient bulk data loading for large datasets\n",
"- **Index Management**: Performance optimization through indexing\n",
"\n",
"### Key Takeaways\n",
"\n",
"- **Backend Selection**: Use Neo4j for production, FalkorDB for high-performance applications\n",
"- **Best Practices**: Use batch operations, parameterized queries, and proper connection management\n",
"- **Next Steps**: Explore advanced analytics, graph quality, and visualization modules\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,178 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/11_Graph_Analytics.ipynb)\n",
"\n",
"# 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",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/kg/)\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",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## 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_result = centrality_calculator.calculate_degree_centrality(kg)\n",
"centrality_scores = centrality_result.get('centrality', {})\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
}
@@ -0,0 +1,853 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/11_Chunking_and_Splitting.ipynb)\n",
"\n",
"# Chunking and Splitting - Comprehensive Guide\n",
"\n",
"## Overview\n",
"\n",
"This notebook provides a **comprehensive walkthrough** of Semantica's split module, demonstrating all chunking strategies and methods for optimal document processing. You'll learn to use 15+ splitting methods including standard, semantic, and knowledge graph-aware approaches.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/split/)\n",
"\n",
"### Learning Objectives\n",
"\n",
"By the end of this notebook, you will be able to:\n",
"\n",
"- Use `TextSplitter` with multiple methods\n",
"- Apply standard splitting methods (recursive, token, sentence, paragraph)\n",
"- Use semantic chunking for topic coherence\n",
"- Apply KG-aware chunking (entity-aware, relation-aware, graph-based)\n",
"- Use specialized chunkers (structural, sliding window, table, hierarchical)\n",
"- Validate chunk quality with `ChunkValidator`\n",
"- Track provenance with `ProvenanceTracker`\n",
"- Choose the right method for your use case\n",
"\n",
"### What You'll Learn\n",
"\n",
"| Component | Purpose | When to Use |\n",
"|-----------|---------|-------------|\n",
"| `TextSplitter` | Unified splitter | All chunking needs |\n",
"| `SemanticChunker` | Semantic boundaries | Topic-based chunks |\n",
"| `EntityAwareChunker` | Preserve entities | GraphRAG workflows |\n",
"| `RelationAwareChunker` | Preserve triples | KG construction |\n",
"| `StructuralChunker` | Document structure | Formatted documents |\n",
"| `HierarchicalChunker` | Multi-level chunks | Large documents |\n",
"\n",
"---\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Basic Chunking with TextSplitter\n",
"\n",
"Let's start with the unified `TextSplitter` interface, which provides access to all chunking methods.\n",
"\n",
"### What is TextSplitter?\n",
"\n",
"`TextSplitter` is a unified interface that supports 15+ chunking methods:\n",
"- **Standard**: recursive, token, sentence, paragraph, character, word\n",
"- **Semantic**: semantic_transformer, llm, huggingface, nltk\n",
"- **KG/Ontology**: entity_aware, relation_aware, graph_based, ontology_aware\n",
"- **Advanced**: hierarchical, structural, sliding_window, table"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.split import TextSplitter\n",
"\n",
"# Sample long text\n",
"text = \"\"\"\n",
"Apple Inc. is a technology company founded by Steve Jobs, Steve Wozniak, and Ronald Wayne \n",
"in Cupertino, California on April 1, 1976. The company's current CEO is Tim Cook, who took \n",
"over from Steve Jobs in August 2011. Apple is headquartered at One Apple Park Way in Cupertino.\n",
"\n",
"Apple develops and sells consumer electronics, computer software, and online services. The company's \n",
"hardware products include the iPhone smartphone, the iPad tablet computer, the Mac personal computer, \n",
"the iPod portable media player, the Apple Watch smartwatch, the Apple TV digital media player, and the \n",
"HomePod smart speaker.\n",
"\n",
"Apple's software includes the macOS and iOS operating systems, the iTunes media player, the Safari web \n",
"browser, and the iLife and iWork creativity and productivity suites. Its online services include the \n",
"iTunes Store, the iOS App Store and Mac App Store, Apple Music, and iCloud.\n",
"\"\"\"\n",
"\n",
"# Basic recursive splitting\n",
"splitter = TextSplitter(\n",
" method=\"recursive\",\n",
" chunk_size=200,\n",
" chunk_overlap=50\n",
")\n",
"\n",
"chunks = splitter.split(text)\n",
"\n",
"print(f\"Split into {len(chunks)} chunks using recursive method\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, chunk in enumerate(chunks, 1):\n",
" print(f\"\\nChunk {i}:\")\n",
" print(f\" Length: {len(chunk.text)} characters\")\n",
" print(f\" Start: {chunk.start}, End: {chunk.end}\")\n",
" print(f\" Text: {chunk.text[:100]}...\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Standard Splitting Methods\n",
"\n",
"Let's compare different standard splitting methods.\n",
"\n",
"### Method Comparison\n",
"\n",
"| Method | Best For | Speed | Accuracy |\n",
"|--------|----------|-------|----------|\n",
"| **recursive** | General text | Fast | Good |\n",
"| **sentence** | Coherent chunks | Medium | Very Good |\n",
"| **token** | LLM context | Medium | Excellent |\n",
"| **paragraph** | Natural breaks | Fast | Good |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Compare different methods\n",
"methods = [\"recursive\", \"sentence\", \"paragraph\"]\n",
"\n",
"print(\"Comparing Standard Splitting Methods:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for method in methods:\n",
" splitter = TextSplitter(\n",
" method=method,\n",
" chunk_size=200,\n",
" chunk_overlap=50\n",
" )\n",
" \n",
" chunks = splitter.split(text)\n",
" \n",
" print(f\"\\nMethod: {method.upper()}\")\n",
" print(\"-\" * 40)\n",
" print(f\" Chunks created: {len(chunks)}\")\n",
" print(f\" Avg chunk size: {sum(len(c.text) for c in chunks) / len(chunks):.0f} chars\")\n",
" print(f\" First chunk: {chunks[0].text[:80]}...\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Token-Based Splitting\n",
"\n",
"Token-based splitting is crucial for LLM applications where you need to respect token limits.\n",
"\n",
"### Why Token-Based?\n",
"\n",
"- **LLM Context Windows**: GPT-4 has 8K/32K token limits\n",
"- **Accurate Counting**: Character count ≠ token count\n",
"- **Cost Optimization**: Tokens determine API costs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.split import split_by_tokens\n",
"\n",
"# Token-based splitting\n",
"chunks = split_by_tokens(\n",
" text,\n",
" chunk_size=100, # 100 tokens\n",
" chunk_overlap=20,\n",
" tokenizer=\"tiktoken\",\n",
" model=\"gpt-4\"\n",
")\n",
"\n",
"print(\"Token-Based Splitting Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, chunk in enumerate(chunks, 1):\n",
" token_count = chunk.metadata.get('token_count', 'N/A')\n",
" print(f\"\\nChunk {i}:\")\n",
" print(f\" Tokens: {token_count}\")\n",
" print(f\" Characters: {len(chunk.text)}\")\n",
" print(f\" Ratio: {len(chunk.text)/token_count if token_count != 'N/A' else 'N/A':.2f} chars/token\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Semantic Chunking\n",
"\n",
"Semantic chunking creates chunks based on semantic boundaries using embeddings.\n",
"\n",
"### How It Works\n",
"\n",
"1. Split text into sentences\n",
"2. Generate embeddings for each sentence\n",
"3. Calculate similarity between consecutive sentences\n",
"4. Create boundaries where similarity drops below threshold"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.split import SemanticChunker\n",
"\n",
"# Semantic chunking\n",
"semantic_chunker = SemanticChunker(\n",
" chunk_size=200,\n",
" chunk_overlap=50,\n",
" embedding_model=\"all-MiniLM-L6-v2\",\n",
" similarity_threshold=0.7\n",
")\n",
"\n",
"chunks = semantic_chunker.chunk(text)\n",
"\n",
"print(\"Semantic Chunking Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, chunk in enumerate(chunks, 1):\n",
" coherence = chunk.metadata.get('coherence_score', 'N/A')\n",
" print(f\"\\nChunk {i}:\")\n",
" print(f\" Length: {len(chunk.text)} chars\")\n",
" print(f\" Coherence: {coherence}\")\n",
" print(f\" Text: {chunk.text[:100]}...\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Entity-Aware Chunking for GraphRAG\n",
"\n",
"Entity-aware chunking preserves entity boundaries, crucial for GraphRAG workflows.\n",
"\n",
"### Why Entity-Aware?\n",
"\n",
"- **Preserve Entities**: Don't split \"Steve Jobs\" across chunks\n",
"- **Better Extraction**: Complete entities improve NER accuracy\n",
"- **GraphRAG**: Essential for knowledge graph construction"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.split import EntityAwareChunker\n",
"\n",
"# Entity-aware chunking\n",
"entity_chunker = EntityAwareChunker(\n",
" chunk_size=200,\n",
" chunk_overlap=50,\n",
" ner_method=\"spacy\", # or \"llm\" for better accuracy\n",
" preserve_entities=True\n",
")\n",
"\n",
"chunks = entity_chunker.chunk(text)\n",
"\n",
"print(\"Entity-Aware Chunking Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, chunk in enumerate(chunks, 1):\n",
" entities = chunk.metadata.get('entities', [])\n",
" print(f\"\\nChunk {i}:\")\n",
" print(f\" Length: {len(chunk.text)} chars\")\n",
" print(f\" Entities: {len(entities)}\")\n",
" \n",
" if entities:\n",
" entity_texts = [e.get('text', e.get('entity', '')) if isinstance(e, dict) else str(e) for e in entities[:3]]\n",
" print(f\" Sample entities: {entity_texts}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Relation-Aware Chunking\n",
"\n",
"Relation-aware chunking preserves relationship triples within chunks.\n",
"\n",
"### Why Relation-Aware?\n",
"\n",
"- **Preserve Triples**: Keep (subject, predicate, object) together\n",
"- **KG Construction**: Better for building knowledge graphs\n",
"- **Context**: Relationships need complete context"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.split import RelationAwareChunker\n",
"\n",
"# Relation-aware chunking\n",
"relation_chunker = RelationAwareChunker(\n",
" chunk_size=200,\n",
" chunk_overlap=50,\n",
" preserve_triples=True\n",
")\n",
"\n",
"chunks = relation_chunker.chunk(text)\n",
"\n",
"print(\"Relation-Aware Chunking Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, chunk in enumerate(chunks, 1):\n",
" triples = chunk.metadata.get('triples', [])\n",
" relationships = chunk.metadata.get('relationships', [])\n",
" \n",
" print(f\"\\nChunk {i}:\")\n",
" print(f\" Length: {len(chunk.text)} chars\")\n",
" print(f\" Triples: {len(triples)}\")\n",
" print(f\" Relationships: {len(relationships)}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7: Structural Chunking\n",
"\n",
"Structural chunking respects document structure like headings, paragraphs, and lists.\n",
"\n",
"### When to Use?\n",
"\n",
"- **Formatted Documents**: Markdown, HTML, structured text\n",
"- **Preserve Hierarchy**: Keep sections together\n",
"- **Better Context**: Headings provide context"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.split import StructuralChunker\n",
"\n",
"# Markdown text with structure\n",
"markdown_text = \"\"\"\n",
"# Apple Inc.\n",
"\n",
"## History\n",
"\n",
"Apple Inc. was founded by Steve Jobs, Steve Wozniak, and Ronald Wayne in 1976.\n",
"\n",
"## Products\n",
"\n",
"### Hardware\n",
"- iPhone\n",
"- iPad\n",
"- Mac\n",
"\n",
"### Software\n",
"- macOS\n",
"- iOS\n",
"- Safari\n",
"\"\"\"\n",
"\n",
"# Structural chunking\n",
"structural_chunker = StructuralChunker(\n",
" respect_headings=True,\n",
" respect_paragraphs=True,\n",
" respect_lists=True,\n",
" max_chunk_size=500\n",
")\n",
"\n",
"chunks = structural_chunker.chunk(markdown_text)\n",
"\n",
"print(\"Structural Chunking Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, chunk in enumerate(chunks, 1):\n",
" section = chunk.metadata.get('section_title', 'N/A')\n",
" level = chunk.metadata.get('heading_level', 'N/A')\n",
" \n",
" print(f\"\\nChunk {i}:\")\n",
" print(f\" Section: {section}\")\n",
" print(f\" Level: {level}\")\n",
" print(f\" Text: {chunk.text[:80]}...\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8: Hierarchical Chunking\n",
"\n",
"Hierarchical chunking creates multi-level chunks for large documents.\n",
"\n",
"### Benefits\n",
"\n",
"- **Multiple Granularities**: Document → Section → Paragraph\n",
"- **Better Navigation**: Parent-child relationships\n",
"- **Flexible Retrieval**: Query at different levels"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.split import HierarchicalChunker\n",
"\n",
"# Hierarchical chunking\n",
"hierarchical_chunker = HierarchicalChunker(\n",
" chunk_sizes=[400, 200, 100], # 3 levels\n",
" chunk_overlaps=[80, 40, 20],\n",
" create_parent_chunks=True\n",
")\n",
"\n",
"chunks = hierarchical_chunker.chunk(text)\n",
"\n",
"print(\"Hierarchical Chunking Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, chunk in enumerate(chunks, 1):\n",
" level = chunk.metadata.get('level', 'N/A')\n",
" parent_id = chunk.metadata.get('parent_id', None)\n",
" child_ids = chunk.metadata.get('child_ids', [])\n",
" \n",
" print(f\"\\nChunk {i}:\")\n",
" print(f\" Level: {level}\")\n",
" print(f\" Length: {len(chunk.text)} chars\")\n",
" print(f\" Parent: {parent_id if parent_id else 'None (root)'}\")\n",
" print(f\" Children: {len(child_ids)}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 9: Sliding Window Chunking\n",
"\n",
"Sliding window creates overlapping fixed-size chunks.\n",
"\n",
"### Use Cases\n",
"\n",
"- **Dense Retrieval**: Ensure no information is missed\n",
"- **Fixed Context**: Consistent chunk sizes\n",
"- **Overlap Control**: Precise overlap management"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.split import SlidingWindowChunker\n",
"\n",
"# Sliding window chunking\n",
"sliding_chunker = SlidingWindowChunker(\n",
" window_size=150,\n",
" step_size=100, # 50 char overlap\n",
" min_chunk_size=50\n",
")\n",
"\n",
"chunks = sliding_chunker.chunk(text)\n",
"\n",
"print(\"Sliding Window Chunking Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, chunk in enumerate(chunks, 1):\n",
" overlap = chunk.metadata.get('overlap_chars', 0)\n",
" \n",
" print(f\"\\nWindow {i}:\")\n",
" print(f\" Position: {chunk.start}-{chunk.end}\")\n",
" print(f\" Length: {len(chunk.text)} chars\")\n",
" print(f\" Overlap with previous: {overlap} chars\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 10: Table Chunking\n",
"\n",
"Table chunking preserves table structure while splitting large tables.\n",
"\n",
"### Features\n",
"\n",
"- **Preserve Headers**: Keep column headers in each chunk\n",
"- **Row-Based Splitting**: Split by rows, not characters\n",
"- **Context Inclusion**: Include surrounding text"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.split import TableChunker\n",
"\n",
"# Text with table\n",
"text_with_table = \"\"\"\n",
"Apple's product lineup includes:\n",
"\n",
"| Product | Category | Release Year |\n",
"|---------|----------|-------------|\n",
"| iPhone | Smartphone | 2007 |\n",
"| iPad | Tablet | 2010 |\n",
"| Mac | Computer | 1984 |\n",
"| Apple Watch | Wearable | 2015 |\n",
"| AirPods | Audio | 2016 |\n",
"\n",
"These products have revolutionized their respective categories.\n",
"\"\"\"\n",
"\n",
"# Table chunking\n",
"table_chunker = TableChunker(\n",
" preserve_headers=True,\n",
" max_rows_per_chunk=3,\n",
" include_context=True,\n",
" table_format=\"markdown\"\n",
")\n",
"\n",
"chunks = table_chunker.chunk(text_with_table)\n",
"\n",
"print(\"Table Chunking Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"for i, chunk in enumerate(chunks, 1):\n",
" is_table = chunk.metadata.get('is_table', False)\n",
" \n",
" print(f\"\\nChunk {i}:\")\n",
" print(f\" Type: {'Table' if is_table else 'Text'}\")\n",
" \n",
" if is_table:\n",
" rows = chunk.metadata.get('row_count', 'N/A')\n",
" cols = chunk.metadata.get('column_count', 'N/A')\n",
" print(f\" Rows: {rows}, Columns: {cols}\")\n",
" \n",
" print(f\" Content: {chunk.text[:100]}...\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 11: Chunk Validation\n",
"\n",
"Validate chunk quality to ensure optimal processing.\n",
"\n",
"### Validation Checks\n",
"\n",
"- **Size Constraints**: Min/max chunk size\n",
"- **Overlap**: Appropriate overlap percentage\n",
"- **Completeness**: Full text coverage\n",
"- **Quality Score**: Overall quality metric"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.split import ChunkValidator\n",
"\n",
"# Create chunks\n",
"splitter = TextSplitter(method=\"recursive\", chunk_size=200, chunk_overlap=50)\n",
"chunks = splitter.split(text)\n",
"\n",
"# Validate chunks\n",
"validator = ChunkValidator(\n",
" min_chunk_size=50,\n",
" max_chunk_size=300,\n",
" min_overlap=20,\n",
" max_overlap=100\n",
")\n",
"\n",
"validation_result = validator.validate(chunks)\n",
"\n",
"print(\"Chunk Validation Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"print(f\"\\nOverall Valid: {validation_result.get('valid', False)}\")\n",
"print(f\"Quality Score: {validation_result.get('quality_score', 0):.2f}\")\n",
"\n",
"issues = validation_result.get('issues', [])\n",
"if issues:\n",
" print(f\"\\nIssues Found: {len(issues)}\")\n",
" for issue in issues[:3]:\n",
" print(f\" - {issue}\")\n",
"else:\n",
" print(\"\\nNo issues found!\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 12: Provenance Tracking\n",
"\n",
"Track chunk origins for data lineage and debugging.\n",
"\n",
"### Why Track Provenance?\n",
"\n",
"- **Data Lineage**: Know where chunks came from\n",
"- **Debugging**: Trace issues back to source\n",
"- **Compliance**: Required for some use cases"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.split import ProvenanceTracker\n",
"\n",
"# Create chunks\n",
"splitter = TextSplitter(method=\"recursive\", chunk_size=200, chunk_overlap=50)\n",
"chunks = splitter.split(text)\n",
"\n",
"# Track provenance\n",
"tracker = ProvenanceTracker()\n",
"\n",
"for chunk in chunks:\n",
" tracker.track(\n",
" chunk=chunk,\n",
" source={\n",
" \"document_id\": \"apple_doc_001\",\n",
" \"file_path\": \"data/apple.txt\",\n",
" \"timestamp\": \"2024-01-01T00:00:00Z\",\n",
" \"method\": \"recursive\"\n",
" }\n",
" )\n",
"\n",
"print(\"Provenance Tracking Results:\\n\")\n",
"print(\"=\" * 80)\n",
"\n",
"# Get lineage for first chunk\n",
"if chunks:\n",
" lineage = tracker.get_lineage(chunks[0].id)\n",
" \n",
" print(f\"\\nLineage for Chunk 1:\")\n",
" print(f\" Source Document: {lineage.get('source', {}).get('document_id')}\")\n",
" print(f\" File Path: {lineage.get('source', {}).get('file_path')}\")\n",
" print(f\" Method: {lineage.get('source', {}).get('method')}\")\n",
" print(f\" Timestamp: {lineage.get('source', {}).get('timestamp')}\")\n",
"\n",
"print(\"\\n\" + \"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 13: Method Comparison\n",
"\n",
"Let's compare all methods side-by-side to help you choose the right one.\n",
"\n",
"### Comparison Criteria\n",
"\n",
"- **Chunk Count**: Number of chunks created\n",
"- **Average Size**: Average chunk size\n",
"- **Processing Time**: Speed of chunking"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"\n",
"# Methods to compare\n",
"methods_to_compare = [\n",
" (\"recursive\", {}),\n",
" (\"sentence\", {}),\n",
" (\"paragraph\", {}),\n",
" (\"token\", {\"tokenizer\": \"tiktoken\"}),\n",
"]\n",
"\n",
"print(\"Method Comparison:\\n\")\n",
"print(\"=\" * 80)\n",
"print(f\"{'Method':<15} {'Chunks':<10} {'Avg Size':<12} {'Time (ms)':<12}\")\n",
"print(\"-\" * 80)\n",
"\n",
"for method, kwargs in methods_to_compare:\n",
" try:\n",
" start_time = time.time()\n",
" \n",
" splitter = TextSplitter(\n",
" method=method,\n",
" chunk_size=200,\n",
" chunk_overlap=50,\n",
" **kwargs\n",
" )\n",
" \n",
" chunks = splitter.split(text)\n",
" \n",
" elapsed = (time.time() - start_time) * 1000\n",
" avg_size = sum(len(c.text) for c in chunks) / len(chunks) if chunks else 0\n",
" \n",
" print(f\"{method:<15} {len(chunks):<10} {avg_size:<12.0f} {elapsed:<12.2f}\")\n",
" \n",
" except Exception as e:\n",
" print(f\"{method:<15} Error: {str(e)[:40]}\")\n",
"\n",
"print(\"=\" * 80)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 14: Best Practices\n",
"\n",
"### Choosing the Right Method\n",
"\n",
"1. **General Documents**: Use `recursive` for speed and simplicity\n",
"2. **LLM Applications**: Use `token` to respect context windows\n",
"3. **Semantic Search**: Use `semantic_transformer` for topic coherence\n",
"4. **GraphRAG**: Use `entity_aware` or `relation_aware`\n",
"5. **Structured Docs**: Use `structural` for formatted documents\n",
"6. **Large Documents**: Use `hierarchical` for multi-level access\n",
"\n",
"### Chunk Size Guidelines\n",
"\n",
"| Use Case | Recommended Size | Overlap |\n",
"|----------|------------------|----------|\n",
"| Semantic Search | 512-1024 chars | 20% |\n",
"| LLM Context | 2000-4000 chars | 10-20% |\n",
"| Entity Extraction | 500-1500 chars | 15-25% |\n",
"| Question Answering | 1000-2000 chars | 20% |\n",
"\n",
"### Overlap Recommendations\n",
"\n",
"- **10-15%**: Fast processing, less redundancy\n",
"- **20-25%**: Balanced (recommended)\n",
"- **30-40%**: Maximum context preservation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"### What You've Learned\n",
"\n",
"In this notebook, you've learned how to:\n",
"\n",
"- Use `TextSplitter` with multiple methods\n",
"- Apply standard splitting (recursive, token, sentence, paragraph)\n",
"- Use semantic chunking for topic coherence\n",
"- Apply KG-aware chunking (entity-aware, relation-aware)\n",
"- Use specialized chunkers (structural, hierarchical, sliding window, table)\n",
"- Validate chunk quality\n",
"- Track provenance\n",
"- Choose the right method for your use case\n",
"\n",
"### Key Takeaways\n",
"\n",
"1. **Method Selection Matters**: Different methods for different needs\n",
"2. **Chunk Size is Critical**: Balance between context and processing\n",
"3. **Overlap Helps**: 20% overlap is a good default\n",
"4. **Validate Quality**: Always validate chunks before use\n",
"5. **Track Provenance**: Important for debugging and compliance\n",
"6. **KG-Aware for GraphRAG**: Use entity/relation-aware for knowledge graphs\n",
"\n",
"### Next Steps\n",
"\n",
"**Next Notebook**: [12_Embedding_Generation.ipynb](./12_Embedding_Generation.ipynb) \n",
"Learn how to generate embeddings for your chunks!\n",
"\n",
"**Further Reading**:\n",
"- [Split Module API Reference](https://semantica.readthedocs.io/reference/split/)\n",
"- [Advanced Chunking Strategies](../advanced/11_Text_Chunking_Strategies.ipynb)\n",
"- [GraphRAG Pipeline](../use_cases/advanced_rag/01_GraphRAG_Complete.ipynb)\n",
"\n",
"---\n",
"\n",
"**Questions or Issues?** Check out our [GitHub repository](https://github.com/Hawksight-AI/semantica) or [documentation](https://semantica.readthedocs.io)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,178 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Notice**: The `semantica.kg_qa` module is temporarily unavailable and will be reintroduced in a future release. The quality assessment examples in this notebook are disabled."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/12_Graph_Quality.ipynb)\n",
"\n",
"# 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",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/kg_qa/)\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",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## Step 1: Quality Assessment\n",
"\n",
"Assess overall graph quality.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.kg import GraphBuilder\n",
"\n",
"builder = GraphBuilder()\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": [
"\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": [
"\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": [
"\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
}
@@ -0,0 +1,142 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/13_Embedding_Generation.ipynb)\n",
"\n",
"# 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",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/embeddings/)\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",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## 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_embeddings(texts, data_type=\"text\")\n",
"\n",
"print(f\"Generated embeddings for {len(texts)} texts\")\n",
"print(f\"Embeddings shape: {embeddings.shape}\")\n",
"print(f\"First embedding dimension: {len(embeddings[0]) if len(embeddings) > 0 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\"First 5 values: {embedding[:5]}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Model Selection & Dynamic Switching\n",
"\n",
"Semantica allows you to choose between different embedding providers (e.g., Sentence Transformers, FastEmbed) and switch models dynamically.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Initialize with a specific provider and model\n",
"embedder = TextEmbedder(method=\"sentence_transformers\", model_name=\"all-MiniLM-L6-v2\")\n",
"print(f\"Current method: {embedder.get_method()}\")\n",
"\n",
"# Switch to FastEmbed dynamically\n",
"try:\n",
" embedder.set_model(method=\"fastembed\", model_name=\"BAAI/bge-small-en-v1.5\")\n",
" print(f\"Switched to: {embedder.get_method()}\")\n",
" print(f\"Model Info: {embedder.get_model_info()}\")\n",
"except ImportError:\n",
" print(\"FastEmbed not installed. Install with: pip install fastembed\")\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
}
+575
View File
@@ -0,0 +1,575 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/13_Vector_Store.ipynb)\n",
"\n",
"# Vector Store - Comprehensive Guide\n",
"\n",
"## Overview\n",
"\n",
"This notebook provides a **comprehensive walkthrough** of Semantica's vector_store module, demonstrating vector storage, similarity search, hybrid search, and multi-backend support for semantic retrieval.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/vector_store/)\n",
"\n",
"### Learning Objectives\n",
"\n",
"By the end of this notebook, you will be able to:\n",
"\n",
"- Store and manage vectors with metadata\n",
"- Perform similarity search with different metrics\n",
"- Use hybrid search combining vectors and metadata\n",
"- Work with multiple vector store backends (FAISS, Pinecone, etc.)\n",
"- Create and manage vector indices\n",
"- Filter and rank search results\n",
"- Implement namespace isolation for multi-tenancy\n",
"\n",
"### What You'll Learn\n",
"\n",
"| Component | Purpose | When to Use |\n",
"|-----------|---------|-------------|\n",
"| `VectorStore` | Main vector storage | All vector operations |\n",
"| `VectorIndexer` | Index creation | Performance optimization |\n",
"| `VectorRetriever` | Similarity search | Finding similar vectors |\n",
"| `HybridSearch` | Combined search | Vector + metadata filtering |\n",
"| `MetadataFilter` | Metadata filtering | Filtering by attributes |\n",
"| `MetadataStore` | Metadata management | Storing vector metadata |\n",
"| `NamespaceManager` | Multi-tenancy | Isolating vector collections |\n",
"\n",
"---\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Basic Vector Storage\n",
"\n",
"Let's start with the `VectorStore` for basic vector storage and retrieval.\n",
"\n",
"### What is VectorStore?\n",
"\n",
"`VectorStore` is the main interface for vector operations:\n",
"- **Storage**: Store vectors with metadata\n",
"- **Search**: Find similar vectors\n",
"- **CRUD**: Create, Read, Update, Delete operations\n",
"- **Multi-backend**: Support for FAISS, Pinecone, Weaviate, Qdrant, Milvus"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import VectorStore\n",
"from semantica.embeddings import TextEmbedder\n",
"import numpy as np\n",
"\n",
"# 1. Initialize Embedder (Select Provider & Model)\n",
"# You can choose 'sentence_transformers' or 'fastembed'\n",
"embedder = TextEmbedder(method=\"sentence_transformers\", model_name=\"all-MiniLM-L6-v2\")\n",
"dimension = embedder.get_embedding_dimension()\n",
"\n",
"# 2. Create vector store\n",
"store = VectorStore(backend=\"faiss\", dimension=dimension)\n",
"\n",
"# 3. Generate Real Embeddings\n",
"texts = [f\"Document {i}\" for i in range(100)]\n",
"vectors = embedder.embed_batch(texts)\n",
"\n",
"metadata = [\n",
" {\"text\": txt, \"category\": \"science\" if i % 2 == 0 else \"technology\", \"year\": 2020 + (i % 4)}\n",
" for i, txt in enumerate(texts)\n",
"]\n",
"\n",
"# 4. Store vectors\n",
"vector_ids = store.store_vectors(vectors, metadata=metadata)\n",
"\n",
"print(f\"Stored {len(vector_ids)} vectors\")\n",
"print(f\"First 3 IDs: {vector_ids[:3]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Similarity Search\n",
"\n",
"Search for similar vectors using different similarity metrics.\n",
"\n",
"### Similarity Metrics\n",
"\n",
"- **Cosine Similarity**: Best for semantic similarity\n",
"- **L2 Distance**: Euclidean distance\n",
"- **Dot Product**: Fast, requires normalized vectors"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Create query vector\n",
"query_vector = np.random.rand(768)\n",
"\n",
"# Search for similar vectors\n",
"results = store.search_vectors(query_vector, k=10)\n",
"\n",
"print(f\"Found {len(results)} similar vectors\")\n",
"print(\"\\nTop 5 results:\")\n",
"for i, result in enumerate(results[:5], 1):\n",
" print(f\"{i}. ID: {result['id']}, Score: {result['score']:.3f}\")\n",
" print(f\" Metadata: {result.get('metadata', {})}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Vector Indexing\n",
"\n",
"Create indices for faster search on large datasets.\n",
"\n",
"### Index Types (FAISS)\n",
"\n",
"- **Flat**: Exact search (brute force)\n",
"- **IVF**: Inverted file index (approximate)\n",
"- **HNSW**: Hierarchical graph (best balance)\n",
"- **PQ**: Product quantization (compressed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import VectorIndexer, FAISSAdapter\n",
"\n",
"# Create indexer\n",
"indexer = VectorIndexer(backend=\"faiss\", dimension=768)\n",
"\n",
"# Create HNSW index for fast approximate search\n",
"adapter = FAISSAdapter(dimension=768)\n",
"index = adapter.create_index(index_type=\"hnsw\", metric=\"L2\", m=16)\n",
"\n",
"# Add vectors to index\n",
"vectors_array = np.array(vectors).astype('float32')\n",
"adapter.add_vectors(index, vectors_array, ids=vector_ids)\n",
"\n",
"# Search using index\n",
"query_array = query_vector.astype('float32')\n",
"distances, indices = adapter.search(index, query_array, k=10)\n",
"\n",
"print(f\"Index search found {len(indices)} results\")\n",
"print(f\"Distances: {distances[:5]}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Hybrid Search\n",
"\n",
"Combine vector similarity with metadata filtering.\n",
"\n",
"### Hybrid Search Benefits\n",
"\n",
"- Filter by metadata before vector search\n",
"- Combine multiple search criteria\n",
"- More precise results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import HybridSearch, MetadataFilter\n",
"\n",
"# Create hybrid search\n",
"hybrid_search = HybridSearch()\n",
"\n",
"# Create metadata filter\n",
"filter = MetadataFilter() \\\n",
" .eq(\"category\", \"science\") \\\n",
" .gt(\"year\", 2021)\n",
"\n",
"# Perform hybrid search\n",
"hybrid_results = hybrid_search.search(\n",
" query_vector,\n",
" vectors,\n",
" metadata,\n",
" vector_ids,\n",
" filter=filter,\n",
" k=10\n",
")\n",
"\n",
"print(f\"Hybrid search found {len(hybrid_results)} results\")\n",
"print(\"\\nFiltered results (science, year > 2021):\")\n",
"for i, result in enumerate(hybrid_results[:5], 1):\n",
" meta = result.get('metadata', {})\n",
" print(f\"{i}. Category: {meta.get('category')}, Year: {meta.get('year')}, Score: {result['score']:.3f}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Metadata Management\n",
"\n",
"Store and query metadata separately from vectors.\n",
"\n",
"### Metadata Operations\n",
"\n",
"- Store metadata for vectors\n",
"- Query by metadata conditions\n",
"- Update metadata\n",
"- Schema validation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import MetadataStore, MetadataSchema\n",
"\n",
"# Create metadata store\n",
"meta_store = MetadataStore()\n",
"\n",
"# Store metadata\n",
"for i, vec_id in enumerate(vector_ids[:10]):\n",
" meta_store.store_metadata(vec_id, metadata[i])\n",
"\n",
"# Query metadata\n",
"matching_ids = meta_store.query_metadata(\n",
" {\"category\": \"science\"},\n",
" operator=\"AND\"\n",
")\n",
"\n",
"print(f\"Found {len(matching_ids)} vectors with category='science'\")\n",
"\n",
"# Define schema for validation\n",
"schema = MetadataSchema({\n",
" \"text\": {\"type\": str, \"required\": True},\n",
" \"category\": {\"type\": str, \"required\": True},\n",
" \"year\": {\"type\": int, \"required\": True}\n",
"})\n",
"\n",
"# Validate metadata\n",
"is_valid = schema.validate(metadata[0])\n",
"print(f\"\\nMetadata validation: {is_valid}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Result Ranking and Fusion\n",
"\n",
"Combine and rank results from multiple searches.\n",
"\n",
"### Ranking Strategies\n",
"\n",
"- **Reciprocal Rank Fusion (RRF)**: Combine ranked lists\n",
"- **Weighted Average**: Weight scores from different sources"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import SearchRanker\n",
"\n",
"# Create ranker with RRF strategy\n",
"ranker = SearchRanker(strategy=\"reciprocal_rank_fusion\")\n",
"\n",
"# Simulate multiple search results\n",
"results1 = [\n",
" {\"id\": \"vec_1\", \"score\": 0.9},\n",
" {\"id\": \"vec_2\", \"score\": 0.8},\n",
" {\"id\": \"vec_3\", \"score\": 0.7}\n",
"]\n",
"\n",
"results2 = [\n",
" {\"id\": \"vec_2\", \"score\": 0.85},\n",
" {\"id\": \"vec_4\", \"score\": 0.75},\n",
" {\"id\": \"vec_1\", \"score\": 0.7}\n",
"]\n",
"\n",
"# Fuse results using RRF\n",
"fused_results = ranker.rank([results1, results2], k=60)\n",
"\n",
"print(\"Fused results using RRF:\")\n",
"for i, result in enumerate(fused_results, 1):\n",
" print(f\"{i}. ID: {result['id']}, Fused Score: {result['score']:.3f}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7: Namespace Management\n",
"\n",
"Isolate vectors for multi-tenant applications.\n",
"\n",
"### Namespace Features\n",
"\n",
"- Tenant isolation\n",
"- Access control\n",
"- Per-namespace operations"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import NamespaceManager\n",
"\n",
"# Create namespace manager\n",
"ns_manager = NamespaceManager()\n",
"\n",
"# Create namespaces for different tenants\n",
"ns1 = ns_manager.create_namespace(\"tenant1\", \"Tenant 1 vectors\")\n",
"ns2 = ns_manager.create_namespace(\"tenant2\", \"Tenant 2 vectors\")\n",
"\n",
"# Add vectors to namespaces\n",
"for i in range(5):\n",
" ns_manager.add_vector_to_namespace(f\"t1_vec_{i}\", \"tenant1\")\n",
" ns_manager.add_vector_to_namespace(f\"t2_vec_{i}\", \"tenant2\")\n",
"\n",
"# Get namespace vectors\n",
"tenant1_vectors = ns_manager.get_namespace_vectors(\"tenant1\")\n",
"tenant2_vectors = ns_manager.get_namespace_vectors(\"tenant2\")\n",
"\n",
"print(f\"Tenant 1: {len(tenant1_vectors)} vectors\")\n",
"print(f\"Tenant 2: {len(tenant2_vectors)} vectors\")\n",
"\n",
"# Set access control\n",
"ns1.set_access_control(\"user1\", [\"read\", \"write\"])\n",
"ns1.set_access_control(\"user2\", [\"read\"])\n",
"\n",
"print(f\"\\nUser1 can write: {ns1.has_permission('user1', 'write')}\")\n",
"print(f\"User2 can write: {ns1.has_permission('user2', 'write')}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8: Convenience Functions\n",
"\n",
"Use convenience functions for quick operations.\n",
"\n",
"### Available Functions\n",
"\n",
"- `store_vectors()`: Store vectors\n",
"- `search_vectors()`: Search vectors\n",
"- `hybrid_search()`: Hybrid search\n",
"- `update_vectors()`: Update vectors\n",
"- `delete_vectors()`: Delete vectors"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import (\n",
" store_vectors,\n",
" search_vectors,\n",
" hybrid_search as hybrid_search_func,\n",
" update_vectors,\n",
" delete_vectors\n",
")\n",
"\n",
"# Store vectors using convenience function\n",
"new_vectors = [np.random.rand(768) for _ in range(10)]\n",
"new_metadata = [{\"text\": f\"New doc {i}\"} for i in range(10)]\n",
"new_ids = store_vectors(new_vectors, metadata=new_metadata, method=\"default\")\n",
"\n",
"print(f\"Stored {len(new_ids)} new vectors\")\n",
"\n",
"# Search using convenience function\n",
"search_results = search_vectors(\n",
" query_vector,\n",
" new_vectors,\n",
" new_ids,\n",
" k=5,\n",
" method=\"default\"\n",
")\n",
"\n",
"print(f\"Search found {len(search_results)} results\")\n",
"\n",
"# Update vectors\n",
"updated_vectors = [np.random.rand(768) for _ in range(2)]\n",
"success = update_vectors(new_ids[:2], updated_vectors, method=\"default\")\n",
"print(f\"\\nUpdated vectors: {success}\")\n",
"\n",
"# Delete vectors\n",
"success = delete_vectors(new_ids[-2:], method=\"default\")\n",
"print(f\"Deleted vectors: {success}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 9: Multi-Backend Support\n",
"\n",
"Work with different vector store backends.\n",
"\n",
"### Supported Backends\n",
"\n",
"| Backend | Type | Best For |\n",
"|---------|------|----------|\n",
"| FAISS | Local | Development, small datasets |\n",
"| Pinecone | Cloud | Production, managed service |\n",
"| Weaviate | Self-hosted | Schema-aware storage |\n",
"| Qdrant | Self-hosted | High performance |\n",
"| Milvus | Cloud/Self-hosted | Large scale |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.vector_store import FAISSAdapter, VectorManager\n",
"\n",
"# FAISS (local)\n",
"faiss_adapter = FAISSAdapter(dimension=768)\n",
"faiss_index = faiss_adapter.create_index(index_type=\"flat\", metric=\"L2\")\n",
"print(\"Created FAISS index\")\n",
"\n",
"# Vector Manager for multi-store management\n",
"manager = VectorManager()\n",
"faiss_store = manager.create_store(\"faiss\", {\"dimension\": 768})\n",
"print(f\"\\nCreated store via manager\")\n",
"\n",
"# List all stores\n",
"stores = manager.list_stores()\n",
"print(f\"Active stores: {stores}\")\n",
"\n",
"# Note: For cloud backends (Pinecone, Weaviate, etc.),\n",
"# you would need API keys and endpoints\n",
"# Example:\n",
"# from semantica.vector_store import PineconeAdapter\n",
"# pinecone = PineconeAdapter(api_key=\"your-key\", environment=\"us-west1-gcp\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 10: Best Practices\n",
"\n",
"### Performance Tips\n",
"\n",
"1. **Normalize Vectors**: Always normalize for cosine similarity\n",
"2. **Use HNSW**: Best balance for speed/accuracy\n",
"3. **Batch Operations**: Process in batches (100-1000)\n",
"4. **Filter First**: Apply metadata filters before vector search\n",
"\n",
"### Backend Selection\n",
"\n",
"- **Development**: FAISS (local, fast)\n",
"- **Production**: Pinecone (managed, scalable)\n",
"- **Self-hosted**: Qdrant or Milvus (control, performance)\n",
"- **Schema-aware**: Weaviate (rich metadata)\n",
"\n",
"### Index Configuration\n",
"\n",
"- **Small datasets (<10K)**: Flat index\n",
"- **Medium datasets (10K-1M)**: HNSW\n",
"- **Large datasets (>1M)**: IVF + PQ"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"### What You've Learned\n",
"\n",
"In this notebook, you've learned how to:\n",
"\n",
"- Store and search vectors with VectorStore\n",
"- Create indices for performance optimization\n",
"- Use hybrid search with metadata filtering\n",
"- Manage metadata separately from vectors\n",
"- Rank and fuse search results\n",
"- Implement namespace isolation\n",
"- Use convenience functions for quick operations\n",
"- Work with multiple backend adapters\n",
"- Apply best practices for production use\n",
"\n",
"### Key Takeaways\n",
"\n",
"1. **Multi-Backend**: Choose the right backend for your needs\n",
"2. **Hybrid Search**: Combine vectors with metadata for precision\n",
"3. **Indexing**: Use appropriate index types for performance\n",
"4. **Metadata**: Separate metadata management for flexibility\n",
"5. **Namespaces**: Isolate vectors for multi-tenancy\n",
"\n",
"### Next Steps\n",
"\n",
"**Further Reading**:\n",
"- [Vector Store API Reference](https://semantica.readthedocs.io/reference/vector_store/)\n",
"- [Advanced Vector Store Notebook](../advanced/Advanced_Vector_Store_and_Search.ipynb)\n",
"- [Embedding Generation](12_Embedding_Generation.ipynb)\n",
"\n",
"---\n",
"\n",
"**Questions or Issues?** Check out our [GitHub repository](https://github.com/Hawksight-AI/semantica) or [documentation](https://semantica.readthedocs.io)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
+543
View File
@@ -0,0 +1,543 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/14_Ontology.ipynb)\n",
"\n",
"# Mastering Ontology Generation with Semantica\n",
"\n",
"Welcome to the comprehensive guide on Semantica's Ontology Module. This module is the powerhouse for structuring your data into meaningful knowledge graphs, providing a complete 6-stage pipeline from raw data to validated OWL ontologies.\n",
"\n",
"In this notebook, we will dive deep into:\n",
"1. **The 6-Stage Generation Pipeline**: Understanding how Semantica transforms data into knowledge.\n",
"2. **Core Components in Focus**: Detailed usage of `ClassInferrer`, `PropertyGenerator`, and `OntologyOptimizer`.\n",
"3. **Validation & Quality**: ensuring your ontology is consistent and structurally sound.\n",
"4. **Visualize**: exploring your ontology with interactive charts and hierarchies.\n",
"5. **Advanced Usage**: Text-to-Ontology (LLM), Competency Questions, and Lifecycle Management.\n",
"6. **Exporting & Interoperability**: Saving your work in standard formats like Turtle and RDF/XML.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/ontology/)\n",
"\n",
"## Getting Started\n",
"\n",
"First, let's setup our environment and initialize the `OntologyEngine`. This engine is the unified entry point for all ontology operations."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Install semantica if not already installed\n",
"# !pip install semantica\n",
"# !pip install plotly # Required for visualization\n",
"\n",
"from semantica.ontology import OntologyEngine, OntologyGenerator\n",
"from semantica.utils.logging import get_logger\n",
"\n",
"# Initialize logger for visibility\n",
"logger = get_logger(\"ontology_guide\")\n",
"\n",
"# Initialize the Engine\n",
"# base_uri defines the namespace root for your ontology\n",
"engine = OntologyEngine(base_uri=\"https://docs.semantica.dev/ontology/\")\n",
"\n",
"print(\"Ontology Engine initialized successfully!\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## The 6-Stage Generation Pipeline\n",
"\n",
"Semantica uses a sophisticated 6-stage pipeline to robustly generate ontologies. This automated process takes raw entity and relationship data and produces a high-quality OWL ontology.\n",
"\n",
"### The Stages:\n",
"1. **Semantic Network Parsing**: Extracts raw concepts and connections from your inputs.\n",
"2. **YAML-to-Definition**: Transforms concepts into structured class definitions.\n",
"3. **Definition-to-Types**: Maps definitions to formal OWL types (e.g., `owl:Class`, `owl:ObjectProperty`).\n",
"4. **Hierarchy Generation**: Builds a taxonomic structure (parent-child relationships) using `associatedWith` or linguistic patterns.\n",
"5. **TTL Generation**: Serializes the in-memory structure into Turtle format logic.\n",
"6. **Symbolic Validation**: Validates the result using reasoners like HermiT (if available) or structural checks.\n",
"\n",
"Let's see this in action with some sample data."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Sample Data: A simple corporate structure\n",
"entities = [\n",
" {\"id\": \"e1\", \"type\": \"Company\", \"name\": \"TechCorp\", \"founded\": \"2010\"},\n",
" {\"id\": \"e2\", \"type\": \"Person\", \"name\": \"Alice\", \"role\": \"CEO\"},\n",
" {\"id\": \"e3\", \"type\": \"Person\", \"name\": \"Bob\", \"role\": \"CTO\"},\n",
" {\"id\": \"e4\", \"type\": \"Department\", \"name\": \"Engineering\"},\n",
" {\"id\": \"e5\", \"type\": \"Project\", \"name\": \"Project Phoenix\"}\n",
"]\n",
"\n",
"relationships = [\n",
" {\"source\": \"e2\", \"target\": \"e1\", \"type\": \"leads\"},\n",
" {\"source\": \"e3\", \"target\": \"e4\", \"type\": \"manages\"},\n",
" {\"source\": \"e4\", \"target\": \"e1\", \"type\": \"part_of\"},\n",
" {\"source\": \"e3\", \"target\": \"e5\", \"type\": \"works_on\"}\n",
"]\n",
"\n",
"data = {\n",
" \"entities\": entities,\n",
" \"relationships\": relationships\n",
"}\n",
"\n",
"# Run the full pipeline\n",
"ontology = engine.from_data(data, name=\"CorporateOntology\")\n",
"\n",
"print(f\"Generated Ontology: {ontology['name']}\")\n",
"print(f\"Classes Found: {len(ontology['classes'])}\")\n",
"print(f\"Properties Found: {len(ontology['properties'])}\")\n",
"print(f\"Validation Status: Valid={ontology.get('validation_result', {}).get('valid', 'Unknown')}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Inspecting the Results\n",
"\n",
"The generated `ontology` object is a rich dictionary containing all the inferred structure. Let's peek inside to see what Classes and Properties were created."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Inspect Classes\n",
"print(\"--- Inferred Classes ---\")\n",
"for cls in ontology['classes']:\n",
" print(f\"Class: {cls['name']}\")\n",
" print(f\" URI: {cls.get('uri')}\")\n",
" # Check if a hierarchy was inferred\n",
" if cls.get('subClassOf'):\n",
" print(f\" Parent: {cls['subClassOf']}\")\n",
" print(\"\")\n",
"\n",
"# Inspect Properties\n",
"print(\"--- Inferred Properties ---\")\n",
"for prop in ontology['properties']:\n",
" type_label = \"Object Property\" if prop['type'] == 'object' else \"Data Property\"\n",
" print(f\"{prop['name']} [{type_label}]\")\n",
" print(f\" Domain: {prop.get('domain')}\")\n",
" print(f\" Range: {prop.get('range')}\")\n",
" print(\"\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Deep Dive: Component by Component\n",
"\n",
"While `OntologyEngine` is great for one-shot generation, you often need fine-grained control. Let's look at the individual tools that power the engine.\n",
"\n",
"### 1. `ClassInferrer`: Mastering Class Discovery\n",
"\n",
"The `ClassInferrer` analyzes entities to find patterns. It can handle noise and only creates classes for types that appear frequently enough.\n",
"\n",
"* **`min_occurrences`**: Ignores types with fewer entities than this count.\n",
"* **`build_class_hierarchy`**: Toggles automatic parent-child detection.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ontology import ClassInferrer\n",
"\n",
"# Initialize inferrer with a threshold\n",
"# We set min_occurrences=1 here to capture everything in our small example\n",
"inferrer = ClassInferrer(min_occurrences=1)\n",
"\n",
"raw_entities = [\n",
" {\"type\": \"Manager\", \"name\": \"Dave\", \"level\": 5},\n",
" {\"type\": \"Manager\", \"name\": \"Eve\", \"level\": 4},\n",
" {\"type\": \"Employee\", \"name\": \"Frank\"}, # Only 1 employee\n",
" {\"type\": \"TemporaryWorker\", \"name\": \"Grace\"} \n",
"]\n",
"\n",
"# Infer classes\n",
"classes = inferrer.infer_classes(raw_entities, build_hierarchy=True)\n",
"\n",
"print(f\"Inferred {len(classes)} classes from raw entities.\")\n",
"for c in classes:\n",
" print(f\"- {c['name']} (Count: {c['entity_count']})\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. `PropertyGenerator`: The Glue of the Ontology\n",
"\n",
"Properties define relationships. Semantica distinguishes between:\n",
"* **Object Properties**: Links between two entities (e.g., `leads` between Person and Company).\n",
"* **Data Properties**: Attributes of an entity (e.g., `founded` year of a Company).\n",
"\n",
"The `PropertyGenerator` automatically detects this distinction."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ontology import PropertyGenerator\n",
"\n",
"prop_gen = PropertyGenerator()\n",
"\n",
"# We need the classes first to help property generation context\n",
"context_classes = classes # reusing from previous step\n",
"\n",
"# Let's define some relationships and attributes implicitly via entities\n",
"# Note: 'level' in Manager entities is a potential data property\n",
"complex_entities = [\n",
" {\"id\": \"m1\", \"type\": \"Manager\", \"name\": \"Dave\", \"level\": 5},\n",
" {\"id\": \"e1\", \"type\": \"Employee\", \"name\": \"Frank\"}\n",
"]\n",
"complex_relationships = [\n",
" {\"source\": \"m1\", \"target\": \"e1\", \"type\": \"supervises\"} # Object property\n",
"]\n",
"\n",
"properties = prop_gen.infer_properties(\n",
" entities=complex_entities,\n",
" relationships=complex_relationships,\n",
" classes=context_classes\n",
")\n",
"\n",
"print(\"--- Property Types Identified ---\")\n",
"for p in properties:\n",
" print(f\"Property: {p['name']}\")\n",
" print(f\" Type: {p['type']}\")\n",
" print(f\" Domain: {p['domain']} -> Range: {p['range']}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. `OntologyOptimizer`: Refining the Structure\n",
"\n",
"Before finalizing, it's good practice to optimize. The optimizer removes redundancies and improves coherence, such as ensuring all classes have proper labels and valid URIs."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ontology import OntologyOptimizer\n",
"\n",
"optimizer = OntologyOptimizer()\n",
"\n",
"# Let's pretend we have a messy ontology dict\n",
"messy_ontology = {\n",
" \"classes\": [\n",
" {\"name\": \"Person\", \"uri\": \"...Person\"},\n",
" {\"name\": \"Person\", \"uri\": \"...Person\"} # Duplicate!\n",
" ],\n",
" \"properties\": []\n",
"}\n",
"\n",
"clean_ontology = optimizer.optimize_ontology(messy_ontology, remove_redundancy=True)\n",
"\n",
"print(f\"Original Classes: {len(messy_ontology['classes'])}\")\n",
"print(f\"Optimized Classes: {len(clean_ontology['classes'])}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Validation and Quality Control\n",
"\n",
"Semantica includes a robust `OntologyValidator`. It checks for:\n",
"1. **Structure**: Missing fields, malformed URIs.\n",
"2. **Consistency**: Circular hierarchies, contradictory definitions.\n",
"3. **Metrics**: Depth of hierarchy, property usage.\n",
"\n",
"If you have `Owlready2` installed, it can even run a reasoner (HermiT or Pellet) to prove logical consistency."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ontology import OntologyValidator\n",
"\n",
"validator = OntologyValidator(\n",
" check_consistency=True,\n",
" check_satisfiability=True\n",
")\n",
"\n",
"# Validate our previously generated 'ontology'\n",
"result = validator.validate_ontology(ontology)\n",
"\n",
"print(f\"Is Valid? {result.valid}\")\n",
"print(f\"Is Consistent? {result.consistent}\")\n",
"\n",
"if result.errors:\n",
" print(\"Errors Found:\", result.errors)\n",
"if result.warnings:\n",
" print(\"Warnings:\", result.warnings)\n",
" \n",
"# Check Metrics\n",
"print(\"Metrics:\", result.metrics)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Visualization\n",
"\n",
"A picture is worth a thousand triples! The `OntologyVisualizer` lets you explore your ontology's structure interactively.\n",
"\n",
"We can visualize:\n",
"* **Class Hierarchies**: Tree diagrams of class inheritance.\n",
"* **Structure Networks**: The full graph of classes and properties.\n",
"* **Metrics Dashboards**: High-level stats at a glance."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.visualization import OntologyVisualizer\n",
"\n",
"viz = OntologyVisualizer()\n",
"\n",
"# 1. Interactive Class Hierarchy\n",
"# Returns a Plotly figure you can interact with\n",
"fig_hierarchy = viz.visualize_hierarchy(ontology, output=\"interactive\")\n",
"if fig_hierarchy:\n",
" fig_hierarchy.show()\n",
"\n",
"# 2. Ontology Structure Network\n",
"# See how classes and properties connect\n",
"fig_structure = viz.visualize_structure(ontology, output=\"interactive\")\n",
"if fig_structure:\n",
" fig_structure.show()\n",
"\n",
"# 3. Metrics Dashboard\n",
"# View counts, depths, and statistics\n",
"fig_metrics = viz.visualize_metrics(ontology, output=\"interactive\")\n",
"if fig_metrics:\n",
" fig_metrics.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Advanced Usage: Lifecycle & AI\n",
"\n",
"Enterprise ontologies are living artifacts. Semantica provides tools to manage their entire lifecycle and accelerate creation with AI.\n",
"\n",
"### 1. Text-to-Ontology (LLM Integration)\n",
"\n",
"Instead of manually creating entities, use the `LLMOntologyGenerator` to extract an ontology directly from text requirements or documents."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ontology import LLMOntologyGenerator\n",
"\n",
"try:\n",
" # Note: Requires an API key in your environment variables\n",
" llm_gen = LLMOntologyGenerator(provider=\"openai\", model=\"gpt-4\")\n",
"\n",
" text_description = \"\"\"\n",
" A University has many Departments. Each Department offers several Courses.\n",
" Professors teach Courses and belong to a Department.\n",
" Students enroll in Courses.\n",
" \"\"\"\n",
"\n",
" llm_ontology = llm_gen.generate_ontology_from_text(\n",
" text=text_description,\n",
" name=\"UniversityOntology\"\n",
" )\n",
"\n",
" print(\"Generated Classes:\", [c['name'] for c in llm_ontology['classes']])\n",
"except Exception:\n",
" print(\"Skipping LLM generation: No API key or provider configured in this environment.\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Test-Driven Design (Competency Questions)\n",
"\n",
"Formalize your requirements as \"Competency Questions\" (CQs). The `CompetencyQuestionsManager` can check if your ontology contains the necessary terms to answer them."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ontology import CompetencyQuestionsManager\n",
"\n",
"cq_manager = CompetencyQuestionsManager()\n",
"\n",
"# Define what our ontology SHOULD answer\n",
"cq_manager.add_question(\"Who leads TechCorp?\", category=\"organizational\")\n",
"cq_manager.add_question(\"Which projects does Bob manage?\", category=\"operational\")\n",
"\n",
"# Validate our 'ontology' against these questions\n",
"validation_results = cq_manager.validate_ontology(ontology)\n",
"\n",
"print(f\"Answerable Questions: {validation_results['answerable']} / {validation_results['total_questions']}\")\n",
"for q in cq_manager.questions:\n",
" status = \"✅\" if q.answerable else \"❌\"\n",
" print(f\"{status} {q.question}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Lifecycle Management (Versioning & Reuse)\n",
"\n",
"Manage iterations with `VersionManager` and import external standards like FOAF or Dublin Core with `ReuseManager`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.ontology import VersionManager, ReuseManager\n",
"\n",
"# --- Versioning ---\n",
"v_manager = VersionManager(base_uri=\"https://example.org/ontology/\")\n",
"v1 = v_manager.create_version(\"1.0\", ontology, changes=[\"Initial creation\"])\n",
"print(f\"Created Version: {v1.version} at {v1.ontology_iri}\")\n",
"\n",
"# --- Reuse ---\n",
"reuse_manager = ReuseManager()\n",
"\n",
"# Check if we can reuse FOAF\n",
"foaf_info = reuse_manager.research_ontology(\"http://xmlns.com/foaf/0.1/\")\n",
"if foaf_info:\n",
" print(f\"Found standard ontology: {foaf_info['name']}\")\n",
" # We could now import this into our ontology\n",
" ontology['imports'].append(foaf_info['uri'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Exporting Your Ontology\n",
"\n",
"Once your ontology is built and validated, you'll want to save it. Semantica focuses on **Turtle (`.ttl`)** as the primary format, but supports others via `rdflib`.\n",
"\n",
"You can export to a string or directly to a file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Get Turtle string representation\n",
"ttl_output = engine.to_owl(ontology, format=\"turtle\")\n",
"\n",
"print(\"--- Turtle Preview (First 500 chars) ---\")\n",
"print(ttl_output[:500])\n",
"print(\"...\")\n",
"\n",
"# Save to file\n",
"output_path = \"corporate_ontology.ttl\"\n",
"engine.export_owl(ontology, path=output_path, format=\"turtle\")\n",
"print(f\"Successfully saved ontology to {output_path}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"You have now mastered the essentials of Semantica's Ontology Module!\n",
"\n",
"* **Automated Generation**: Used the 6-stage pipeline to go from raw data to a structured ontology.\n",
"* **Component Control**: Used `ClassInferrer` and `PropertyGenerator` for fine-tuned modeling.\n",
"* **Quality Assurance**: Validated your model against strict standards.\n",
"* **Visualization**: Explored the ontology structure interactively.\n",
"* **Advanced Lifecycle**: Used AI generation, competency questions, and versioning.\n",
"* **Export**: Serialized your knowledge graph for use in other semantic web tools.\n",
"\n",
"**Next Steps**:\n",
"* Try customizing the `NamespaceManager` to use your organization's URL.\n",
"* Explore `OntologyEvaluator` for deeper quality metrics.\n",
"* Feed the generated ontology into the **Knowledge Graph** module to start reasoning over your data!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+357
View File
@@ -0,0 +1,357 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/15_Export.ipynb)\n",
"\n",
"# Export Module - Comprehensive Guide\n",
"\n",
"## Overview\n",
"\n",
"This notebook provides a comprehensive guide to Semantica's **Export Module**, which enables exporting knowledge graphs, entities, relationships, and data to multiple formats. The module supports **8 export formats** (RDF, JSON, CSV, Graph, YAML, OWL, Vector, LPG) plus report generation capabilities.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/export/)\n",
"\n",
"### Export Module Architecture\n",
"\n",
"The Export Module consists of:\n",
"\n",
"#### **Core Exporter Classes** (9 classes)\n",
"- `RDFExporter` - RDF format export (Turtle, RDF/XML, JSON-LD, N-Triples, N3)\n",
"- `JSONExporter` - JSON and JSON-LD format export\n",
"- `CSVExporter` - CSV format export for tabular data\n",
"- `GraphExporter` - Graph format export (GraphML, GEXF, DOT)\n",
"- `SemanticNetworkYAMLExporter` - Semantic network YAML export\n",
"- `YAMLSchemaExporter` - Schema YAML export\n",
"- `OWLExporter` - OWL format export for ontologies\n",
"- `VectorExporter` - Vector embedding export for vector stores\n",
"- `LPGExporter` - LPG format export for Neo4j, Memgraph, and similar databases\n",
"\n",
"#### **Supporting Classes** (4 classes)\n",
"- `RDFSerializer` - RDF serialization engine for format conversion\n",
"- `RDFValidator` - RDF validation engine for syntax checking\n",
"- `NamespaceManager` - RDF namespace management and conflict resolution\n",
"- `ReportGenerator` - Report generation (HTML, Markdown, JSON, Text)\n",
"\n",
"#### **Registry & Configuration** (2 classes + 2 instances)\n",
"- `MethodRegistry` - Registry for custom export methods\n",
"- `method_registry` - Global registry instance\n",
"- `ExportConfig` - Configuration manager for export module\n",
"- `export_config` - Global configuration instance\n",
"\n",
"### Learning Objectives\n",
"\n",
"By the end of this notebook, you will be able to:\n",
"- Export knowledge graphs to all supported formats (RDF, JSON, CSV, Graph, YAML, OWL, Vector, LPG)\n",
"- Use exporter classes directly for fine-grained control\n",
"- Generate professional reports in multiple formats\n",
"- Register and use custom export methods\n",
"- Configure export settings via environment variables or config files\n",
"- Use RDF serialization, validation, and namespace management\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## Step 1: JSON Export\n",
"\n",
"Export knowledge graph to JSON format using `JSONExporter` class.\n",
"\n",
"**JSONExporter Methods:**\n",
"- `export()` - Export any data to JSON\n",
"- `export_knowledge_graph()` - Export knowledge graph to JSON/JSON-LD\n",
"- `export_entities()` - Export entities to JSON\n",
"- `export_relationships()` - Export relationships 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",
"# Create exporter and builder\n",
"json_exporter = JSONExporter()\n",
"builder = GraphBuilder()\n",
"\n",
"# Create sample knowledge graph\n",
"entities = [{\"id\": \"e1\", \"type\": \"Organization\", \"name\": \"Apple Inc.\", \"properties\": {}}]\n",
"relationships = []\n",
"\n",
"kg = builder.build(entities + relationships)\n",
"\n",
"# Export to JSON\n",
"json_exporter.export_knowledge_graph(kg, \"output.json\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: CSV Export\n",
"\n",
"Export entities and relationships to CSV format using `CSVExporter` class.\n",
"\n",
"**CSVExporter Methods:**\n",
"- `export()` - Export knowledge graph to CSV\n",
"- `export_entities()` - Export entities to CSV file\n",
"- `export_relationships()` - Export relationships to CSV file\n",
"- `export_knowledge_graph()` - Export complete knowledge graph to CSV\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import CSVExporter\n",
"\n",
"# Create CSV exporter\n",
"csv_exporter = CSVExporter()\n",
"\n",
"# Export entities to CSV\n",
"csv_exporter.export_entities(entities, \"entities.csv\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: RDF Export\n",
"\n",
"Export knowledge graph to RDF format using `RDFExporter` class.\n",
"\n",
"**RDFExporter Methods:**\n",
"- `export()` - Export to RDF (supports multiple formats)\n",
"- `export_knowledge_graph()` - Export knowledge graph to RDF\n",
"- `export_entities()` - Export entities to RDF\n",
"- `export_relationships()` - Export relationships to RDF\n",
"\n",
"**Supported RDF Formats:**\n",
"- `turtle` - Turtle format (human-readable)\n",
"- `rdfxml` - RDF/XML format\n",
"- `jsonld` - JSON-LD format\n",
"- `ntriples` - N-Triples format\n",
"- `n3` - N3 format\n",
"\n",
"**Additional RDF Classes:**\n",
"- `RDFSerializer` - Serialize RDF data between formats\n",
"- `RDFValidator` - Validate RDF syntax and consistency\n",
"- `NamespaceManager` - Manage RDF namespaces\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import RDFExporter\n",
"\n",
"# Create RDF exporter\n",
"rdf_exporter = RDFExporter()\n",
"\n",
"# Export to RDF format (Turtle by default)\n",
"rdf_exporter.export_knowledge_graph(kg, \"output.ttl\", format=\"turtle\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Graph Export\n",
"\n",
"Export to graph formats using `GraphExporter` class for visualization tools.\n",
"\n",
"**GraphExporter Methods:**\n",
"- `export()` - Export to graph format\n",
"- `export_knowledge_graph()` - Export knowledge graph to graph format\n",
"\n",
"**Supported Graph Formats:**\n",
"- `graphml` - GraphML format (for Cytoscape, yEd, etc.)\n",
"- `gexf` - GEXF format (for Gephi)\n",
"- `dot` - Graphviz DOT format\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import GraphExporter\n",
"\n",
"# Create graph exporter\n",
"graph_exporter = GraphExporter()\n",
"\n",
"# Export to GraphML format\n",
"graph_exporter.export_knowledge_graph(kg, \"output.graphml\", format=\"graphml\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: OWL Export\n",
"\n",
"Export ontologies to OWL format using `OWLExporter` class.\n",
"\n",
"**OWLExporter Methods:**\n",
"- `export()` - Export ontology to OWL\n",
"- `export_ontology()` - Export complete ontology\n",
"- `export_classes()` - Export class definitions only\n",
"- `export_properties()` - Export property definitions only\n",
"\n",
"**Supported OWL Formats:**\n",
"- `owl-xml` - OWL/XML format (default)\n",
"- `turtle` - OWL in Turtle format\n",
"\n",
"**Note:** OWLExporter expects an ontology structure (with classes, properties), not a knowledge graph. Use `OntologyGenerator` to convert a knowledge graph to an ontology first.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.export import OWLExporter\n",
"from semantica.ontology import OntologyGenerator\n",
"\n",
"# Create OWL exporter and ontology generator\n",
"owl_exporter = OWLExporter()\n",
"generator = OntologyGenerator()\n",
"\n",
"# Generate ontology from entities and relationships\n",
"ontology = generator.generate(entities, relationships)\n",
"\n",
"# Export ontology to OWL\n",
"owl_exporter.export(ontology, \"output.owl\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Additional Export Formats\n",
"\n",
"### YAML Export\n",
"\n",
"Export to YAML format using YAML exporters:\n",
"\n",
"**YAML Exporter Classes:**\n",
"- `SemanticNetworkYAMLExporter` - Export semantic networks to YAML\n",
"- `YAMLSchemaExporter` - Export ontology schemas to YAML\n",
"\n",
"### Vector Export\n",
"\n",
"Export vector embeddings using `VectorExporter`:\n",
"\n",
"**VectorExporter Methods:**\n",
"- `export()` - Export vectors to various formats\n",
"\n",
"**Supported Vector Formats:**\n",
"- `json` - JSON format\n",
"- `numpy` - NumPy format\n",
"- `binary` - Binary format\n",
"- `faiss` - FAISS format\n",
"\n",
"### LPG Export\n",
"\n",
"Export to Labeled Property Graph format using `LPGExporter`:\n",
"\n",
"**LPGExporter Methods:**\n",
"- `export()` - Export to LPG format\n",
"- `export_knowledge_graph()` - Export knowledge graph to LPG\n",
"\n",
"**Supported LPG Formats:**\n",
"- `cypher` - Cypher query format (for Neo4j, Memgraph)\n",
"- `lpg` - Labeled Property Graph format\n",
"\n",
"### Report Generation\n",
"\n",
"Generate professional reports using `ReportGenerator`:\n",
"\n",
"**ReportGenerator Methods:**\n",
"- `generate_report()` - Generate report in various formats\n",
"\n",
"**Supported Report Formats:**\n",
"- `html` - HTML report\n",
"- `markdown` - Markdown report\n",
"- `json` - JSON report\n",
"- `text` - Plain text report\n",
"\n",
"## Step 7: Method Registry\n",
"\n",
"Register and use custom export methods using the MethodRegistry system:\n",
"\n",
"```python\n",
"from semantica.export import MethodRegistry, method_registry, JSONExporter\n",
"\n",
"# Register custom method\n",
"def my_custom_export(data, file_path, **kwargs):\n",
" # Custom export logic\n",
" pass\n",
"\n",
"MethodRegistry.register(\"json\", \"custom\", my_custom_export)\n",
"\n",
"# List available methods\n",
"methods = method_registry.list_all()\n",
"print(methods)\n",
"```\n",
"\n",
"## Summary\n",
"\n",
"You've learned how to export data using Semantica's Export Module:\n",
"\n",
"### **Core Exporter Classes:**\n",
"- **JSONExporter**: Export to JSON/JSON-LD format\n",
"- **CSVExporter**: Export to CSV format\n",
"- **RDFExporter**: Export to RDF format (Turtle, RDF/XML, JSON-LD, N-Triples, N3)\n",
"- **GraphExporter**: Export to graph formats (GraphML, GEXF, DOT)\n",
"- **OWLExporter**: Export ontologies to OWL\n",
"- **VectorExporter**: Export vectors to multiple formats\n",
"- **LPGExporter**: Export to Labeled Property Graph format\n",
"- **SemanticNetworkYAMLExporter**: Export semantic networks to YAML\n",
"- **YAMLSchemaExporter**: Export schemas to YAML\n",
"\n",
"### **Supporting Classes:**\n",
"- **RDFSerializer**: RDF format conversion\n",
"- **RDFValidator**: RDF validation\n",
"- **NamespaceManager**: RDF namespace management\n",
"- **ReportGenerator**: Generate professional reports\n",
"\n",
"### **Registry & Configuration:**\n",
"- `MethodRegistry`: Register custom export methods\n",
"- `ExportConfig`: Configure export settings\n",
"- `method_registry`: Global registry instance for accessing registered methods\n",
"- `export_config`: Global configuration instance for export settings\n",
"\n",
"**Next Steps:**\n",
"- Learn advanced export techniques in the [Multi-Format Export notebook](../advanced/05_Multi_Format_Export.ipynb)\n",
"- Learn how to visualize data in the Visualization notebook\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,208 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/17_Visualization.ipynb)\n",
"\n",
"# 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",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/visualization/)\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",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## 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"
]
},
{
"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"
]
},
{
"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_embeddings(texts, data_type=\"text\")\n",
"labels = [\"Apple\", \"Microsoft\", \"Amazon\"]\n",
"\n",
"visualization = embedding_visualizer.visualize_2d_projection(embeddings, labels, method=\"umap\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Semantic Network Visualization\n",
"\n",
"Visualize semantic networks: structure, node types, and edge types."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.visualization import SemanticNetworkVisualizer\n",
"\n",
"semantic_network = {\n",
" \"nodes\": [\n",
" {\"id\": \"n1\", \"label\": \"Node 1\", \"type\": \"Entity\"},\n",
" {\"id\": \"n2\", \"label\": \"Node 2\", \"type\": \"Entity\"}\n",
" ],\n",
" \"edges\": [\n",
" {\"source\": \"n1\", \"target\": \"n2\", \"label\": \"related_to\"}\n",
" ]\n",
"}\n",
"\n",
"sem_viz = SemanticNetworkVisualizer()\n",
"sem_viz.visualize_network(semantic_network, output=\"interactive\")\n",
"sem_viz.visualize_node_types(semantic_network, output=\"interactive\")\n",
"sem_viz.visualize_edge_types(semantic_network, output=\"interactive\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Advanced Embedding Visualization\n",
"\n",
"Compare multi-modal embeddings and assess embedding quality."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from semantica.visualization import EmbeddingVisualizer\n",
"\n",
"# Synthetic multi-modal embeddings (text, image, audio)\n",
"text_emb = np.random.rand(50, 128)\n",
"image_emb = np.random.rand(50, 128)\n",
"audio_emb = np.random.rand(50, 128)\n",
"\n",
"emb_viz = EmbeddingVisualizer()\n",
"emb_viz.visualize_multimodal_comparison(text_emb, image_emb, audio_emb, output=\"interactive\")\n",
"\n",
"# Embedding quality metrics\n",
"quality_fig = emb_viz.visualize_quality_metrics(text_emb, output=\"interactive\")\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, multi-modal and quality metrics\n",
"- **SemanticNetworkVisualizer**: Visualize semantic network structure and type distributions\n",
"\n",
"Next: Learn how to detect conflicts in the Conflict_Detection notebook.\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,604 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/18_Conflict_Detection.ipynb)\n",
"\n",
"# 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",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/conflicts/)\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",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---\n",
"\n",
"## 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",
"from datetime import datetime\n",
"\n",
"# Initialize detector with configuration\n",
"detector = ConflictDetector(\n",
" confidence_threshold=0.7,\n",
" track_provenance=True,\n",
" conflict_fields={\"Company\": [\"name\", \"founded\", \"revenue\"]}\n",
")\n",
"\n",
"# Sample entities from multiple sources\n",
"entities = [\n",
" {\"id\": \"e1\", \"name\": \"Apple Inc.\", \"founded\": 1976, \"type\": \"Company\", \n",
" \"source\": \"wikipedia\", \"confidence\": 0.9},\n",
" {\"id\": \"e1\", \"name\": \"Apple Incorporated\", \"founded\": 1976, \"type\": \"Company\",\n",
" \"source\": \"official_site\", \"confidence\": 0.95},\n",
" {\"id\": \"e1\", \"name\": \"Apple Inc.\", \"founded\": 1977, \"type\": \"Company\",\n",
" \"source\": \"news\", \"confidence\": 0.7},\n",
" {\"id\": \"e2\", \"name\": \"Microsoft\", \"type\": \"Company\", \"founded\": 1975, \"source\": \"source1\"},\n",
" {\"id\": \"e2\", \"name\": \"Microsoft Corporation\", \"type\": \"Organization\", \n",
" \"founded\": 1975, \"source\": \"source2\"},\n",
"]\n",
"\n",
"# 1.1 Value Conflict Detection\n",
"value_conflicts = detector.detect_value_conflicts(entities, \"name\")\n",
"\n",
"# 1.2 Type Conflict Detection\n",
"type_conflicts = detector.detect_type_conflicts(entities)\n",
"\n",
"# 1.3 Temporal Conflict Detection\n",
"temporal_conflicts = detector.detect_temporal_conflicts(entities)\n",
"\n",
"# 1.4 Logical Conflict Detection\n",
"logical_entities = [\n",
" {\"id\": \"e3\", \"type\": \"Person\", \"name\": \"John Doe\", \"source\": \"source1\"},\n",
" {\"id\": \"e3\", \"type\": \"Organization\", \"name\": \"John Doe\", \"source\": \"source2\"},\n",
"]\n",
"logical_conflicts = detector.detect_logical_conflicts(logical_entities)\n",
"\n",
"# 1.5 Relationship Conflict Detection\n",
"relationships = [\n",
" {\"id\": \"rel1\", \"source_id\": \"e1\", \"target_id\": \"e2\", \"type\": \"competes_with\", \"source\": \"source1\"},\n",
" {\"id\": \"rel1\", \"source_id\": \"e1\", \"target_id\": \"e2\", \"type\": \"partners_with\", \"source\": \"source2\"},\n",
"]\n",
"rel_conflicts = detector.detect_relationship_conflicts(relationships)\n",
"\n",
"# 1.6 General Conflict Detection (all types)\n",
"all_conflicts = detector.detect_conflicts(entities)\n",
"\n",
"# Get conflict report\n",
"report = detector.get_conflict_report()\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, SourceReference\n",
"from datetime import datetime\n",
"\n",
"# Initialize source tracker\n",
"tracker = SourceTracker()\n",
"\n",
"# Create source references with metadata\n",
"source1 = SourceReference(\n",
" document=\"wikipedia\",\n",
" page=1,\n",
" section=\"Company Information\",\n",
" timestamp=datetime(2023, 1, 15),\n",
" confidence=0.9\n",
")\n",
"\n",
"source2 = SourceReference(\n",
" document=\"official_site\",\n",
" section=\"About Us\",\n",
" timestamp=datetime(2023, 3, 20),\n",
" confidence=0.95\n",
")\n",
"\n",
"# Track property sources\n",
"tracker.track_property_source(\"e1\", \"name\", \"Apple Inc.\", source1)\n",
"tracker.track_property_source(\"e1\", \"name\", \"Apple Incorporated\", source2)\n",
"tracker.track_property_source(\"e1\", \"founded\", 1976, source1)\n",
"\n",
"# Track entity sources\n",
"tracker.track_entity_source(\"e1\", source1)\n",
"\n",
"# Set source credibility scores\n",
"tracker.set_source_credibility(\"wikipedia\", 0.85)\n",
"tracker.set_source_credibility(\"official_site\", 0.95)\n",
"\n",
"# Retrieve property sources\n",
"prop_source = tracker.get_property_sources(\"e1\", \"name\")\n",
"\n",
"# Get entity sources\n",
"entity_sources = tracker.get_entity_sources(\"e1\")\n",
"\n",
"# Get all source credibilities\n",
"all_credibilities = tracker.get_all_source_credibilities()\n",
"\n",
"# Generate traceability chain\n",
"chain = tracker.generate_traceability_chain(\"e1\", \"name\")\n",
"\n",
"# Generate source report\n",
"report = tracker.generate_source_report(\"e1\")\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",
"# Initialize resolver with source tracker\n",
"resolver = ConflictResolver(\n",
" default_strategy=\"voting\",\n",
" source_tracker=tracker\n",
")\n",
"\n",
"# Resolve conflicts using different strategies\n",
"if value_conflicts:\n",
" # Voting strategy\n",
" voting_results = resolver.resolve_conflicts(value_conflicts, strategy=\"voting\")\n",
" \n",
" # Credibility-weighted strategy\n",
" credibility_results = resolver.resolve_conflicts(value_conflicts, strategy=\"credibility_weighted\")\n",
" \n",
" # Most recent strategy\n",
" recent_results = resolver.resolve_conflicts(value_conflicts, strategy=\"most_recent\")\n",
" \n",
" # Highest confidence strategy\n",
" confidence_results = resolver.resolve_conflicts(value_conflicts, strategy=\"highest_confidence\")\n",
" \n",
" # First seen strategy\n",
" first_seen_results = resolver.resolve_conflicts(value_conflicts, strategy=\"first_seen\")\n",
" \n",
" # Manual review strategy\n",
" manual_results = resolver.resolve_conflicts(value_conflicts, strategy=\"manual_review\")\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"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.conflicts import ConflictAnalyzer\n",
"\n",
"# Initialize analyzer\n",
"analyzer = ConflictAnalyzer()\n",
"\n",
"# Comprehensive analysis\n",
"analysis = analyzer.analyze_conflicts(all_conflicts)\n",
"\n",
"# Analysis by type, severity, and source\n",
"by_type = analysis['by_type']['counts']\n",
"by_severity = analysis['by_severity']['counts']\n",
"by_source = analysis['by_source']['counts']\n",
"\n",
"# Top entities and properties\n",
"top_entities = analysis['by_entity']['top_entities']\n",
"top_properties = analysis['by_property']['top_properties']\n",
"\n",
"# Patterns and recommendations\n",
"patterns = analysis['patterns']\n",
"recommendations = analysis['recommendations']\n",
"\n",
"# Trend analysis\n",
"trends = analyzer.analyze_trends(all_conflicts)\n",
"\n",
"# Generate insights report\n",
"insights = analyzer.generate_insights_report(all_conflicts)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 5: Investigation Guides\n",
"\n",
"`InvestigationGuideGenerator` creates guides for manual review.\n",
"\n",
"**Guide Components:**\n",
"- Conflict summary, investigation steps, recommended actions\n",
"- Source information, context, severity assessment\n",
"\n",
"**Use Cases:** High-severity conflicts, ambiguous cases, compliance, QA workflows\n",
"\n",
"**Export Formats:** Markdown checklists, detailed reports, structured context\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.conflicts import InvestigationGuideGenerator\n",
"\n",
"# Initialize guide generator\n",
"guide_generator = InvestigationGuideGenerator(source_tracker=tracker)\n",
"\n",
"# Generate guide for a conflict\n",
"if value_conflicts:\n",
" guide = guide_generator.generate_guide(value_conflicts[0])\n",
"\n",
"# Generate guides for multiple conflicts\n",
"guides = guide_generator.generate_guides(value_conflicts[:3])\n",
"\n",
"# Export checklist\n",
"checklist = guide_generator.export_investigation_checklist(guide, format=\"markdown\")\n",
"\n",
"# Generate conflict report\n",
"conflict_report = guide_generator.generate_conflict_report(value_conflicts, format=\"detailed\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 6: Methods Module\n",
"\n",
"`semantica.conflicts.methods` provides convenience functions for functional-style access.\n",
"\n",
"**Functions:**\n",
"- `detect_conflicts()`: Methods: `value`, `type`, `temporal`, `logical`, `relationship`\n",
"- `resolve_conflicts()`: Methods: `voting`, `credibility_weighted`, `most_recent`, `highest_confidence`, `first_seen`, `manual_review`\n",
"- `analyze_conflicts()`: Methods: `pattern`, `type`, `severity`, `source`, `trend`\n",
"- `track_sources()`: Methods: `property`, `entity`, `relationship`\n",
"- `generate_investigation_guide()`: Methods: `guide`, `checklist`, `context`\n",
"- `list_available_methods()`: List all methods by task type\n",
"- `get_conflict_method()`: Retrieve specific method function\n",
"\n",
"**Benefits:** Simpler API, method discovery, consistent interface, extensible\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.conflicts.methods import (\n",
" detect_conflicts,\n",
" resolve_conflicts,\n",
" analyze_conflicts,\n",
" track_sources,\n",
" generate_investigation_guide,\n",
" list_available_methods,\n",
" get_conflict_method\n",
")\n",
"\n",
"# Detection methods\n",
"value_conflicts_method = detect_conflicts(entities, method=\"value\", property_name=\"name\")\n",
"type_conflicts_method = detect_conflicts(entities, method=\"type\")\n",
"temporal_conflicts_method = detect_conflicts(entities, method=\"temporal\")\n",
"logical_conflicts_method = detect_conflicts(logical_entities, method=\"logical\")\n",
"\n",
"# Resolution methods\n",
"if value_conflicts_method:\n",
" voting_results = resolve_conflicts(value_conflicts_method, method=\"voting\")\n",
" credibility_results = resolve_conflicts(value_conflicts_method, method=\"credibility_weighted\")\n",
"\n",
"# Analysis methods\n",
"pattern_analysis = analyze_conflicts(all_conflicts, method=\"pattern\")\n",
"type_analysis = analyze_conflicts(all_conflicts, method=\"type\")\n",
"severity_analysis = analyze_conflicts(all_conflicts, method=\"severity\")\n",
"source_analysis = analyze_conflicts(all_conflicts, method=\"source\")\n",
"trend_analysis = analyze_conflicts(all_conflicts, method=\"trend\")\n",
"\n",
"# Source tracking methods\n",
"source_ref = SourceReference(document=\"test_source\", confidence=0.9)\n",
"track_sources(\"e1\", method=\"property\", property_name=\"name\", value=\"Test\", source=source_ref)\n",
"track_sources(\"e1\", method=\"entity\", source=source_ref)\n",
"\n",
"# Investigation guide methods\n",
"if value_conflicts_method:\n",
" guide_method = generate_investigation_guide(value_conflicts_method[0], method=\"guide\")\n",
" checklist_method = generate_investigation_guide(value_conflicts_method[0], method=\"checklist\")\n",
" context_method = generate_investigation_guide(value_conflicts_method[0], method=\"context\")\n",
"\n",
"# List available methods\n",
"all_methods = list_available_methods()\n",
"\n",
"# Get specific method\n",
"voting_method = get_conflict_method(\"resolution\", \"voting\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 7: Method Registry\n",
"\n",
"`method_registry` provides a plugin system for custom methods.\n",
"\n",
"**Registration:** Task type (`detection`, `resolution`, `analysis`, `tracking`, `investigation`), method name, function\n",
"\n",
"**Use Cases:**\n",
"- Domain-specific resolution logic\n",
"- External system integration\n",
"- A/B testing strategies\n",
"- ML model integration\n",
"- Hybrid resolution approaches\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.conflicts import method_registry, ResolutionResult\n",
"\n",
"# Register custom resolution method\n",
"def custom_resolution(conflicts, **kwargs):\n",
" \"\"\"Custom resolution that always picks the first value.\"\"\"\n",
" results = []\n",
" for conflict in conflicts:\n",
" if conflict.conflicting_values:\n",
" result = ResolutionResult(\n",
" conflict_id=conflict.conflict_id,\n",
" resolved=True,\n",
" resolved_value=conflict.conflicting_values[0],\n",
" resolution_strategy=\"custom_first\",\n",
" confidence=0.8,\n",
" resolution_notes=\"Custom: Always use first value\"\n",
" )\n",
" results.append(result)\n",
" return results\n",
"\n",
"# Register the custom method\n",
"method_registry.register(\"resolution\", \"custom_first\", custom_resolution)\n",
"\n",
"# List registered methods\n",
"registered = method_registry.list_all(\"resolution\")\n",
"\n",
"# Use custom method\n",
"if value_conflicts:\n",
" custom_results = resolve_conflicts(value_conflicts, method=\"custom_first\")\n",
"\n",
"# Unregister method\n",
"method_registry.unregister(\"resolution\", \"custom_first\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 8: Configuration\n",
"\n",
"`ConflictsConfig` manages settings programmatically, via environment variables, or method-specific.\n",
"\n",
"**Global Settings:**\n",
"- `confidence_threshold`, `default_strategy`, `auto_resolve`, `track_provenance`\n",
"\n",
"**Method-Specific:** Voting (`min_sources`, `tie_breaker`), credibility-weighted (`min_credibility`), most_recent (`time_field`), etc.\n",
"\n",
"**Priority:** Method-specific → Global → Environment variables → Defaults\n",
"\n",
"**Best Practices:** Set source credibility early, configure conflict fields, use method-specific configs\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.conflicts import ConflictsConfig, conflicts_config\n",
"\n",
"# Using global config instance\n",
"conflicts_config.set(\"confidence_threshold\", 0.8)\n",
"conflicts_config.set(\"default_strategy\", \"credibility_weighted\")\n",
"threshold = conflicts_config.get(\"confidence_threshold\", default=0.7)\n",
"\n",
"# Method-specific configuration\n",
"conflicts_config.set_method_config(\"voting\", min_sources=2, tie_breaker=\"confidence\")\n",
"conflicts_config.set_method_config(\"credibility_weighted\", min_credibility=0.5)\n",
"voting_config = conflicts_config.get_method_config(\"voting\")\n",
"\n",
"# Create custom config instance\n",
"custom_config = ConflictsConfig()\n",
"custom_config.set(\"confidence_threshold\", 0.9)\n",
"custom_config.set(\"auto_resolve\", True)\n",
"all_config = custom_config.get_all()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part 9: Complete Workflow\n",
"\n",
"End-to-end example: integrating company data from multiple sources.\n",
"\n",
"**Workflow:** Initialize → Track Sources → Detect → Resolve → Analyze → Generate Guides → Build Final Entity\n",
"\n",
"**Scenario:** Three sources (Wikipedia, Official Site, Financial DB) with conflicts in name, founding year, and type classifications.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Complete workflow: Company data integration from multiple sources\n",
"from semantica.conflicts import (\n",
" ConflictDetector, ConflictResolver, ConflictAnalyzer,\n",
" SourceTracker, InvestigationGuideGenerator, SourceReference\n",
")\n",
"\n",
"# Sample company data from multiple sources\n",
"company_data = [\n",
" {\"id\": \"company_1\", \"name\": \"Apple Inc.\", \"founded\": 1976, \"revenue\": 394328000000,\n",
" \"headquarters\": \"Cupertino, California\", \"type\": \"Company\",\n",
" \"source\": \"wikipedia\", \"confidence\": 0.85, \"timestamp\": datetime(2023, 1, 15)},\n",
" {\"id\": \"company_1\", \"name\": \"Apple Inc.\", \"founded\": 1976, \"revenue\": 394328000000,\n",
" \"headquarters\": \"Cupertino, CA\", \"type\": \"Company\",\n",
" \"source\": \"official_site\", \"confidence\": 0.95, \"timestamp\": datetime(2023, 3, 20)},\n",
" {\"id\": \"company_1\", \"name\": \"Apple Incorporated\", \"founded\": 1977, \"revenue\": 394328000000,\n",
" \"headquarters\": \"Cupertino\", \"type\": \"Organization\",\n",
" \"source\": \"financial_db\", \"confidence\": 0.80, \"timestamp\": datetime(2023, 2, 10)},\n",
"]\n",
"\n",
"# Initialize all components\n",
"detector = ConflictDetector(\n",
" confidence_threshold=0.7,\n",
" track_provenance=True,\n",
" conflict_fields={\"Company\": [\"name\", \"founded\", \"revenue\", \"headquarters\"]}\n",
")\n",
"\n",
"tracker = SourceTracker()\n",
"tracker.set_source_credibility(\"wikipedia\", 0.85)\n",
"tracker.set_source_credibility(\"official_site\", 0.95)\n",
"tracker.set_source_credibility(\"financial_db\", 0.80)\n",
"\n",
"resolver = ConflictResolver(default_strategy=\"credibility_weighted\", source_tracker=tracker)\n",
"analyzer = ConflictAnalyzer()\n",
"guide_generator = InvestigationGuideGenerator(source_tracker=tracker)\n",
"\n",
"# Step 1: Track sources\n",
"for entity in company_data:\n",
" source_ref = SourceReference(\n",
" document=entity[\"source\"],\n",
" confidence=entity[\"confidence\"],\n",
" timestamp=entity[\"timestamp\"]\n",
" )\n",
" tracker.track_property_source(entity[\"id\"], \"name\", entity[\"name\"], source_ref)\n",
" tracker.track_property_source(entity[\"id\"], \"founded\", entity[\"founded\"], source_ref)\n",
"\n",
"# Step 2: Detect conflicts\n",
"detected_conflicts = detector.detect_entity_conflicts(company_data, entity_type=\"Company\")\n",
"\n",
"# Step 3: Resolve conflicts\n",
"resolved_data = {}\n",
"for conflict in detected_conflicts:\n",
" results = resolver.resolve_conflicts([conflict], strategy=\"credibility_weighted\")\n",
" if results[0].resolved:\n",
" resolved_data[conflict.property_name] = results[0].resolved_value\n",
"\n",
"# Step 4: Analyze\n",
"analysis = analyzer.analyze_conflicts(detected_conflicts)\n",
"\n",
"# Step 5: Generate guides for unresolved conflicts\n",
"unresolved = [c for c in detected_conflicts if c.property_name not in resolved_data]\n",
"if unresolved:\n",
" guides = guide_generator.generate_guides(unresolved)\n",
"\n",
"# Final resolved entity\n",
"final_entity = {\"id\": \"company_1\", \"type\": \"Company\", **resolved_data}\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"### Key Features\n",
"\n",
"✅ **Detection**: Value, type, temporal, logical, relationship conflicts \n",
"✅ **Resolution**: 6 strategies (voting, credibility-weighted, most_recent, first_seen, highest_confidence, manual_review) \n",
"✅ **Source Tracking**: Provenance, credibility, traceability chains \n",
"✅ **Analysis**: Patterns, trends, recommendations \n",
"✅ **Investigation Guides**: Automated guides and checklists \n",
"✅ **Methods Module**: Convenience functions for all operations \n",
"✅ **Method Registry**: Custom method registration \n",
"✅ **Configuration**: Global and method-specific settings\n",
"\n",
"### Best Practices\n",
"\n",
"1. Set source credibility before detection\n",
"2. Choose strategies based on data characteristics\n",
"3. Enable provenance tracking for audits\n",
"4. Analyze patterns before resolving\n",
"5. Use guides for high-severity conflicts\n",
"6. Configure conflict fields to focus on critical properties\n",
"\n",
"### Common Patterns\n",
"\n",
"- **Integration**: Track → Detect → Resolve → Analyze\n",
"- **QA**: Detect → Analyze → Generate guides → Review\n",
"- **Auto**: Detect → Resolve → Analyze\n",
"- **Assessment**: Track → Analyze → Adjust credibility\n",
"\n",
"### Next Steps\n",
"\n",
"- `04_Conflict_Resolution_Strategies.ipynb` - Advanced strategies\n",
"- `06_Multi_Source_Data_Integration.ipynb` - Integration workflows\n",
"- [API Reference](https://semantica.readthedocs.io/reference/conflicts/)\n",
"- [Usage Guide](../semantica/conflicts/conflicts_usage.md)\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,758 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/18_Deduplication.ipynb)\n",
"\n",
"# Deduplication Module\n",
"\n",
"## What is the Deduplication Module?\n",
"\n",
"The Deduplication Module is a comprehensive system for identifying and merging duplicate entities in knowledge graphs. It helps maintain data quality by detecting semantically similar entities, calculating similarity scores, and merging duplicates into canonical representations while preserving provenance and handling conflicts.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/deduplication/)\n",
"\n",
"## Module Capabilities\n",
"\n",
"### Core Functionality\n",
"\n",
"1. **Similarity Calculation**\n",
" - Multiple algorithms: Exact matching, Levenshtein distance, Jaro-Winkler, Cosine similarity, Jaccard similarity\n",
" - Multi-factor aggregation: Combines string, property, relationship, and embedding similarity\n",
" - Configurable weights for different similarity components\n",
" - Batch processing for efficient pairwise comparisons\n",
"\n",
"2. **Duplicate Detection**\n",
" - Pairwise comparison: Compare all entity pairs for duplicates\n",
" - Group detection: Find clusters of duplicates using Union-Find algorithm\n",
" - Incremental detection: Efficiently detect duplicates between new and existing entities\n",
" - Confidence scoring: Multi-factor confidence calculation (similarity + name match + property matches)\n",
" - Relationship duplicate detection: Identify duplicate relationships\n",
"\n",
"3. **Entity Merging**\n",
" - Multiple merge strategies: Keep first, last, most complete, highest confidence, or merge all\n",
" - Automatic duplicate detection before merging\n",
" - Conflict resolution: Handle property and relationship conflicts\n",
" - Provenance preservation: Track which entities were merged\n",
" - Merge history: Maintain record of all merge operations\n",
" - Quality validation: Validate merged entities for completeness\n",
"\n",
"4. **Clustering**\n",
" - Graph-based clustering: Union-Find algorithm for connected components\n",
" - Hierarchical clustering: Agglomerative clustering for large datasets\n",
" - Cluster quality metrics: Cohesion and separation measures\n",
" - Incremental updates: Update clusters with new entities\n",
"\n",
"5. **Advanced Features**\n",
" - Property-specific merge rules: Different strategies for different properties\n",
" - Custom conflict resolution: Define custom functions for resolving conflicts\n",
" - Method registry: Register and use custom deduplication methods\n",
" - Configuration management: Centralized configuration from multiple sources\n",
" - Extensibility: Add custom similarity, detection, merge, and clustering methods\n",
"\n",
"## Module Architecture\n",
"\n",
"### Main Components\n",
"\n",
"**Core Classes:**\n",
"- `DuplicateDetector`: Detects duplicate entities and relationships\n",
"- `EntityMerger`: Merges duplicate entities with configurable strategies\n",
"- `SimilarityCalculator`: Calculates multi-factor similarity between entities\n",
"- `ClusterBuilder`: Builds clusters for efficient batch deduplication\n",
"- `MergeStrategyManager`: Manages merge strategies and conflict resolution\n",
"- `MethodRegistry`: Registry for custom deduplication methods\n",
"- `DeduplicationConfig`: Centralized configuration management\n",
"\n",
"**Data Structures:**\n",
"- `DuplicateCandidate`: Duplicate pair with confidence scores\n",
"- `DuplicateGroup`: Group of duplicate entities\n",
"- `MergeOperation`: Merge operation record with metadata\n",
"- `SimilarityResult`: Similarity calculation result with components\n",
"- `Cluster`: Entity cluster representation\n",
"- `ClusterResult`: Cluster building result with quality metrics\n",
"- `MergeResult`: Merge operation result with conflicts\n",
"- `MergeStrategy`: Enumeration of merge strategies\n",
"\n",
"**Convenience Functions:**\n",
"- `detect_duplicates()`: Duplicate detection wrapper\n",
"- `merge_entities()`: Entity merging wrapper\n",
"- `calculate_similarity()`: Similarity calculation wrapper\n",
"- `build_clusters()`: Cluster building wrapper\n",
"- `get_deduplication_method()`: Get method by name\n",
"- `list_available_methods()`: List all available methods\n",
"\n",
"## Algorithms Used\n",
"\n",
"**Similarity Calculation:**\n",
"- Levenshtein Distance: Dynamic programming for edit distance\n",
"- Jaro Similarity: Character-based similarity with match window\n",
"- Jaro-Winkler: Jaro with prefix bonus (up to 4 characters)\n",
"- Cosine Similarity: Vector dot product for embeddings\n",
"- Jaccard Similarity: Intersection over union for sets\n",
"- Multi-factor Aggregation: Weighted sum of similarity components\n",
"\n",
"**Duplicate Detection:**\n",
"- Pairwise Comparison: O(n²) all-pairs similarity calculation\n",
"- Union-Find Algorithm: Disjoint set union for group formation\n",
"- Confidence Scoring: Multi-factor confidence calculation\n",
"- Incremental Processing: O(n×m) efficient new vs existing comparison\n",
"\n",
"**Clustering:**\n",
"- Union-Find (DSU): Connected component detection\n",
"- Hierarchical Clustering: Agglomerative bottom-up clustering\n",
"- Similarity Graph: Graph construction from similarity scores\n",
"\n",
"**Entity Merging:**\n",
"- Strategy Pattern: Multiple merge strategies\n",
"- Conflict Resolution: Voting, credibility-weighted, temporal, confidence-based\n",
"- Property Merging: Rule-based property combination\n",
"- Provenance Tracking: Metadata preservation during merges\n",
"\n",
"## Installation\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"## Table of Contents\n",
"\n",
"1. [Module Overview](#module-overview)\n",
"2. [Setup and Sample Data](#setup)\n",
"3. [Similarity Calculation](#similarity)\n",
"4. [Duplicate Detection](#detection)\n",
"5. [Entity Merging](#merging)\n",
"6. [Clustering](#clustering)\n",
"7. [Advanced Features](#advanced)\n",
"8. [Complete Workflow](#workflow)\n",
"\n",
"---\n",
"\n",
"## Module Overview {#module-overview}\n",
"\n",
"The deduplication module provides a complete solution for maintaining clean knowledge graphs by identifying and merging duplicate entities. It supports multiple similarity algorithms, detection methods, merge strategies, and clustering approaches, making it suitable for various use cases from simple exact matching to advanced semantic deduplication.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import all deduplication classes\n",
"from semantica.deduplication import (\n",
" # Main Classes\n",
" DuplicateDetector,\n",
" EntityMerger,\n",
" SimilarityCalculator,\n",
" ClusterBuilder,\n",
" MergeStrategyManager,\n",
" MethodRegistry,\n",
" DeduplicationConfig,\n",
" # Data Classes\n",
" DuplicateCandidate,\n",
" DuplicateGroup,\n",
" MergeOperation,\n",
" SimilarityResult,\n",
" Cluster,\n",
" ClusterResult,\n",
" MergeResult,\n",
" MergeStrategy,\n",
" # Global Instances\n",
" method_registry,\n",
" dedup_config,\n",
")\n",
"\n",
"# Create sample entities with potential duplicates\n",
"entities = [\n",
" {\n",
" \"id\": \"e1\",\n",
" \"name\": \"Apple Inc.\",\n",
" \"type\": \"Company\",\n",
" \"founded\": 1976,\n",
" \"properties\": {\"industry\": \"Technology\", \"headquarters\": \"Cupertino\"},\n",
" \"relationships\": [{\"subject\": \"e1\", \"predicate\": \"founded_by\", \"object\": \"Steve Jobs\"}],\n",
" },\n",
" {\n",
" \"id\": \"e2\",\n",
" \"name\": \"Apple Inc\",\n",
" \"type\": \"Company\",\n",
" \"founded\": 1976,\n",
" \"properties\": {\"industry\": \"Tech\", \"headquarters\": \"Cupertino, CA\"},\n",
" \"relationships\": [{\"subject\": \"e2\", \"predicate\": \"founded_by\", \"object\": \"Steve Jobs\"}],\n",
" },\n",
" {\n",
" \"id\": \"e3\",\n",
" \"name\": \"Microsoft Corporation\",\n",
" \"type\": \"Company\",\n",
" \"founded\": 1975,\n",
" \"properties\": {\"industry\": \"Technology\", \"headquarters\": \"Redmond\"},\n",
" },\n",
" {\n",
" \"id\": \"e4\",\n",
" \"name\": \"Microsoft\",\n",
" \"type\": \"Company\",\n",
" \"founded\": 1975,\n",
" \"properties\": {\"industry\": \"Tech\", \"headquarters\": \"Redmond, WA\"},\n",
" },\n",
" {\n",
" \"id\": \"e5\",\n",
" \"name\": \"Google LLC\",\n",
" \"type\": \"Company\",\n",
" \"founded\": 1998,\n",
" \"properties\": {\"industry\": \"Technology\"},\n",
" },\n",
"]\n",
"\n",
"print(f\"Created {len(entities)} sample entities\")\n",
"print(\"\\nEntity names:\")\n",
"for e in entities:\n",
" print(f\" - {e['name']} (ID: {e['id']})\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Similarity Calculation {#similarity}\n",
"\n",
"The module provides multiple algorithms for calculating similarity between entities. Similarity is the foundation of duplicate detection.\n",
"\n",
"### Available Similarity Methods\n",
"\n",
"1. **Exact Matching**: Binary match/no-match for identical strings\n",
"2. **Levenshtein Distance**: Edit distance between strings (insertions, deletions, substitutions)\n",
"3. **Jaro-Winkler**: Character-based similarity with prefix bonus for common prefixes\n",
"4. **Cosine Similarity**: Vector similarity for embeddings using dot product\n",
"5. **Jaccard Similarity**: Set-based similarity (intersection over union)\n",
"6. **Property Similarity**: Weighted comparison of property values\n",
"7. **Relationship Similarity**: Jaccard similarity of relationship sets\n",
"8. **Multi-factor Similarity**: Weighted aggregation of all components\n",
"\n",
"### SimilarityCalculator Class\n",
"\n",
"The `SimilarityCalculator` class provides a unified interface for all similarity calculations with configurable weights for different components.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Example: Duplicate Detection\n",
"\n",
"# Initialize DuplicateDetector\n",
"detector = DuplicateDetector(\n",
" similarity_threshold=0.7,\n",
" confidence_threshold=0.6,\n",
" use_clustering=True,\n",
")\n",
"\n",
"# Detect duplicate candidates (pairwise)\n",
"candidates = detector.detect_duplicates(entities)\n",
"print(f\"Found {len(candidates)} duplicate candidate(s)\")\n",
"for candidate in candidates:\n",
" print(f\" {candidate.entity1['name']} <-> {candidate.entity2['name']}\")\n",
" print(f\" Similarity: {candidate.similarity_score:.3f}, Confidence: {candidate.confidence:.3f}\")\n",
"\n",
"# Detect duplicate groups\n",
"duplicate_groups = detector.detect_duplicate_groups(entities)\n",
"print(f\"\\nFound {len(duplicate_groups)} duplicate group(s)\")\n",
"for i, group in enumerate(duplicate_groups, 1):\n",
" print(f\" Group {i}: {[e['name'] for e in group.entities]} (confidence: {group.confidence:.3f})\")\n",
"\n",
"# Incremental detection\n",
"existing_entities = entities[:3]\n",
"new_entities = entities[3:]\n",
"incremental_candidates = detector.incremental_detect(new_entities, existing_entities, threshold=0.7)\n",
"print(f\"\\nFound {len(incremental_candidates)} incremental duplicate(s)\")\n",
"for candidate in incremental_candidates:\n",
" print(f\" {candidate.entity1['name']} duplicates {candidate.entity2['name']} (confidence: {candidate.confidence:.3f})\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Duplicate Detection {#detection}\n",
"\n",
"The module detects duplicate entities using similarity metrics and confidence scoring. Detection can be performed pairwise, in groups, or incrementally.\n",
"\n",
"### Detection Methods\n",
"\n",
"1. **Pairwise Detection**: Compare all entity pairs (O(n²) complexity)\n",
"2. **Group Detection**: Find clusters of duplicates using Union-Find algorithm\n",
"3. **Incremental Detection**: Efficiently detect duplicates between new and existing entities (O(n×m))\n",
"4. **Relationship Detection**: Identify duplicate relationships\n",
"\n",
"### Confidence Scoring\n",
"\n",
"The module calculates confidence scores using multiple factors:\n",
"- Similarity score between entities\n",
"- Name matching (exact or fuzzy)\n",
"- Property value matches\n",
"- Entity type matches\n",
"- Relationship overlap\n",
"\n",
"### DuplicateDetector Class\n",
"\n",
"The `DuplicateDetector` class provides all duplicate detection capabilities with configurable thresholds and clustering options.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Example: Similarity Calculation\n",
"\n",
"# Initialize SimilarityCalculator\n",
"calculator = SimilarityCalculator(\n",
" string_weight=0.4,\n",
" property_weight=0.3,\n",
" relationship_weight=0.2,\n",
" embedding_weight=0.1,\n",
")\n",
"\n",
"# Calculate overall similarity (multi-factor)\n",
"entity1, entity2 = entities[0], entities[1]\n",
"result = calculator.calculate_similarity(entity1, entity2)\n",
"print(f\"Overall Similarity: {result.score:.3f}\")\n",
"print(f\"Components: {result.components}\")\n",
"\n",
"# String similarity methods\n",
"str1, str2 = \"Apple Inc.\", \"Apple Inc\"\n",
"for method in [\"levenshtein\", \"jaro_winkler\", \"cosine\"]:\n",
" score = calculator.calculate_string_similarity(str1, str2, method=method)\n",
" print(f\"{method}: {score:.3f}\")\n",
"\n",
"# Property and relationship similarity\n",
"prop_score = calculator.calculate_property_similarity(entity1, entity2)\n",
"rel_score = calculator.calculate_relationship_similarity(entity1, entity2)\n",
"print(f\"\\nProperty Similarity: {prop_score:.3f}\")\n",
"print(f\"Relationship Similarity: {rel_score:.3f}\")\n",
"\n",
"# Batch similarity calculation\n",
"similarity_pairs = calculator.batch_calculate_similarity(entities, threshold=0.5)\n",
"print(f\"\\nFound {len(similarity_pairs)} similar pairs (threshold >= 0.5)\")\n",
"for e1, e2, score in similarity_pairs:\n",
" print(f\" {e1['name']} <-> {e2['name']}: {score:.3f}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Entity Merging {#merging}\n",
"\n",
"The module merges duplicate entities into single canonical representations using configurable strategies. Merging preserves provenance, handles conflicts, and maintains merge history.\n",
"\n",
"### Merge Strategies\n",
"\n",
"1. **KEEP_FIRST**: Preserve the first entity encountered, merge others into it\n",
"2. **KEEP_LAST**: Preserve the last entity encountered, merge others into it\n",
"3. **KEEP_MOST_COMPLETE**: Preserve entity with most properties and relationships\n",
"4. **KEEP_HIGHEST_CONFIDENCE**: Preserve entity with highest confidence score\n",
"5. **MERGE_ALL**: Create new entity combining all properties and relationships\n",
"6. **CUSTOM**: User-defined merge logic\n",
"\n",
"### Conflict Resolution\n",
"\n",
"When merging entities with conflicting property values, the module supports:\n",
"- Voting: Majority value selection\n",
"- Credibility-weighted: Weighted by source credibility\n",
"- Temporal: Most recent value\n",
"- Confidence-based: Highest confidence value\n",
"- Custom functions: User-defined resolution logic\n",
"\n",
"### EntityMerger Class\n",
"\n",
"The `EntityMerger` class provides entity merging with automatic duplicate detection, provenance preservation, and merge history tracking.\n",
"\n",
"### MergeStrategyManager Class\n",
"\n",
"The `MergeStrategyManager` class provides advanced merge management with property-specific rules and custom conflict resolution functions.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Example: Entity Merging\n",
"\n",
"# Initialize EntityMerger\n",
"merger = EntityMerger(preserve_provenance=True)\n",
"\n",
"# Merge duplicates (automatic detection)\n",
"merge_operations = merger.merge_duplicates(entities)\n",
"print(f\"Original entities: {len(entities)}\")\n",
"print(f\"Merge operations: {len(merge_operations)}\")\n",
"for i, op in enumerate(merge_operations, 1):\n",
" print(f\" Operation {i}: Merged {len(op.source_entities)} entities → {op.merged_entity.get('name')}\")\n",
" if op.merge_result.conflicts:\n",
" print(f\" Conflicts: {len(op.merge_result.conflicts)}\")\n",
"\n",
"# Merge with specific strategy\n",
"operations = merger.merge_duplicates(entities, strategy=MergeStrategy.KEEP_MOST_COMPLETE)\n",
"print(f\"\\nMerged using KEEP_MOST_COMPLETE: {len(operations)} operations\")\n",
"\n",
"# Merge specific group\n",
"duplicate_entities = [entities[0], entities[1]]\n",
"operation = merger.merge_entity_group(duplicate_entities, strategy=MergeStrategy.KEEP_FIRST)\n",
"print(f\"\\nMerged group: {[e['name'] for e in operation.source_entities]} → {operation.merged_entity['name']}\")\n",
"\n",
"# Get merge history\n",
"history = merger.get_merge_history()\n",
"print(f\"\\nTotal merge operations in history: {len(history)}\")\n",
"\n",
"# Validate merge quality\n",
"if operations:\n",
" validation = merger.validate_merge_quality(operations[0])\n",
" print(f\"\\nValidation: Valid={validation['valid']}, Quality={validation['quality_score']:.3f}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Clustering {#clustering}\n",
"\n",
"The module provides clustering capabilities for efficient batch deduplication of large datasets. Clustering groups similar entities together before deduplication.\n",
"\n",
"### Clustering Methods\n",
"\n",
"1. **Graph-Based Clustering**: Union-Find algorithm for connected components\n",
" - Builds similarity graph from pairwise similarities\n",
" - Uses Union-Find (Disjoint Set Union) for efficient component detection\n",
" - Suitable for medium-sized datasets\n",
"\n",
"2. **Hierarchical Clustering**: Agglomerative bottom-up clustering\n",
" - Builds cluster hierarchy by merging similar clusters\n",
" - Suitable for large datasets\n",
" - Provides cluster quality metrics (cohesion, separation)\n",
"\n",
"### Cluster Quality Metrics\n",
"\n",
"- **Cohesion**: Average similarity within clusters\n",
"- **Separation**: Average similarity between clusters\n",
"- **Cluster Quality Score**: Combined metric for cluster evaluation\n",
"\n",
"### ClusterBuilder Class\n",
"\n",
"The `ClusterBuilder` class provides clustering with configurable thresholds, quality metrics, and incremental update capabilities.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Example: Advanced Merge Strategies\n",
"\n",
"# Initialize MergeStrategyManager\n",
"strategy_manager = MergeStrategyManager(default_strategy=\"keep_most_complete\")\n",
"\n",
"# Add property-specific rules\n",
"strategy_manager.add_property_rule(\"name\", MergeStrategy.KEEP_FIRST, priority=1)\n",
"strategy_manager.add_property_rule(\"description\", MergeStrategy.MERGE_ALL, priority=1)\n",
"\n",
"# Custom conflict resolution\n",
"def resolve_longest(values):\n",
" return max(values, key=len)\n",
"\n",
"strategy_manager.add_property_rule(\n",
" \"headquarters\", MergeStrategy.CUSTOM, conflict_resolution=resolve_longest, priority=2\n",
")\n",
"\n",
"print(\"Added merge rules: name=KEEP_FIRST, description=MERGE_ALL, headquarters=CUSTOM\")\n",
"\n",
"# Merge entities with property rules\n",
"duplicate_pair = [entities[0], entities[1]]\n",
"merge_result = strategy_manager.merge_entities(duplicate_pair)\n",
"print(f\"\\nMerged Entity: {merge_result.merged_entity.get('name')}\")\n",
"print(f\"Conflicts: {len(merge_result.conflicts)}\")\n",
"\n",
"# Validate merge quality\n",
"validation = strategy_manager.validate_merge(merge_result)\n",
"print(f\"Validation: Valid={validation['valid']}, Quality={validation['quality_score']:.3f}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Advanced Features {#advanced}\n",
"\n",
"The module provides advanced features for extensibility, configuration, and custom method registration.\n",
"\n",
"### Configuration Management\n",
"\n",
"The `DeduplicationConfig` class provides centralized configuration from multiple sources:\n",
"- Programmatic configuration (via `set()` method)\n",
"- Environment variables\n",
"- Configuration files (YAML, JSON, TOML)\n",
"- Default values\n",
"\n",
"Configuration can be set globally or per-method for fine-grained control.\n",
"\n",
"### Method Registry\n",
"\n",
"The `MethodRegistry` class allows registration of custom deduplication methods:\n",
"- Custom similarity calculation methods\n",
"- Custom duplicate detection methods\n",
"- Custom entity merging methods\n",
"- Custom clustering methods\n",
"\n",
"Registered methods can be used throughout the module via the registry.\n",
"\n",
"### Property-Specific Merge Rules\n",
"\n",
"The `MergeStrategyManager` supports property-specific merge rules:\n",
"- Different merge strategies for different properties\n",
"- Custom conflict resolution functions per property\n",
"- Priority-based rule application\n",
"- Flexible rule composition\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Example: Clustering\n",
"\n",
"# Initialize ClusterBuilder\n",
"cluster_builder = ClusterBuilder(\n",
" similarity_threshold=0.7,\n",
" min_cluster_size=2,\n",
" max_cluster_size=100,\n",
" use_hierarchical=False,\n",
")\n",
"\n",
"# Graph-based clustering\n",
"cluster_result = cluster_builder.build_clusters(entities)\n",
"print(f\"Clusters: {len(cluster_result.clusters)}, Unclustered: {len(cluster_result.unclustered)}\")\n",
"print(f\"Quality Metrics: {cluster_result.quality_metrics}\")\n",
"for cluster in cluster_result.clusters:\n",
" print(f\" {cluster.cluster_id}: {len(cluster.entities)} entities (quality: {cluster.quality_score:.3f})\")\n",
"\n",
"# Hierarchical clustering\n",
"hierarchical_builder = ClusterBuilder(similarity_threshold=0.7, use_hierarchical=True)\n",
"hierarchical_result = hierarchical_builder.build_clusters(entities)\n",
"print(f\"\\nHierarchical Clustering: {len(hierarchical_result.clusters)} clusters\")\n",
"print(f\"Quality Metrics: {hierarchical_result.quality_metrics}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Complete Workflow {#workflow}\n",
"\n",
"A typical deduplication workflow involves:\n",
"1. Configuration: Set similarity thresholds and method preferences\n",
"2. Clustering: Build clusters of similar entities (optional, for large datasets)\n",
"3. Detection: Identify duplicate entities within clusters or entire dataset\n",
"4. Merging: Merge duplicate entities using appropriate strategies\n",
"5. Validation: Validate merge quality and completeness\n",
"6. History: Track merge operations for audit and rollback\n",
"\n",
"The module provides both class-based and function-based interfaces for flexibility.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Example: Configuration and Method Registry\n",
"\n",
"# Configuration management\n",
"threshold = dedup_config.get(\"similarity_threshold\", default=0.7)\n",
"confidence = dedup_config.get(\"confidence_threshold\", default=0.6)\n",
"print(f\"Current: similarity_threshold={threshold}, confidence_threshold={confidence}\")\n",
"\n",
"# Set configuration programmatically\n",
"dedup_config.set(\"similarity_threshold\", 0.8)\n",
"dedup_config.set(\"confidence_threshold\", 0.7)\n",
"print(f\"Updated: similarity_threshold={dedup_config.get('similarity_threshold')}\")\n",
"\n",
"# Method-specific configuration\n",
"dedup_config.set_method_config(\"levenshtein\", case_sensitive=False)\n",
"levenshtein_config = dedup_config.get_method_config(\"levenshtein\")\n",
"print(f\"Method config (levenshtein): {levenshtein_config}\")\n",
"\n",
"# Custom method registration\n",
"def word_overlap_similarity(entity1, entity2, **kwargs):\n",
" \"\"\"Custom similarity based on word overlap.\"\"\"\n",
" name1 = entity1.get(\"name\", \"\").lower().split()\n",
" name2 = entity2.get(\"name\", \"\").lower().split()\n",
" \n",
" if not name1 or not name2:\n",
" return SimilarityResult(score=0.0, method=\"word_overlap\")\n",
" \n",
" set1, set2 = set(name1), set(name2)\n",
" intersection = len(set1 & set2)\n",
" union = len(set1 | set2)\n",
" score = intersection / union if union > 0 else 0.0\n",
" return SimilarityResult(score=score, method=\"word_overlap\")\n",
"\n",
"# Register custom method\n",
"method_registry.register(\"similarity\", \"word_overlap\", word_overlap_similarity)\n",
"print(\"\\nRegistered 'word_overlap' similarity method\")\n",
"\n",
"# Use custom method\n",
"custom_method = method_registry.get(\"similarity\", \"word_overlap\")\n",
"if custom_method:\n",
" result = custom_method(entities[0], entities[1])\n",
" print(f\"Word Overlap Similarity: {result.score:.3f}\")\n",
"\n",
"# List all registered methods\n",
"all_registered = method_registry.list_all()\n",
"print(f\"\\nRegistered Methods: {all_registered}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"---\n",
"\n",
"## Practical Examples\n",
"\n",
"The following sections demonstrate practical usage of the module components.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"### Example: Complete Workflow\n",
"\n",
"# Complete deduplication workflow\n",
"\n",
"# Step 1: Configure\n",
"dedup_config.set(\"similarity_threshold\", 0.75)\n",
"dedup_config.set(\"confidence_threshold\", 0.65)\n",
"\n",
"# Step 2: Build clusters\n",
"cluster_builder = ClusterBuilder(similarity_threshold=0.75, min_cluster_size=2, max_cluster_size=50)\n",
"cluster_result = cluster_builder.build_clusters(entities)\n",
"print(f\"Step 1: Created {len(cluster_result.clusters)} clusters\")\n",
"\n",
"# Step 3: Detect duplicates\n",
"detector = DuplicateDetector(similarity_threshold=0.75, confidence_threshold=0.65)\n",
"all_duplicate_groups = []\n",
"for cluster in cluster_result.clusters:\n",
" groups = detector.detect_duplicate_groups(cluster.entities)\n",
" all_duplicate_groups.extend(groups)\n",
"print(f\"Step 2: Found {len(all_duplicate_groups)} duplicate groups\")\n",
"\n",
"# Step 4: Merge duplicates\n",
"merger = EntityMerger(preserve_provenance=True)\n",
"merge_operations = merger.merge_duplicates(entities, strategy=MergeStrategy.KEEP_MOST_COMPLETE)\n",
"print(f\"Step 3: Performed {len(merge_operations)} merge operations\")\n",
"\n",
"# Step 5: Extract results\n",
"merged_entities = [op.merged_entity for op in merge_operations]\n",
"print(f\"\\nResults:\")\n",
"print(f\" Original: {len(entities)} entities\")\n",
"print(f\" Merged: {len(merged_entities)} entities\")\n",
"print(f\" Reduction: {len(entities) - len(merged_entities)} entities\")\n",
"\n",
"# Step 6: Validate merge quality\n",
"for i, op in enumerate(merge_operations, 1):\n",
" validation = merger.validate_merge_quality(op)\n",
" print(f\" Merge {i}: Valid={validation['valid']}, Quality={validation['quality_score']:.3f}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example: Similarity Calculation\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Complete deduplication workflow\n",
"\n",
"# Step 1: Configure\n",
"dedup_config.set(\"similarity_threshold\", 0.75)\n",
"dedup_config.set(\"confidence_threshold\", 0.65)\n",
"\n",
"# Step 2: Build clusters\n",
"cluster_builder = ClusterBuilder(similarity_threshold=0.75, min_cluster_size=2, max_cluster_size=50)\n",
"cluster_result = cluster_builder.build_clusters(entities)\n",
"print(f\"Step 1: Created {len(cluster_result.clusters)} clusters\")\n",
"\n",
"# Step 3: Detect duplicates\n",
"detector = DuplicateDetector(similarity_threshold=0.75, confidence_threshold=0.65)\n",
"all_duplicate_groups = []\n",
"for cluster in cluster_result.clusters:\n",
" groups = detector.detect_duplicate_groups(cluster.entities)\n",
" all_duplicate_groups.extend(groups)\n",
"print(f\"Step 2: Found {len(all_duplicate_groups)} duplicate groups\")\n",
"\n",
"# Step 4: Merge duplicates\n",
"merger = EntityMerger(preserve_provenance=True)\n",
"merge_operations = merger.merge_duplicates(entities, strategy=MergeStrategy.KEEP_MOST_COMPLETE)\n",
"print(f\"Step 3: Performed {len(merge_operations)} merge operations\")\n",
"\n",
"# Step 5: Extract results\n",
"merged_entities = [op.merged_entity for op in merge_operations]\n",
"print(f\"\\nResults:\")\n",
"print(f\" Original: {len(entities)} entities\")\n",
"print(f\" Merged: {len(merged_entities)} entities\")\n",
"print(f\" Reduction: {len(entities) - len(merged_entities)} entities\")\n",
"\n",
"# Step 6: Validate merge quality\n",
"for i, op in enumerate(merge_operations, 1):\n",
" validation = merger.validate_merge_quality(op)\n",
" print(f\" Merge {i}: Valid={validation['valid']}, Quality={validation['quality_score']:.3f}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Example: Duplicate Detection\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -0,0 +1,279 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/19_Context_Module.ipynb)\n",
"\n",
"# Context Engineering Module\n",
"\n",
"## Overview\n",
"\n",
"This notebook provides a comprehensive guide to Semantica's **Context Engineering Module** - a powerful system for building context graphs, managing agent memory, retrieving context, and linking entities. You'll learn how to use the new synchronous Architecture 2.0 features, including hierarchical memory with token management.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/context/)\n",
"\n",
"### Learning Objectives\n",
"\n",
"- **Hierarchical Memory**: Manage short-term (token-buffered) and long-term (vector-stored) memory\n",
"- **Context Graph**: Build and query dynamic knowledge graphs\n",
"- **Hybrid Retrieval**: Combine vector search, graph traversal, and keyword matching\n",
"- **Entity Linking**: Resolve entities across conversations\n",
"- **Configuration**: Customize behavior via YAML or environment variables\n",
"\n",
"---\n",
"\n",
"## Installation\n",
"\n",
"```bash\n",
"pip install semantica\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Setup: Create a mock vector store for demonstration\n",
"from typing import List, Dict, Any, Optional\n",
"from semantica.context import VectorStore\n",
"\n",
"class MockVectorStore(VectorStore):\n",
" def __init__(self):\n",
" self.items = {}\n",
" self.counter = 0\n",
" \n",
" def add(self, texts: List[str], metadata: Optional[List[Dict[str, Any]]] = None, **kwargs) -> List[str]:\n",
" ids = []\n",
" for i, text in enumerate(texts):\n",
" id_ = f\"id_{self.counter}\"\n",
" self.items[id_] = {\"text\": text, \"metadata\": metadata[i] if metadata else {}}\n",
" ids.append(id_)\n",
" self.counter += 1\n",
" return ids\n",
" \n",
" def search(self, query: str, limit: int = 5, **kwargs) -> List[Dict[str, Any]]:\n",
" # Simple keyword match for mock\n",
" results = []\n",
" for id_, item in self.items.items():\n",
" if any(w.lower() in item[\"text\"].lower() for w in query.split()):\n",
" results.append({\n",
" \"id\": id_,\n",
" \"content\": item[\"text\"],\n",
" \"score\": 0.9,\n",
" \"metadata\": item[\"metadata\"]\n",
" })\n",
" return results[:limit]\n",
" \n",
" def delete(self, ids: List[str], **kwargs) -> bool:\n",
" for id_ in ids:\n",
" self.items.pop(id_, None)\n",
" return True\n",
"\n",
"vs = MockVectorStore()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. High-Level Interface: AgentContext\n",
"\n",
"The `AgentContext` class is the easiest way to get started. It unifies vector storage, knowledge graphs, and memory management."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.context import AgentContext, ContextGraph\n",
"\n",
"# Initialize with vector store and a new in-memory knowledge graph\n",
"kg = ContextGraph()\n",
"context = AgentContext(\n",
" vector_store=vs,\n",
" knowledge_graph=kg,\n",
" token_limit=2000, # Max tokens in short-term memory\n",
" short_term_limit=10 # Max items in short-term memory\n",
")\n",
"\n",
"# Store a memory (automatically goes to short-term and long-term)\n",
"context.store(\n",
" \"The user, Alice, is a data scientist interested in Python.\",\n",
" conversation_id=\"conv_1\",\n",
" user_id=\"alice_01\"\n",
")\n",
"\n",
"# Retrieve context (automatically uses hybrid retrieval)\n",
"results = context.retrieve(\"What does Alice do?\")\n",
"\n",
"for res in results:\n",
" print(f\"Found: {res['content']} (Score: {res['score']})\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Hierarchical Memory Management\n",
"\n",
"Semantica uses a two-tier memory system:\n",
"1. **Short-Term Memory**: A fast, in-memory buffer limited by tokens (to fit in LLM context windows) and item count.\n",
"2. **Long-Term Memory**: Persistent storage backed by the vector store.\n",
"\n",
"Let's observe how the token limit works."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.context import AgentMemory\n",
"\n",
"# Initialize memory with strict limits for demonstration\n",
"memory = AgentMemory(\n",
" vector_store=vs,\n",
" token_limit=50, # Very small token limit\n",
" short_term_limit=5 # Max 5 items\n",
")\n",
"\n",
"# Add memories\n",
"for i in range(10):\n",
" memory.store(f\"Memory item {i}: This is a sentence with some tokens.\")\n",
" print(f\"Added item {i}. Short-term size: {len(memory.short_term_memory)}\")\n",
"\n",
"print(\"\\nFinal short-term memory content:\")\n",
"for item in memory.short_term_memory:\n",
" print(f\"- {item.content}\")\n",
" \n",
"# Notice that older items are pruned to respect the token limit and item count."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Context Graph & GraphRAG\n",
"\n",
"The `ContextGraph` allows you to structure information as nodes and edges, enabling \"GraphRAG\" - retrieving information based on relationships rather than just semantic similarity."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.context import ContextGraph\n",
"\n",
"graph = ContextGraph()\n",
"\n",
"# Manually building a graph\n",
"graph.add_node(\"n1\", \"person\", \"Alice\")\n",
"graph.add_node(\"n2\", \"language\", \"Python\")\n",
"graph.add_node(\"n3\", \"library\", \"Semantica\")\n",
"\n",
"graph.add_edge(\"n1\", \"n2\", \"uses\")\n",
"graph.add_edge(\"n2\", \"n3\", \"powers\")\n",
"\n",
"# Query the graph\n",
"neighbors = graph.get_neighbors(\"n2\", hops=1)\n",
"print(\"Neighbors of Python:\", neighbors)\n",
"\n",
"# Using the graph in AgentContext\n",
"context = AgentContext(vector_store=vs, knowledge_graph=graph)\n",
"\n",
"# Retrieve with graph expansion\n",
"results = context.retrieve(\n",
" \"Alice\",\n",
" use_graph=True,\n",
" expand_graph=True # Will pull in 'Python' because Alice uses it\n",
")\n",
"\n",
"print(\"\\nGraph-enhanced Retrieval:\")\n",
"for res in results:\n",
" print(f\"- {res['content']}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Entity Linking\n",
"\n",
"The `EntityLinker` helps ensure that \"Alice\", \"Alice Smith\", and \"she\" (in context) refer to the same entity ID."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.context import EntityLinker\n",
"\n",
"linker = EntityLinker()\n",
"\n",
"# Generate a canonical URI\n",
"uri = linker.generate_uri(\"Python Programming Language\")\n",
"print(f\"Canonical URI: {uri}\")\n",
"\n",
"# Check similarity\n",
"score = linker._calculate_text_similarity(\"Python\", \"Python Lang\")\n",
"print(f\"Similarity Score: {score}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. Configuration\n",
"\n",
"You can configure the context module using the `config` object or environment variables."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.context import config\n",
"\n",
"# Set global configuration\n",
"config.context_config.set(\"token_limit\", 4096)\n",
"config.context_config.set(\"retention_days\", 30)\n",
"\n",
"print(f\"Current Token Limit: {config.context_config.get('token_limit')}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.10"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
+774
View File
@@ -0,0 +1,774 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/Hawksight-AI/semantica/blob/main/cookbook/introduction/20_Triple_Store.ipynb)\n",
"\n",
"# Triple Store - Comprehensive Guide\n",
"\n",
"## Overview\n",
"\n",
"This notebook provides a **comprehensive walkthrough** of Semantica's triple_store module, demonstrating RDF triple storage, SPARQL querying, and multi-backend support for knowledge graph persistence.\n",
"\n",
"**Documentation**: [API Reference](https://semantica.readthedocs.io/reference/triple_store/)\n",
"\n",
"### Learning Objectives\n",
"\n",
"By the end of this notebook, you will be able to:\n",
"\n",
"- Register and manage triple stores (Blazegraph, Jena, RDF4J, Virtuoso)\n",
"- Perform CRUD operations on RDF triples\n",
"- Execute SPARQL queries with optimization\n",
"- Use bulk loading for large datasets\n",
"- Work with multiple store backends\n",
"- Validate and track triple operations\n",
"- Choose the right backend for your use case\n",
"\n",
"### What You'll Learn\n",
"\n",
"| Component | Purpose | When to Use |\n",
"|-----------|---------|-------------|\n",
"| `TripleManager` | Store coordination | All triple operations |\n",
"| `QueryEngine` | SPARQL execution | Query optimization |\n",
"| `BulkLoader` | High-volume loading | Large datasets |\n",
"| `BlazegraphAdapter` | Blazegraph backend | High performance |\n",
"| `JenaAdapter` | Jena backend | Java integration |\n",
"| `RDF4JAdapter` | RDF4J backend | Transaction support |\n",
"| `VirtuosoAdapter` | Virtuoso backend | Enterprise scale |\n",
"\n",
"---\n",
"\n",
"## Installation\n",
"\n",
"Install Semantica from PyPI:\n",
"\n",
"```bash\n",
"pip install semantica\n",
"# Or with all optional dependencies:\n",
"pip install semantica[all]\n",
"```\n",
"\n",
"---"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 1: Basic Triple Store Operations\n",
"\n",
"Let's start with the `TripleManager` for basic triple store operations.\n",
"\n",
"### What is TripleManager?\n",
"\n",
"`TripleManager` is the main coordinator for triple store operations:\n",
"- **Store Registration**: Register multiple backends\n",
"- **CRUD Operations**: Add, get, update, delete triples\n",
"- **Multi-Store**: Manage multiple stores simultaneously"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.triple_store import TripleManager\n",
"from semantica.semantic_extract.triple_extractor import Triple\n",
"\n",
"# Create triple manager\n",
"manager = TripleManager()\n",
"\n",
"# Register a Blazegraph store (in-memory for demo)\n",
"store = manager.register_store(\n",
" store_id=\"demo\",\n",
" store_type=\"blazegraph\",\n",
" endpoint=\"http://localhost:9999/blazegraph/sparql\"\n",
")\n",
"\n",
"print(f\"Registered store: {store.store_id}\")\n",
"print(f\"Store type: {store.store_type}\")\n",
"print(f\"Endpoint: {store.endpoint}\")\n",
"\n",
"# Create a triple\n",
"triple = Triple(\n",
" subject=\"http://example.org/Alice\",\n",
" predicate=\"http://example.org/knows\",\n",
" object=\"http://example.org/Bob\",\n",
" confidence=0.95\n",
")\n",
"\n",
"# Add triple to store\n",
"result = manager.add_triple(triple, store_id=\"demo\")\n",
"print(f\"\\nTriple added: {result['success']}\")\n",
"print(f\"Triple: {triple.subject} -> {triple.predicate} -> {triple.object}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Store Registration and Management\n",
"\n",
"Register multiple stores and manage them.\n",
"\n",
"### Supported Backends\n",
"\n",
"| Backend | Best For | Performance | Features |\n",
"|---------|----------|-------------|----------|\n",
"| **Blazegraph** | Large datasets | Excellent | GPU acceleration, full-text |\n",
"| **Jena** | Java apps | Good | SHACL, inference |\n",
"| **RDF4J** | Transactions | Good | ACID, federation |\n",
"| **Virtuoso** | Enterprise | Excellent | SQL integration, clustering |"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.triple_store import register_store\n",
"\n",
"# Register multiple stores using convenience function\n",
"blazegraph_store = register_store(\n",
" \"blazegraph_main\",\n",
" \"blazegraph\",\n",
" \"http://localhost:9999/blazegraph/sparql\"\n",
")\n",
"\n",
"jena_store = register_store(\n",
" \"jena_backup\",\n",
" \"jena\",\n",
" \"http://localhost:3030/ds\"\n",
")\n",
"\n",
"# List all registered stores\n",
"stores = manager.list_stores()\n",
"print(f\"Registered stores: {stores}\")\n",
"\n",
"# Get specific store\n",
"store = manager.get_store(\"blazegraph_main\")\n",
"print(f\"\\nStore details:\")\n",
"print(f\" ID: {store.store_id}\")\n",
"print(f\" Type: {store.store_type}\")\n",
"print(f\" Endpoint: {store.endpoint}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: CRUD Operations\n",
"\n",
"Perform Create, Read, Update, Delete operations on triples.\n",
"\n",
"### Operations Overview\n",
"\n",
"- **Create**: `add_triple()`, `add_triples()`\n",
"- **Read**: `get_triple()`\n",
"- **Update**: `update_triple()`\n",
"- **Delete**: `delete_triple()`"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.triple_store import add_triple, add_triples, get_triples, update_triple, delete_triple\n",
"\n",
"# Create - Add single triple\n",
"triple1 = Triple(\n",
" subject=\"http://example.org/Alice\",\n",
" predicate=\"http://example.org/hasAge\",\n",
" object=\"30\"\n",
")\n",
"result = add_triple(triple1, store_id=\"demo\")\n",
"print(f\"Added single triple: {result['success']}\")\n",
"\n",
"# Create - Add multiple triples\n",
"triples = [\n",
" Triple(\"http://example.org/Alice\", \"http://example.org/hasCity\", \"New York\"),\n",
" Triple(\"http://example.org/Bob\", \"http://example.org/hasAge\", \"25\"),\n",
" Triple(\"http://example.org/Bob\", \"http://example.org/hasCity\", \"Boston\")\n",
"]\n",
"result = add_triples(triples, store_id=\"demo\")\n",
"print(f\"\\nAdded {result['total_triples']} triples in {result['batches']} batches\")\n",
"\n",
"# Read - Get triples for a subject\n",
"alice_triples = get_triples(\n",
" subject=\"http://example.org/Alice\",\n",
" store_id=\"demo\"\n",
")\n",
"print(f\"\\nFound {len(alice_triples)} triples for Alice\")\n",
"\n",
"# Update - Change Alice's age\n",
"old_triple = Triple(\"http://example.org/Alice\", \"http://example.org/hasAge\", \"30\")\n",
"new_triple = Triple(\"http://example.org/Alice\", \"http://example.org/hasAge\", \"31\")\n",
"result = update_triple(old_triple, new_triple, store_id=\"demo\")\n",
"print(f\"\\nUpdated triple: {result['success']}\")\n",
"\n",
"# Delete - Remove a triple\n",
"triple_to_delete = Triple(\"http://example.org/Bob\", \"http://example.org/hasCity\", \"Boston\")\n",
"result = delete_triple(triple_to_delete, store_id=\"demo\")\n",
"print(f\"Deleted triple: {result['success']}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: SPARQL Query Execution\n",
"\n",
"Execute SPARQL queries with the QueryEngine.\n",
"\n",
"### Query Types\n",
"\n",
"- **SELECT**: Retrieve variable bindings\n",
"- **ASK**: Boolean queries\n",
"- **CONSTRUCT**: Build RDF graphs\n",
"- **DESCRIBE**: Describe resources"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.triple_store import QueryEngine, BlazegraphAdapter\n",
"\n",
"# Create query engine with caching\n",
"engine = QueryEngine(enable_caching=True, enable_optimization=True)\n",
"\n",
"# Create adapter\n",
"adapter = BlazegraphAdapter(endpoint=\"http://localhost:9999/blazegraph/sparql\")\n",
"\n",
"# SELECT query\n",
"select_query = \"\"\"\n",
"PREFIX ex: <http://example.org/>\n",
"\n",
"SELECT ?person ?age ?city\n",
"WHERE {\n",
" ?person ex:hasAge ?age .\n",
" ?person ex:hasCity ?city .\n",
"}\n",
"ORDER BY DESC(?age)\n",
"LIMIT 10\n",
"\"\"\"\n",
"\n",
"result = engine.execute_query(select_query, adapter)\n",
"\n",
"print(f\"Query Results:\")\n",
"print(f\" Variables: {result.variables}\")\n",
"print(f\" Results: {len(result.bindings)}\")\n",
"print(f\" Execution time: {result.execution_time:.2f}s\")\n",
"print(f\" Cached: {result.metadata.get('cached', False)}\")\n",
"\n",
"print(\"\\nResults:\")\n",
"for binding in result.bindings:\n",
" person = binding.get('person', {}).get('value', '')\n",
" age = binding.get('age', {}).get('value', '')\n",
" city = binding.get('city', {}).get('value', '')\n",
" print(f\" {person}: Age {age}, City {city}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Query Optimization\n",
"\n",
"Optimize SPARQL queries for better performance.\n",
"\n",
"### Optimization Features\n",
"\n",
"- **Query Planning**: Analyze execution steps\n",
"- **Cost Estimation**: Estimate query cost\n",
"- **Query Rewriting**: Optimize query structure\n",
"- **Caching**: Cache query results"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.triple_store import optimize_query, plan_query\n",
"\n",
"# Original query\n",
"query = \"\"\"\n",
"SELECT ?s ?p ?o\n",
"WHERE {\n",
" ?s ?p ?o .\n",
"}\n",
"\"\"\"\n",
"\n",
"# Optimize query (adds LIMIT if missing)\n",
"optimized = optimize_query(query, add_limit=True, default_limit=1000)\n",
"print(\"Optimized Query:\")\n",
"print(optimized)\n",
"\n",
"# Create query plan\n",
"plan = plan_query(query)\n",
"print(f\"\\nQuery Plan:\")\n",
"print(f\" Original length: {len(plan.query)}\")\n",
"print(f\" Optimized length: {len(plan.optimized_query)}\")\n",
"print(f\" Estimated cost: {plan.estimated_cost}\")\n",
"print(f\" Execution steps:\")\n",
"for i, step in enumerate(plan.execution_steps, 1):\n",
" print(f\" {i}. {step}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 6: Bulk Loading\n",
"\n",
"Load large datasets efficiently with progress tracking.\n",
"\n",
"### Bulk Loading Features\n",
"\n",
"- **Batch Processing**: Process in configurable batches\n",
"- **Progress Tracking**: Monitor loading progress\n",
"- **Retry Mechanism**: Handle failures gracefully\n",
"- **Validation**: Validate before loading"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.triple_store import BulkLoader, LoadProgress\n",
"\n",
"# Create bulk loader\n",
"loader = BulkLoader(\n",
" batch_size=1000,\n",
" max_retries=3\n",
")\n",
"\n",
"# Generate sample triples\n",
"large_dataset = [\n",
" Triple(\n",
" f\"http://example.org/entity{i}\",\n",
" \"http://example.org/hasName\",\n",
" f\"Entity {i}\"\n",
" )\n",
" for i in range(5000)\n",
"]\n",
"\n",
"# Progress callback\n",
"def progress_callback(progress: LoadProgress):\n",
" print(f\"Progress: {progress.progress_percentage:.1f}% \"\n",
" f\"({progress.loaded_triples}/{progress.total_triples}) \"\n",
" f\"Batch {progress.current_batch}/{progress.total_batches}\")\n",
"\n",
"# Load triples with progress tracking\n",
"adapter = BlazegraphAdapter(endpoint=\"http://localhost:9999/blazegraph/sparql\")\n",
"progress = loader.load_triples(\n",
" large_dataset,\n",
" adapter,\n",
" progress_callback=progress_callback\n",
")\n",
"\n",
"print(f\"\\nLoading Complete:\")\n",
"print(f\" Loaded: {progress.loaded_triples}/{progress.total_triples}\")\n",
"print(f\" Failed: {progress.failed_triples}\")\n",
"print(f\" Elapsed time: {progress.elapsed_time:.2f}s\")\n",
"print(f\" Throughput: {progress.metadata.get('throughput', 0):.0f} triples/sec\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 7: Store Adapters\n",
"\n",
"Work with different triple store backends.\n",
"\n",
"### Blazegraph Adapter\n",
"\n",
"High-performance triple store with GPU acceleration."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.triple_store import BlazegraphAdapter\n",
"\n",
"# Create Blazegraph adapter\n",
"blazegraph = BlazegraphAdapter(\n",
" endpoint=\"http://localhost:9999/blazegraph/sparql\",\n",
" namespace=\"kb\",\n",
" timeout=30\n",
")\n",
"\n",
"# Add triples\n",
"triples = [\n",
" Triple(\"http://example.org/Alice\", \"http://example.org/hasSkill\", \"Python\")\n",
"]\n",
"result = blazegraph.add_triples(triples)\n",
"print(f\"Blazegraph - Added: {result['success']}\")\n",
"\n",
"# Execute SPARQL query\n",
"query = \"SELECT ?s ?p ?o WHERE { ?s ?p ?o } LIMIT 5\"\n",
"result = blazegraph.execute_sparql(query)\n",
"print(f\"Blazegraph - Found {len(result['bindings'])} results\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Jena Adapter\n",
"\n",
"Full-featured RDF framework with inference support."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.triple_store import JenaAdapter\n",
"\n",
"# Create Jena adapter (in-memory)\n",
"jena = JenaAdapter()\n",
"\n",
"# Or connect to Fuseki endpoint\n",
"# jena = JenaAdapter(\n",
"# endpoint=\"http://localhost:3030/ds\",\n",
"# dataset=\"default\",\n",
"# enable_inference=True\n",
"# )\n",
"\n",
"# Add triples with inference\n",
"triples = [\n",
" Triple(\n",
" \"http://example.org/Dog\",\n",
" \"http://www.w3.org/2000/01/rdf-schema#subClassOf\",\n",
" \"http://example.org/Animal\"\n",
" ),\n",
" Triple(\n",
" \"http://example.org/Fido\",\n",
" \"http://www.w3.org/1999/02/22-rdf-syntax-ns#type\",\n",
" \"http://example.org/Dog\"\n",
" )\n",
"]\n",
"result = jena.add_triples(triples)\n",
"print(f\"Jena - Added: {result['success']}\")\n",
"\n",
"# Query with inference (Fido is inferred to be an Animal)\n",
"query = \"\"\"\n",
"PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n",
"PREFIX ex: <http://example.org/>\n",
"SELECT ?animal WHERE {\n",
" ?animal rdf:type ex:Animal .\n",
"}\n",
"\"\"\"\n",
"result = jena.query(query)\n",
"print(f\"Jena - Found {len(result)} animals (with inference)\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### RDF4J Adapter\n",
"\n",
"Java-based RDF framework with transaction support."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.triple_store import RDF4JAdapter\n",
"\n",
"# Create RDF4J adapter\n",
"rdf4j = RDF4JAdapter(\n",
" server_url=\"http://localhost:8080/rdf4j-server\",\n",
" repository_id=\"test\"\n",
")\n",
"\n",
"# Add triples with transaction\n",
"rdf4j.begin_transaction()\n",
"try:\n",
" triple = Triple(\n",
" \"http://example.org/Alice\",\n",
" \"http://example.org/hasEmail\",\n",
" \"alice@example.org\"\n",
" )\n",
" rdf4j.add_triple(\n",
" subject=triple.subject,\n",
" predicate=triple.predicate,\n",
" object_literal=triple.object\n",
" )\n",
" rdf4j.commit_transaction()\n",
" print(\"RDF4J - Transaction committed\")\n",
"except Exception as e:\n",
" rdf4j.rollback_transaction()\n",
" print(f\"RDF4J - Transaction rolled back: {e}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Virtuoso Adapter\n",
"\n",
"Enterprise-grade RDF store with SQL integration."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.triple_store import VirtuosoAdapter\n",
"\n",
"# Create Virtuoso adapter\n",
"virtuoso = VirtuosoAdapter(\n",
" host=\"localhost\",\n",
" port=1111,\n",
" user=\"dba\",\n",
" password=\"dba\"\n",
")\n",
"\n",
"# Create named graph\n",
"graph_uri = \"http://example.org/graph1\"\n",
"virtuoso.create_graph(graph_uri)\n",
"\n",
"# Add triples to named graph\n",
"triple = Triple(\n",
" \"http://example.org/Alice\",\n",
" \"http://example.org/worksAt\",\n",
" \"http://example.org/Company1\"\n",
")\n",
"virtuoso.add_triple(\n",
" subject=triple.subject,\n",
" predicate=triple.predicate,\n",
" object=triple.object,\n",
" graph=graph_uri\n",
")\n",
"\n",
"print(f\"Virtuoso - Added triple to graph: {graph_uri}\")\n",
"\n",
"# Query specific graph\n",
"query = f\"\"\"\n",
"PREFIX ex: <http://example.org/>\n",
"SELECT ?person ?company\n",
"FROM <{graph_uri}>\n",
"WHERE {{\n",
" ?person ex:worksAt ?company .\n",
"}}\n",
"\"\"\"\n",
"result = virtuoso.query(query)\n",
"print(f\"Virtuoso - Found {len(result)} results in graph\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 8: Triple Validation\n",
"\n",
"Validate triples before adding them to the store.\n",
"\n",
"### Validation Checks\n",
"\n",
"- **Required Fields**: Subject, predicate, object\n",
"- **Confidence Range**: 0-1 if provided\n",
"- **URI Format**: Valid URIs\n",
"- **Empty Components**: No empty values"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.triple_store import validate_triples\n",
"\n",
"# Create triples (some invalid)\n",
"triples_to_validate = [\n",
" Triple(\"http://example.org/Alice\", \"http://example.org/knows\", \"http://example.org/Bob\"), # Valid\n",
" Triple(\"\", \"http://example.org/knows\", \"http://example.org/Charlie\"), # Invalid (empty subject)\n",
" Triple(\"http://example.org/Dave\", \"\", \"http://example.org/Eve\"), # Invalid (empty predicate)\n",
" Triple(\"http://example.org/Frank\", \"http://example.org/knows\", \"http://example.org/Grace\", confidence=1.5), # Invalid (confidence > 1)\n",
"]\n",
"\n",
"# Validate triples\n",
"validation = validate_triples(triples_to_validate)\n",
"\n",
"print(\"Validation Results:\")\n",
"print(f\" Valid: {validation['valid']}\")\n",
"print(f\" Valid triples: {validation['valid_triples']}/{validation['total_triples']}\")\n",
"print(f\"\\nErrors: {len(validation['errors'])}\")\n",
"for error in validation['errors']:\n",
" print(f\" - {error}\")\n",
"print(f\"\\nWarnings: {len(validation['warnings'])}\")\n",
"for warning in validation['warnings']:\n",
" print(f\" - {warning}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 9: Multi-Store Operations\n",
"\n",
"Work with multiple stores simultaneously.\n",
"\n",
"### Use Cases\n",
"\n",
"- **Primary/Backup**: Replicate to backup store\n",
"- **Read/Write Split**: Separate read and write stores\n",
"- **Multi-Tenant**: Different stores for different tenants"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Register multiple stores\n",
"manager = TripleManager()\n",
"\n",
"primary = manager.register_store(\n",
" \"primary\",\n",
" \"blazegraph\",\n",
" \"http://localhost:9999/blazegraph/sparql\"\n",
")\n",
"\n",
"backup = manager.register_store(\n",
" \"backup\",\n",
" \"jena\",\n",
" \"http://localhost:3030/ds\"\n",
")\n",
"\n",
"# Add to primary store\n",
"triple = Triple(\n",
" \"http://example.org/Document1\",\n",
" \"http://example.org/hasAuthor\",\n",
" \"http://example.org/Alice\"\n",
")\n",
"manager.add_triple(triple, store_id=\"primary\")\n",
"print(\"Added to primary store\")\n",
"\n",
"# Replicate to backup store\n",
"manager.add_triple(triple, store_id=\"backup\")\n",
"print(\"Replicated to backup store\")\n",
"\n",
"# List all stores\n",
"stores = manager.list_stores()\n",
"print(f\"\\nActive stores: {stores}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 10: Best Practices\n",
"\n",
"### Choosing the Right Backend\n",
"\n",
"1. **Blazegraph**: High-performance, large datasets, GPU acceleration\n",
"2. **Jena**: Java integration, SHACL validation, inference\n",
"3. **RDF4J**: Transaction support, ACID guarantees, federation\n",
"4. **Virtuoso**: Enterprise scale, SQL integration, clustering\n",
"\n",
"### Performance Tips\n",
"\n",
"- **Batch Operations**: Use `add_triples()` for multiple triples\n",
"- **Query Optimization**: Enable optimization and caching\n",
"- **Bulk Loading**: Use `BulkLoader` for large datasets\n",
"- **Validation**: Validate before loading to avoid errors\n",
"\n",
"### Configuration\n",
"\n",
"- **Batch Size**: 1000-10000 for bulk loading\n",
"- **Cache Size**: 1000-5000 for query caching\n",
"- **Timeout**: 30-60 seconds for queries\n",
"- **Retries**: 3-5 for bulk operations"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"### What You've Learned\n",
"\n",
"In this notebook, you've learned how to:\n",
"\n",
"- Register and manage triple stores\n",
"- Perform CRUD operations on RDF triples\n",
"- Execute and optimize SPARQL queries\n",
"- Use bulk loading for large datasets\n",
"- Work with multiple store backends\n",
"- Validate triples before operations\n",
"- Choose the right backend for your use case\n",
"\n",
"### Key Takeaways\n",
"\n",
"1. **Multi-Backend Support**: Choose the right backend for your needs\n",
"2. **SPARQL Power**: Full SPARQL 1.1 support with optimization\n",
"3. **Bulk Loading**: Efficient loading with progress tracking\n",
"4. **Query Optimization**: Automatic query optimization and caching\n",
"5. **Validation**: Pre-load validation prevents errors\n",
"6. **Multi-Store**: Manage multiple stores simultaneously\n",
"\n",
"### Next Steps\n",
"\n",
"**Further Reading**:\n",
"- [Triple Store API Reference](https://semantica.readthedocs.io/reference/triple_store/)\n",
"- [SPARQL 1.1 Specification](https://www.w3.org/TR/sparql11-query/)\n",
"- [Knowledge Graph Building](../use_cases/advanced_rag/01_GraphRAG_Complete.ipynb)\n",
"\n",
"---\n",
"\n",
"**Questions or Issues?** Check out our [GitHub repository](https://github.com/Hawksight-AI/semantica) or [documentation](https://semantica.readthedocs.io)."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.0"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -1,168 +0,0 @@
{
"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
}
@@ -1,352 +0,0 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Configuration Basics\n",
"\n",
"## Overview\n",
"\n",
"This notebook teaches you how to configure Semantica using `ConfigManager`, environment variables, and configuration files. Proper configuration is essential for using Semantica effectively.\n",
"\n",
"### Learning Objectives\n",
"\n",
"- Understand how to use `ConfigManager` for configuration management\n",
"- Learn to set and use environment variables\n",
"- Create and load configuration files (YAML/JSON)\n",
"- Configure common settings for API keys, models, and processing\n",
"- Follow best practices for configuration management\n",
"\n",
"---\n",
"\n",
"## Configuration Methods\n",
"\n",
"Semantica supports three main configuration methods:\n",
"\n",
"1. **ConfigManager** - Programmatic configuration management\n",
"2. **Environment Variables** - For sensitive data like API keys\n",
"3. **Config Files** - YAML or JSON files for structured configuration\n",
"\n",
"Each method is demonstrated in the code cells below.\n",
"\n",
"---\n",
"\n",
"## Step 1: ConfigManager Basics\n",
"\n",
"`ConfigManager` is the primary way to manage configuration in Semantica. It provides a unified interface for loading and accessing configuration values.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.core import ConfigManager\n",
"\n",
"config_manager = ConfigManager()\n",
"\n",
"print(\"ConfigManager initialized successfully!\")\n",
"print(f\"ConfigManager instance: {config_manager}\")\n",
"\n",
"try:\n",
" print(\"\\nAccessing configuration values:\")\n",
" print(\" Use config_manager.get('path.to.config', default='default_value')\")\n",
" print(\" Example: config_manager.get('llm_provider.provider', default='openai')\")\n",
"except Exception as e:\n",
" print(f\"Error accessing config: {e}\")\n",
"\n",
"try:\n",
" config = config_manager.config\n",
" print(f\"\\n✓ Config object created: {config is not None}\")\n",
"except Exception as e:\n",
" print(f\"Error creating config object: {e}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 2: Environment Variables\n",
"\n",
"Environment variables are the recommended way to store sensitive information like API keys. They're secure and don't get committed to version control.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"\n",
"print(\"Environment Variables (SEMANTICA_*):\")\n",
"semantica_env_vars = {k: v for k, v in os.environ.items() if k.startswith('SEMANTICA_')}\n",
"if semantica_env_vars:\n",
" for key, value in semantica_env_vars.items():\n",
" masked_value = value[:4] + \"...\" if len(value) > 4 else \"***\"\n",
" print(f\" {key} = {masked_value}\")\n",
"else:\n",
" print(\" No SEMANTICA_* environment variables found\")\n",
" print(\" To set: os.environ['SEMANTICA_API_KEY'] = 'your_key'\")\n",
"\n",
"api_key = os.getenv(\"SEMANTICA_API_KEY\")\n",
"model_name = os.getenv(\"SEMANTICA_MODEL_NAME\", \"default-model\")\n",
"\n",
"print(f\"\\nRetrieved values:\")\n",
"print(f\" API Key set: {api_key is not None}\")\n",
"print(f\" Model name: {model_name}\")\n",
"\n",
"print(\"\\nNote: Environment variables with SEMANTICA_ prefix\")\n",
"print(\" are automatically loaded by ConfigManager\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 3: Configuration Files\n",
"\n",
"Configuration files (YAML or JSON) are great for storing non-sensitive settings like model names, batch sizes, and processing parameters. They provide a structured way to manage configuration.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import yaml\n",
"import json\n",
"from pathlib import Path\n",
"\n",
"sample_config_yaml = \"\"\"\n",
"# Semantica Configuration File\n",
"api_keys:\n",
" openai: your_openai_key_here\n",
" anthropic: your_anthropic_key_here\n",
"\n",
"llm_provider:\n",
" provider: openai\n",
" model: gpt-4\n",
" temperature: 0.7\n",
"\n",
"embedding:\n",
" provider: openai\n",
" model: text-embedding-3-large\n",
" dimensions: 3072\n",
"\n",
"knowledge_graph:\n",
" backend: networkx\n",
" temporal: true\n",
"\n",
"processing:\n",
" batch_size: 32\n",
" max_workers: 4\n",
"\n",
"logging:\n",
" level: INFO\n",
" file: semantica.log\n",
"\"\"\"\n",
"\n",
"config_yaml_path = Path(\"sample_config.yaml\")\n",
"config_yaml_path.write_text(sample_config_yaml)\n",
"\n",
"print(\"Sample config.yaml created:\")\n",
"print(f\" Path: {config_yaml_path}\")\n",
"print(\"\\nConfig file contents:\")\n",
"print(sample_config_yaml)\n",
"\n",
"try:\n",
" config_from_file = config_manager.load_from_file(str(config_yaml_path))\n",
" print(\"\\n✓ Configuration loaded from YAML file!\")\n",
" print(f\" Config object: {config_from_file is not None}\")\n",
"except Exception as e:\n",
" print(f\"\\n✗ Error loading config file: {e}\")\n",
"\n",
"sample_config_json = {\n",
" \"api_keys\": {\n",
" \"openai\": \"your_openai_key_here\",\n",
" \"anthropic\": \"your_anthropic_key_here\"\n",
" },\n",
" \"llm_provider\": {\n",
" \"provider\": \"openai\",\n",
" \"model\": \"gpt-4\",\n",
" \"temperature\": 0.7\n",
" },\n",
" \"embedding\": {\n",
" \"provider\": \"openai\",\n",
" \"model\": \"text-embedding-3-large\",\n",
" \"dimensions\": 3072\n",
" }\n",
"}\n",
"\n",
"config_json_path = Path(\"sample_config.json\")\n",
"with open(config_json_path, 'w') as f:\n",
" json.dump(sample_config_json, f, indent=2)\n",
"\n",
"print(f\"\\n✓ Sample config.json created: {config_json_path}\")\n",
"print(\"\\nNote: ConfigManager can load from both YAML and JSON files\")\n",
"print(\" config_manager.load_from_file('config.yaml')\")\n",
"print(\" config_manager.load_from_file('config.json')\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 4: Common Settings\n",
"\n",
"This section covers the most commonly used configuration settings, including API keys, model parameters, embedding settings, and processing options.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.core import Config\n",
"\n",
"print(\"Common Configuration Settings:\")\n",
"print(\"\\n1. API Keys:\")\n",
"print(\" - OpenAI API key\")\n",
"print(\" - Anthropic API key\")\n",
"print(\" - Cohere API key\")\n",
"print(\" - Other provider keys\")\n",
"\n",
"print(\"\\n2. Model Names and Parameters:\")\n",
"print(\" - LLM provider (openai, anthropic, etc.)\")\n",
"print(\" - Model name (gpt-4, claude-3, etc.)\")\n",
"print(\" - Temperature, max_tokens, etc.\")\n",
"\n",
"print(\"\\n3. Embedding Settings:\")\n",
"print(\" - Embedding provider\")\n",
"print(\" - Embedding model\")\n",
"print(\" - Embedding dimensions\")\n",
"\n",
"print(\"\\n4. Graph Database Connections:\")\n",
"print(\" - Backend (networkx, neo4j, arangodb)\")\n",
"print(\" - Connection strings\")\n",
"print(\" - Temporal graph settings\")\n",
"\n",
"print(\"\\n5. Logging Levels:\")\n",
"print(\" - DEBUG, INFO, WARNING, ERROR\")\n",
"print(\" - Log file paths\")\n",
"\n",
"print(\"\\n6. Cache Settings:\")\n",
"print(\" - Enable/disable caching\")\n",
"print(\" - Cache directory\")\n",
"\n",
"try:\n",
" custom_config_dict = {\n",
" \"llm_provider\": {\n",
" \"provider\": \"openai\",\n",
" \"model\": \"gpt-4\",\n",
" \"temperature\": 0.7\n",
" },\n",
" \"embedding\": {\n",
" \"provider\": \"openai\",\n",
" \"model\": \"text-embedding-3-large\",\n",
" \"dimensions\": 3072\n",
" },\n",
" \"processing\": {\n",
" \"batch_size\": 32,\n",
" \"max_workers\": 4\n",
" }\n",
" }\n",
" \n",
" custom_config = Config(config_dict=custom_config_dict)\n",
" print(\"\\n✓ Custom Config object created with settings:\")\n",
" print(f\" LLM Provider: {custom_config.llm_provider.get('provider', 'N/A')}\")\n",
" print(f\" Embedding Provider: {custom_config.embedding_model.get('provider', 'N/A')}\")\n",
" print(f\" Batch Size: {custom_config.processing.get('batch_size', 'N/A')}\")\n",
" \n",
"except Exception as e:\n",
" print(f\"\\n✗ Error creating custom config: {e}\")\n",
"\n",
"try:\n",
" if config_yaml_path.exists():\n",
" config_yaml_path.unlink()\n",
" if config_json_path.exists():\n",
" config_json_path.unlink()\n",
" print(\"\\n✓ Sample config files cleaned up\")\n",
"except:\n",
" pass\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: Best Practices\n",
"\n",
"Follow these best practices to ensure secure, maintainable, and effective configuration management.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Configuration Best Practices:\")\n",
"print(\"\\n1. Use Environment Variables for Sensitive Data:\")\n",
"print(\" - Never commit API keys to version control\")\n",
"print(\" - Use environment variables or secret management\")\n",
"print(\" - Example: export SEMANTICA_API_KEY=your_key\")\n",
"\n",
"print(\"\\n2. Use Config Files for Non-Sensitive Settings:\")\n",
"print(\" - Store model names, batch sizes, etc. in config files\")\n",
"print(\" - Use YAML for readability or JSON for compatibility\")\n",
"print(\" - Keep config files in version control (without secrets)\")\n",
"\n",
"print(\"\\n3. Configuration Hierarchy:\")\n",
"print(\" - Environment variables override config file values\")\n",
"print(\" - Config file values override defaults\")\n",
"print(\" - Use defaults as fallback\")\n",
"\n",
"print(\"\\n4. Validate Configuration:\")\n",
"print(\" - Check required settings are present\")\n",
"print(\" - Validate API keys are set before use\")\n",
"print(\" - Use ConfigManager validation features\")\n",
"\n",
"print(\"\\n5. Separate Configurations by Environment:\")\n",
"print(\" - Development: dev_config.yaml\")\n",
"print(\" - Production: prod_config.yaml\")\n",
"print(\" - Testing: test_config.yaml\")\n",
"\n",
"print(\"\\n6. Document Configuration Options:\")\n",
"print(\" - Document all available settings\")\n",
"print(\" - Provide examples and defaults\")\n",
"print(\" - Explain the impact of each setting\")\n",
"\n",
"print(\"\\n\" + \"=\"*60)\n",
"print(\"Example: Checking if required configuration is set\")\n",
"print(\"=\"*60)\n",
"\n",
"required_settings = [\n",
" (\"API Key\", os.getenv(\"SEMANTICA_API_KEY\")),\n",
" (\"Model Name\", os.getenv(\"SEMANTICA_MODEL_NAME\", \"default\")),\n",
"]\n",
"\n",
"print(\"\\nRequired Settings Status:\")\n",
"for setting_name, value in required_settings:\n",
" status = \"✓ Set\" if value and value != \"default\" else \"✗ Not Set\"\n",
" print(f\" {setting_name}: {status}\")\n",
"\n",
"print(\"\\nRecommendation:\")\n",
"print(\" Set up your configuration before running Semantica workflows\")\n",
"print(\" Use ConfigManager to load and validate your settings\")\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
@@ -1,125 +0,0 @@
{
"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
}
-208
View File
@@ -1,208 +0,0 @@
{
"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
}
@@ -1,228 +0,0 @@
{
"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
}
-123
View File
@@ -1,123 +0,0 @@
{
"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
}
@@ -1,247 +0,0 @@
{
"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 = \"\"\"<?xml version=\"1.0\"?>\n",
"<companies>\n",
" <company name=\"Apple Inc.\" ceo=\"Tim Cook\"/>\n",
" <company name=\"Microsoft Corporation\" ceo=\"Satya Nadella\"/>\n",
"</companies>\"\"\"\n",
"\n",
"with open(xml_file, 'w') as f:\n",
" f.write(xml_content)\n",
"\n",
"xml_data = xml_parser.parse(xml_file)\n",
"\n",
"print(f\"Parsed XML with {len(xml_data.elements)} elements\")\n",
"print(f\"Root element: {xml_data.root.tag if xml_data.root else 'None'}\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Step 5: HTML Parser\n",
"\n",
"Parse HTML files to extract content and structure.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from semantica.parse import HTMLParser\n",
"\n",
"html_parser = HTMLParser()\n",
"html_file = os.path.join(temp_dir, \"page.html\")\n",
"\n",
"html_content = \"\"\"<html>\n",
"<head><title>Sample Page</title></head>\n",
"<body>\n",
" <h1>Technology Companies</h1>\n",
" <p>Apple Inc. is a technology company.</p>\n",
"</body>\n",
"</html>\"\"\"\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
}
@@ -1,100 +0,0 @@
{
"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
}
@@ -1,106 +0,0 @@
{
"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
}
-177
View File
@@ -1,177 +0,0 @@
{
"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
}
-162
View File
@@ -1,162 +0,0 @@
{
"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
}
-156
View File
@@ -1,156 +0,0 @@
{
"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
}
-155
View File
@@ -1,155 +0,0 @@
{
"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
}
@@ -1,105 +0,0 @@
{
"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
}

Some files were not shown because too many files have changed in this diff Show More