JAVA—咖啡馆

——欢迎访问rogerfan的博客,常来《JAVA——咖啡馆》坐坐,喝杯浓香的咖啡,彼此探讨一下JAVA技术,交流工作经验,分享JAVA带来的快乐!本网站部分转载文章,如果有版权问题请与我联系。

BlogJava 首页 新随笔 联系 聚合 管理
  447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks

在搜索引擎,语音识别等领域常会统计单词的出现频率,下面给出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
            
posted on 2008-12-04 10:59 rogerfan 阅读(323) 评论(0)  编辑  收藏 所属分类: 【Groovy学习】

只有注册用户登录后才能发表评论。


网站导航: