扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
在搜索引擎,语音识别等领域常会统计单词的出现频率,下面给出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 |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者