Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
## Changelog 🔄
All notable changes to semchunk will be documented here. This project adheres to [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Fixed chunks of Hindi, Marathi, Bengali and other Indic-script text being split in the middle of sentences by treating the danda (`।`) and double danda (`॥`) as sentence terminators.

## [4.1.1] - 2026-06-13
### Changed
- Sped up chunking, particularly of real-world documents at typical chunk sizes, by only ever materializing the comparatively rare multi-character whitespace runs when identifying the most structurally meaningful splitter, sparing `_split_text()` from materializing the far more numerous single-character runs (such as the single spaces between words) that can never be the longest run unless no longer run exists.
Expand Down Expand Up @@ -196,6 +200,7 @@ All notable changes to semchunk will be documented here. This project adheres to
### Added
- Added the `chunk()` function, which splits text into semantically meaningful chunks of a specified size as determined by a provided token counter.

[Unreleased]: https://github.com/isaacus-dev/semchunk/compare/v4.1.1...HEAD
[4.1.1]: https://github.com/isaacus-dev/semchunk/compare/v4.1.0...v4.1.1
[4.1.0]: https://github.com/isaacus-dev/semchunk/compare/v4.0.0...v4.1.0
[4.0.0]: https://github.com/isaacus-dev/semchunk/compare/v3.2.5...v4.0.0
Expand Down
2 changes: 2 additions & 0 deletions src/semchunk/semchunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
".",
"?",
"!",
"।", # Danda (U+0964), which ends sentences in Hindi, Marathi, Bengali and other Indic scripts.
"॥", # Double danda (U+0965).
# Clause separators.
";",
",",
Expand Down
24 changes: 23 additions & 1 deletion tests/test_semchunk.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,5 +199,27 @@ def test_semchunk() -> None:
# Test chunking whitespace to ensure no errors are raised.
semchunk.chunk('\n\n', 512, lambda *args: 0)

DANDA_TEST_CHUNK_SIZE = 9
DANDA_TEST_OUTPUT_CHUNKS = {
# Hindi, Marathi and Bengali end sentences with the danda (।) and the double danda (॥).
'hindi': ['भारत एक विशाल देश है।', 'इसकी राजधानी नई दिल्ली है।', 'गंगा एक पवित्र नदी है।', 'किसान खेतों में मेहनत करते हैं।', 'वे सबके लिए अनाज उगाते हैं।'],
'marathi': ['महाराष्ट्र हे भारतातील एक मोठे राज्य आहे।', 'मुंबई ही त्याची राजधानी आहे।', 'पुणे हे शिक्षणाचे माहेरघर आहे। शेतकरी शेतात कष्ट करतात।', 'ते सर्वांसाठी धान्य पिकवतात॥'],
'bengali': ['বাংলা একটি সুন্দর ভাষা। কলকাতা পশ্চিমবঙ্গের রাজধানী।', 'এখানে অনেক মানুষ থাকে। রবীন্দ্রনাথ ঠাকুর নোবেল পুরস্কার পেয়েছিলেন।', 'তিনি একজন কবি ছিলেন।'],
'english': ['India is a large country.', 'Its capital is New Delhi.', 'The Ganga is a holy river.', 'Farmers work hard in the fields.', 'They grow grain for everyone.'],
}

def test_danda_sentence_terminators() -> None:
"""Test that chunks of Indic-script text end at the danda rather than in the middle of a sentence."""

word_counter = lambda text: len(text.split())

for language, expected_chunks in DANDA_TEST_OUTPUT_CHUNKS.items():
text = ' '.join(expected_chunks)
chunks, offsets = semchunk.chunk(text, DANDA_TEST_CHUNK_SIZE, word_counter, offsets = True)

assert chunks == expected_chunks, language
assert [text[start:end] for start, end in offsets] == chunks, language

if __name__ == '__main__':
test_semchunk()
test_semchunk()
test_danda_sentence_terminators()