在搜索引擎,語音識別等領域常會統計單詞的出現頻率,下面給出Groovy實現,打印出現頻率最高的6個單詞以及相應的出現次數:
def content =
"""
The Java Collections API is the basis for all the nice support that Groovy gives you
through lists and maps. In fact, Groovy not only uses the same abstractions, it
even works on the very same classes that make up the Java Collections API.
"""
def words = content.tokenize()
def wordFrequency = [:]
words.each {
wordFrequency[it] = wordFrequency.get(it, 0 ) + 1
}
def wordList = wordFrequency.keySet().toList()
wordList.sort {wordFrequency[it]}
def result = ''
wordList[ - 1 .. - 6 ].each {
result += it.padLeft( 12 ) + " : " + wordFrequency[it] + " \n "
}
println result
|
運行結果:
the: 5
Groovy: 2
that: 2
Collections: 2
Java: 2
same: 2
|