chunking-strategy


Namechunking-strategy JSON
Version 0.4.1 PyPI version JSON
download
home_pageNone
SummaryA comprehensive chunking library for text, documents, audio, video, and data streams (Linux and macOS only)
upload_time2025-10-10 11:26:49
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT
keywords chunking text-processing document-processing audio-processing video-processing data-streams content-defined-chunking semantic-chunking rag
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Chunking Strategy Library

[![PyPI version](https://img.shields.io/pypi/v/chunking-strategy.svg)](https://pypi.org/project/chunking-strategy/)
[![Python versions](https://img.shields.io/pypi/pyversions/chunking-strategy.svg)](https://pypi.org/project/chunking-strategy/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Downloads](https://img.shields.io/pypi/dm/chunking-strategy.svg)](https://pypi.org/project/chunking-strategy/)
[![codecov](https://codecov.io/gh/sharanharsoor/chunking/branch/main/graph/badge.svg)](https://codecov.io/gh/sharanharsoor/chunking)

**A comprehensive Python library for intelligent document chunking with extensive format support and streaming capabilities.**

Transform your documents into perfectly sized chunks for RAG systems, vector databases, LLM processing, and content analysis with multi-core processing and memory-efficient streaming for large files.

> **Platform Support**: Linux and macOS only. Windows support is not currently available.

---

## ๐Ÿš€ **Quick Start**

### Installation

```bash
# Basic installation
pip install chunking-strategy

# With all features (recommended)
pip install chunking-strategy[all]

# Specific feature sets
pip install chunking-strategy[document,hardware,tika]
```

### 30-Second Example

```python
from chunking_strategy import ChunkerOrchestrator

# Auto-select best strategy for any file
orchestrator = ChunkerOrchestrator()

# Works with ANY file type - documents, code, multimedia!
result = orchestrator.chunk_file("document.pdf")      # PDF processing
result = orchestrator.chunk_file("podcast.mp3")       # Audio with silence detection
result = orchestrator.chunk_file("video.mp4")         # Video scene analysis
result = orchestrator.chunk_file("image.jpg")         # Image tiling

# Use your perfectly chunked content
for chunk in result.chunks:
    print(f"Chunk: {chunk.content}")
    print(f"Strategy used: {result.strategy_used}")
```

### Quick CLI Examples

```bash
# Simple text file chunking
python -m chunking_strategy chunk my_document.txt --strategy sentence_based

# PDF with specific output format
python -m chunking_strategy chunk report.pdf --strategy pdf_chunker --format json

# See all available strategies
python -m chunking_strategy list-strategies

# Process multiple files at once
python -m chunking_strategy batch *.txt --strategy paragraph_based --workers 4

# Get help for any command
python -m chunking_strategy chunk --help
```

### Choose Your Approach

**๐Ÿค– Intelligent Auto Selection (Recommended)**
```python
# Let the system choose the best strategy
config = {"strategies": {"primary": "auto"}}
orchestrator = ChunkerOrchestrator(config=config)
result = orchestrator.chunk_file("any_file.ext")  # Automatic optimization!
```

**๐ŸŽฏ Universal Strategies (Any file type)**
```python
# Apply ANY strategy to ANY file type
from chunking_strategy import apply_universal_strategy

result = apply_universal_strategy("paragraph", "script.py")     # Paragraph chunking on code
result = apply_universal_strategy("sentence", "document.pdf")   # Sentence chunking on PDF
result = apply_universal_strategy("rolling_hash", "data.json") # Rolling hash on JSON
```

**๐Ÿ”ง Specialized Chunkers (Maximum precision)**
```python
# Deep understanding of specific formats
from chunking_strategy import create_chunker

python_chunker = create_chunker("python_code")    # AST-aware Python chunking
pdf_chunker = create_chunker("pdf_chunker")       # PDF structure + images + tables
cpp_chunker = create_chunker("c_cpp_code")        # C/C++ syntax understanding
```

---

## ๐ŸŽฏ **Why Choose This Library?**

### โœจ **Key Features**
- **Multi-Format Support**: PDF, DOC, DOCX, code files, **audio/video/images**, and more. Universal processing via Apache Tika integration
- **Multimedia Intelligence**: Smart audio chunking with silence detection, video scene analysis, image tiling for ML workflows
- **Performance Optimization**: Multi-core batch processing and memory-efficient streaming for large files
- **Batch Processing**: Process thousands of files efficiently with multiprocessing
- **Robust Processing**: Comprehensive error handling, logging, and quality metrics

### ๐Ÿง  **Intelligent Chunking Strategies**
- **Text-Based**: Sentence, paragraph, semantic, and topic-based chunking
- **Document-Aware**: PDF with image/table extraction, structured document processing
- **Multimedia-Smart**: Audio silence detection, video scene analysis, image tiling and patches
- **Universal**: Apache Tika integration for any file format (1,400+ formats)
- **Custom**: Easy to create domain-specific chunking strategies

### โšก **Performance & Scalability**
- **True Streaming Processing**: Handle multi-gigabyte files with constant memory usage through memory-mapped streaming
- **Parallel Processing**: Multi-core batch processing for multiple files
- **40+ Chunking Strategies**: Comprehensive variety of text, code, document, and multimedia chunkers
- **Quality Metrics**: Built-in evaluation and optimization

### ๐Ÿ”ฅ **Key Differentiators**
- **Memory-Mapped Streaming**: Process massive documents (1GB+) that would crash other libraries
- **Format Variety**: 40+ specialized chunkers vs. 5-8 in most libraries
- **True Universal Framework**: Apply any strategy to any file type
- **Token-Precise Control**: Advanced tokenizer integration (tiktoken, transformers, etc.) for LLM applications
- **Comprehensive Testing**: Extensively tested with real-world files and edge cases

---

## ๐ŸŽญ **Three Powerful Approaches - Choose What Fits Your Needs**

Our library offers three distinct approaches to handle different use cases. Understanding when to use each approach will help you get the best results.

### ๐Ÿค– **Auto Selection**: Intelligent Strategy Selection

**Best for**: Quick start, prototyping, general-purpose applications

The system automatically chooses the optimal strategy based on file extension and content characteristics. Zero configuration required!

```python
from chunking_strategy import ChunkerOrchestrator

# Zero configuration - just works!
orchestrator = ChunkerOrchestrator(config={"strategies": {"primary": "auto"}})

# System intelligently selects:
result = orchestrator.chunk_file("script.py")      # โ†’ paragraph (preserves code structure)
result = orchestrator.chunk_file("document.txt")   # โ†’ sentence (readable chunks)
result = orchestrator.chunk_file("data.json")      # โ†’ fixed_size (consistent processing)
result = orchestrator.chunk_file("large_file.pdf") # โ†’ rolling_hash (efficient for large files)
```

**Auto Selection Rules:**
- **Code files** (`.py`, `.js`, `.cpp`, `.java`): `paragraph` - preserves logical structure
- **Text files** (`.txt`, `.md`, `.rst`): `sentence` - optimizes readability
- **Documents** (`.pdf`, `.doc`, `.docx`): `paragraph` - maintains document structure
- **Data files** (`.json`, `.xml`, `.csv`): `fixed_size` - consistent processing
- **Large files** (>10MB): `rolling_hash` - memory efficient
- **Small files** (<1KB): `sentence` - optimal for small content

### ๐ŸŒ **Universal Strategies**: Any Strategy + Any File Type

**Best for**: Consistency across formats, custom workflows, RAG applications

Apply ANY chunking strategy to ANY file type through our universal framework. Perfect when you need the same chunking approach across different file formats.

```python
from chunking_strategy import apply_universal_strategy

# Same strategy works across ALL file types!
result = apply_universal_strategy("sentence", "document.pdf")    # Sentence chunking on PDF
result = apply_universal_strategy("paragraph", "script.py")      # Paragraph chunking on Python
result = apply_universal_strategy("rolling_hash", "data.xlsx")   # Rolling hash on Excel
result = apply_universal_strategy("overlapping_window", "video.mp4")  # Overlapping windows on video

# Perfect for RAG systems requiring consistent chunk sizes
for file_path in document_collection:
    result = apply_universal_strategy("fixed_size", file_path, chunk_size=1000)
    # All files get exactly 1000-character chunks regardless of format!
```

**Universal Strategies Available:**
- `fixed_size` - Consistent chunk sizes with overlap support
- `sentence` - Sentence-boundary aware chunking
- `paragraph` - Paragraph-based logical grouping
- `overlapping_window` - Sliding window with customizable overlap
- `rolling_hash` - Content-defined boundaries using hash functions

### ๐Ÿ”ง **Specialized Chunkers**: Maximum Precision & Rich Metadata

**Best for**: Advanced applications, code analysis, document intelligence, detailed metadata requirements

Deep understanding of specific file formats with semantic boundaries and comprehensive metadata extraction.

```python
from chunking_strategy import create_chunker

# Python AST-aware chunking
python_chunker = create_chunker("python_code")
result = python_chunker.chunk("complex_script.py")
for chunk in result.chunks:
    meta = chunk.metadata.extra
    print(f"Element: {meta['element_name']} ({meta['element_type']})")
    print(f"Has docstring: {bool(meta.get('docstring'))}")
    print(f"Arguments: {meta.get('args', [])}")

# PDF with image and table extraction
pdf_chunker = create_chunker("pdf_chunker")
result = pdf_chunker.chunk("report.pdf", extract_images=True, extract_tables=True)
for chunk in result.chunks:
    if chunk.modality == ModalityType.IMAGE:
        print(f"Found image on page {chunk.metadata.page}")
    elif "table" in chunk.metadata.extra.get("chunk_type", ""):
        print(f"Found table: {chunk.content[:100]}...")

# C/C++ syntax-aware chunking
cpp_chunker = create_chunker("c_cpp_code")
result = cpp_chunker.chunk("algorithm.cpp")
for chunk in result.chunks:
    if chunk.metadata.extra.get("element_type") == "function":
        print(f"Function: {chunk.metadata.extra['element_name']}")
```

**Specialized Chunkers Available:**
- `python_code` - AST parsing, function/class boundaries, docstring extraction
- `c_cpp_code` - C/C++ syntax understanding, preprocessor directives
- `universal_code` - Multi-language code chunking (JavaScript, Go, Rust, etc.)
- `pdf_chunker` - PDF structure, images, tables, metadata
- `universal_document` - Apache Tika integration for comprehensive format support *(coming soon)*

### ๐Ÿ“Š **Comparison: When to Use Each Approach**

| Use Case | Auto Selection | Universal Strategies | Specialized Chunkers |
|----------|---------------|---------------------|-------------------|
| **Quick prototyping** | โญโญโญโญโญ Perfect | โญโญโญ Good | โญโญ Overkill |
| **RAG systems** | โญโญโญโญ Great | โญโญโญโญโญ Perfect | โญโญโญ Good |
| **Code analysis** | โญโญโญ Good | โญโญ Basic | โญโญโญโญโญ Perfect |
| **Document intelligence** | โญโญโญ Good | โญโญ Basic | โญโญโญโญโญ Perfect |
| **Cross-format consistency** | โญโญโญ Good | โญโญโญโญโญ Perfect | โญโญ Limited |
| **Advanced applications** | โญโญโญโญ Great | โญโญโญโญ Great | โญโญโญโญโญ Perfect |

### ๐Ÿ”ฎ **Future File Format Support**

We're actively expanding format support. **Coming soon**:

| Category | Formats | Strategy Recommendations |
|----------|---------|------------------------|
| **Spreadsheets** | `.xls`, `.xlsx`, `.ods`, `.csv` | `fixed_size` or specialized `excel_chunker` |
| **Presentations** | `.ppt`, `.pptx`, `.odp` | `paragraph` or specialized `presentation_chunker` |
| **Data Formats** | `.parquet`, `.avro`, `.orc` | `fixed_size` or specialized `data_chunker` |
| **Media Files** | `.mp4`, `.avi`, `.mp3`, `.wav` | `overlapping_window` or specialized `media_chunker` |
| **Archives** | `.zip`, `.tar`, `.7z` | Content-aware or specialized `archive_chunker` |
| **CAD/Design** | `.dwg`, `.dxf`, `.svg` | Specialized `design_chunker` |

**Request format support**: [Open an issue](https://github.com/sharanharsoor/chunking/issues) for priority formats!

---

## ๐Ÿ“– **Usage Examples**

### Basic Text Chunking

```python
from chunking_strategy import create_chunker

# Sentence-based chunking (best for semantic coherence)
chunker = create_chunker("sentence_based", max_sentences=3)
result = chunker.chunk("Your text here...")

# Fixed-size chunking (consistent chunk sizes)
chunker = create_chunker("fixed_size", chunk_size=1000, overlap_size=100)
result = chunker.chunk("Your text here...")

# Paragraph-based chunking (natural boundaries)
chunker = create_chunker("paragraph_based", max_paragraphs=2)
result = chunker.chunk("Your text here...")
```

### PDF Processing with Images & Tables

```python
from chunking_strategy import create_chunker

# Advanced PDF processing
chunker = create_chunker(
    "pdf_chunker",
    pages_per_chunk=1,
    extract_images=True,
    extract_tables=True,
    backend="pymupdf"  # or "pypdf2", "pdfminer"
)

result = chunker.chunk("document.pdf")

# Access different content types
for chunk in result.chunks:
    chunk_type = chunk.metadata.extra.get('chunk_type')
    if chunk_type == 'text':
        print(f"Text: {chunk.content}")
    elif chunk_type == 'image':
        print(f"Image on page {chunk.metadata.page}")
    elif chunk_type == 'table':
        print(f"Table: {chunk.content}")
```

### Universal Document Processing

```python
from chunking_strategy import create_chunker

# Process ANY file format with Apache Tika
chunker = create_chunker(
    "universal_document",
    chunk_size=1000,
    preserve_structure=True,
    extract_metadata=True
)

# Works with PDF, DOC, DOCX, Excel, PowerPoint, code files, etc.
result = chunker.chunk("any_document.docx")

print(f"File type: {result.source_info['file_type']}")
print(f"Extracted metadata: {result.source_info['tika_metadata']}")
```

### Batch Processing with Hardware Optimization

```python
from chunking_strategy.core.batch import BatchProcessor

# Automatic hardware optimization
processor = BatchProcessor()

result = processor.process_files(
    files=["doc1.pdf", "doc2.txt", "doc3.docx"],
    default_strategy="sentence_based",
    parallel_mode="process",  # Uses multiple CPU cores
    workers=None  # Auto-detected optimal worker count
)

print(f"Processed {result.total_files} files")
print(f"Created {result.total_chunks} chunks")
print(f"Performance: {result.files_per_second:.1f} files/second")
```

---

## ๐Ÿ–ฅ๏ธ **Command Line Interface**

### Quick Commands

```bash
# List available strategies
chunking-strategy list-strategies

# Check your hardware capabilities
chunking-strategy hardware --recommendations

# Chunk a single file
chunking-strategy chunk document.pdf --strategy pdf_chunker --format json

# Batch process multiple files
chunking-strategy batch *.txt --strategy sentence_based --workers 4

# Use configuration file
chunking-strategy chunk document.pdf --config my_config.yaml
```

### Advanced CLI Usage

```bash
# Hardware-optimized batch processing
chunking-strategy batch documents/*.pdf \
    --strategy universal_document \
    --workers 8 \
    --mode process \
    --output-dir results \
    --format json

# PDF processing with specific backend
chunking-strategy chunk document.pdf \
    --strategy pdf_chunker \
    --backend pymupdf \
    --extract-images \
    --extract-tables \
    --pages-per-chunk 1

# Process entire directory with custom strategies per file type
chunking-strategy batch-smart ./documents/ \
    --pdf-strategy "enhanced_pdf_chunker" \
    --text-strategy "semantic" \
    --code-strategy "python_code" \
    --output-format json \
    --generate-embeddings \
    --embedding-model all-MiniLM-L6-v2

# Real-time processing with monitoring
chunking-strategy process-watch ./incoming/ \
    --auto-strategy \
    --streaming \
    --max-memory 4GB \
    --webhook http://localhost:8080/chunked \
    --metrics-dashboard
```

---

## โš™๏ธ **Configuration-Driven Processing**

### YAML Configuration

Create a `config.yaml` file:

```yaml
profile_name: "rag_optimized"

strategies:
  primary: "sentence_based"
  fallbacks: ["paragraph_based", "fixed_size"]
  configs:
    sentence_based:
      max_sentences: 3
      overlap_sentences: 1
    paragraph_based:
      max_paragraphs: 2
    fixed_size:
      chunk_size: 1000

preprocessing:
  enabled: true
  normalize_whitespace: true

postprocessing:
  enabled: true
  merge_short_chunks: true
  min_chunk_size: 100

quality_evaluation:
  enabled: true
  threshold: 0.7
```

Use with Python:

```python
from chunking_strategy import ChunkerOrchestrator

orchestrator = ChunkerOrchestrator(config_path="config.yaml")
result = orchestrator.chunk_file("document.pdf")
```

Use with CLI:

```bash
chunking-strategy chunk document.pdf --config config.yaml
```

---

## ๐ŸŽญ **Complete Chunking Algorithms Reference (40+ Total)**

### ๐Ÿ“ **Text-Based Strategies** (9 strategies)
- `sentence_based` - Semantic coherence with sentence boundaries (RAG, Q&A)
- `paragraph_based` - Natural paragraph structure (Document analysis, summarization)
- `token_based` - Precise token-level chunking with multiple tokenizer support (LLM optimization)
- `semantic` - AI-powered semantic similarity with embeddings (High-quality understanding)
- `boundary_aware` - Intelligent boundary detection (Clean, readable chunks)
- `recursive` - Hierarchical multi-level chunking (Complex document structure)
- `overlapping_window` - Sliding window with customizable overlap (Context preservation)
- `fixed_length_word` - Fixed word count per chunk (Consistent word-based processing)
- `embedding_based` - Embedding similarity for boundaries (Advanced semantic understanding)

### ๐Ÿ’ป **Code-Aware Strategies** (7 strategies)
- `python_code` - AST-aware Python parsing with function/class boundaries (Python analysis)
- `c_cpp_code` - C/C++ syntax understanding with preprocessor handling (Systems programming)
- `javascript_code` - JavaScript/TypeScript AST parsing (Web development, Node.js)
- `java_code` - Java syntax parsing with package structure (Enterprise Java codebases)
- `go_code` - Go language structure awareness (Go codebase analysis)
- `css_code` - CSS rule and selector-aware chunking (Web styling analysis)
- `universal_code` - Multi-language code chunking (Cross-language processing)

### ๐Ÿ“„ **Document-Aware Strategies** (5 strategies)
- `pdf_chunker` - Advanced PDF processing with images, tables, layout (PDF intelligence)
- `enhanced_pdf_chunker` - Premium PDF with OCR, structure analysis (Complex PDF workflows)
- `doc_chunker` - Microsoft Word document processing (Corporate documents)
- `markdown_chunker` - Markdown structure-aware (headers, lists, code blocks)
- `xml_html_chunker` - XML/HTML tag-aware with structure preservation (Web content)

### ๐Ÿ“Š **Data Format Strategies** (2 strategies)
- `csv_chunker` - CSV row and column-aware processing (Tabular data analysis)
- `json_chunker` - JSON structure-preserving chunking (API data, configuration files)

### ๐ŸŽต **Multimedia Strategies** (6 strategies)
- `time_based_audio` - Audio chunking by time intervals (Podcast transcription)
- `silence_based_audio` - Audio chunking at silence boundaries (Speech processing)
- `time_based_video` - Video chunking by time segments (Video content analysis)
- `scene_based_video` - Scene change detection for intelligent cuts (Video processing)
- `grid_based_image` - Spatial grid-based image tiling (Computer vision)
- `patch_based_image` - Overlapping patch extraction (Machine learning, patterns)

### ๐Ÿ”ง **Content-Defined Chunking (CDC)** (7 strategies)
- `fastcdc` - Fast content-defined chunking with rolling hash (Deduplication, backup)
- `rabin_fingerprinting` - Rabin polynomial rolling hash boundaries (Content-addressable storage)
- `rolling_hash` - Generic rolling hash with configurable parameters (Variable-size chunking)
- `buzhash` - BuzHash algorithm for content boundaries (Efficient content splitting)
- `gear_cdc` - Gear-based content-defined chunking (High-performance CDC)
- `ml_cdc` - Machine learning-enhanced boundary detection (Intelligent boundaries)
- `tttd` - Two Threshold Two Divisor algorithm (Advanced CDC with dual thresholds)

### ๐Ÿง  **Advanced & Adaptive Strategies** (4 strategies)
- `adaptive` - Self-learning chunker that adapts based on feedback (Dynamic optimization)
- `context_enriched` - Context-aware chunking with NLP enhancement (Advanced text understanding)
- `discourse_aware` - Discourse structure and topic transition detection (Academic papers)
- `fixed_size` - Simple fixed-size chunking with overlap support (Baseline, simple needs)

### Strategy Selection Guide

```python
# For RAG systems and LLM processing
chunker = create_chunker("sentence_based", max_sentences=3)

# For vector databases with token limits
chunker = create_chunker("fixed_size", chunk_size=512)

# For document analysis and summarization
chunker = create_chunker("paragraph_based", max_paragraphs=2)

# For complex PDFs with mixed content
chunker = create_chunker("pdf_chunker", extract_images=True)

# For any file format
chunker = create_chunker("universal_document")
```

---

## ๐ŸŒŠ **Streaming Support for Large Files**

### Memory-Efficient Processing
The library provides comprehensive streaming capabilities for processing massive files (1GB+) with constant memory usage.

```python
from chunking_strategy import StreamingChunker

# Process huge files with constant memory usage
streamer = StreamingChunker("sentence_based",
                           block_size=64*1024,    # 64KB blocks
                           overlap_size=1024)     # 1KB overlap

# Memory usage stays constant regardless of file size
for chunk in streamer.stream_file("huge_10gb_file.txt"):
    process_chunk(chunk)  # Memory: ~10MB constant vs 10GB regular loading
```

### Streaming Advantages
- **Constant Memory Usage**: Fixed ~10-100MB footprint regardless of file size
- **Early Chunk Availability**: Start processing chunks as they're generated
- **Fault Tolerance**: Built-in checkpointing and resume capabilities
- **Better Resource Utilization**: Smooth resource usage, system-friendly

### Resume from Interruption
```python
# Automatic resume on interruption
streamer = StreamingChunker("semantic", enable_checkpoints=True)

try:
    for chunk in streamer.stream_file("massive_dataset.txt"):
        process_chunk(chunk)
except KeyboardInterrupt:
    print("Interrupted - progress saved")

# Later - resumes from last checkpoint automatically
for chunk in streamer.stream_file("massive_dataset.txt"):
    process_chunk(chunk)  # Continues from where it left off
```

### Performance Monitoring
```python
for chunk in streamer.stream_file("large_file.txt"):
    progress = streamer.get_progress()
    print(f"๐Ÿ“Š Progress: {progress.progress_percentage:.1f}%")
    print(f"โšก Throughput: {progress.throughput_mbps:.1f} MB/s")
    print(f"โฑ๏ธ  ETA: {progress.eta_seconds:.0f}s")
```

---

## ๐Ÿ“Š **Performance Metrics & Benchmarking**

### Comprehensive Performance Analysis
The library provides extensive performance monitoring to help you optimize strategies and understand real-world efficiency.

#### Quality Metrics
```python
from chunking_strategy.core.metrics import ChunkingQualityEvaluator

evaluator = ChunkingQualityEvaluator()
metrics = evaluator.evaluate(result.chunks)

print(f"๐Ÿ“ Size Consistency: {metrics.size_consistency:.3f}")      # How uniform chunk sizes are
print(f"๐Ÿง  Semantic Coherence: {metrics.coherence:.3f}")           # Internal coherence of chunks
print(f"๐Ÿ“‹ Content Coverage: {metrics.coverage:.3f}")              # Coverage of source content
print(f"๐ŸŽฏ Boundary Quality: {metrics.boundary_quality:.3f}")      # Quality of chunk boundaries
print(f"๐Ÿ’ก Information Density: {metrics.information_density:.3f}") # Information content per chunk
print(f"๐Ÿ† Overall Score: {metrics.overall_score:.3f}")            # Weighted combination
```

#### Performance Metrics
```python
from chunking_strategy.benchmarking import ChunkingBenchmark

benchmark = ChunkingBenchmark(enable_memory_profiling=True)
metrics = benchmark.benchmark_strategy("semantic", "document.pdf")

print(f"โฑ๏ธ  Processing Time: {metrics.processing_time:.3f}s")
print(f"๐Ÿง  Memory Usage: {metrics.memory_usage_mb:.1f} MB")
print(f"๐Ÿ“Š Peak Memory: {metrics.peak_memory_mb:.1f} MB")
print(f"๐Ÿš€ Throughput: {metrics.throughput_mb_per_sec:.1f} MB/s")
print(f"๐Ÿ’ป CPU Usage: {metrics.cpu_usage_percent:.1f}%")
```

### Why These Metrics Matter

#### Real-World Efficiency Interpretation
- **Size Consistency > 0.8**: Predictable for vector databases with token limits
- **Semantic Coherence > 0.8**: Better for LLM understanding and Q&A systems
- **Throughput > 10 MB/s**: Suitable for real-time applications
- **Memory usage < 100MB per GB**: Efficient for batch processing

#### Strategy Comparison
```python
# Compare multiple strategies
strategies = ["sentence_based", "semantic", "fixed_size"]
results = {}

for strategy in strategies:
    results[strategy] = benchmark.benchmark_strategy(strategy, "test_doc.pdf")

best_quality = max(results, key=lambda s: results[s].quality_score)
best_speed = max(results, key=lambda s: results[s].throughput_mb_per_sec)

print(f"๐Ÿ† Best Quality: {best_quality}")
print(f"โšก Best Speed: {best_speed}")
```

---

## ๐ŸŽฌ **Multimedia Support**

### Comprehensive Format Support
The library supports extensive multimedia processing with intelligent strategies for audio, video, and images. From podcast transcription to video analysis and computer vision workflows.

**๐Ÿ”ฅ Key Multimedia Features:**
- **Smart Audio Chunking**: Silence detection, time-based segments, speech boundaries
- **Intelligent Video Processing**: Scene change detection, frame extraction, temporal analysis
- **Advanced Image Tiling**: Grid-based, patch-based, ML-ready formats
- **Rich Metadata Extraction**: Resolution, frame rates, audio properties, timestamps
- **Universal Format Support**: 1,400+ multimedia formats via Apache Tika integration

#### Audio Processing
```python
# Time-based audio chunking
audio_chunker = create_chunker(
    "time_based_audio",
    segment_duration=30,        # 30-second segments
    overlap_duration=2,         # 2-second overlap
    format_support=['mp3', 'wav', 'flac', 'ogg']
)

# Silence-based intelligent chunking
silence_chunker = create_chunker(
    "silence_based_audio",
    silence_threshold_db=-40,   # Silence detection threshold
    min_silence_duration=0.5    # Natural speech boundaries
)
```

#### Video Processing
```python
# Scene-based video chunking with intelligent cuts
scene_chunker = create_chunker(
    "scene_based_video",
    scene_change_threshold=0.3,    # Scene change sensitivity
    extract_frames=True,           # Extract key frames
    include_audio=True             # Include audio analysis
)

# Time-based video segments
video_chunker = create_chunker(
    "time_based_video",
    segment_duration=60,           # 1-minute segments
    frame_extraction_interval=10   # Extract frame every 10s
)
```

#### Image Processing
```python
# Grid-based image tiling
image_chunker = create_chunker(
    "grid_based_image",
    grid_size=(4, 4),              # 4x4 grid (16 tiles)
    tile_overlap=0.1,              # 10% overlap between tiles
    preserve_aspect_ratio=True     # Maintain proportions
)

# Patch-based for machine learning
patch_chunker = create_chunker(
    "patch_based_image",
    patch_size=(224, 224),         # 224x224 pixel patches
    stride=(112, 112)              # 50% overlap for ML workflows
)
```

### Supported Multimedia Formats
- **Audio**: MP3, WAV, FLAC, AAC, OGG, M4A, WMA
- **Video**: MP4, AVI, MOV, MKV, WMV, WebM, FLV
- **Images**: JPEG, PNG, GIF, BMP, TIFF, WebP, SVG
- **Universal**: 1,400+ formats via Apache Tika integration

### Rich Multimedia Metadata
```python
result = chunker.chunk("video_with_audio.mp4")
for chunk in result.chunks:
    metadata = chunk.metadata.extra

    if chunk.modality == ModalityType.VIDEO:
        print(f"Resolution: {metadata['width']}x{metadata['height']}")
        print(f"Frame rate: {metadata['fps']}")
        print(f"Duration: {metadata['duration_seconds']:.2f}s")
        print(f"Codec: {metadata['video_codec']}")
    elif chunk.modality == ModalityType.AUDIO:
        print(f"Sample rate: {metadata['sample_rate']}")
        print(f"Channels: {metadata['channels']}")
        print(f"Bitrate: {metadata['bitrate']}")
        print(f"Audio codec: {metadata['audio_codec']}")
    elif chunk.modality == ModalityType.IMAGE:
        print(f"Dimensions: {metadata['width']}x{metadata['height']}")
        print(f"Color space: {metadata['color_space']}")
        print(f"File size: {metadata['file_size_bytes']} bytes")
```

### CLI Multimedia Processing
```bash
# Process audio files with silence detection
chunking-strategy chunk podcast.mp3 \
    --strategy silence_based_audio \
    --silence-threshold -35 \
    --min-silence-duration 1.0 \
    --output-format json

# Batch process video files with scene detection
chunking-strategy batch videos/*.mp4 \
    --strategy scene_based_video \
    --extract-frames \
    --scene-threshold 0.3 \
    --output-dir processed_videos

# Image tiling for computer vision datasets
chunking-strategy chunk dataset_image.jpg \
    --strategy grid_based_image \
    --grid-size 8x8 \
    --tile-overlap 0.15 \
    --preserve-aspect-ratio
```

### Real-World Multimedia Use Cases
```python
# ๐ŸŽ™๏ธ Podcast transcription workflow
audio_chunker = create_chunker(
    "silence_based_audio",
    silence_threshold_db=-30,      # Detect natural speech pauses
    min_silence_duration=1.0,      # 1-second minimum silence
    max_segment_duration=300       # Max 5-minute segments
)
segments = audio_chunker.chunk("interview_podcast.mp3")
# Perfect for feeding to speech-to-text APIs

# ๐ŸŽฌ Video content analysis
video_chunker = create_chunker(
    "scene_based_video",
    scene_change_threshold=0.25,   # Sensitive scene detection
    extract_frames=True,           # Extract key frames
    frame_interval=5,              # Every 5 seconds
    include_audio=True             # Audio analysis too
)
scenes = video_chunker.chunk("documentary.mp4")
# Ideal for content summarization and indexing

# ๐Ÿ–ผ๏ธ Computer vision dataset preparation
image_chunker = create_chunker(
    "patch_based_image",
    patch_size=(256, 256),         # Standard ML patch size
    stride=(128, 128),             # 50% overlap
    normalize_patches=True,        # Normalize pixel values
    augment_patches=False          # Disable augmentation
)
patches = image_chunker.chunk("satellite_image.tiff")
# Ready for training ML models
```

---

## ๐Ÿง  **Adaptive Chunking with Machine Learning**

### Intelligent Self-Learning Chunking System
The **Adaptive Chunker** is a sophisticated AI-powered meta-chunker that automatically optimizes chunking strategies and parameters based on content characteristics, performance feedback, and historical data. It literally learns from your usage patterns to continuously improve performance.

**๐Ÿ”ฅ Key Adaptive Features:**
- **Content Profiling**: Automatic analysis of content characteristics (entropy, structure, repetition)
- **Strategy Selection**: AI-driven selection of optimal chunking strategies based on content type
- **Performance Learning**: Learns from historical performance to make better decisions
- **Parameter Optimization**: Real-time adaptation of chunking parameters
- **Feedback Processing**: Incorporates user feedback to improve future performance
- **Session Persistence**: Saves learned knowledge across sessions
- **Multi-Strategy Orchestration**: Intelligently combines multiple strategies

#### Basic Adaptive Chunking
```python
from chunking_strategy import create_chunker

# Create adaptive chunker with learning enabled
adaptive_chunker = create_chunker("adaptive",
    # Strategy pool to choose from
    available_strategies=["sentence_based", "paragraph_based", "fixed_size", "semantic"],

    # Learning parameters
    adaptation_threshold=0.1,    # Minimum improvement needed to adapt
    learning_rate=0.1,           # How quickly to adapt
    exploration_rate=0.05,       # Rate of trying new strategies

    # Enable intelligent features
    enable_content_profiling=True,      # Analyze content characteristics
    enable_performance_learning=True,   # Learn from performance data
    enable_strategy_comparison=True,    # Compare multiple strategies

    # Persistence for session learning
    persistence_file="chunking_history.json",
    auto_save_interval=10        # Save every 10 operations
)

# The chunker will automatically:
# 1. Analyze your content characteristics
# 2. Select the optimal strategy
# 3. Optimize parameters based on content
# 4. Learn from performance and adapt
result = adaptive_chunker.chunk("document.pdf")

print(f"๐ŸŽฏ Selected Strategy: {result.source_info['adaptive_strategy']}")
print(f"โš™๏ธ  Optimized Parameters: {result.source_info['optimized_parameters']}")
print(f"๐Ÿ“Š Performance Score: {result.source_info['performance_metrics']['get_overall_score']}")
```

#### Content-Aware Adaptation
```python
# The adaptive chunker automatically profiles content characteristics:

# For structured documents (high structure score)
result = adaptive_chunker.chunk("technical_manual.md")
# โ†’ Automatically selects paragraph_based or section_based

# For repetitive logs (high repetition score)
result = adaptive_chunker.chunk("server_logs.txt")
# โ†’ Automatically selects fastcdc or pattern-based chunking

# For conversational text (low structure, high entropy)
result = adaptive_chunker.chunk("chat_transcript.txt")
# โ†’ Automatically selects sentence_based or dialog-aware chunking

# For dense technical content (high complexity)
result = adaptive_chunker.chunk("research_paper.pdf")
# โ†’ Automatically optimizes chunk sizes and overlap parameters
```

#### Performance Learning and Feedback
```python
# Provide feedback to improve future performance
feedback_score = 0.8  # 0.0-1.0 scale (0.8 = good performance)

# The chunker learns from different types of feedback:
adaptive_chunker.adapt_parameters(feedback_score, "quality")     # Quality-based feedback
adaptive_chunker.adapt_parameters(feedback_score, "performance") # Speed/efficiency feedback
adaptive_chunker.adapt_parameters(feedback_score, "size")       # Chunk size appropriateness

# Learning happens automatically - it will:
# โœ… Increase learning rate for poor performance (learn faster)
# โœ… Adjust strategy selection probabilities
# โœ… Optimize parameters based on feedback type
# โœ… Build content-strategy mappings for similar content in future
```

#### Advanced Adaptive Features
```python
# Get detailed adaptation information
adaptation_info = adaptive_chunker.get_adaptation_info()

print(f"๐Ÿ“Š Total Operations: {adaptation_info['operation_count']}")
print(f"๐Ÿ”„ Total Adaptations: {adaptation_info['total_adaptations']}")
print(f"๐ŸŽฏ Current Best Strategy: {adaptation_info['current_strategy']}")
print(f"๐Ÿ“ˆ Learning Rate: {adaptation_info['learning_rate']:.3f}")

# View strategy performance history
for strategy, stats in adaptation_info['strategy_performance'].items():
    print(f"๐Ÿงช {strategy}: {stats['usage_count']} uses, "
          f"avg score: {stats['avg_score']:.3f}")

# Content-to-strategy mappings learned over time
print(f"๐Ÿ—บ๏ธ  Learned Mappings: {len(adaptation_info['content_strategy_mappings'])}")
```

#### Exploration vs Exploitation
```python
# Control exploration of new strategies vs exploiting known good ones
adaptive_chunker.set_exploration_mode(True)   # More exploration - try new strategies
adaptive_chunker.set_exploration_mode(False)  # More exploitation - use known best

# Fine-tune exploration rate
adaptive_chunker.exploration_rate = 0.1  # 10% chance to try suboptimal strategies for learning
```

#### Session Persistence and Historical Learning
```python
# Adaptive chunker can persist learned knowledge across sessions
adaptive_chunker = create_chunker("adaptive",
    persistence_file="my_chunking_knowledge.json",  # Save/load learned data
    auto_save_interval=5,                           # Save every 5 operations
    history_size=1000,                              # Remember last 1000 operations
)

# The system automatically saves:
# โœ… Strategy performance statistics
# โœ… Content-strategy mappings
# โœ… Optimized parameter sets
# โœ… Adaptation history and patterns

# On next session, it loads this data and starts with learned knowledge!
```

### Why Adaptive Chunking?

**๐ŸŽฏ Use Adaptive Chunking When:**
- Processing diverse content types (documents, logs, conversations, code)
- Performance requirements vary by use case
- You want optimal results without manual tuning
- Building production systems that need to self-optimize
- Processing large volumes where efficiency matters
- Content characteristics change over time

**โšก Performance Benefits:**
- **30-50% better chunk quality** through content-aware strategy selection
- **20-40% faster processing** via learned parameter optimization
- **Self-improving over time** - gets better with more usage
- **Zero manual tuning** - adapts automatically to your data
- **Production-ready** with persistence and error handling

**๐Ÿ”ฌ Technical Implementation:**
The adaptive chunker uses multiple machine learning concepts:
- **Content profiling** via entropy analysis, text ratios, and structure detection
- **Multi-armed bandit algorithms** for strategy selection
- **Reinforcement learning** from performance feedback
- **Parameter optimization** using gradient-free methods
- **Historical pattern recognition** for similar content matching

Try the comprehensive demo to see all features in action:
```bash
python examples/22_adaptive_chunking_learning_demo.py
```

---

## ๐Ÿ”ง **Extending the Library**

### Creating Custom Chunking Algorithms
The library provides a powerful framework for integrating your own custom algorithms with full feature support.

#### Quick Custom Algorithm Example
```python
from chunking_strategy.core.base import BaseChunker, ChunkingResult, Chunk, ChunkMetadata
from chunking_strategy.core.registry import register_chunker, ComplexityLevel

@register_chunker(
    name="word_count_chunker",
    category="text",
    description="Chunks text based on word count",
    complexity=ComplexityLevel.LOW,
    default_parameters={"words_per_chunk": 50}
)
class WordCountChunker(BaseChunker):
    def __init__(self, words_per_chunk=50, **kwargs):
        super().__init__(name="word_count_chunker", category="text", **kwargs)
        self.words_per_chunk = words_per_chunk

    def chunk(self, content, **kwargs):
        words = content.split()
        chunks = []

        for i in range(0, len(words), self.words_per_chunk):
            chunk_words = words[i:i + self.words_per_chunk]
            chunk_content = " ".join(chunk_words)

            chunk = Chunk(
                id=f"word_chunk_{i // self.words_per_chunk}",
                content=chunk_content,
                metadata=ChunkMetadata(word_count=len(chunk_words))
            )
            chunks.append(chunk)

        return ChunkingResult(chunks=chunks, strategy_used=self.name)

# Use your custom chunker
from chunking_strategy import create_chunker
chunker = create_chunker("word_count_chunker", words_per_chunk=30)
result = chunker.chunk("Your text content here")
```

#### Advanced Custom Algorithm with Streaming
```python
from chunking_strategy.core.base import StreamableChunker, AdaptableChunker
from typing import Iterator, Union

@register_chunker(name="advanced_custom")
class AdvancedCustomChunker(StreamableChunker, AdaptableChunker):
    def chunk_stream(self, content_stream: Iterator[Union[str, bytes]], **kwargs):
        """Enable streaming support for large files"""
        buffer = ""
        chunk_id = 0

        for content_piece in content_stream:
            buffer += content_piece

            # Process when buffer reaches threshold
            if len(buffer) >= self.buffer_threshold:
                chunk = self.process_buffer(buffer, chunk_id)
                yield chunk
                chunk_id += 1
                buffer = ""

        # Process remaining buffer
        if buffer:
            chunk = self.process_buffer(buffer, chunk_id)
            yield chunk

    def adapt_parameters(self, feedback_score: float, feedback_type: str):
        """Enable adaptive learning from user feedback"""
        if feedback_score < 0.5:
            self.buffer_threshold *= 0.8  # Make chunks smaller
        elif feedback_score > 0.8:
            self.buffer_threshold *= 1.2  # Make chunks larger
```

### Integration Methods

#### File-Based Loading
```python
# Save algorithm in custom_algorithms/my_algorithm.py
from chunking_strategy import load_custom_algorithms

load_custom_algorithms("custom_algorithms/")
chunker = create_chunker("my_custom_chunker")
```

#### Configuration Integration
```yaml
# config.yaml
custom_algorithms:
  - path: "custom_algorithms/sentiment_chunker.py"
    enabled: true

strategies:
  primary: "sentiment_chunker"  # Use your custom algorithm
```

#### CLI Integration
```bash
# Load and use custom algorithms via CLI
chunking-strategy --custom-algorithms custom_algorithms/ chunk document.txt --strategy my_algorithm

# Compare custom vs built-in algorithms
chunking-strategy compare document.txt --strategies my_algorithm,sentence_based,fixed_size
```

### Validation and Testing Framework
```python
from chunking_strategy.core.custom_validation import CustomAlgorithmValidator
from chunking_strategy.benchmarking import ChunkingBenchmark

# Validate your custom algorithm
validator = CustomAlgorithmValidator()
report = validator.validate_algorithm("my_custom_chunker")

print(f"โœ… Validation passed: {report.is_valid}")
for issue in report.issues:
    print(f"โš ๏ธ  {issue.level}: {issue.message}")

# Performance testing
benchmark = ChunkingBenchmark()
metrics = benchmark.benchmark_strategy("my_custom_chunker", "test_document.txt")
print(f"โฑ๏ธ  Processing time: {metrics.processing_time:.3f}s")
print(f"๐Ÿ† Quality score: {metrics.quality_score:.3f}")
```

**For detailed custom algorithm development, see [CUSTOM_ALGORITHMS_GUIDE.md](CUSTOM_ALGORITHMS_GUIDE.md).**

---

## โšก **Advanced Features & Best Practices**

### Hardware Optimization
```python
from chunking_strategy.core.hardware import get_hardware_info

# Automatic hardware detection and optimization
hardware = get_hardware_info()
print(f"๐Ÿ–ฅ๏ธ  CPU cores: {hardware.cpu_count}")
print(f"๐Ÿง  Memory: {hardware.memory_total_gb:.1f} GB")
print(f"๐Ÿ“ฆ Recommended batch size: {hardware.recommended_batch_size}")

# Hardware-optimized batch processing
from chunking_strategy.core.batch import BatchProcessor

processor = BatchProcessor()
result = processor.process_files(
    files=document_list,
    default_strategy="sentence_based",
    parallel_mode="process",    # Multi-core processing
    workers=None               # Auto-detected optimal count
)
```

### Comprehensive Logging & Debugging
```python
import chunking_strategy as cs

# Configure detailed logging
cs.configure_logging(
    level=cs.LogLevel.VERBOSE,     # Show detailed operations
    file_output=True,              # Save logs to file
    collect_performance=True,      # Track performance metrics
    collect_metrics=True           # Track quality metrics
)

# Enable debug mode for troubleshooting
cs.enable_debug_mode()

# Generate debug archive for bug reports
debug_archive = cs.create_debug_archive("Description of the issue")
print(f"๐Ÿ› Debug archive: {debug_archive['archive_path']}")
# Share this file for support

# Quick debugging examples
cs.user_info("Processing started")              # User-friendly messages
cs.debug_operation("chunk_processing", {"chunks": 42})  # Developer details
cs.performance_log({"time": 1.23, "memory": "45MB"})    # Performance tracking
```

**CLI Debugging:**
```bash
# Enable debug mode with detailed logging
chunking-strategy --debug --log-level verbose chunk document.pdf

# Collect debug information
chunking-strategy debug collect --description "PDF processing issue"

# Test logging configuration
chunking-strategy debug test-logging
```

### Quality Assessment & Adaptive Learning
```python
# Adaptive chunker learns from feedback
adaptive_chunker = create_chunker("adaptive")
result = adaptive_chunker.chunk("document.pdf")

# Simulate user feedback (0.0 = poor, 1.0 = excellent)
user_satisfaction = 0.3  # Poor results
adaptive_chunker.adapt_parameters(user_satisfaction, "quality")

# The chunker automatically adjusts its parameters for better results
result2 = adaptive_chunker.chunk("document2.pdf")  # Should perform better
```

### Error Handling with Fallbacks
```python
def robust_chunking(file_path, strategies=None):
    """Chunk with automatic fallback strategies."""
    if strategies is None:
        strategies = ["pdf_chunker", "sentence_based", "paragraph_based", "fixed_size"]

    for strategy in strategies:
        try:
            chunker = create_chunker(strategy)
            result = chunker.chunk(file_path)
            cs.user_success(f"โœ… Successfully processed with {strategy}")
            return result
        except Exception as e:
            cs.user_warning(f"โš ๏ธ  Strategy {strategy} failed: {e}")
            continue

    raise Exception("โŒ All chunking strategies failed")

# Guaranteed to work with automatic fallbacks
result = robust_chunking("any_document.pdf")
```

**For comprehensive debugging instructions, see [DEBUGGING_GUIDE.md](DEBUGGING_GUIDE.md).**

---

## ๐Ÿ—๏ธ **Integration Examples**

### ๐Ÿš€ **Complete Integration Demos Available!**

We provide **comprehensive, production-ready demo applications** for major frameworks:

| **Framework** | **Demo File** | **Features** | **Run Command** |
|---------------|---------------|--------------|-----------------|
| **๐Ÿฆœ LangChain** | [`examples/18_langchain_integration_demo.py`](examples/18_langchain_integration_demo.py) | RAG pipelines, vector stores, QA chains, embeddings | `python examples/18_langchain_integration_demo.py` |
| **๐ŸŽˆ Streamlit** | [`examples/19_streamlit_app_demo.py`](examples/19_streamlit_app_demo.py) | Web UI, file uploads, real-time chunking, **performance metrics** | `streamlit run examples/19_streamlit_app_demo.py` |
| **โšก Performance Metrics** | [`examples/21_metrics_and_performance_demo.py`](examples/21_metrics_and_performance_demo.py) | Strategy benchmarking, memory tracking, performance analysis | `python examples/21_metrics_and_performance_demo.py` |
| **๐Ÿ”ง Integration Helpers** | [`examples/integration_helpers.py`](examples/integration_helpers.py) | Utility functions for any framework | `from examples.integration_helpers import ChunkingFrameworkAdapter` |

### With Vector Databases

```python
from chunking_strategy import create_chunker
import weaviate  # or qdrant, pinecone, etc.

# Chunk documents
chunker = create_chunker("sentence_based", max_sentences=3)
result = chunker.chunk("document.pdf")

# Store in vector database
client = weaviate.Client("http://localhost:8080")

for chunk in result.chunks:
    client.data_object.create(
        {
            "content": chunk.content,
            "source": chunk.metadata.source,
            "page": chunk.metadata.page,
            "chunk_id": chunk.id
        },
        "Document"
    )
```

### With LangChain (Quick Example)

```python
from chunking_strategy import create_chunker
from langchain.schema import Document

# Chunk with our library
chunker = create_chunker("paragraph_based", max_paragraphs=2)
result = chunker.chunk("document.pdf")

# Convert to LangChain documents
langchain_docs = [
    Document(
        page_content=chunk.content,
        metadata={
            "source": chunk.metadata.source,
            "page": chunk.metadata.page,
            "chunk_id": chunk.id
        }
    )
    for chunk in result.chunks
]
```

**๐ŸŽฏ For complete LangChain integration** including RAG pipelines, embeddings, and QA chains, see [`examples/18_langchain_integration_demo.py`](examples/18_langchain_integration_demo.py).

### With Streamlit (Quick Example)

```python
import streamlit as st
from chunking_strategy import create_chunker, list_strategies

st.title("Document Chunking App")

# Strategy selection from all available strategies
strategy = st.selectbox("Chunking Strategy", list_strategies())

# File upload
uploaded_file = st.file_uploader("Choose a file")

if uploaded_file and st.button("Process"):
    chunker = create_chunker(strategy)
    result = chunker.chunk(uploaded_file)

    st.success(f"Created {len(result.chunks)} chunks using {strategy}")

    # Display chunks with metadata
    for i, chunk in enumerate(result.chunks):
        with st.expander(f"Chunk {i+1} ({len(chunk.content)} chars)"):
            st.text(chunk.content)
            st.json(chunk.metadata.__dict__)
```

**๐ŸŽฏ For a complete Streamlit app** with file uploads, real-time processing, visualizations, **comprehensive performance metrics dashboard**, see [`examples/19_streamlit_app_demo.py`](examples/19_streamlit_app_demo.py).

---

## ๐Ÿš€ **Performance & Hardware Optimization**

### Automatic Hardware Detection

```python
from chunking_strategy.core.hardware import get_hardware_info

# Check your system capabilities
hardware = get_hardware_info()
print(f"CPU cores: {hardware.cpu_count}")
print(f"Memory: {hardware.memory_total_gb:.1f} GB")
print(f"GPUs: {hardware.gpu_count}")
print(f"Recommended batch size: {hardware.recommended_batch_size}")
```

### Optimized Batch Processing

```python
from chunking_strategy.core.batch import BatchProcessor

processor = BatchProcessor()

# Automatic optimization based on your hardware
result = processor.process_files(
    files=file_list,
    default_strategy="universal_document",
    parallel_mode="process",  # or "thread", "sequential"
    workers=None,  # Auto-detected
    batch_size=None  # Auto-detected
)
```

### Performance Monitoring

```python
from chunking_strategy.core.metrics import ChunkingQualityEvaluator

# Evaluate chunk quality
evaluator = ChunkingQualityEvaluator()
metrics = evaluator.evaluate(result.chunks)

print(f"Quality Score: {metrics.coherence:.3f}")
print(f"Size Consistency: {metrics.size_consistency:.3f}")
print(f"Coverage: {metrics.coverage:.3f}")
```

---

## ๐Ÿ”ง **Installation Options**

### Feature-Specific Installation

```bash
# Basic text processing
pip install chunking-strategy

# PDF processing
pip install chunking-strategy[document]

# Hardware optimization
pip install chunking-strategy[hardware]

# Universal document support (Apache Tika)
pip install chunking-strategy[tika]

# Vector database integrations
pip install chunking-strategy[vectordb]

# Everything included
pip install chunking-strategy[all]
```

### Dependencies by Feature

| Feature | Dependencies | Description |
|---------|-------------|-------------|
| `document` | PyMuPDF, PyPDF2, pdfminer.six | PDF processing with multiple backends |
| `hardware` | psutil, GPUtil | Hardware detection and optimization |
| `tika` | tika, python-magic | Universal document processing |
| `text` | spacy, nltk, sentence-transformers>=5.1.0, huggingface-hub | Advanced text processing + embeddings |
| `vectordb` | qdrant-client, weaviate-client | Vector database integrations |

---

## ๐ŸŽฏ **Use Cases**

### RAG (Retrieval-Augmented Generation)

```python
# Optimal for RAG systems
chunker = create_chunker(
    "sentence_based",
    max_sentences=3,      # Good balance of context and specificity
    overlap_sentences=1   # Overlap for better retrieval
)
```

### Vector Database Indexing

```python
# Consistent sizes for vector DBs
chunker = create_chunker(
    "fixed_size",
    chunk_size=512,      # Fits most embedding models
    overlap_size=50      # Prevents information loss at boundaries
)
```

### ๐Ÿ”ฎ **Embeddings & Vector Database Integration**

**Complete workflow from chunking โ†’ embeddings โ†’ vector database:**

```python
from chunking_strategy.orchestrator import ChunkerOrchestrator
from chunking_strategy.core.embeddings import (
    EmbeddingConfig, EmbeddingModel, OutputFormat,
    embed_chunking_result, export_for_vector_db
)

# Step 1: Chunk your documents
orchestrator = ChunkerOrchestrator()
chunks = orchestrator.chunk_file("document.pdf")

# Step 2: Generate embeddings with multiple model options
config = EmbeddingConfig(
    model=EmbeddingModel.ALL_MINILM_L6_V2,    # Fast & lightweight (384D)
    # model=EmbeddingModel.ALL_MPNET_BASE_V2,  # High quality (768D)
    # model=EmbeddingModel.CLIP_VIT_B_32,      # Multimodal (512D)
    output_format=OutputFormat.FULL_METADATA,  # Include all metadata
    batch_size=32,
    normalize_embeddings=True
)

embedding_result = embed_chunking_result(chunks, config)
print(f"โœ… Generated {embedding_result.total_chunks} embeddings ({embedding_result.embedding_dim}D)")

# Step 3: Export ready for vector databases
vector_data = export_for_vector_db(embedding_result, format="dict")
# Now ready for Qdrant, Weaviate, Pinecone, ChromaDB!
```

**๐Ÿ”‘ HuggingFace Authentication Setup:**

1. **Get your token**: Visit https://huggingface.co/settings/tokens
2. **Method 1 - Config file**:
   ```bash
   cp config/huggingface_token.py.template config/huggingface_token.py
   # Edit and add your token: HUGGINGFACE_TOKEN = "hf_your_token_here"
   ```

3. **Method 2 - Environment variable**:
   ```bash
   export HF_TOKEN="hf_your_token_here"
   ```

**Supported Embedding Models:**

| Model | Dimensions | Use Case | Speed |
|-------|------------|----------|-------|
| `ALL_MINILM_L6_V2` | 384 | Fast development, testing | โšกโšกโšก |
| `ALL_MPNET_BASE_V2` | 768 | High quality | โšกโšก |
| `ALL_DISTILROBERTA_V1` | 768 | Code embeddings | โšกโšก |
| `CLIP_VIT_B_32` | 512 | Text + images | โšก |

**CLI Embeddings:**

```bash
# Generate embeddings for all files in directory
chunking-strategy embed documents/ --model all-MiniLM-L6-v2 --output-format full_metadata

# Batch process with specific settings
chunking-strategy embed-batch data/ --batch-size 64 --normalize
```

### Document Analysis & Summarization

```python
# Preserve document structure
chunker = create_chunker(
    "paragraph_based",
    max_paragraphs=2,
    preserve_structure=True
)
```

### Multi-Format Document Processing

```python
# Handle any file type
chunker = create_chunker(
    "universal_document",
    chunk_size=1000,
    extract_metadata=True,
    preserve_structure=True
)
```

---

## ๐Ÿ“Š **Quality & Metrics**

### Built-in Quality Evaluation

```python
from chunking_strategy.core.metrics import ChunkingQualityEvaluator

chunker = create_chunker("sentence_based", max_sentences=3)
result = chunker.chunk("document.pdf")

# Evaluate quality
evaluator = ChunkingQualityEvaluator()
metrics = evaluator.evaluate(result.chunks)

print(f"Size Consistency: {metrics.size_consistency:.3f}")
print(f"Semantic Coherence: {metrics.coherence:.3f}")
print(f"Content Coverage: {metrics.coverage:.3f}")
print(f"Boundary Quality: {metrics.boundary_quality:.3f}")
```

### Adaptive Optimization

```python
# Chunkers can adapt based on feedback
chunker = create_chunker("fixed_size", chunk_size=1000)

# Simulate quality feedback
chunker.adapt_parameters(0.3, "quality")  # Low quality score
# Chunker automatically adjusts parameters for better quality
```

---

## ๐Ÿ› ๏ธ **Advanced Features**

### Custom Chunking Strategy

```python
from chunking_strategy.core.base import BaseChunker
from chunking_strategy.core.registry import register_chunker

@register_chunker(name="custom_chunker", category="custom")
class CustomChunker(BaseChunker):
    def chunk(self, content, **kwargs):
        # Your custom chunking logic
        chunks = self.custom_logic(content)
        return ChunkingResult(chunks=chunks)

# Use your custom chunker
chunker = create_chunker("custom_chunker")
```

### Pipeline Processing

```python
from chunking_strategy.core.pipeline import ChunkingPipeline

pipeline = ChunkingPipeline([
    ("preprocessing", preprocessor),
    ("chunking", chunker),
    ("postprocessing", postprocessor),
    ("quality_check", quality_evaluator)
])

result = pipeline.process("document.pdf")
```

### Streaming for Large Files

```python
# Memory-efficient processing of large files
chunker = create_chunker("fixed_size", chunk_size=1000)

def file_stream():
    with open("huge_file.txt", 'r') as f:
        for line in f:
            yield line

# Process without loading entire file into memory
for chunk in chunker.chunk_stream(file_stream()):
    process_chunk(chunk)
```

---

## ๐Ÿ” **Error Handling & Debugging**

### Robust Error Handling

```python
def safe_chunking(file_path, strategies=None):
    """Chunk with fallback strategies."""
    if strategies is None:
        strategies = ["sentence_based", "paragraph_based", "fixed_size"]

    for strategy in strategies:
        try:
            chunker = create_chunker(strategy)
            return chunker.chunk(file_path)
        except Exception as e:
            print(f"Strategy {strategy} failed: {e}")
            continue

    raise Exception("All chunking strategies failed")

result = safe_chunking("document.pdf")
```

### Logging and Monitoring

```python
import logging

# Enable detailed logging
logging.basicConfig(level=logging.INFO)

# Chunking operations will now log detailed information
chunker = create_chunker("sentence_based")
result = chunker.chunk("document.pdf")

# Monitor performance
print(f"Processing time: {result.processing_time:.3f}s")
print(f"Chunks created: {len(result.chunks)}")
print(f"Average chunk size: {sum(len(c.content) for c in result.chunks) / len(result.chunks):.1f}")
```

---

## ๐Ÿ“š **API Reference**

### Core Functions

```python
# Create chunkers
create_chunker(strategy_name, **params) -> BaseChunker

# List available strategies
list_chunkers() -> List[str]

# Get chunker metadata
get_chunker_metadata(strategy_name) -> ChunkerMetadata

# Configuration-driven processing
ChunkerOrchestrator(config_path) -> orchestrator
```

### Chunking Results

```python
# ChunkingResult attributes
result.chunks          # List[Chunk]
result.processing_time  # float
result.strategy_used    # str
result.source_info     # Dict[str, Any]
result.total_chunks    # int

# Chunk attributes
chunk.id              # str
chunk.content         # str
chunk.modality        # ModalityType
chunk.metadata        # ChunkMetadata
```

### Hardware Optimization

```python
# Hardware detection
get_hardware_info() -> HardwareInfo

# Batch configuration
get_optimal_batch_config(total_files, avg_file_size_mb) -> Dict

# Batch processing
BatchProcessor().process_files(files, strategy, **options) -> BatchResult
```

---

## ๐Ÿค **Contributing**

We welcome contributions! Please feel free to submit a Pull Request or open an issue for:
- Bug fixes and improvements
- New chunking strategies
- Documentation improvements
- Performance optimizations

### Development Setup

```bash
git clone https://github.com/sharanharsoor/chunking.git
cd chunking
pip install -e .[dev,all]
pytest tests/
```

---

## ๐Ÿ“„ **License**

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

---

## ๐Ÿ”— **Links**

- **Repository**: [GitHub repository](https://github.com/sharanharsoor/chunking)
- **PyPI**: [Package on PyPI](https://pypi.org/project/chunking-strategy/)
- **Issues**: [Bug reports and feature requests](https://github.com/sharanharsoor/chunking/issues)

### ๐Ÿ“š **Demo Applications**

- **๐Ÿฆœ LangChain Integration**: [`examples/18_langchain_integration_demo.py`](examples/18_langchain_integration_demo.py) - Complete RAG pipeline demo
- **๐ŸŽˆ Streamlit Web App**: [`examples/19_streamlit_app_demo.py`](examples/19_streamlit_app_demo.py) - Interactive web interface with performance metrics
- **๐Ÿ”ง Integration Helpers**: [`examples/integration_helpers.py`](examples/integration_helpers.py) - Utility functions for any framework
- **๐Ÿ“– Helper Usage Guide**: [`examples/20_using_integration_helpers.py`](examples/20_using_integration_helpers.py) - How to use integration utilities
- **โšก Performance Metrics**: [`examples/21_metrics_and_performance_demo.py`](examples/21_metrics_and_performance_demo.py) - Comprehensive benchmarking and performance analysis
- **๐Ÿง  Adaptive Learning**: [`examples/22_adaptive_chunking_learning_demo.py`](examples/22_adaptive_chunking_learning_demo.py) - AI-powered adaptive chunking with machine learning
- **๐Ÿ“‚ All Examples**: [Browse all examples](examples/) - 20+ demos and tutorials

**๐Ÿš€ Quick Start with Demos:**
```bash
# Install with all integration dependencies
pip install chunking-strategy[all]

# Or install specific integrations only:
# pip install chunking-strategy[streamlit]    # For Streamlit app
# pip install chunking-strategy[langchain]    # For LangChain integration
# pip install chunking-strategy[text,document] # For basic functionality

# Run the interactive Streamlit app
streamlit run examples/19_streamlit_app_demo.py

# Or run the LangChain integration demo
python examples/18_langchain_integration_demo.py

# Or explore adaptive learning capabilities
python examples/22_adaptive_chunking_learning_demo.py
```

---

**Ready to transform your document processing?** Install now and start chunking smarter! ๐Ÿš€

```bash
pip install chunking-strategy[all]
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "chunking-strategy",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Sharan Harsoor <sharanharsoor@gmail.com>",
    "keywords": "chunking, text-processing, document-processing, audio-processing, video-processing, data-streams, content-defined-chunking, semantic-chunking, RAG",
    "author": null,
    "author_email": "Sharan Harsoor <sharanharsoor@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/c6/58/92d1625781b92cb66714fb444ca7e353c94589414c68762e81d9ce7d42bf/chunking_strategy-0.4.1.tar.gz",
    "platform": null,
    "description": "# Chunking Strategy Library\n\n[![PyPI version](https://img.shields.io/pypi/v/chunking-strategy.svg)](https://pypi.org/project/chunking-strategy/)\n[![Python versions](https://img.shields.io/pypi/pyversions/chunking-strategy.svg)](https://pypi.org/project/chunking-strategy/)\n[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Downloads](https://img.shields.io/pypi/dm/chunking-strategy.svg)](https://pypi.org/project/chunking-strategy/)\n[![codecov](https://codecov.io/gh/sharanharsoor/chunking/branch/main/graph/badge.svg)](https://codecov.io/gh/sharanharsoor/chunking)\n\n**A comprehensive Python library for intelligent document chunking with extensive format support and streaming capabilities.**\n\nTransform your documents into perfectly sized chunks for RAG systems, vector databases, LLM processing, and content analysis with multi-core processing and memory-efficient streaming for large files.\n\n> **Platform Support**: Linux and macOS only. Windows support is not currently available.\n\n---\n\n## \ud83d\ude80 **Quick Start**\n\n### Installation\n\n```bash\n# Basic installation\npip install chunking-strategy\n\n# With all features (recommended)\npip install chunking-strategy[all]\n\n# Specific feature sets\npip install chunking-strategy[document,hardware,tika]\n```\n\n### 30-Second Example\n\n```python\nfrom chunking_strategy import ChunkerOrchestrator\n\n# Auto-select best strategy for any file\norchestrator = ChunkerOrchestrator()\n\n# Works with ANY file type - documents, code, multimedia!\nresult = orchestrator.chunk_file(\"document.pdf\")      # PDF processing\nresult = orchestrator.chunk_file(\"podcast.mp3\")       # Audio with silence detection\nresult = orchestrator.chunk_file(\"video.mp4\")         # Video scene analysis\nresult = orchestrator.chunk_file(\"image.jpg\")         # Image tiling\n\n# Use your perfectly chunked content\nfor chunk in result.chunks:\n    print(f\"Chunk: {chunk.content}\")\n    print(f\"Strategy used: {result.strategy_used}\")\n```\n\n### Quick CLI Examples\n\n```bash\n# Simple text file chunking\npython -m chunking_strategy chunk my_document.txt --strategy sentence_based\n\n# PDF with specific output format\npython -m chunking_strategy chunk report.pdf --strategy pdf_chunker --format json\n\n# See all available strategies\npython -m chunking_strategy list-strategies\n\n# Process multiple files at once\npython -m chunking_strategy batch *.txt --strategy paragraph_based --workers 4\n\n# Get help for any command\npython -m chunking_strategy chunk --help\n```\n\n### Choose Your Approach\n\n**\ud83e\udd16 Intelligent Auto Selection (Recommended)**\n```python\n# Let the system choose the best strategy\nconfig = {\"strategies\": {\"primary\": \"auto\"}}\norchestrator = ChunkerOrchestrator(config=config)\nresult = orchestrator.chunk_file(\"any_file.ext\")  # Automatic optimization!\n```\n\n**\ud83c\udfaf Universal Strategies (Any file type)**\n```python\n# Apply ANY strategy to ANY file type\nfrom chunking_strategy import apply_universal_strategy\n\nresult = apply_universal_strategy(\"paragraph\", \"script.py\")     # Paragraph chunking on code\nresult = apply_universal_strategy(\"sentence\", \"document.pdf\")   # Sentence chunking on PDF\nresult = apply_universal_strategy(\"rolling_hash\", \"data.json\") # Rolling hash on JSON\n```\n\n**\ud83d\udd27 Specialized Chunkers (Maximum precision)**\n```python\n# Deep understanding of specific formats\nfrom chunking_strategy import create_chunker\n\npython_chunker = create_chunker(\"python_code\")    # AST-aware Python chunking\npdf_chunker = create_chunker(\"pdf_chunker\")       # PDF structure + images + tables\ncpp_chunker = create_chunker(\"c_cpp_code\")        # C/C++ syntax understanding\n```\n\n---\n\n## \ud83c\udfaf **Why Choose This Library?**\n\n### \u2728 **Key Features**\n- **Multi-Format Support**: PDF, DOC, DOCX, code files, **audio/video/images**, and more. Universal processing via Apache Tika integration\n- **Multimedia Intelligence**: Smart audio chunking with silence detection, video scene analysis, image tiling for ML workflows\n- **Performance Optimization**: Multi-core batch processing and memory-efficient streaming for large files\n- **Batch Processing**: Process thousands of files efficiently with multiprocessing\n- **Robust Processing**: Comprehensive error handling, logging, and quality metrics\n\n### \ud83e\udde0 **Intelligent Chunking Strategies**\n- **Text-Based**: Sentence, paragraph, semantic, and topic-based chunking\n- **Document-Aware**: PDF with image/table extraction, structured document processing\n- **Multimedia-Smart**: Audio silence detection, video scene analysis, image tiling and patches\n- **Universal**: Apache Tika integration for any file format (1,400+ formats)\n- **Custom**: Easy to create domain-specific chunking strategies\n\n### \u26a1 **Performance & Scalability**\n- **True Streaming Processing**: Handle multi-gigabyte files with constant memory usage through memory-mapped streaming\n- **Parallel Processing**: Multi-core batch processing for multiple files\n- **40+ Chunking Strategies**: Comprehensive variety of text, code, document, and multimedia chunkers\n- **Quality Metrics**: Built-in evaluation and optimization\n\n### \ud83d\udd25 **Key Differentiators**\n- **Memory-Mapped Streaming**: Process massive documents (1GB+) that would crash other libraries\n- **Format Variety**: 40+ specialized chunkers vs. 5-8 in most libraries\n- **True Universal Framework**: Apply any strategy to any file type\n- **Token-Precise Control**: Advanced tokenizer integration (tiktoken, transformers, etc.) for LLM applications\n- **Comprehensive Testing**: Extensively tested with real-world files and edge cases\n\n---\n\n## \ud83c\udfad **Three Powerful Approaches - Choose What Fits Your Needs**\n\nOur library offers three distinct approaches to handle different use cases. Understanding when to use each approach will help you get the best results.\n\n### \ud83e\udd16 **Auto Selection**: Intelligent Strategy Selection\n\n**Best for**: Quick start, prototyping, general-purpose applications\n\nThe system automatically chooses the optimal strategy based on file extension and content characteristics. Zero configuration required!\n\n```python\nfrom chunking_strategy import ChunkerOrchestrator\n\n# Zero configuration - just works!\norchestrator = ChunkerOrchestrator(config={\"strategies\": {\"primary\": \"auto\"}})\n\n# System intelligently selects:\nresult = orchestrator.chunk_file(\"script.py\")      # \u2192 paragraph (preserves code structure)\nresult = orchestrator.chunk_file(\"document.txt\")   # \u2192 sentence (readable chunks)\nresult = orchestrator.chunk_file(\"data.json\")      # \u2192 fixed_size (consistent processing)\nresult = orchestrator.chunk_file(\"large_file.pdf\") # \u2192 rolling_hash (efficient for large files)\n```\n\n**Auto Selection Rules:**\n- **Code files** (`.py`, `.js`, `.cpp`, `.java`): `paragraph` - preserves logical structure\n- **Text files** (`.txt`, `.md`, `.rst`): `sentence` - optimizes readability\n- **Documents** (`.pdf`, `.doc`, `.docx`): `paragraph` - maintains document structure\n- **Data files** (`.json`, `.xml`, `.csv`): `fixed_size` - consistent processing\n- **Large files** (>10MB): `rolling_hash` - memory efficient\n- **Small files** (<1KB): `sentence` - optimal for small content\n\n### \ud83c\udf10 **Universal Strategies**: Any Strategy + Any File Type\n\n**Best for**: Consistency across formats, custom workflows, RAG applications\n\nApply ANY chunking strategy to ANY file type through our universal framework. Perfect when you need the same chunking approach across different file formats.\n\n```python\nfrom chunking_strategy import apply_universal_strategy\n\n# Same strategy works across ALL file types!\nresult = apply_universal_strategy(\"sentence\", \"document.pdf\")    # Sentence chunking on PDF\nresult = apply_universal_strategy(\"paragraph\", \"script.py\")      # Paragraph chunking on Python\nresult = apply_universal_strategy(\"rolling_hash\", \"data.xlsx\")   # Rolling hash on Excel\nresult = apply_universal_strategy(\"overlapping_window\", \"video.mp4\")  # Overlapping windows on video\n\n# Perfect for RAG systems requiring consistent chunk sizes\nfor file_path in document_collection:\n    result = apply_universal_strategy(\"fixed_size\", file_path, chunk_size=1000)\n    # All files get exactly 1000-character chunks regardless of format!\n```\n\n**Universal Strategies Available:**\n- `fixed_size` - Consistent chunk sizes with overlap support\n- `sentence` - Sentence-boundary aware chunking\n- `paragraph` - Paragraph-based logical grouping\n- `overlapping_window` - Sliding window with customizable overlap\n- `rolling_hash` - Content-defined boundaries using hash functions\n\n### \ud83d\udd27 **Specialized Chunkers**: Maximum Precision & Rich Metadata\n\n**Best for**: Advanced applications, code analysis, document intelligence, detailed metadata requirements\n\nDeep understanding of specific file formats with semantic boundaries and comprehensive metadata extraction.\n\n```python\nfrom chunking_strategy import create_chunker\n\n# Python AST-aware chunking\npython_chunker = create_chunker(\"python_code\")\nresult = python_chunker.chunk(\"complex_script.py\")\nfor chunk in result.chunks:\n    meta = chunk.metadata.extra\n    print(f\"Element: {meta['element_name']} ({meta['element_type']})\")\n    print(f\"Has docstring: {bool(meta.get('docstring'))}\")\n    print(f\"Arguments: {meta.get('args', [])}\")\n\n# PDF with image and table extraction\npdf_chunker = create_chunker(\"pdf_chunker\")\nresult = pdf_chunker.chunk(\"report.pdf\", extract_images=True, extract_tables=True)\nfor chunk in result.chunks:\n    if chunk.modality == ModalityType.IMAGE:\n        print(f\"Found image on page {chunk.metadata.page}\")\n    elif \"table\" in chunk.metadata.extra.get(\"chunk_type\", \"\"):\n        print(f\"Found table: {chunk.content[:100]}...\")\n\n# C/C++ syntax-aware chunking\ncpp_chunker = create_chunker(\"c_cpp_code\")\nresult = cpp_chunker.chunk(\"algorithm.cpp\")\nfor chunk in result.chunks:\n    if chunk.metadata.extra.get(\"element_type\") == \"function\":\n        print(f\"Function: {chunk.metadata.extra['element_name']}\")\n```\n\n**Specialized Chunkers Available:**\n- `python_code` - AST parsing, function/class boundaries, docstring extraction\n- `c_cpp_code` - C/C++ syntax understanding, preprocessor directives\n- `universal_code` - Multi-language code chunking (JavaScript, Go, Rust, etc.)\n- `pdf_chunker` - PDF structure, images, tables, metadata\n- `universal_document` - Apache Tika integration for comprehensive format support *(coming soon)*\n\n### \ud83d\udcca **Comparison: When to Use Each Approach**\n\n| Use Case | Auto Selection | Universal Strategies | Specialized Chunkers |\n|----------|---------------|---------------------|-------------------|\n| **Quick prototyping** | \u2b50\u2b50\u2b50\u2b50\u2b50 Perfect | \u2b50\u2b50\u2b50 Good | \u2b50\u2b50 Overkill |\n| **RAG systems** | \u2b50\u2b50\u2b50\u2b50 Great | \u2b50\u2b50\u2b50\u2b50\u2b50 Perfect | \u2b50\u2b50\u2b50 Good |\n| **Code analysis** | \u2b50\u2b50\u2b50 Good | \u2b50\u2b50 Basic | \u2b50\u2b50\u2b50\u2b50\u2b50 Perfect |\n| **Document intelligence** | \u2b50\u2b50\u2b50 Good | \u2b50\u2b50 Basic | \u2b50\u2b50\u2b50\u2b50\u2b50 Perfect |\n| **Cross-format consistency** | \u2b50\u2b50\u2b50 Good | \u2b50\u2b50\u2b50\u2b50\u2b50 Perfect | \u2b50\u2b50 Limited |\n| **Advanced applications** | \u2b50\u2b50\u2b50\u2b50 Great | \u2b50\u2b50\u2b50\u2b50 Great | \u2b50\u2b50\u2b50\u2b50\u2b50 Perfect |\n\n### \ud83d\udd2e **Future File Format Support**\n\nWe're actively expanding format support. **Coming soon**:\n\n| Category | Formats | Strategy Recommendations |\n|----------|---------|------------------------|\n| **Spreadsheets** | `.xls`, `.xlsx`, `.ods`, `.csv` | `fixed_size` or specialized `excel_chunker` |\n| **Presentations** | `.ppt`, `.pptx`, `.odp` | `paragraph` or specialized `presentation_chunker` |\n| **Data Formats** | `.parquet`, `.avro`, `.orc` | `fixed_size` or specialized `data_chunker` |\n| **Media Files** | `.mp4`, `.avi`, `.mp3`, `.wav` | `overlapping_window` or specialized `media_chunker` |\n| **Archives** | `.zip`, `.tar`, `.7z` | Content-aware or specialized `archive_chunker` |\n| **CAD/Design** | `.dwg`, `.dxf`, `.svg` | Specialized `design_chunker` |\n\n**Request format support**: [Open an issue](https://github.com/sharanharsoor/chunking/issues) for priority formats!\n\n---\n\n## \ud83d\udcd6 **Usage Examples**\n\n### Basic Text Chunking\n\n```python\nfrom chunking_strategy import create_chunker\n\n# Sentence-based chunking (best for semantic coherence)\nchunker = create_chunker(\"sentence_based\", max_sentences=3)\nresult = chunker.chunk(\"Your text here...\")\n\n# Fixed-size chunking (consistent chunk sizes)\nchunker = create_chunker(\"fixed_size\", chunk_size=1000, overlap_size=100)\nresult = chunker.chunk(\"Your text here...\")\n\n# Paragraph-based chunking (natural boundaries)\nchunker = create_chunker(\"paragraph_based\", max_paragraphs=2)\nresult = chunker.chunk(\"Your text here...\")\n```\n\n### PDF Processing with Images & Tables\n\n```python\nfrom chunking_strategy import create_chunker\n\n# Advanced PDF processing\nchunker = create_chunker(\n    \"pdf_chunker\",\n    pages_per_chunk=1,\n    extract_images=True,\n    extract_tables=True,\n    backend=\"pymupdf\"  # or \"pypdf2\", \"pdfminer\"\n)\n\nresult = chunker.chunk(\"document.pdf\")\n\n# Access different content types\nfor chunk in result.chunks:\n    chunk_type = chunk.metadata.extra.get('chunk_type')\n    if chunk_type == 'text':\n        print(f\"Text: {chunk.content}\")\n    elif chunk_type == 'image':\n        print(f\"Image on page {chunk.metadata.page}\")\n    elif chunk_type == 'table':\n        print(f\"Table: {chunk.content}\")\n```\n\n### Universal Document Processing\n\n```python\nfrom chunking_strategy import create_chunker\n\n# Process ANY file format with Apache Tika\nchunker = create_chunker(\n    \"universal_document\",\n    chunk_size=1000,\n    preserve_structure=True,\n    extract_metadata=True\n)\n\n# Works with PDF, DOC, DOCX, Excel, PowerPoint, code files, etc.\nresult = chunker.chunk(\"any_document.docx\")\n\nprint(f\"File type: {result.source_info['file_type']}\")\nprint(f\"Extracted metadata: {result.source_info['tika_metadata']}\")\n```\n\n### Batch Processing with Hardware Optimization\n\n```python\nfrom chunking_strategy.core.batch import BatchProcessor\n\n# Automatic hardware optimization\nprocessor = BatchProcessor()\n\nresult = processor.process_files(\n    files=[\"doc1.pdf\", \"doc2.txt\", \"doc3.docx\"],\n    default_strategy=\"sentence_based\",\n    parallel_mode=\"process\",  # Uses multiple CPU cores\n    workers=None  # Auto-detected optimal worker count\n)\n\nprint(f\"Processed {result.total_files} files\")\nprint(f\"Created {result.total_chunks} chunks\")\nprint(f\"Performance: {result.files_per_second:.1f} files/second\")\n```\n\n---\n\n## \ud83d\udda5\ufe0f **Command Line Interface**\n\n### Quick Commands\n\n```bash\n# List available strategies\nchunking-strategy list-strategies\n\n# Check your hardware capabilities\nchunking-strategy hardware --recommendations\n\n# Chunk a single file\nchunking-strategy chunk document.pdf --strategy pdf_chunker --format json\n\n# Batch process multiple files\nchunking-strategy batch *.txt --strategy sentence_based --workers 4\n\n# Use configuration file\nchunking-strategy chunk document.pdf --config my_config.yaml\n```\n\n### Advanced CLI Usage\n\n```bash\n# Hardware-optimized batch processing\nchunking-strategy batch documents/*.pdf \\\n    --strategy universal_document \\\n    --workers 8 \\\n    --mode process \\\n    --output-dir results \\\n    --format json\n\n# PDF processing with specific backend\nchunking-strategy chunk document.pdf \\\n    --strategy pdf_chunker \\\n    --backend pymupdf \\\n    --extract-images \\\n    --extract-tables \\\n    --pages-per-chunk 1\n\n# Process entire directory with custom strategies per file type\nchunking-strategy batch-smart ./documents/ \\\n    --pdf-strategy \"enhanced_pdf_chunker\" \\\n    --text-strategy \"semantic\" \\\n    --code-strategy \"python_code\" \\\n    --output-format json \\\n    --generate-embeddings \\\n    --embedding-model all-MiniLM-L6-v2\n\n# Real-time processing with monitoring\nchunking-strategy process-watch ./incoming/ \\\n    --auto-strategy \\\n    --streaming \\\n    --max-memory 4GB \\\n    --webhook http://localhost:8080/chunked \\\n    --metrics-dashboard\n```\n\n---\n\n## \u2699\ufe0f **Configuration-Driven Processing**\n\n### YAML Configuration\n\nCreate a `config.yaml` file:\n\n```yaml\nprofile_name: \"rag_optimized\"\n\nstrategies:\n  primary: \"sentence_based\"\n  fallbacks: [\"paragraph_based\", \"fixed_size\"]\n  configs:\n    sentence_based:\n      max_sentences: 3\n      overlap_sentences: 1\n    paragraph_based:\n      max_paragraphs: 2\n    fixed_size:\n      chunk_size: 1000\n\npreprocessing:\n  enabled: true\n  normalize_whitespace: true\n\npostprocessing:\n  enabled: true\n  merge_short_chunks: true\n  min_chunk_size: 100\n\nquality_evaluation:\n  enabled: true\n  threshold: 0.7\n```\n\nUse with Python:\n\n```python\nfrom chunking_strategy import ChunkerOrchestrator\n\norchestrator = ChunkerOrchestrator(config_path=\"config.yaml\")\nresult = orchestrator.chunk_file(\"document.pdf\")\n```\n\nUse with CLI:\n\n```bash\nchunking-strategy chunk document.pdf --config config.yaml\n```\n\n---\n\n## \ud83c\udfad **Complete Chunking Algorithms Reference (40+ Total)**\n\n### \ud83d\udcdd **Text-Based Strategies** (9 strategies)\n- `sentence_based` - Semantic coherence with sentence boundaries (RAG, Q&A)\n- `paragraph_based` - Natural paragraph structure (Document analysis, summarization)\n- `token_based` - Precise token-level chunking with multiple tokenizer support (LLM optimization)\n- `semantic` - AI-powered semantic similarity with embeddings (High-quality understanding)\n- `boundary_aware` - Intelligent boundary detection (Clean, readable chunks)\n- `recursive` - Hierarchical multi-level chunking (Complex document structure)\n- `overlapping_window` - Sliding window with customizable overlap (Context preservation)\n- `fixed_length_word` - Fixed word count per chunk (Consistent word-based processing)\n- `embedding_based` - Embedding similarity for boundaries (Advanced semantic understanding)\n\n### \ud83d\udcbb **Code-Aware Strategies** (7 strategies)\n- `python_code` - AST-aware Python parsing with function/class boundaries (Python analysis)\n- `c_cpp_code` - C/C++ syntax understanding with preprocessor handling (Systems programming)\n- `javascript_code` - JavaScript/TypeScript AST parsing (Web development, Node.js)\n- `java_code` - Java syntax parsing with package structure (Enterprise Java codebases)\n- `go_code` - Go language structure awareness (Go codebase analysis)\n- `css_code` - CSS rule and selector-aware chunking (Web styling analysis)\n- `universal_code` - Multi-language code chunking (Cross-language processing)\n\n### \ud83d\udcc4 **Document-Aware Strategies** (5 strategies)\n- `pdf_chunker` - Advanced PDF processing with images, tables, layout (PDF intelligence)\n- `enhanced_pdf_chunker` - Premium PDF with OCR, structure analysis (Complex PDF workflows)\n- `doc_chunker` - Microsoft Word document processing (Corporate documents)\n- `markdown_chunker` - Markdown structure-aware (headers, lists, code blocks)\n- `xml_html_chunker` - XML/HTML tag-aware with structure preservation (Web content)\n\n### \ud83d\udcca **Data Format Strategies** (2 strategies)\n- `csv_chunker` - CSV row and column-aware processing (Tabular data analysis)\n- `json_chunker` - JSON structure-preserving chunking (API data, configuration files)\n\n### \ud83c\udfb5 **Multimedia Strategies** (6 strategies)\n- `time_based_audio` - Audio chunking by time intervals (Podcast transcription)\n- `silence_based_audio` - Audio chunking at silence boundaries (Speech processing)\n- `time_based_video` - Video chunking by time segments (Video content analysis)\n- `scene_based_video` - Scene change detection for intelligent cuts (Video processing)\n- `grid_based_image` - Spatial grid-based image tiling (Computer vision)\n- `patch_based_image` - Overlapping patch extraction (Machine learning, patterns)\n\n### \ud83d\udd27 **Content-Defined Chunking (CDC)** (7 strategies)\n- `fastcdc` - Fast content-defined chunking with rolling hash (Deduplication, backup)\n- `rabin_fingerprinting` - Rabin polynomial rolling hash boundaries (Content-addressable storage)\n- `rolling_hash` - Generic rolling hash with configurable parameters (Variable-size chunking)\n- `buzhash` - BuzHash algorithm for content boundaries (Efficient content splitting)\n- `gear_cdc` - Gear-based content-defined chunking (High-performance CDC)\n- `ml_cdc` - Machine learning-enhanced boundary detection (Intelligent boundaries)\n- `tttd` - Two Threshold Two Divisor algorithm (Advanced CDC with dual thresholds)\n\n### \ud83e\udde0 **Advanced & Adaptive Strategies** (4 strategies)\n- `adaptive` - Self-learning chunker that adapts based on feedback (Dynamic optimization)\n- `context_enriched` - Context-aware chunking with NLP enhancement (Advanced text understanding)\n- `discourse_aware` - Discourse structure and topic transition detection (Academic papers)\n- `fixed_size` - Simple fixed-size chunking with overlap support (Baseline, simple needs)\n\n### Strategy Selection Guide\n\n```python\n# For RAG systems and LLM processing\nchunker = create_chunker(\"sentence_based\", max_sentences=3)\n\n# For vector databases with token limits\nchunker = create_chunker(\"fixed_size\", chunk_size=512)\n\n# For document analysis and summarization\nchunker = create_chunker(\"paragraph_based\", max_paragraphs=2)\n\n# For complex PDFs with mixed content\nchunker = create_chunker(\"pdf_chunker\", extract_images=True)\n\n# For any file format\nchunker = create_chunker(\"universal_document\")\n```\n\n---\n\n## \ud83c\udf0a **Streaming Support for Large Files**\n\n### Memory-Efficient Processing\nThe library provides comprehensive streaming capabilities for processing massive files (1GB+) with constant memory usage.\n\n```python\nfrom chunking_strategy import StreamingChunker\n\n# Process huge files with constant memory usage\nstreamer = StreamingChunker(\"sentence_based\",\n                           block_size=64*1024,    # 64KB blocks\n                           overlap_size=1024)     # 1KB overlap\n\n# Memory usage stays constant regardless of file size\nfor chunk in streamer.stream_file(\"huge_10gb_file.txt\"):\n    process_chunk(chunk)  # Memory: ~10MB constant vs 10GB regular loading\n```\n\n### Streaming Advantages\n- **Constant Memory Usage**: Fixed ~10-100MB footprint regardless of file size\n- **Early Chunk Availability**: Start processing chunks as they're generated\n- **Fault Tolerance**: Built-in checkpointing and resume capabilities\n- **Better Resource Utilization**: Smooth resource usage, system-friendly\n\n### Resume from Interruption\n```python\n# Automatic resume on interruption\nstreamer = StreamingChunker(\"semantic\", enable_checkpoints=True)\n\ntry:\n    for chunk in streamer.stream_file(\"massive_dataset.txt\"):\n        process_chunk(chunk)\nexcept KeyboardInterrupt:\n    print(\"Interrupted - progress saved\")\n\n# Later - resumes from last checkpoint automatically\nfor chunk in streamer.stream_file(\"massive_dataset.txt\"):\n    process_chunk(chunk)  # Continues from where it left off\n```\n\n### Performance Monitoring\n```python\nfor chunk in streamer.stream_file(\"large_file.txt\"):\n    progress = streamer.get_progress()\n    print(f\"\ud83d\udcca Progress: {progress.progress_percentage:.1f}%\")\n    print(f\"\u26a1 Throughput: {progress.throughput_mbps:.1f} MB/s\")\n    print(f\"\u23f1\ufe0f  ETA: {progress.eta_seconds:.0f}s\")\n```\n\n---\n\n## \ud83d\udcca **Performance Metrics & Benchmarking**\n\n### Comprehensive Performance Analysis\nThe library provides extensive performance monitoring to help you optimize strategies and understand real-world efficiency.\n\n#### Quality Metrics\n```python\nfrom chunking_strategy.core.metrics import ChunkingQualityEvaluator\n\nevaluator = ChunkingQualityEvaluator()\nmetrics = evaluator.evaluate(result.chunks)\n\nprint(f\"\ud83d\udccf Size Consistency: {metrics.size_consistency:.3f}\")      # How uniform chunk sizes are\nprint(f\"\ud83e\udde0 Semantic Coherence: {metrics.coherence:.3f}\")           # Internal coherence of chunks\nprint(f\"\ud83d\udccb Content Coverage: {metrics.coverage:.3f}\")              # Coverage of source content\nprint(f\"\ud83c\udfaf Boundary Quality: {metrics.boundary_quality:.3f}\")      # Quality of chunk boundaries\nprint(f\"\ud83d\udca1 Information Density: {metrics.information_density:.3f}\") # Information content per chunk\nprint(f\"\ud83c\udfc6 Overall Score: {metrics.overall_score:.3f}\")            # Weighted combination\n```\n\n#### Performance Metrics\n```python\nfrom chunking_strategy.benchmarking import ChunkingBenchmark\n\nbenchmark = ChunkingBenchmark(enable_memory_profiling=True)\nmetrics = benchmark.benchmark_strategy(\"semantic\", \"document.pdf\")\n\nprint(f\"\u23f1\ufe0f  Processing Time: {metrics.processing_time:.3f}s\")\nprint(f\"\ud83e\udde0 Memory Usage: {metrics.memory_usage_mb:.1f} MB\")\nprint(f\"\ud83d\udcca Peak Memory: {metrics.peak_memory_mb:.1f} MB\")\nprint(f\"\ud83d\ude80 Throughput: {metrics.throughput_mb_per_sec:.1f} MB/s\")\nprint(f\"\ud83d\udcbb CPU Usage: {metrics.cpu_usage_percent:.1f}%\")\n```\n\n### Why These Metrics Matter\n\n#### Real-World Efficiency Interpretation\n- **Size Consistency > 0.8**: Predictable for vector databases with token limits\n- **Semantic Coherence > 0.8**: Better for LLM understanding and Q&A systems\n- **Throughput > 10 MB/s**: Suitable for real-time applications\n- **Memory usage < 100MB per GB**: Efficient for batch processing\n\n#### Strategy Comparison\n```python\n# Compare multiple strategies\nstrategies = [\"sentence_based\", \"semantic\", \"fixed_size\"]\nresults = {}\n\nfor strategy in strategies:\n    results[strategy] = benchmark.benchmark_strategy(strategy, \"test_doc.pdf\")\n\nbest_quality = max(results, key=lambda s: results[s].quality_score)\nbest_speed = max(results, key=lambda s: results[s].throughput_mb_per_sec)\n\nprint(f\"\ud83c\udfc6 Best Quality: {best_quality}\")\nprint(f\"\u26a1 Best Speed: {best_speed}\")\n```\n\n---\n\n## \ud83c\udfac **Multimedia Support**\n\n### Comprehensive Format Support\nThe library supports extensive multimedia processing with intelligent strategies for audio, video, and images. From podcast transcription to video analysis and computer vision workflows.\n\n**\ud83d\udd25 Key Multimedia Features:**\n- **Smart Audio Chunking**: Silence detection, time-based segments, speech boundaries\n- **Intelligent Video Processing**: Scene change detection, frame extraction, temporal analysis\n- **Advanced Image Tiling**: Grid-based, patch-based, ML-ready formats\n- **Rich Metadata Extraction**: Resolution, frame rates, audio properties, timestamps\n- **Universal Format Support**: 1,400+ multimedia formats via Apache Tika integration\n\n#### Audio Processing\n```python\n# Time-based audio chunking\naudio_chunker = create_chunker(\n    \"time_based_audio\",\n    segment_duration=30,        # 30-second segments\n    overlap_duration=2,         # 2-second overlap\n    format_support=['mp3', 'wav', 'flac', 'ogg']\n)\n\n# Silence-based intelligent chunking\nsilence_chunker = create_chunker(\n    \"silence_based_audio\",\n    silence_threshold_db=-40,   # Silence detection threshold\n    min_silence_duration=0.5    # Natural speech boundaries\n)\n```\n\n#### Video Processing\n```python\n# Scene-based video chunking with intelligent cuts\nscene_chunker = create_chunker(\n    \"scene_based_video\",\n    scene_change_threshold=0.3,    # Scene change sensitivity\n    extract_frames=True,           # Extract key frames\n    include_audio=True             # Include audio analysis\n)\n\n# Time-based video segments\nvideo_chunker = create_chunker(\n    \"time_based_video\",\n    segment_duration=60,           # 1-minute segments\n    frame_extraction_interval=10   # Extract frame every 10s\n)\n```\n\n#### Image Processing\n```python\n# Grid-based image tiling\nimage_chunker = create_chunker(\n    \"grid_based_image\",\n    grid_size=(4, 4),              # 4x4 grid (16 tiles)\n    tile_overlap=0.1,              # 10% overlap between tiles\n    preserve_aspect_ratio=True     # Maintain proportions\n)\n\n# Patch-based for machine learning\npatch_chunker = create_chunker(\n    \"patch_based_image\",\n    patch_size=(224, 224),         # 224x224 pixel patches\n    stride=(112, 112)              # 50% overlap for ML workflows\n)\n```\n\n### Supported Multimedia Formats\n- **Audio**: MP3, WAV, FLAC, AAC, OGG, M4A, WMA\n- **Video**: MP4, AVI, MOV, MKV, WMV, WebM, FLV\n- **Images**: JPEG, PNG, GIF, BMP, TIFF, WebP, SVG\n- **Universal**: 1,400+ formats via Apache Tika integration\n\n### Rich Multimedia Metadata\n```python\nresult = chunker.chunk(\"video_with_audio.mp4\")\nfor chunk in result.chunks:\n    metadata = chunk.metadata.extra\n\n    if chunk.modality == ModalityType.VIDEO:\n        print(f\"Resolution: {metadata['width']}x{metadata['height']}\")\n        print(f\"Frame rate: {metadata['fps']}\")\n        print(f\"Duration: {metadata['duration_seconds']:.2f}s\")\n        print(f\"Codec: {metadata['video_codec']}\")\n    elif chunk.modality == ModalityType.AUDIO:\n        print(f\"Sample rate: {metadata['sample_rate']}\")\n        print(f\"Channels: {metadata['channels']}\")\n        print(f\"Bitrate: {metadata['bitrate']}\")\n        print(f\"Audio codec: {metadata['audio_codec']}\")\n    elif chunk.modality == ModalityType.IMAGE:\n        print(f\"Dimensions: {metadata['width']}x{metadata['height']}\")\n        print(f\"Color space: {metadata['color_space']}\")\n        print(f\"File size: {metadata['file_size_bytes']} bytes\")\n```\n\n### CLI Multimedia Processing\n```bash\n# Process audio files with silence detection\nchunking-strategy chunk podcast.mp3 \\\n    --strategy silence_based_audio \\\n    --silence-threshold -35 \\\n    --min-silence-duration 1.0 \\\n    --output-format json\n\n# Batch process video files with scene detection\nchunking-strategy batch videos/*.mp4 \\\n    --strategy scene_based_video \\\n    --extract-frames \\\n    --scene-threshold 0.3 \\\n    --output-dir processed_videos\n\n# Image tiling for computer vision datasets\nchunking-strategy chunk dataset_image.jpg \\\n    --strategy grid_based_image \\\n    --grid-size 8x8 \\\n    --tile-overlap 0.15 \\\n    --preserve-aspect-ratio\n```\n\n### Real-World Multimedia Use Cases\n```python\n# \ud83c\udf99\ufe0f Podcast transcription workflow\naudio_chunker = create_chunker(\n    \"silence_based_audio\",\n    silence_threshold_db=-30,      # Detect natural speech pauses\n    min_silence_duration=1.0,      # 1-second minimum silence\n    max_segment_duration=300       # Max 5-minute segments\n)\nsegments = audio_chunker.chunk(\"interview_podcast.mp3\")\n# Perfect for feeding to speech-to-text APIs\n\n# \ud83c\udfac Video content analysis\nvideo_chunker = create_chunker(\n    \"scene_based_video\",\n    scene_change_threshold=0.25,   # Sensitive scene detection\n    extract_frames=True,           # Extract key frames\n    frame_interval=5,              # Every 5 seconds\n    include_audio=True             # Audio analysis too\n)\nscenes = video_chunker.chunk(\"documentary.mp4\")\n# Ideal for content summarization and indexing\n\n# \ud83d\uddbc\ufe0f Computer vision dataset preparation\nimage_chunker = create_chunker(\n    \"patch_based_image\",\n    patch_size=(256, 256),         # Standard ML patch size\n    stride=(128, 128),             # 50% overlap\n    normalize_patches=True,        # Normalize pixel values\n    augment_patches=False          # Disable augmentation\n)\npatches = image_chunker.chunk(\"satellite_image.tiff\")\n# Ready for training ML models\n```\n\n---\n\n## \ud83e\udde0 **Adaptive Chunking with Machine Learning**\n\n### Intelligent Self-Learning Chunking System\nThe **Adaptive Chunker** is a sophisticated AI-powered meta-chunker that automatically optimizes chunking strategies and parameters based on content characteristics, performance feedback, and historical data. It literally learns from your usage patterns to continuously improve performance.\n\n**\ud83d\udd25 Key Adaptive Features:**\n- **Content Profiling**: Automatic analysis of content characteristics (entropy, structure, repetition)\n- **Strategy Selection**: AI-driven selection of optimal chunking strategies based on content type\n- **Performance Learning**: Learns from historical performance to make better decisions\n- **Parameter Optimization**: Real-time adaptation of chunking parameters\n- **Feedback Processing**: Incorporates user feedback to improve future performance\n- **Session Persistence**: Saves learned knowledge across sessions\n- **Multi-Strategy Orchestration**: Intelligently combines multiple strategies\n\n#### Basic Adaptive Chunking\n```python\nfrom chunking_strategy import create_chunker\n\n# Create adaptive chunker with learning enabled\nadaptive_chunker = create_chunker(\"adaptive\",\n    # Strategy pool to choose from\n    available_strategies=[\"sentence_based\", \"paragraph_based\", \"fixed_size\", \"semantic\"],\n\n    # Learning parameters\n    adaptation_threshold=0.1,    # Minimum improvement needed to adapt\n    learning_rate=0.1,           # How quickly to adapt\n    exploration_rate=0.05,       # Rate of trying new strategies\n\n    # Enable intelligent features\n    enable_content_profiling=True,      # Analyze content characteristics\n    enable_performance_learning=True,   # Learn from performance data\n    enable_strategy_comparison=True,    # Compare multiple strategies\n\n    # Persistence for session learning\n    persistence_file=\"chunking_history.json\",\n    auto_save_interval=10        # Save every 10 operations\n)\n\n# The chunker will automatically:\n# 1. Analyze your content characteristics\n# 2. Select the optimal strategy\n# 3. Optimize parameters based on content\n# 4. Learn from performance and adapt\nresult = adaptive_chunker.chunk(\"document.pdf\")\n\nprint(f\"\ud83c\udfaf Selected Strategy: {result.source_info['adaptive_strategy']}\")\nprint(f\"\u2699\ufe0f  Optimized Parameters: {result.source_info['optimized_parameters']}\")\nprint(f\"\ud83d\udcca Performance Score: {result.source_info['performance_metrics']['get_overall_score']}\")\n```\n\n#### Content-Aware Adaptation\n```python\n# The adaptive chunker automatically profiles content characteristics:\n\n# For structured documents (high structure score)\nresult = adaptive_chunker.chunk(\"technical_manual.md\")\n# \u2192 Automatically selects paragraph_based or section_based\n\n# For repetitive logs (high repetition score)\nresult = adaptive_chunker.chunk(\"server_logs.txt\")\n# \u2192 Automatically selects fastcdc or pattern-based chunking\n\n# For conversational text (low structure, high entropy)\nresult = adaptive_chunker.chunk(\"chat_transcript.txt\")\n# \u2192 Automatically selects sentence_based or dialog-aware chunking\n\n# For dense technical content (high complexity)\nresult = adaptive_chunker.chunk(\"research_paper.pdf\")\n# \u2192 Automatically optimizes chunk sizes and overlap parameters\n```\n\n#### Performance Learning and Feedback\n```python\n# Provide feedback to improve future performance\nfeedback_score = 0.8  # 0.0-1.0 scale (0.8 = good performance)\n\n# The chunker learns from different types of feedback:\nadaptive_chunker.adapt_parameters(feedback_score, \"quality\")     # Quality-based feedback\nadaptive_chunker.adapt_parameters(feedback_score, \"performance\") # Speed/efficiency feedback\nadaptive_chunker.adapt_parameters(feedback_score, \"size\")       # Chunk size appropriateness\n\n# Learning happens automatically - it will:\n# \u2705 Increase learning rate for poor performance (learn faster)\n# \u2705 Adjust strategy selection probabilities\n# \u2705 Optimize parameters based on feedback type\n# \u2705 Build content-strategy mappings for similar content in future\n```\n\n#### Advanced Adaptive Features\n```python\n# Get detailed adaptation information\nadaptation_info = adaptive_chunker.get_adaptation_info()\n\nprint(f\"\ud83d\udcca Total Operations: {adaptation_info['operation_count']}\")\nprint(f\"\ud83d\udd04 Total Adaptations: {adaptation_info['total_adaptations']}\")\nprint(f\"\ud83c\udfaf Current Best Strategy: {adaptation_info['current_strategy']}\")\nprint(f\"\ud83d\udcc8 Learning Rate: {adaptation_info['learning_rate']:.3f}\")\n\n# View strategy performance history\nfor strategy, stats in adaptation_info['strategy_performance'].items():\n    print(f\"\ud83e\uddea {strategy}: {stats['usage_count']} uses, \"\n          f\"avg score: {stats['avg_score']:.3f}\")\n\n# Content-to-strategy mappings learned over time\nprint(f\"\ud83d\uddfa\ufe0f  Learned Mappings: {len(adaptation_info['content_strategy_mappings'])}\")\n```\n\n#### Exploration vs Exploitation\n```python\n# Control exploration of new strategies vs exploiting known good ones\nadaptive_chunker.set_exploration_mode(True)   # More exploration - try new strategies\nadaptive_chunker.set_exploration_mode(False)  # More exploitation - use known best\n\n# Fine-tune exploration rate\nadaptive_chunker.exploration_rate = 0.1  # 10% chance to try suboptimal strategies for learning\n```\n\n#### Session Persistence and Historical Learning\n```python\n# Adaptive chunker can persist learned knowledge across sessions\nadaptive_chunker = create_chunker(\"adaptive\",\n    persistence_file=\"my_chunking_knowledge.json\",  # Save/load learned data\n    auto_save_interval=5,                           # Save every 5 operations\n    history_size=1000,                              # Remember last 1000 operations\n)\n\n# The system automatically saves:\n# \u2705 Strategy performance statistics\n# \u2705 Content-strategy mappings\n# \u2705 Optimized parameter sets\n# \u2705 Adaptation history and patterns\n\n# On next session, it loads this data and starts with learned knowledge!\n```\n\n### Why Adaptive Chunking?\n\n**\ud83c\udfaf Use Adaptive Chunking When:**\n- Processing diverse content types (documents, logs, conversations, code)\n- Performance requirements vary by use case\n- You want optimal results without manual tuning\n- Building production systems that need to self-optimize\n- Processing large volumes where efficiency matters\n- Content characteristics change over time\n\n**\u26a1 Performance Benefits:**\n- **30-50% better chunk quality** through content-aware strategy selection\n- **20-40% faster processing** via learned parameter optimization\n- **Self-improving over time** - gets better with more usage\n- **Zero manual tuning** - adapts automatically to your data\n- **Production-ready** with persistence and error handling\n\n**\ud83d\udd2c Technical Implementation:**\nThe adaptive chunker uses multiple machine learning concepts:\n- **Content profiling** via entropy analysis, text ratios, and structure detection\n- **Multi-armed bandit algorithms** for strategy selection\n- **Reinforcement learning** from performance feedback\n- **Parameter optimization** using gradient-free methods\n- **Historical pattern recognition** for similar content matching\n\nTry the comprehensive demo to see all features in action:\n```bash\npython examples/22_adaptive_chunking_learning_demo.py\n```\n\n---\n\n## \ud83d\udd27 **Extending the Library**\n\n### Creating Custom Chunking Algorithms\nThe library provides a powerful framework for integrating your own custom algorithms with full feature support.\n\n#### Quick Custom Algorithm Example\n```python\nfrom chunking_strategy.core.base import BaseChunker, ChunkingResult, Chunk, ChunkMetadata\nfrom chunking_strategy.core.registry import register_chunker, ComplexityLevel\n\n@register_chunker(\n    name=\"word_count_chunker\",\n    category=\"text\",\n    description=\"Chunks text based on word count\",\n    complexity=ComplexityLevel.LOW,\n    default_parameters={\"words_per_chunk\": 50}\n)\nclass WordCountChunker(BaseChunker):\n    def __init__(self, words_per_chunk=50, **kwargs):\n        super().__init__(name=\"word_count_chunker\", category=\"text\", **kwargs)\n        self.words_per_chunk = words_per_chunk\n\n    def chunk(self, content, **kwargs):\n        words = content.split()\n        chunks = []\n\n        for i in range(0, len(words), self.words_per_chunk):\n            chunk_words = words[i:i + self.words_per_chunk]\n            chunk_content = \" \".join(chunk_words)\n\n            chunk = Chunk(\n                id=f\"word_chunk_{i // self.words_per_chunk}\",\n                content=chunk_content,\n                metadata=ChunkMetadata(word_count=len(chunk_words))\n            )\n            chunks.append(chunk)\n\n        return ChunkingResult(chunks=chunks, strategy_used=self.name)\n\n# Use your custom chunker\nfrom chunking_strategy import create_chunker\nchunker = create_chunker(\"word_count_chunker\", words_per_chunk=30)\nresult = chunker.chunk(\"Your text content here\")\n```\n\n#### Advanced Custom Algorithm with Streaming\n```python\nfrom chunking_strategy.core.base import StreamableChunker, AdaptableChunker\nfrom typing import Iterator, Union\n\n@register_chunker(name=\"advanced_custom\")\nclass AdvancedCustomChunker(StreamableChunker, AdaptableChunker):\n    def chunk_stream(self, content_stream: Iterator[Union[str, bytes]], **kwargs):\n        \"\"\"Enable streaming support for large files\"\"\"\n        buffer = \"\"\n        chunk_id = 0\n\n        for content_piece in content_stream:\n            buffer += content_piece\n\n            # Process when buffer reaches threshold\n            if len(buffer) >= self.buffer_threshold:\n                chunk = self.process_buffer(buffer, chunk_id)\n                yield chunk\n                chunk_id += 1\n                buffer = \"\"\n\n        # Process remaining buffer\n        if buffer:\n            chunk = self.process_buffer(buffer, chunk_id)\n            yield chunk\n\n    def adapt_parameters(self, feedback_score: float, feedback_type: str):\n        \"\"\"Enable adaptive learning from user feedback\"\"\"\n        if feedback_score < 0.5:\n            self.buffer_threshold *= 0.8  # Make chunks smaller\n        elif feedback_score > 0.8:\n            self.buffer_threshold *= 1.2  # Make chunks larger\n```\n\n### Integration Methods\n\n#### File-Based Loading\n```python\n# Save algorithm in custom_algorithms/my_algorithm.py\nfrom chunking_strategy import load_custom_algorithms\n\nload_custom_algorithms(\"custom_algorithms/\")\nchunker = create_chunker(\"my_custom_chunker\")\n```\n\n#### Configuration Integration\n```yaml\n# config.yaml\ncustom_algorithms:\n  - path: \"custom_algorithms/sentiment_chunker.py\"\n    enabled: true\n\nstrategies:\n  primary: \"sentiment_chunker\"  # Use your custom algorithm\n```\n\n#### CLI Integration\n```bash\n# Load and use custom algorithms via CLI\nchunking-strategy --custom-algorithms custom_algorithms/ chunk document.txt --strategy my_algorithm\n\n# Compare custom vs built-in algorithms\nchunking-strategy compare document.txt --strategies my_algorithm,sentence_based,fixed_size\n```\n\n### Validation and Testing Framework\n```python\nfrom chunking_strategy.core.custom_validation import CustomAlgorithmValidator\nfrom chunking_strategy.benchmarking import ChunkingBenchmark\n\n# Validate your custom algorithm\nvalidator = CustomAlgorithmValidator()\nreport = validator.validate_algorithm(\"my_custom_chunker\")\n\nprint(f\"\u2705 Validation passed: {report.is_valid}\")\nfor issue in report.issues:\n    print(f\"\u26a0\ufe0f  {issue.level}: {issue.message}\")\n\n# Performance testing\nbenchmark = ChunkingBenchmark()\nmetrics = benchmark.benchmark_strategy(\"my_custom_chunker\", \"test_document.txt\")\nprint(f\"\u23f1\ufe0f  Processing time: {metrics.processing_time:.3f}s\")\nprint(f\"\ud83c\udfc6 Quality score: {metrics.quality_score:.3f}\")\n```\n\n**For detailed custom algorithm development, see [CUSTOM_ALGORITHMS_GUIDE.md](CUSTOM_ALGORITHMS_GUIDE.md).**\n\n---\n\n## \u26a1 **Advanced Features & Best Practices**\n\n### Hardware Optimization\n```python\nfrom chunking_strategy.core.hardware import get_hardware_info\n\n# Automatic hardware detection and optimization\nhardware = get_hardware_info()\nprint(f\"\ud83d\udda5\ufe0f  CPU cores: {hardware.cpu_count}\")\nprint(f\"\ud83e\udde0 Memory: {hardware.memory_total_gb:.1f} GB\")\nprint(f\"\ud83d\udce6 Recommended batch size: {hardware.recommended_batch_size}\")\n\n# Hardware-optimized batch processing\nfrom chunking_strategy.core.batch import BatchProcessor\n\nprocessor = BatchProcessor()\nresult = processor.process_files(\n    files=document_list,\n    default_strategy=\"sentence_based\",\n    parallel_mode=\"process\",    # Multi-core processing\n    workers=None               # Auto-detected optimal count\n)\n```\n\n### Comprehensive Logging & Debugging\n```python\nimport chunking_strategy as cs\n\n# Configure detailed logging\ncs.configure_logging(\n    level=cs.LogLevel.VERBOSE,     # Show detailed operations\n    file_output=True,              # Save logs to file\n    collect_performance=True,      # Track performance metrics\n    collect_metrics=True           # Track quality metrics\n)\n\n# Enable debug mode for troubleshooting\ncs.enable_debug_mode()\n\n# Generate debug archive for bug reports\ndebug_archive = cs.create_debug_archive(\"Description of the issue\")\nprint(f\"\ud83d\udc1b Debug archive: {debug_archive['archive_path']}\")\n# Share this file for support\n\n# Quick debugging examples\ncs.user_info(\"Processing started\")              # User-friendly messages\ncs.debug_operation(\"chunk_processing\", {\"chunks\": 42})  # Developer details\ncs.performance_log({\"time\": 1.23, \"memory\": \"45MB\"})    # Performance tracking\n```\n\n**CLI Debugging:**\n```bash\n# Enable debug mode with detailed logging\nchunking-strategy --debug --log-level verbose chunk document.pdf\n\n# Collect debug information\nchunking-strategy debug collect --description \"PDF processing issue\"\n\n# Test logging configuration\nchunking-strategy debug test-logging\n```\n\n### Quality Assessment & Adaptive Learning\n```python\n# Adaptive chunker learns from feedback\nadaptive_chunker = create_chunker(\"adaptive\")\nresult = adaptive_chunker.chunk(\"document.pdf\")\n\n# Simulate user feedback (0.0 = poor, 1.0 = excellent)\nuser_satisfaction = 0.3  # Poor results\nadaptive_chunker.adapt_parameters(user_satisfaction, \"quality\")\n\n# The chunker automatically adjusts its parameters for better results\nresult2 = adaptive_chunker.chunk(\"document2.pdf\")  # Should perform better\n```\n\n### Error Handling with Fallbacks\n```python\ndef robust_chunking(file_path, strategies=None):\n    \"\"\"Chunk with automatic fallback strategies.\"\"\"\n    if strategies is None:\n        strategies = [\"pdf_chunker\", \"sentence_based\", \"paragraph_based\", \"fixed_size\"]\n\n    for strategy in strategies:\n        try:\n            chunker = create_chunker(strategy)\n            result = chunker.chunk(file_path)\n            cs.user_success(f\"\u2705 Successfully processed with {strategy}\")\n            return result\n        except Exception as e:\n            cs.user_warning(f\"\u26a0\ufe0f  Strategy {strategy} failed: {e}\")\n            continue\n\n    raise Exception(\"\u274c All chunking strategies failed\")\n\n# Guaranteed to work with automatic fallbacks\nresult = robust_chunking(\"any_document.pdf\")\n```\n\n**For comprehensive debugging instructions, see [DEBUGGING_GUIDE.md](DEBUGGING_GUIDE.md).**\n\n---\n\n## \ud83c\udfd7\ufe0f **Integration Examples**\n\n### \ud83d\ude80 **Complete Integration Demos Available!**\n\nWe provide **comprehensive, production-ready demo applications** for major frameworks:\n\n| **Framework** | **Demo File** | **Features** | **Run Command** |\n|---------------|---------------|--------------|-----------------|\n| **\ud83e\udd9c LangChain** | [`examples/18_langchain_integration_demo.py`](examples/18_langchain_integration_demo.py) | RAG pipelines, vector stores, QA chains, embeddings | `python examples/18_langchain_integration_demo.py` |\n| **\ud83c\udf88 Streamlit** | [`examples/19_streamlit_app_demo.py`](examples/19_streamlit_app_demo.py) | Web UI, file uploads, real-time chunking, **performance metrics** | `streamlit run examples/19_streamlit_app_demo.py` |\n| **\u26a1 Performance Metrics** | [`examples/21_metrics_and_performance_demo.py`](examples/21_metrics_and_performance_demo.py) | Strategy benchmarking, memory tracking, performance analysis | `python examples/21_metrics_and_performance_demo.py` |\n| **\ud83d\udd27 Integration Helpers** | [`examples/integration_helpers.py`](examples/integration_helpers.py) | Utility functions for any framework | `from examples.integration_helpers import ChunkingFrameworkAdapter` |\n\n### With Vector Databases\n\n```python\nfrom chunking_strategy import create_chunker\nimport weaviate  # or qdrant, pinecone, etc.\n\n# Chunk documents\nchunker = create_chunker(\"sentence_based\", max_sentences=3)\nresult = chunker.chunk(\"document.pdf\")\n\n# Store in vector database\nclient = weaviate.Client(\"http://localhost:8080\")\n\nfor chunk in result.chunks:\n    client.data_object.create(\n        {\n            \"content\": chunk.content,\n            \"source\": chunk.metadata.source,\n            \"page\": chunk.metadata.page,\n            \"chunk_id\": chunk.id\n        },\n        \"Document\"\n    )\n```\n\n### With LangChain (Quick Example)\n\n```python\nfrom chunking_strategy import create_chunker\nfrom langchain.schema import Document\n\n# Chunk with our library\nchunker = create_chunker(\"paragraph_based\", max_paragraphs=2)\nresult = chunker.chunk(\"document.pdf\")\n\n# Convert to LangChain documents\nlangchain_docs = [\n    Document(\n        page_content=chunk.content,\n        metadata={\n            \"source\": chunk.metadata.source,\n            \"page\": chunk.metadata.page,\n            \"chunk_id\": chunk.id\n        }\n    )\n    for chunk in result.chunks\n]\n```\n\n**\ud83c\udfaf For complete LangChain integration** including RAG pipelines, embeddings, and QA chains, see [`examples/18_langchain_integration_demo.py`](examples/18_langchain_integration_demo.py).\n\n### With Streamlit (Quick Example)\n\n```python\nimport streamlit as st\nfrom chunking_strategy import create_chunker, list_strategies\n\nst.title(\"Document Chunking App\")\n\n# Strategy selection from all available strategies\nstrategy = st.selectbox(\"Chunking Strategy\", list_strategies())\n\n# File upload\nuploaded_file = st.file_uploader(\"Choose a file\")\n\nif uploaded_file and st.button(\"Process\"):\n    chunker = create_chunker(strategy)\n    result = chunker.chunk(uploaded_file)\n\n    st.success(f\"Created {len(result.chunks)} chunks using {strategy}\")\n\n    # Display chunks with metadata\n    for i, chunk in enumerate(result.chunks):\n        with st.expander(f\"Chunk {i+1} ({len(chunk.content)} chars)\"):\n            st.text(chunk.content)\n            st.json(chunk.metadata.__dict__)\n```\n\n**\ud83c\udfaf For a complete Streamlit app** with file uploads, real-time processing, visualizations, **comprehensive performance metrics dashboard**, see [`examples/19_streamlit_app_demo.py`](examples/19_streamlit_app_demo.py).\n\n---\n\n## \ud83d\ude80 **Performance & Hardware Optimization**\n\n### Automatic Hardware Detection\n\n```python\nfrom chunking_strategy.core.hardware import get_hardware_info\n\n# Check your system capabilities\nhardware = get_hardware_info()\nprint(f\"CPU cores: {hardware.cpu_count}\")\nprint(f\"Memory: {hardware.memory_total_gb:.1f} GB\")\nprint(f\"GPUs: {hardware.gpu_count}\")\nprint(f\"Recommended batch size: {hardware.recommended_batch_size}\")\n```\n\n### Optimized Batch Processing\n\n```python\nfrom chunking_strategy.core.batch import BatchProcessor\n\nprocessor = BatchProcessor()\n\n# Automatic optimization based on your hardware\nresult = processor.process_files(\n    files=file_list,\n    default_strategy=\"universal_document\",\n    parallel_mode=\"process\",  # or \"thread\", \"sequential\"\n    workers=None,  # Auto-detected\n    batch_size=None  # Auto-detected\n)\n```\n\n### Performance Monitoring\n\n```python\nfrom chunking_strategy.core.metrics import ChunkingQualityEvaluator\n\n# Evaluate chunk quality\nevaluator = ChunkingQualityEvaluator()\nmetrics = evaluator.evaluate(result.chunks)\n\nprint(f\"Quality Score: {metrics.coherence:.3f}\")\nprint(f\"Size Consistency: {metrics.size_consistency:.3f}\")\nprint(f\"Coverage: {metrics.coverage:.3f}\")\n```\n\n---\n\n## \ud83d\udd27 **Installation Options**\n\n### Feature-Specific Installation\n\n```bash\n# Basic text processing\npip install chunking-strategy\n\n# PDF processing\npip install chunking-strategy[document]\n\n# Hardware optimization\npip install chunking-strategy[hardware]\n\n# Universal document support (Apache Tika)\npip install chunking-strategy[tika]\n\n# Vector database integrations\npip install chunking-strategy[vectordb]\n\n# Everything included\npip install chunking-strategy[all]\n```\n\n### Dependencies by Feature\n\n| Feature | Dependencies | Description |\n|---------|-------------|-------------|\n| `document` | PyMuPDF, PyPDF2, pdfminer.six | PDF processing with multiple backends |\n| `hardware` | psutil, GPUtil | Hardware detection and optimization |\n| `tika` | tika, python-magic | Universal document processing |\n| `text` | spacy, nltk, sentence-transformers>=5.1.0, huggingface-hub | Advanced text processing + embeddings |\n| `vectordb` | qdrant-client, weaviate-client | Vector database integrations |\n\n---\n\n## \ud83c\udfaf **Use Cases**\n\n### RAG (Retrieval-Augmented Generation)\n\n```python\n# Optimal for RAG systems\nchunker = create_chunker(\n    \"sentence_based\",\n    max_sentences=3,      # Good balance of context and specificity\n    overlap_sentences=1   # Overlap for better retrieval\n)\n```\n\n### Vector Database Indexing\n\n```python\n# Consistent sizes for vector DBs\nchunker = create_chunker(\n    \"fixed_size\",\n    chunk_size=512,      # Fits most embedding models\n    overlap_size=50      # Prevents information loss at boundaries\n)\n```\n\n### \ud83d\udd2e **Embeddings & Vector Database Integration**\n\n**Complete workflow from chunking \u2192 embeddings \u2192 vector database:**\n\n```python\nfrom chunking_strategy.orchestrator import ChunkerOrchestrator\nfrom chunking_strategy.core.embeddings import (\n    EmbeddingConfig, EmbeddingModel, OutputFormat,\n    embed_chunking_result, export_for_vector_db\n)\n\n# Step 1: Chunk your documents\norchestrator = ChunkerOrchestrator()\nchunks = orchestrator.chunk_file(\"document.pdf\")\n\n# Step 2: Generate embeddings with multiple model options\nconfig = EmbeddingConfig(\n    model=EmbeddingModel.ALL_MINILM_L6_V2,    # Fast & lightweight (384D)\n    # model=EmbeddingModel.ALL_MPNET_BASE_V2,  # High quality (768D)\n    # model=EmbeddingModel.CLIP_VIT_B_32,      # Multimodal (512D)\n    output_format=OutputFormat.FULL_METADATA,  # Include all metadata\n    batch_size=32,\n    normalize_embeddings=True\n)\n\nembedding_result = embed_chunking_result(chunks, config)\nprint(f\"\u2705 Generated {embedding_result.total_chunks} embeddings ({embedding_result.embedding_dim}D)\")\n\n# Step 3: Export ready for vector databases\nvector_data = export_for_vector_db(embedding_result, format=\"dict\")\n# Now ready for Qdrant, Weaviate, Pinecone, ChromaDB!\n```\n\n**\ud83d\udd11 HuggingFace Authentication Setup:**\n\n1. **Get your token**: Visit https://huggingface.co/settings/tokens\n2. **Method 1 - Config file**:\n   ```bash\n   cp config/huggingface_token.py.template config/huggingface_token.py\n   # Edit and add your token: HUGGINGFACE_TOKEN = \"hf_your_token_here\"\n   ```\n\n3. **Method 2 - Environment variable**:\n   ```bash\n   export HF_TOKEN=\"hf_your_token_here\"\n   ```\n\n**Supported Embedding Models:**\n\n| Model | Dimensions | Use Case | Speed |\n|-------|------------|----------|-------|\n| `ALL_MINILM_L6_V2` | 384 | Fast development, testing | \u26a1\u26a1\u26a1 |\n| `ALL_MPNET_BASE_V2` | 768 | High quality | \u26a1\u26a1 |\n| `ALL_DISTILROBERTA_V1` | 768 | Code embeddings | \u26a1\u26a1 |\n| `CLIP_VIT_B_32` | 512 | Text + images | \u26a1 |\n\n**CLI Embeddings:**\n\n```bash\n# Generate embeddings for all files in directory\nchunking-strategy embed documents/ --model all-MiniLM-L6-v2 --output-format full_metadata\n\n# Batch process with specific settings\nchunking-strategy embed-batch data/ --batch-size 64 --normalize\n```\n\n### Document Analysis & Summarization\n\n```python\n# Preserve document structure\nchunker = create_chunker(\n    \"paragraph_based\",\n    max_paragraphs=2,\n    preserve_structure=True\n)\n```\n\n### Multi-Format Document Processing\n\n```python\n# Handle any file type\nchunker = create_chunker(\n    \"universal_document\",\n    chunk_size=1000,\n    extract_metadata=True,\n    preserve_structure=True\n)\n```\n\n---\n\n## \ud83d\udcca **Quality & Metrics**\n\n### Built-in Quality Evaluation\n\n```python\nfrom chunking_strategy.core.metrics import ChunkingQualityEvaluator\n\nchunker = create_chunker(\"sentence_based\", max_sentences=3)\nresult = chunker.chunk(\"document.pdf\")\n\n# Evaluate quality\nevaluator = ChunkingQualityEvaluator()\nmetrics = evaluator.evaluate(result.chunks)\n\nprint(f\"Size Consistency: {metrics.size_consistency:.3f}\")\nprint(f\"Semantic Coherence: {metrics.coherence:.3f}\")\nprint(f\"Content Coverage: {metrics.coverage:.3f}\")\nprint(f\"Boundary Quality: {metrics.boundary_quality:.3f}\")\n```\n\n### Adaptive Optimization\n\n```python\n# Chunkers can adapt based on feedback\nchunker = create_chunker(\"fixed_size\", chunk_size=1000)\n\n# Simulate quality feedback\nchunker.adapt_parameters(0.3, \"quality\")  # Low quality score\n# Chunker automatically adjusts parameters for better quality\n```\n\n---\n\n## \ud83d\udee0\ufe0f **Advanced Features**\n\n### Custom Chunking Strategy\n\n```python\nfrom chunking_strategy.core.base import BaseChunker\nfrom chunking_strategy.core.registry import register_chunker\n\n@register_chunker(name=\"custom_chunker\", category=\"custom\")\nclass CustomChunker(BaseChunker):\n    def chunk(self, content, **kwargs):\n        # Your custom chunking logic\n        chunks = self.custom_logic(content)\n        return ChunkingResult(chunks=chunks)\n\n# Use your custom chunker\nchunker = create_chunker(\"custom_chunker\")\n```\n\n### Pipeline Processing\n\n```python\nfrom chunking_strategy.core.pipeline import ChunkingPipeline\n\npipeline = ChunkingPipeline([\n    (\"preprocessing\", preprocessor),\n    (\"chunking\", chunker),\n    (\"postprocessing\", postprocessor),\n    (\"quality_check\", quality_evaluator)\n])\n\nresult = pipeline.process(\"document.pdf\")\n```\n\n### Streaming for Large Files\n\n```python\n# Memory-efficient processing of large files\nchunker = create_chunker(\"fixed_size\", chunk_size=1000)\n\ndef file_stream():\n    with open(\"huge_file.txt\", 'r') as f:\n        for line in f:\n            yield line\n\n# Process without loading entire file into memory\nfor chunk in chunker.chunk_stream(file_stream()):\n    process_chunk(chunk)\n```\n\n---\n\n## \ud83d\udd0d **Error Handling & Debugging**\n\n### Robust Error Handling\n\n```python\ndef safe_chunking(file_path, strategies=None):\n    \"\"\"Chunk with fallback strategies.\"\"\"\n    if strategies is None:\n        strategies = [\"sentence_based\", \"paragraph_based\", \"fixed_size\"]\n\n    for strategy in strategies:\n        try:\n            chunker = create_chunker(strategy)\n            return chunker.chunk(file_path)\n        except Exception as e:\n            print(f\"Strategy {strategy} failed: {e}\")\n            continue\n\n    raise Exception(\"All chunking strategies failed\")\n\nresult = safe_chunking(\"document.pdf\")\n```\n\n### Logging and Monitoring\n\n```python\nimport logging\n\n# Enable detailed logging\nlogging.basicConfig(level=logging.INFO)\n\n# Chunking operations will now log detailed information\nchunker = create_chunker(\"sentence_based\")\nresult = chunker.chunk(\"document.pdf\")\n\n# Monitor performance\nprint(f\"Processing time: {result.processing_time:.3f}s\")\nprint(f\"Chunks created: {len(result.chunks)}\")\nprint(f\"Average chunk size: {sum(len(c.content) for c in result.chunks) / len(result.chunks):.1f}\")\n```\n\n---\n\n## \ud83d\udcda **API Reference**\n\n### Core Functions\n\n```python\n# Create chunkers\ncreate_chunker(strategy_name, **params) -> BaseChunker\n\n# List available strategies\nlist_chunkers() -> List[str]\n\n# Get chunker metadata\nget_chunker_metadata(strategy_name) -> ChunkerMetadata\n\n# Configuration-driven processing\nChunkerOrchestrator(config_path) -> orchestrator\n```\n\n### Chunking Results\n\n```python\n# ChunkingResult attributes\nresult.chunks          # List[Chunk]\nresult.processing_time  # float\nresult.strategy_used    # str\nresult.source_info     # Dict[str, Any]\nresult.total_chunks    # int\n\n# Chunk attributes\nchunk.id              # str\nchunk.content         # str\nchunk.modality        # ModalityType\nchunk.metadata        # ChunkMetadata\n```\n\n### Hardware Optimization\n\n```python\n# Hardware detection\nget_hardware_info() -> HardwareInfo\n\n# Batch configuration\nget_optimal_batch_config(total_files, avg_file_size_mb) -> Dict\n\n# Batch processing\nBatchProcessor().process_files(files, strategy, **options) -> BatchResult\n```\n\n---\n\n## \ud83e\udd1d **Contributing**\n\nWe welcome contributions! Please feel free to submit a Pull Request or open an issue for:\n- Bug fixes and improvements\n- New chunking strategies\n- Documentation improvements\n- Performance optimizations\n\n### Development Setup\n\n```bash\ngit clone https://github.com/sharanharsoor/chunking.git\ncd chunking\npip install -e .[dev,all]\npytest tests/\n```\n\n---\n\n## \ud83d\udcc4 **License**\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n\n---\n\n## \ud83d\udd17 **Links**\n\n- **Repository**: [GitHub repository](https://github.com/sharanharsoor/chunking)\n- **PyPI**: [Package on PyPI](https://pypi.org/project/chunking-strategy/)\n- **Issues**: [Bug reports and feature requests](https://github.com/sharanharsoor/chunking/issues)\n\n### \ud83d\udcda **Demo Applications**\n\n- **\ud83e\udd9c LangChain Integration**: [`examples/18_langchain_integration_demo.py`](examples/18_langchain_integration_demo.py) - Complete RAG pipeline demo\n- **\ud83c\udf88 Streamlit Web App**: [`examples/19_streamlit_app_demo.py`](examples/19_streamlit_app_demo.py) - Interactive web interface with performance metrics\n- **\ud83d\udd27 Integration Helpers**: [`examples/integration_helpers.py`](examples/integration_helpers.py) - Utility functions for any framework\n- **\ud83d\udcd6 Helper Usage Guide**: [`examples/20_using_integration_helpers.py`](examples/20_using_integration_helpers.py) - How to use integration utilities\n- **\u26a1 Performance Metrics**: [`examples/21_metrics_and_performance_demo.py`](examples/21_metrics_and_performance_demo.py) - Comprehensive benchmarking and performance analysis\n- **\ud83e\udde0 Adaptive Learning**: [`examples/22_adaptive_chunking_learning_demo.py`](examples/22_adaptive_chunking_learning_demo.py) - AI-powered adaptive chunking with machine learning\n- **\ud83d\udcc2 All Examples**: [Browse all examples](examples/) - 20+ demos and tutorials\n\n**\ud83d\ude80 Quick Start with Demos:**\n```bash\n# Install with all integration dependencies\npip install chunking-strategy[all]\n\n# Or install specific integrations only:\n# pip install chunking-strategy[streamlit]    # For Streamlit app\n# pip install chunking-strategy[langchain]    # For LangChain integration\n# pip install chunking-strategy[text,document] # For basic functionality\n\n# Run the interactive Streamlit app\nstreamlit run examples/19_streamlit_app_demo.py\n\n# Or run the LangChain integration demo\npython examples/18_langchain_integration_demo.py\n\n# Or explore adaptive learning capabilities\npython examples/22_adaptive_chunking_learning_demo.py\n```\n\n---\n\n**Ready to transform your document processing?** Install now and start chunking smarter! \ud83d\ude80\n\n```bash\npip install chunking-strategy[all]\n```\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A comprehensive chunking library for text, documents, audio, video, and data streams (Linux and macOS only)",
    "version": "0.4.1",
    "project_urls": {
        "Changelog": "https://github.com/sharanharsoor/chunking/blob/main/CHANGELOG.md",
        "Homepage": "https://github.com/sharanharsoor/chunking",
        "Issues": "https://github.com/sharanharsoor/chunking/issues",
        "Repository": "https://github.com/sharanharsoor/chunking"
    },
    "split_keywords": [
        "chunking",
        " text-processing",
        " document-processing",
        " audio-processing",
        " video-processing",
        " data-streams",
        " content-defined-chunking",
        " semantic-chunking",
        " rag"
    ],
    "urls": [
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "befcc3e80e49c7e51bd6fd03a921bdb881b6f12970e6878c9fe4741aec6d6521",
                "md5": "057fc3d39a1e7c1d33d036d01f2fc55d",
                "sha256": "6fac006e5d64a94f1a49e1f7fd4c8df1ca049486a16b67639b6cc6054130004c"
            },
            "downloads": -1,
            "filename": "chunking_strategy-0.4.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "057fc3d39a1e7c1d33d036d01f2fc55d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 485818,
            "upload_time": "2025-10-10T11:26:47",
            "upload_time_iso_8601": "2025-10-10T11:26:47.870586Z",
            "url": "https://files.pythonhosted.org/packages/be/fc/c3e80e49c7e51bd6fd03a921bdb881b6f12970e6878c9fe4741aec6d6521/chunking_strategy-0.4.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c65892d1625781b92cb66714fb444ca7e353c94589414c68762e81d9ce7d42bf",
                "md5": "534e90a3e4fae11e35446ca07a72b35e",
                "sha256": "649618bbcaa53561fb02ce8a97f9d62b8476dc9914b2e875b21636a575b3ceaf"
            },
            "downloads": -1,
            "filename": "chunking_strategy-0.4.1.tar.gz",
            "has_sig": false,
            "md5_digest": "534e90a3e4fae11e35446ca07a72b35e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 442259,
            "upload_time": "2025-10-10T11:26:49",
            "upload_time_iso_8601": "2025-10-10T11:26:49.723503Z",
            "url": "https://files.pythonhosted.org/packages/c6/58/92d1625781b92cb66714fb444ca7e353c94589414c68762e81d9ce7d42bf/chunking_strategy-0.4.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-10-10 11:26:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sharanharsoor",
    "github_project": "chunking",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "chunking-strategy"
}
        
Elapsed time: 3.56799s