diff --git a/CHANGELOG.md b/CHANGELOG.md index 0280bb1..693d649 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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. @@ -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 diff --git a/src/semchunk/semchunk.py b/src/semchunk/semchunk.py index 6538383..5f09a9f 100644 --- a/src/semchunk/semchunk.py +++ b/src/semchunk/semchunk.py @@ -40,6 +40,8 @@ ".", "?", "!", + "ред", # Danda (U+0964), which ends sentences in Hindi, Marathi, Bengali and other Indic scripts. + "рее", # Double danda (U+0965). # Clause separators. ";", ",", diff --git a/tests/test_semchunk.py b/tests/test_semchunk.py index 209de8f..5532c96 100644 --- a/tests/test_semchunk.py +++ b/tests/test_semchunk.py @@ -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() \ No newline at end of file + test_semchunk() + test_danda_sentence_terminators() \ No newline at end of file