Created
July 25, 2019 18:45
-
-
Save bitsnaps/d174a2f333f2af1025ea58e6835e6020 to your computer and use it in GitHub Desktop.
Simple wordCounter with Groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@groovy.transform.Canonical | |
class Phrase { | |
String phrase | |
Map wordCount() { | |
words.collectEntries { [ (it): occurrences(it) ] } | |
} | |
List getWords() { | |
phrase.toLowerCase().findAll(/[\w']+/) | |
} | |
int occurrences(String word) { | |
Collections.frequency(words, word) | |
} | |
} | |
println(new Phrase('Hello World, my world is different than your world but I would say hello to you anyway.').wordCount()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment