Skip to content

Instantly share code, notes, and snippets.

@bitsnaps
Created July 25, 2019 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitsnaps/d174a2f333f2af1025ea58e6835e6020 to your computer and use it in GitHub Desktop.
Save bitsnaps/d174a2f333f2af1025ea58e6835e6020 to your computer and use it in GitHub Desktop.
Simple wordCounter with Groovy
@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