JAVA—咖啡館

          ——?dú)g迎訪問(wèn)rogerfan的博客,常來(lái)《JAVA——咖啡館》坐坐,喝杯濃香的咖啡,彼此探討一下JAVA技術(shù),交流工作經(jīng)驗(yàn),分享JAVA帶來(lái)的快樂(lè)!本網(wǎng)站部分轉(zhuǎn)載文章,如果有版權(quán)問(wèn)題請(qǐng)與我聯(lián)系。

          BlogJava 首頁(yè) 新隨筆 聯(lián)系 聚合 管理
            447 Posts :: 145 Stories :: 368 Comments :: 0 Trackbacks

          在搜索引擎,語(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
                      
          posted on 2008-12-04 10:59 rogerfan 閱讀(371) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): 【Groovy學(xué)習(xí)】
          主站蜘蛛池模板: 舒城县| 乾安县| 信阳市| 高台县| 望谟县| 祁门县| 中山市| 兰西县| 山东省| 南澳县| 浦县| 洛南县| 新营市| 兰西县| 象山县| 双辽市| 辽阳县| 岱山县| 金湖县| 瑞安市| 舟曲县| 韶关市| 庆云县| 威宁| 雷波县| 如皋市| 桑日县| 安丘市| 邢台县| 阆中市| 武强县| 禄丰县| 崇礼县| 舞阳县| 镇巴县| 深水埗区| 新乡市| 雷山县| 武威市| 桃园县| 报价|