Skip to content

fix(reading-time): count words in non-Latin scripts - #227

Open
pcbeingused333 wants to merge 2 commits into
Rails-Designer:mainfrom
pcbeingused333:fix/reading-time-unicode
Open

pcbeingused333 wants to merge 2 commits into
Rails-Designer:mainfrom
pcbeingused333:fix/reading-time-unicode

Conversation

@pcbeingused333

Copy link
Copy Markdown
Contributor

estimated_reading_time counts words with content.scan(/\b[a-zA-Z]+\b/), so only ASCII letters count. A post written in Russian, Greek or another space-separated non-ASCII script counts as almost empty and always reports "1 min read"; accented Latin words (café) are split or dropped too.

Fix

Scan Unicode letters and numbers instead: /[\p{L}\p{N}]+(?:['’][\p{L}]+)?/. Contractions like don't count as one word, and numbers now count as words. Scripts written without spaces (Chinese, Japanese) are still counted per space-separated chunk; that would need a per-character estimate and is out of scope here.

Tests

  • Cyrillic text of 400 words gives 2 minutes at 200 wpm (was 1).
  • Accented words and contractions count once each.

Disclosure

This change was prepared with AI assistance (Claude Code) and has not been reviewed line by line by a human before submission. The two new tests fail on main and pass with this change; the full reading-time test file passes.

estimated_reading_time scanned only [a-zA-Z] words, so posts in Russian, Chinese-spaced text, accented Latin etc. were counted as near-empty. Use Unicode letter/number classes.
@eelcoj

eelcoj commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

I think it is worth to extend this while touch upon this feature. Indic, Arabic-with-diacritics and Thai still miscount when doing some quick testing. Real CJK would also be nice to add while at it.

\p{N} addition is different behaviour from before. Probably what we want, but needs to be documented.

…ters

Indic vowel signs, Arabic diacritics and Thai marks are \p{M}, which split words. Include them in the word class, and count Han/Hiragana/Katakana characters individually since those scripts have no spaces.
@pcbeingused333

Copy link
Copy Markdown
Contributor Author

Thanks @eelcoj, extended in the follow-up commit: combining marks are kept inside words (Indic, Arabic with diacritics, Thai) and each Han/Hiragana/Katakana character counts as one word. I documented the \p{N} change in a comment on the regex, as I couldn't find a docs page for reading time. Thai without spaces is still only split at spaces, as that needs a dictionary segmenter. Happy to switch the CJK unit if you prefer another rule.

This PR was prepared with the help of an AI assistant (Claude); the contributor has not reviewed it line by line.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants