From ad6ef59646c875bc1d4e357fbe16f7e7579dfa68 Mon Sep 17 00:00:00 2001 From: Ahmet Arda Kaya Date: Sat, 28 Nov 2026 01:47:59 +0100 Subject: [PATCH 1/2] Add reading_time() to estimate human reading time of text --- src/humanize/__init__.py | 5 ++++ src/humanize/reading.py | 54 ++++++++++++++++++++++++++++++++++++++++ tests/test_reading.py | 32 ++++++++++++++++++++++++ 3 files changed, 91 insertions(+) create mode 100644 src/humanize/reading.py create mode 100644 tests/test_reading.py diff --git a/src/humanize/__init__.py b/src/humanize/__init__.py index 4f54bc46..2e619eea 100644 --- a/src/humanize/__init__.py +++ b/src/humanize/__init__.py @@ -56,3 +56,8 @@ "scientific", "thousands_separator", ] + +# Custom extension (Arda-06 fork): reading-time estimate +from humanize.reading import reading_time # noqa: E402 + +__all__ = [*__all__, "reading_time"] diff --git a/src/humanize/reading.py b/src/humanize/reading.py new file mode 100644 index 00000000..5a96ba78 --- /dev/null +++ b/src/humanize/reading.py @@ -0,0 +1,54 @@ +"""Reading-time humanization (custom extension, Arda-06 fork).""" + +from __future__ import annotations + +import datetime as dt +import math +import re + +from .time import naturaldelta + +__all__ = ["reading_time"] + +_WORD_RE = re.compile(r"\b\w+\b", re.UNICODE) + + +def reading_time(text: str, wpm: int = 200, *, minimum_seconds: int = 1) -> str: + """Return a human-readable estimate of how long *text* takes to read. + + Examples: + ```pycon + >>> reading_time("word " * 400) + '2 minutes' + >>> reading_time("Hello world") + 'a second' + >>> reading_time("") + 'a moment' + + ``` + + Args: + text: The text to estimate. Words are counted with a Unicode-aware regex. + wpm: Reading speed in words per minute. Must be a positive integer. + minimum_seconds: Non-empty text never reports less than this. + + Returns: + A string like ``"3 minutes"`` produced by ``humanize.naturaldelta``. + + Raises: + TypeError: If *text* is not a string. + ValueError: If *wpm* is not positive. + """ + if not isinstance(text, str): + msg = f"text must be str, not {type(text).__name__}" + raise TypeError(msg) + if wpm <= 0: + msg = "wpm must be a positive integer" + raise ValueError(msg) + + words = len(_WORD_RE.findall(text)) + if words == 0: + return naturaldelta(dt.timedelta(0)) + + seconds = max(minimum_seconds, math.ceil(words * 60 / wpm)) + return naturaldelta(dt.timedelta(seconds=seconds)) diff --git a/tests/test_reading.py b/tests/test_reading.py new file mode 100644 index 00000000..29b43348 --- /dev/null +++ b/tests/test_reading.py @@ -0,0 +1,32 @@ +"""Tests for the custom reading_time() extension.""" + +from __future__ import annotations + +import pytest + +import humanize + + +@pytest.mark.parametrize( + "text, wpm, expected", + [ + ("", 200, "a moment"), + ("Hello world", 200, "a second"), + ("word " * 200, 200, "a minute"), + ("word " * 400, 200, "2 minutes"), + ("word " * 300, 100, "3 minutes"), + ("Merhaba dünya, nasılsın?", 200, "a second"), + ], +) +def test_reading_time(text: str, wpm: int, expected: str) -> None: + assert humanize.reading_time(text, wpm=wpm) == expected + + +def test_reading_time_invalid_wpm() -> None: + with pytest.raises(ValueError): + humanize.reading_time("text", wpm=0) + + +def test_reading_time_invalid_type() -> None: + with pytest.raises(TypeError): + humanize.reading_time(123) # type: ignore[arg-type] From 9d2ea0738e719d6731737fb8ad8194a4868b4f64 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 28 Nov 2026 00:51:53 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/humanize/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/humanize/__init__.py b/src/humanize/__init__.py index 2e619eea..acc4134c 100644 --- a/src/humanize/__init__.py +++ b/src/humanize/__init__.py @@ -58,6 +58,6 @@ ] # Custom extension (Arda-06 fork): reading-time estimate -from humanize.reading import reading_time # noqa: E402 +from humanize.reading import reading_time __all__ = [*__all__, "reading_time"]