Commit 4b27e2e
authored
feat(stdlib): add ChunkingStrategy ABC and built-in chunkers (#923)
* feat(stdlib): add ChunkingStrategy ABC and built-in chunkers
Adds mellea/stdlib/chunking.py with ChunkingStrategy ABC and three
built-in implementations: SentenceChunker, WordChunker, ParagraphChunker.
split(accumulated_text) returns complete chunks, holding back trailing
fragments for the next call.
Closes #899
Part of #891
Assisted-by: Claude Code
Signed-off-by: Nigel Jones <jonesn@uk.ibm.com>
* fix(test): remove unused pytest import from test_chunking
Pyright flagged import as unaccessed; no pytest.* calls in the file.
Assisted-by: Claude Code
Signed-off-by: Nigel Jones <jonesn@uk.ibm.com>
* fix(stdlib): address PR #923 review feedback on ChunkingStrategy
- Fix SentenceChunker whitespace leak: lstrip() after match.end() so
double-space / tab separators don't bleed into the next chunk as
leading whitespace
- Add end-of-stream contract to ABC docstring (callers responsible for
trailing fragment after stream terminates)
- Fix incorrect comment "end-of-string" → "whitespace"
- Compile _WHITESPACE / _PARA_BOUNDARY / _PARA_BOUNDARY_END at module
level (consistent with _SENTENCE_BOUNDARY; avoids per-call recompile)
- Expand SentenceChunker char class to include right curly double/single
quotes (U+201D / U+2019) for common LLM output patterns
- Document CRLF limitation on ParagraphChunker
- Re-export ChunkingStrategy + chunkers from mellea.stdlib.__init__
- Add __all__ to chunking.py
- Add tests: closing paren, double-space separator, tab separator,
abbreviation edge case (known-bad split), WordChunker leading-whitespace
Assisted-by: Claude Code
* fix(stdlib): address PR #923 nits — comment accuracy and curly-quote test
- Fix misleading comment on _SENTENCE_BOUNDARY: was "processed by re engine
as \u escapes" but the file contained literal Unicode chars. Now uses
chr(0x201d) + chr(0x2019) for Python 3.12 compatibility (U+2019 is treated
as a string delimiter in single-quoted raw strings on 3.12).
- Add test_sentence_chunker_curly_quotes to verify U+201D/U+2019 matching.
Assisted-by: Claude Code
Signed-off-by: Nigel Jones <jonesn@uk.ibm.com>
* fix(stdlib): apply review feedback on ChunkingStrategy (#923)
- Simplify _SENTENCE_BOUNDARY regex to use \u escapes instead of chr()
concatenation (cleaner, same semantics, Python 3.12-safe)
- Document that SentenceChunker discards inter-sentence whitespace via lstrip()
- Add test_chunking_strategy_is_abstract to document the extension-point contract
Assisted-by: Claude Code
---------
Signed-off-by: Nigel Jones <jonesn@uk.ibm.com>1 parent 96d221a commit 4b27e2e
3 files changed
Lines changed: 421 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
11 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
0 commit comments