#Word count
On this page
Count the lines, words and characters of a text, then use a Dict and sorting to pick the five most frequent words.
#Code
nabi
// Count lines, words and chars, then list the most frequent words.
const TEXT: str = "The quick brown fox jumps over the lazy dog.
The dog sleeps; the fox does not.
A fox, a dog, and the long summer day.
Quick thinking wins the day!";
struct Stats {
lines: usize;
words: usize;
chars: usize;
}
fn stats(text: str) -> Stats {
let s = Stats { lines: 0, words: 0, chars: text.char_count() };
for line in text.lines() {
s.lines += 1;
s.words += line.split_whitespace().count();
}
s
}
// Lowercase a token and drop surrounding punctuation ("Dog," -> "dog").
fn normalize(token: str) -> str {
let sb = StrBuf::new();
for c in token.chars() {
if c.is_alphanumeric() || c == '\'' {
sb.push_str(c.to_lowercase());
}
}
sb.to_str()
}
fn frequencies(text: str) -> Dict<str, i32> {
let counts: Dict<str, i32> = Dict::new();
for token in text.split_whitespace() {
let w = normalize(token);
if w.is_empty() { continue; }
counts[w, default: 0] += 1;
}
counts
}
fn top(counts: Dict<str, i32>, n: usize) -> List<(str, i32)> {
// Most frequent first; ties broken alphabetically so output is stable.
counts.sorted_by_key(|(w, c)| (-c, w)).take(n).to_list()
}
fn main() {
let s = stats(TEXT);
println(f"lines {s.lines:>4}");
println(f"words {s.words:>4}");
println(f"chars {s.chars:>4}");
let counts = frequencies(TEXT);
println(f"\n{counts.len()} distinct words, top 5:");
for (rank, (w, c)) in top(counts, 5).enumerate(start: 1) {
let bar = "#".repeat(c as usize);
println(f"{rank}. {w:<6} {c:>2} {bar}");
}
}
lines 5
words 30
chars 147
18 distinct words, top 5:
1. the 6 ######
2. dog 3 ###
3. fox 3 ###
4. a 2 ##
5. day 2 ##
#Walkthrough
- Iterators on
strdo the counting.lines()yields lines,split_whitespace()yields words, andcount()counts them. The character count ischar_count(), not the byte lengthlen()(Strings,strmethods). normalizebuilds the word in aStrBufone character at a time, dropping punctuation and lowercasing, so"Dog,"and"dog."count as the same word (StrBuf).- The frequency table is a
Dict<str, i32>.counts[w, default: 0] += 1stores0first whenwis missing, then adds one (Collections and iterators,Dict). sorted_by_keyon the dictionary gives its(word, count)pairs as a sorted list. The key(-c, w)puts the highest count first and breaks ties alphabetically, so the output is always the same;take(n).to_list()keeps the firstn.enumerate(start: 1)numbers the ranks from 1 (Collections and iterators).- The table is aligned with f-string format specs:
{w:<6}left-aligns,{c:>2}right-aligns (2.10.8 align and width).
Found a mistake? Edit this page on GitHub. Last updated