Word Count
Count how many times each word occurs in a sentence, ignoring case and punctuation but keeping apostrophes inside contractions.
Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.
// FILE: word_count.nomi
//# Counts the words in a sentence.
import std::regex.Regex
/// How many times each word occurs in `sentence`, lower-cased. A word is a
/// run of letters and digits, and may hold an apostrophe between two of them,
/// as in "can't"; quotes around a word are not part of it.
pub fn count_words(sentence: String) -> Map<String, Int>
sentence
|> String.lowercase()
|> String.find_all(Regex"[a-z0-9]+('[a-z0-9]+)*")
|> Iter.frequencies()
end
// FILE: word_count_test.nomi
import word_count.count_words
test "counts words"
[
("count one word", "word", {"word" => 1}),
("count one of each word", "one of each", {"one" => 1, "of" => 1, "each" => 1}),
(
"multiple occurrences of a word",
"one fish two fish red fish blue fish",
{"one" => 1, "fish" => 4, "two" => 1, "red" => 1, "blue" => 1},
),
("handles cramped lists", "one,two,three", {"one" => 1, "two" => 1, "three" => 1}),
("handles expanded lists", "one,\ntwo,\nthree", {"one" => 1, "two" => 1, "three" => 1}),
(
"ignore punctuation",
"car: carpet as java: javascript!!&@$%^&",
{"car" => 1, "carpet" => 1, "as" => 1, "java" => 1, "javascript" => 1},
),
("include numbers", "testing, 1, 2 testing", {"testing" => 2, "1" => 1, "2" => 1}),
("normalize case", "go Go GO Stop stop", {"go" => 3, "stop" => 2}),
(
"with apostrophes",
"'First: don't laugh. Then: don't cry. You're getting it.'",
{
"first" => 1,
"don't" => 2,
"laugh" => 1,
"then" => 1,
"cry" => 1,
"you're" => 1,
"getting" => 1,
"it" => 1,
},
),
(
"with quotations",
"Joe can't tell between 'large' and large.",
{"joe" => 1, "can't" => 1, "tell" => 1, "between" => 1, "large" => 2, "and" => 1},
),
(
"substrings from the beginning",
"Joe can't tell between app, apple and a.",
{
"joe" => 1,
"can't" => 1,
"tell" => 1,
"between" => 1,
"app" => 1,
"apple" => 1,
"and" => 1,
"a" => 1,
},
),
(
"multiple spaces not detected as a word",
" multiple whitespaces",
{"multiple" => 1, "whitespaces" => 1},
),
(
"alternating word separators not detected as a word",
",\n,one,\n ,two \n 'three'",
{"one" => 1, "two" => 1, "three" => 1},
),
(
"quotation for word with apostrophe",
"can, can't, 'can't'",
{"can" => 1, "can't" => 2},
),
]
|> subtests (description, sentence, expected), description
assert count_words(sentence) == expected
end
endok counts words (14 cases)
test result: ok. 14 passed, 0 failed
How it’s written
Section titled “How it’s written”- The whole solution is a three-stage pipeline: lower-case, find the words
with one regex, and
Iter.frequencies. The regex[a-z0-9]+('[a-z0-9]+)*admits an apostrophe only between two letters or digits, so quotes around a word fall away without a trimming step. - Lower-casing happens before matching, so the regex needs no upper-case class.
- The expected values are map literals in the table;
Mapequality ignores order, so they are written in the canonical order. - The first “with apostrophes” case (uuid 4185a902) is left out because canonical data replaces it with uuid 4ff6c7d7, which is included.