Skip to content

fix: clamp text_standard grade levels to sensible bounds#217

Open
JohnGiorgi wants to merge 5 commits intotextstat:mainfrom
JohnGiorgi:JohnGiorgi/fix-text-standard-bounds
Open

fix: clamp text_standard grade levels to sensible bounds#217
JohnGiorgi wants to merge 5 commits intotextstat:mainfrom
JohnGiorgi:JohnGiorgi/fix-text-standard-bounds

Conversation

@JohnGiorgi
Copy link

@JohnGiorgi JohnGiorgi commented Jan 13, 2026

Summary

Fixes #205

Changes

This PR clamps the "readability-consensus" score computed by textstat.text_standard to reasonable bounds:

  • Minimum: 1 (produces "0th and 1st grade" = Kindergarten/1st grade)
  • Maximum: 18 (produces "17th and 18th grade" = Graduate school)

The clamping is applied before both float_output=True and float_output=False cases.

Testing

  • Updated existing test expectations for empty string input (now clamps to minimum)
  • Added new test cases for very simple and very complex text to verify bounds clamping
  • All 450 tests pass

Rationale for bounds

  • 1 (minimum): Represents kindergarten/1st grade level. No educational text should be rated below this.
  • 18 (maximum): Represents graduate school level. While some readability formulas can produce higher values for extremely complex text, grade 18 is a reasonable upper bound for practical purposes.

Fixes textstat#205

The text_standard() method now clamps the output to educational grade
bounds of 1-18 (kindergarten through graduate school). This prevents
nonsensical outputs like "-1th and 0th grade" for very simple text or
"25th and 26th grade" for very complex text.

The clamping is applied before both float and string outputs.
Updates existing test expectations for empty string (now clamps to
minimum grade level) and adds new test cases for very simple and very
complex text to verify the bounds clamping behavior.
@JohnGiorgi JohnGiorgi marked this pull request as draft January 13, 2026 20:51
@alxwrd
Copy link
Member

alxwrd commented Jan 16, 2026

@JohnGiorgi thanks for raising this! Is there anything you wanted to add, or can we mark it as ready for review?

- Add VERY_SIMPLE_TEXT and VERY_COMPLEX_TEXT to tests/backend/resources.py
- Update test_text_standard.py to import from resources instead of defining locally
- Fix linting issues (blank lines, line length)
@JohnGiorgi
Copy link
Author

@alxwrd I was just a bit unsure if my bounds were reasonable but otherwise this is ready for a look, thanks!

@JohnGiorgi JohnGiorgi marked this pull request as ready for review January 18, 2026 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Readability consensus returns strange non-float scores in the extremes

2 participants