textmetrics/count
Counts of words, sentences, syllables, characters, paragraphs and
polysyllables — the primitives consumed by readability scores in
textmetrics/readability.
Functions in this module are pure, deterministic, and O(n) in
the length of their input. They iterate over extended grapheme
clusters (via gleam/string.to_graphemes) of the input
normalised to Unicode Normalization Form C (NFC), not raw bytes, so
"naïve" is 5 graphemes and 1 word regardless of whether the
diacritic is encoded as a single codepoint or a + combining
mark.
All language-specific heuristics in this module are tuned for
English. Behaviour on other scripts is documented per
function and biased towards “do nothing surprising”: CJK without
whitespace stays one word; non-English text in
syllables_in_word returns 1.
Values
pub fn characters(text: String) -> Int
Count characters that contribute to readability formulas: ASCII letters plus ASCII digits, excluding whitespace, punctuation, symbols, other numbers and emoji. Letters, marks and digits of other scripts (Latin-1 accents, CJK ideographs) also count.
Examples:
count.characters("Hello, World!") // 10
count.characters("123 abc") // 6
pub fn paragraphs(text: String) -> Int
Count paragraphs. A paragraph is a maximal run of non-blank lines separated by one or more blank lines. Trailing blank lines do not produce empty paragraphs.
Examples:
count.paragraphs("a\n\nb\n\nc") // 3
count.paragraphs("one line") // 1
count.paragraphs("") // 0
pub fn polysyllables(text: String) -> Int
Count words with three or more syllables in text. SMOG and
readability.gunning_fog both use
this count with no exclusions.
pub fn sentences(text: String) -> Int
Count sentences. Sentence terminators are ., !, ?, …, the
ideographic and full-width 。 . ! ?, and the Devanagari
dandas । ॥. A run of
consecutive terminators counts as one boundary (so "What?!" is
one sentence). A trailing non-empty fragment that lacks a
terminator still counts as a sentence ("hello" → 1).
A . does not end a sentence when:
- a letter or digit follows it directly, as inside
3.50,example.com, or the first period ofp.m.; - it follows a common English abbreviation —
Mr.,Mrs.,Ms.,Dr.,Prof.,Jr.,Sr.,St.,Ave.,Blvd.,vs.,etc.,Inc.,Ltd.,Mon.…Sun.,Jan.…Dec.and a few more (case-insensitive); - it closes a multi-period abbreviation made of one- or two-letter
parts —
e.g.,i.e.,a.m.,p.m.,U.S.,U.S.A.,Ph.D.— unless the next word starts with a capital letter, so"at 5 p.m. yesterday."is one sentence and"at 5 p.m. Then he left."is two.
Empty input returns 0.
pub fn syllables(text: String) -> Int
Count syllables in text. Sums syllables_in_word
over each word found by words.
pub fn syllables_in_word(word: String) -> Int
Count syllables in a single word using an English heuristic:
- Lowercase the word.
- Keep only ASCII letters and accented Latin vowels (
é,ü, …). - Count maximal vowel groups in
a e i o u yand the accented vowels, withyonly counting at non-initial position. - Subtract one if the word ends in a silent
e(the preceding letter being a consonant), except after consonant +l(table,syllable). - Floor at
1.
Examples:
count.syllables_in_word("the") // 1
count.syllables_in_word("hello") // 2
count.syllables_in_word("syllable") // 3
count.syllables_in_word("rhythm") // 1 (no vowels, floors at 1)
Returns 0 for an empty input. Returns 1 for any other input with
none of the letters kept in step 2 ("123", "日本").
pub fn words(text: String) -> Int
Count words. A word is a maximal run of “letter-like” graphemes separated by whitespace or punctuation.
A grapheme counts as letter-like when its first code point is a
letter, mark, decimal digit or letter number of any script
(General_Category L, M, Nd or Nl). Whitespace, punctuation, symbols,
other numbers (①, ²) and emoji, including keycaps such as
1️⃣, are word boundaries.
Examples:
count.words("") // 0
count.words("hello") // 1
count.words("hello world") // 2
count.words("hello, world!") // 2
count.words("hello world") // 2 (whitespace collapses)