Repository navigation
fix(reading-time): count words in non-Latin scripts - #227
pcbeingused333 wants to merge 2 commits into
Conversation
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.
|
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.
|
…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.
|
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 This PR was prepared with the help of an AI assistant (Claude); the contributor has not reviewed it line by line. |
estimated_reading_timecounts words withcontent.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 likedon'tcount 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
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
mainand pass with this change; the full reading-time test file passes.