findAll with groupingBy and eachCount

Word Count
Word Count in Kotlin
object WordCount {

    fun phrase(phrase: String): Map<String, Int> {
        return Regex("[a-z0-9]+(?:'[a-z]+)?")
                .findAll(phrase.lowercase())
                .groupingBy { it.value }
                .eachCount()
    }
}

An object declaration is used to define WordCount as essentially a singleton object instantiation of the class. This is sufficient, since there is no object state that needs to change with each call of the phrase method.

A regular expression pattern is defined to match the characters expected to be found in the words. At the time of writing, the valid characters are alphanumeric and the apostrophe. The pattern looks for one or more alphanumeric characters ([a-z0-9]+), either followed by an apostrophe and one or more alphabetic characters, or not ((?:'[a-z]+)?).

The findAll method is called on the Regex to match the words in the lowercased input to the pattern.

The found words are passed to the groupingBy method. The lambda of groupingBy uses the it keyword to refer to the single MatchResult parameter for the lambda, and uses its value property for the value to be grouped by.

Each group is then transformed into a Map<String, int> by the eachCount method, which creates a key/value pair for each word and its frequency as an entry for the Map.

Finally, the Map is returned by the function.

4th Sep 2024 · Found it useful?