在搜索引擎,語(yǔ)音識(shí)別等領(lǐng)域常會(huì)統(tǒng)計(jì)單詞的出現(xiàn)頻率,下面給出Groovy實(shí)現(xiàn),打印出現(xiàn)頻率最高的6個(gè)單詞以及相應(yīng)的出現(xiàn)次數(shù):
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 |
運(yùn)行結(jié)果:
the: 5 Groovy: 2 that: 2 Collections: 2 Java: 2 same: 2 |