Skip to main content

HansChunks

English | 中文

High-performance Chinese document extractor and semantic chunker built in Rust with Python bindings.

Features

  • Intelligent Text Chunking: Splits Chinese documents into semantically meaningful chunks while preserving context
  • Element Recognition: Identifies different types of document elements (headings, paragraphs, lists, code blocks, etc.)
  • Semantic Boundary Preservation: Avoids breaking text at poor split points like colons
  • Heading Merging: Option to keep headings with their content
  • Customizable Configuration: Adjust chunk sizes and element weights to optimize for your specific use case
  • High Performance: Implemented in Rust with optimized algorithms for speed and efficiency
  • Python Bindings: Easy to use from Python with a simple, intuitive API
  • Advanced Algorithm: Uses dynamic programming with binary search optimization to find optimal split points, ensuring both efficiency and semantic coherence
  • Context-Aware Processing: Considers document structure, element types, and semantic connections when making chunking decisions

Installation

pip install hanschunks

Quick Start

from hanschunks.hanschunks import TextChunker, ChunkConfig

# Create a chunker with default settings
chunker = TextChunker()

# Process a document
text = """第一章 引言

随着人工智能技术的快速发展,自然语言处理已经成为计算机科学中最重要的研究领域之一。
文本分块作为信息检索和知识管理的基础技术,其重要性日益凸显。
"""

chunks = chunker.chunk(text)
for i, chunk in enumerate(chunks, 1):
    print(f"Chunk {i}: {chunk[:50]}...")

Custom Configuration

# Create custom configuration
config = ChunkConfig()
config.min_size = 160        # Minimum chunk size in characters
config.max_size = 320        # Maximum chunk size in characters
config.merge_headings = True # Merge headings with following content
config.preserve_boundaries = True # Preserve semantic boundaries

# Set element weights for chunking decisions
config.set_element_weights(
    heading_base=100.0,       # Base weight for headings
    heading_level_penalty=10.0, # Penalty per heading level
    code_block=80.0,          # Weight for code blocks
    table=80.0,               # Weight for tables
    list_item=60.0,           # Weight for list items
    paragraph=40.0,           # Weight for paragraphs
    quote=30.0,               # Weight for block quotes
    empty=10.0,               # Weight for empty lines
    footer=0.0                # Weight for footer elements
)

# Create chunker with custom config
chunker = TextChunker(config)

Algorithm

HansChunks uses an optimized dynamic programming algorithm to find the best possible split points in a document:

  1. Document is first parsed into semantic elements (headings, paragraphs, etc.)
  2. Each element is assigned a weight based on its type
  3. Dynamic programming with binary search optimization finds optimal split points
  4. Strong semantic connections are preserved (e.g., avoiding splits after colons)
  5. The result is a set of chunks that balance size constraints with semantic coherence

Development

Prerequisites

  • Rust toolchain (1.75+)
  • Python 3.12+
  • Maturin (for building Python bindings)

Build develop package

uv run maturin develop --release
uv run example/demo.py

Building from source

uv run maturin build --release --out dist 
uv add dist/hanschunks-*.whl

Running tests

cargo test

License

Apache 2.0

Release files for hanschunks 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hanschunks 0.1.0
File Size Uploaded
hanschunks-0.1.0.tar.gz 31.4 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for hanschunks 0.1.0
File
hanschunks-0.1.0-cp312-abi3-win_amd64.whl CPython 3.12 abi3 Windows x86-64 Details
hanschunks-0.1.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.12 abi3 Linux glibc 2.17+ x86-64 Details
hanschunks-0.1.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.12 abi3 Linux glibc 2.17+ ARM64 Details
hanschunks-0.1.0-cp312-abi3-macosx_11_0_arm64.whl CPython 3.12 abi3 macOS 11.0+ ARM64 Details
hanschunks-0.1.0-cp312-abi3-macosx_10_12_x86_64.whl CPython 3.12 abi3 macOS 10.12+ x86-64 Details

Total release size: 13.3 MB

Release files / hanschunks-0.1.0.tar.gz

Download URL hanschunks-0.1.0.tar.gz
Size 31.4 kB
Tags Source
SHA-256 checksum
How to use checksums
15f2c9eefe9720418868395f1976a72d4679eb535bacf37babe3afa0cbfc0d2c
BLAKE2b-256 checksum
How to use checksums
036975918709cd8fd10d03c88cb126100f99423915421793ac811e9b1971685e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.6

Release files / hanschunks-0.1.0-cp312-abi3-win_amd64.whl

Download URL hanschunks-0.1.0-cp312-abi3-win_amd64.whl
Size 2.6 MB
Tags CPython 3.12 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
a36a4668cff36cf1177a369caf9e7ec468ea44a34b34cb403bf9dafda97b6538
BLAKE2b-256 checksum
How to use checksums
deeb40ebe2e9aeef58032452fdae23780396ab2518162a9f7de08551a69a4af1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.6

Release files / hanschunks-0.1.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL hanschunks-0.1.0-cp312-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 2.7 MB
Tags CPython 3.12 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
a73732279bbe95a4d5bebeea6794d1a67b8e6233490b705661604667dc492cfd
BLAKE2b-256 checksum
How to use checksums
d1be69268a5b3bddf5d71025ea37445b8ef4cf09f2279851a17556caa3429a9b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.6

Release files / hanschunks-0.1.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL hanschunks-0.1.0-cp312-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 2.7 MB
Tags CPython 3.12 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
e0aa5c808383136b4fb57605fd39653d56e4441e03bbc882d57a80cab06663b7
BLAKE2b-256 checksum
How to use checksums
23af08956c32074288dccfae49849a6049d23e76a06098f7942c43f58ef5dfa8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.6

Release files / hanschunks-0.1.0-cp312-abi3-macosx_11_0_arm64.whl

Download URL hanschunks-0.1.0-cp312-abi3-macosx_11_0_arm64.whl
Size 2.6 MB
Tags CPython 3.12 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
eddde19de56be270d6966a7c6bf87eebea27ded464155943dce4a288c16f699c
BLAKE2b-256 checksum
How to use checksums
d08698b36289be3dba2152e738c42839ec63bc449ad9b389b49a24dadaf9873f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.6

Release files / hanschunks-0.1.0-cp312-abi3-macosx_10_12_x86_64.whl

Download URL hanschunks-0.1.0-cp312-abi3-macosx_10_12_x86_64.whl
Size 2.7 MB
Tags CPython 3.12 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
3a24376d9661201fbb74b8ba8d673a8afa8256960f67b581060777ad048a01ae
BLAKE2b-256 checksum
How to use checksums
636bd9d32ab893e8cebfd3b46567835c9eb5a8082f7e126cf126f0e7f83a6329
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via maturin/1.8.6

Release history Release notifications | RSS feed

This release

0.1.0 This release

6 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page