Annotation-Based Chunking
Generates ground-truth chunk boundaries from annotation positions using Voronoi midpoints, for benchmarking semantic chunking strategies.
What this file does
Generates ground-truth chunk boundaries from annotation positions using Voronoi midpoints, for benchmarking semantic chunking strategies.
When to use it
- You need reference chunks to evaluate semantic chunking algorithms
- You have annotated text data and want language-agnostic chunk boundaries
- You want reproducible, idempotent chunking with provenance tracking
- You need multiple expansion ratios for context sensitivity testing
Assumes this stack
Annotation-Based Chunking
Ground-truth chunking for benchmarking using Voronoi boundaries with word alignment.
Purpose
Create reference chunks based on actual annotation positions to benchmark semantic chunking strategies. Uses pure geometric algorithm (no NLP) - language-agnostic.
Algorithm
Voronoi Boundaries: Each annotation gets exclusive territory via midpoints to neighbors.
Text: [annotation1] [annotation2] [annotation3]
Territory: |----T1----| |----T2----| |----T3----|
Boundary: 0 mid1 mid1 mid2 mid2 end
Word Alignment: Boundaries adjusted to complete words (no mid-word cutting).
Expansion Ratios:
0.0- Annotation only (minimal context)0.5- 50% of territory (balanced)1.0- Full territory (maximal context)
Usage
# Process all PhenoBERT data
python scripts/generate_chunking_variants.py \
--input-dir tests/data/en/phenobert \
--pattern "*/annotations/*.json"
# Single file
python scripts/generate_chunking_variants.py \
--input tests/data/en/phenobert/GSC_plus/annotations/file.json
# Custom ratios
python scripts/generate_chunking_variants.py \
--input-dir tests/data/en/phenobert \
--expansion-ratios 0.0 0.25 0.5 0.75 1.0
# Dry run (preview)
python scripts/generate_chunking_variants.py \
--input-dir tests/data/en/phenobert \
--dry-run
Output Format
Augments JSON files in-place with chunk_variants field:
{
"doc_id": "GSC+_1003450",
"full_text": "...",
"annotations": [...],
"chunk_variants": {
"voronoi_v1": {
"provenance": {
"script_version": "1.0.0",
"generated_at": "2025-11-18T14:22:42Z",
"parameters": {
"expansion_ratios": [0.0, 0.5, 1.0],
"strategy": "voronoi_midpoint"
}
},
"chunks": [
{
"hpo_id": "HP:0001156",
"annotation_span": [14, 27],
"variants": {
"0.00": {"text": "brachydactyly ", "span": [14, 28]},
"0.50": {"text": "syndrome of brachydactyly and...", "span": [2, 45]},
"1.00": {"text": "A syndrome of brachydactyly...", "span": [0, 71]}
}
}
]
}
}
}
Properties
- ✅ Idempotent - Re-run produces identical output
- ✅ Word-aligned - No mid-word cutting
- ✅ Language-agnostic - Works with any alphabet
- ✅ Reproducible - Full provenance tracking
- ✅ Concept isolation - No annotation overlap
Testing
# Run script tests
make test-scripts
# Run with coverage
pytest scripts/tests/ -v --cov=scripts
# All tests
make test-all
Implementation
scripts/annotation_chunker.py- Core algorithm (100% coverage)scripts/generate_chunking_variants.py- CLI script (97% coverage)scripts/shared_utils.py- Shared utilitiesscripts/tests/- 40 tests, all passing
Limitations
Skips files with overlapping annotations (23% of PhenoBERT dataset). Overlapping annotations violate the Voronoi concept isolation principle and must be manually reviewed.
Example error:
ERROR - Overlapping annotations detected: annotation 2 overlaps with annotation 3
These files need annotation correction before chunking.
What's inside
Algorithm description, CLI usage, output JSON schema, properties list, testing commands, implementation file list, and limitations section
Change this for your project
- Replace
tests/data/en/phenobertwith your own annotation data path - Replace
scripts/annotation_chunker.pywith your actual implementation path - Replace
make test-scriptswith your project's test command
Where it goes
Keep it in your repository where the agent or team that needs it will read it.
Worth borrowing
- Voronoi midpoint algorithm for non-overlapping territory assignment
- Expansion ratios to produce multiple chunk variants from one annotation set
- Provenance metadata embedded in output for reproducibility
Related Documents
基于命题分块以增强RAG
Implements proposition chunking for RAG: decomposes documents into atomic facts, evaluates quality, and compares retrieval against standard chunking.
TileMap Chunk Manager
Divides a large tilemap into fixed-size chunks, loading and rendering only those visible to reduce memory and draw calls by over 99%.
Message Chunking for MCP stdio Transport
Explains macOS 64KB pipe write limit and provides a chunking layer for MCP stdio transport that splits and reassembles large messages transparently.
🤖 n8n AI Agent Mastery Course 2025
Serves as the main README for an n8n course repository, listing 10+ AI agent workflow projects with links to YouTube tutorials and Docker setup instructions.