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 閱讀(372) 評論(0)  編輯  收藏 所屬分類: 【Groovy學習】
          主站蜘蛛池模板: 乌兰县| 周宁县| 甘孜县| 芜湖县| 阿鲁科尔沁旗| 崇左市| 昆山市| 泸溪县| 巴里| 江都市| 乡城县| 时尚| 清河县| 松江区| 东台市| 通化县| 深泽县| 基隆市| 永德县| 元谋县| 珠海市| 安义县| 壶关县| 旺苍县| 乐安县| 宝坻区| 思茅市| 宁武县| 丽江市| 泗水县| 屯留县| 会昌县| 永年县| 长兴县| 昭通市| 崇信县| 农安县| 武夷山市| 东兴市| 深水埗区| 丰原市|