Skip to content

Commit 525890b

Browse files
committed
Added an optional progress bar.
1 parent 40bec2c commit 525890b

File tree

5 files changed

+24
-8
lines changed

5 files changed

+24
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
## Changelog 🔄
22
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).
33

4+
## [1.0.0] - 2024-06-02
5+
### Added
6+
- Added a `progress` argument to the chunker returned by `chunkerify()` that, when set to `True` and multiple texts are passed, displays a progress bar.
7+
48
## [0.3.2] - 2024-06-01
59
### Fixed
610
- Fixed a bug where a `DivisionByZeroError` would be raised where a token counter returned zero tokens when called from `merge_splits()`, courtesy of [@jcobol](https://github.com/jcobol) ([#5](https://github.com/umarbutler/semchunk/pull/5)) ([7fd64eb](https://github.com/umarbutler/semchunk/pull/5/commits/7fd64eb8cf51f45702c59f43795be9a00c7d0d17)), fixing [#4](https://github.com/umarbutler/semchunk/issues/4).
@@ -56,6 +60,7 @@ All notable changes to `semchunk` will be documented here. This project adheres
5660
### Added
5761
- Added the `chunk()` function, which splits text into semantically meaningful chunks of a specified size as determined by a provided token counter.
5862

63+
[1.0.0]: https://github.com/umarbutler/semchunk/compare/v0.3.2...v1.0.0
5964
[0.3.2]: https://github.com/umarbutler/semchunk/compare/v0.3.1...v0.3.2
6065
[0.3.1]: https://github.com/umarbutler/semchunk/compare/v0.3.0...v0.3.1
6166
[0.3.0]: https://github.com/umarbutler/semchunk/compare/v0.2.4...v0.3.0

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# semchunk
2-
<a href="https://pypi.org/project/semchunk/" alt="PyPI Version"><img src="https://img.shields.io/pypi/v/semchunk"></a> <a href="https://github.com/umarbutler/semchunk/actions/workflows/ci.yml" alt="Build Status"><img src="https://img.shields.io/github/actions/workflow/status/umarbutler/semchunk/ci.yml?branch=main"></a> <a href="https://app.codecov.io/gh/umarbutler/semchunk" alt="Code Coverage"><img src="https://img.shields.io/codecov/c/github/umarbutler/semchunk"></a> <!-- <a href="https://pypistats.org/packages/semchunk" alt="Downloads"><img src="https://img.shields.io/pypi/dm/semchunk"></a> -->
2+
<a href="https://pypi.org/project/semchunk/" alt="PyPI Version"><img src="https://img.shields.io/pypi/v/semchunk"></a> <a href="https://github.com/umarbutler/semchunk/actions/workflows/ci.yml" alt="Build Status"><img src="https://img.shields.io/github/actions/workflow/status/umarbutler/semchunk/ci.yml?branch=main"></a> <a href="https://app.codecov.io/gh/umarbutler/semchunk" alt="Code Coverage"><img src="https://img.shields.io/codecov/c/github/umarbutler/semchunk"></a> <a href="https://pypistats.org/packages/semchunk" alt="Downloads"><img src="https://img.shields.io/pypi/dm/semchunk"></a>
33

44
`semchunk` is a fast and lightweight pure Python library for splitting text into semantically meaningful chunks.
55

@@ -35,7 +35,7 @@ chunker = semchunk.chunkerify('umarbutler/emubert', chunk_size) or \
3535
# The resulting `chunker` can take and chunk a single text or a list of texts, returning a list of
3636
# chunks or a list of lists of chunks, respectively.
3737
assert chunker(text) == ['The quick', 'brown', 'fox', 'jumps', 'over the', 'lazy', 'dog.']
38-
assert chunker([text]) == [['The quick', 'brown', 'fox', 'jumps', 'over the', 'lazy', 'dog.']]
38+
assert chunker([text], progress = True) == [['The quick', 'brown', 'fox', 'jumps', 'over the', 'lazy', 'dog.']]
3939
```
4040

4141
### Chunkerify
@@ -59,7 +59,7 @@ def chunkerify(
5959

6060
`memoize` flags whether to memoize the token counter. It defaults to `True`.
6161

62-
This function returns a callable that takes either a single text or a sequence of texts and returns, if a single text has been provided, a list of chunks up to `chunk_size`-tokens-long with any whitespace used to split the text removed, or, if multiple texts have been provided, a list of lists of chunks, with each inner list corresponding to the chunks of one of the provided input texts.
62+
This function returns a callable that takes either a single text or a sequence of texts and returns, if a single text has been provided, a list of chunks up to `chunk_size`-tokens-long with any whitespace used to split the text removed, or, if multiple texts have been provided, a list of lists of chunks, with each inner list corresponding to the chunks of one of the provided input texts. The callable can also be passed a `progress` argument which if set to `True` and multiple texts are passed, will display a progress bar.
6363

6464
### Chunk
6565
```python

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "semchunk"
7-
version = "0.3.2"
7+
version = "1.0.0"
88
authors = [
99
{name="Umar Butler", email="[email protected]"},
1010
]
@@ -45,6 +45,7 @@ classifiers = [
4545
"Typing :: Typed"
4646
]
4747
dependencies = [
48+
"tqdm",
4849
]
4950

5051
[project.urls]

src/semchunk/semchunk.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
from itertools import accumulate
1010
from contextlib import suppress
1111

12+
from tqdm import tqdm
13+
1214
if TYPE_CHECKING:
1315
import tiktoken
1416
import tokenizers
@@ -242,18 +244,23 @@ def faster_token_counter(text: str) -> int:
242244
token_counter = _memoized_token_counters.setdefault(token_counter, cache(token_counter))
243245

244246
# Construct and return the chunker.
245-
def chunker(text_or_texts: str | Sequence[str]) -> list[str] | list[list[str]]:
247+
def chunker(text_or_texts: str | Sequence[str], progress: bool = False) -> list[str] | list[list[str]]:
246248
"""Split text or texts into semantically meaningful chunks of a specified size as determined by the provided tokenizer or token counter.
247249
248250
Args:
249251
text_or_texts (str | Sequence[str]): The text or texts to be chunked.
250252
251253
Returns:
252-
list[str] | list[list[str]]: If a single text has been provided, a list of chunks up to `chunk_size`-tokens-long, with any whitespace used to split the text removed, or, if multiple texts have been provided, a list of lists of chunks, with each inner list corresponding to the chunks of one of the provided input texts."""
254+
list[str] | list[list[str]]: If a single text has been provided, a list of chunks up to `chunk_size`-tokens-long, with any whitespace used to split the text removed, or, if multiple texts have been provided, a list of lists of chunks, with each inner list corresponding to the chunks of one of the provided input texts.
255+
progress (bool, optional): Whether to display a progress bar when chunking multiple texts. Defaults to `False`."""
253256

254257
if isinstance(text_or_texts, str):
255258
return chunk(text_or_texts, chunk_size, token_counter, memoize = False)
256259

257-
return [chunk(text, chunk_size, token_counter, memoize = False) for text in text_or_texts]
260+
if progress:
261+
return [chunk(text, chunk_size, token_counter, memoize = False) for text in tqdm(text_or_texts)]
262+
263+
else:
264+
return [chunk(text, chunk_size, token_counter, memoize = False) for text in text_or_texts]
258265

259266
return chunker

tests/test_semchunk.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,7 @@ def tiktoken_token_counter(text: str) -> int:
8282
except ValueError:
8383
worked = True
8484

85-
assert worked
85+
assert worked
86+
87+
# Try enabling a progress bar.
88+
chunker(['ThisIs\tATest.', 'ThisIs\tATest.'], progress = True)

0 commit comments

Comments
 (0)