A semantic narrative chunker for novels.
Project description
Narrative Chunker
Narrative Chunker is a semantic NLP metadata orchestration library designed to prepare documents for high-quality Vector indexing.
Instead of arbitrarily slicing documents by strict token limits—which often cuts sentences in half and ruins semantic meaning—Narrative Chunker intelligently isolates content by boundaries (like punctuation and paragraphs). Furthermore, it provides native metadata injection, automatically stamping arbitrary context dictionaries directly onto the resulting AST nodes.
Features
- ✂️ Semantic Splitting: Preserves sentence boundaries and narrative flow using customizable sizes and overlaps.
- 📦 Metadata Orchestration: Pass a custom python Dictionary, and the chunker will deterministically stamp it onto every resulting node without requiring you to manually loop over trees.
- 🔌 Interface Agnostic: Override the default
SentenceSplitterandSimpleDirectoryReaderby pushing your own parsers directly into the API (e.g., PyPDF or MarkdownNodeParser).
Installation
pip install narrative-chunker
Quick Start
1. Basic Narrative Splitting
Parse a physical file natively:
from narrative_chunker import NarrativeChunker
chunker = NarrativeChunker(chunk_size=1024, chunk_overlap=200)
nodes = chunker.parse_and_chunk(file_path="novel.epub", filename="novel.epub")
print(f"Generated {len(nodes)} perfectly sliced context nodes.")
2. Multi-Tenant Metadata Injection
Easily attach user identifiers or domain context straight into the LlamaIndex schema for downstream Vector filtering:
context = {
"tenant_id": "ABC-123",
"access_level": "confidential"
}
nodes = chunker.parse_and_chunk(
file_path="contract.pdf",
filename="contract.pdf",
metadata=context
)
# Every extracted chunk natively contains {'tenant_id': 'ABC-123', ...} in its metadata!
3. Customizable Chunking Algorithms
By default, NarrativeChunker uses a robust SentenceSplitter. However, you can seamlessly inject any LlamaIndex NodeParser algorithm into the constructor to adapt to your specific domain (e.g., Semantic Chunking, Token Splitting, or Markdown Splitting):
from narrative_chunker import NarrativeChunker
from llama_index.core.node_parser import SemanticSplitterNodeParser
from llama_index.embeddings.openai import OpenAIEmbedding
# 1. Initialize a state-of-the-art semantic AI chunker
embed_model = OpenAIEmbedding(model="text-embedding-3-small", embed_batch_size=100)
semantic_parser = SemanticSplitterNodeParser(
buffer_size=1, breakpoint_percentile_threshold=95, embed_model=embed_model
)
# 2. Inject the semantic algorithm into the Narrative orchestrator
chunker = NarrativeChunker(parser=semantic_parser)
# 3. All chunks will now be intelligently sliced by semantic cosine-similarity!
nodes = chunker.parse_and_chunk(file_path="dune.epub", filename="dune.epub")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file narrative_chunker-0.1.0.tar.gz.
File metadata
- Download URL: narrative_chunker-0.1.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
15578854cf7f7838d71daef444b4e0da2ef54a29cddaabacdffe967fbc0d128a
|
|
| MD5 |
2a2afdc010a152af65243e532be7acd4
|
|
| BLAKE2b-256 |
77e160b446d03f9d476bc18bc5eadfa2d871273e39a4a40044b938dee1aed168
|
File details
Details for the file narrative_chunker-0.1.0-py3-none-any.whl.
File metadata
- Download URL: narrative_chunker-0.1.0-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fb0ad30fdcfde1646404264bcee1fc448a05919e259698f53d3141c07ba58787
|
|
| MD5 |
0c416727032c2dcd6bb987e7aec53719
|
|
| BLAKE2b-256 |
0fa537f6f911f70169758fbd2c1b2cb94e68ea9e16b2e4d69a7c4abf04e439d8
|