First they ignore you
          then they ridicule you
          then they fight you
          then you win
              -- Mahatma Gandhi
          Chinese => English     英文 => 中文             
          隨筆-221  評論-1047  文章-0  trackbacks-0
          在搜索引擎,語音識別等領(lǐng)域常會統(tǒng)計單詞的出現(xiàn)頻率,下面給出Groovy實現(xiàn),打印出現(xiàn)頻率最高的6個單詞以及相應(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?



          運行結(jié)果:

          ?????????? the:?5
          ?? Groovy:?2
          ????????? that:?2
          ?Collections:?2
          ??????? ?Java:?2
          ????????same:?2?

          ?


          如果所要處理的文本比較復雜,可以使用Regex進行處理,順便說一句,Groovy在語言級別支持Regex!

          posted on 2007-02-01 23:31 山風小子 閱讀(4353) 評論(6)  編輯  收藏 所屬分類: Groovy & Grails

          轉(zhuǎn)載文章,請保留原始鏈接和署名



          Dict.CN
          <2007年3月>
          25262728123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(71)

          隨筆分類

          隨筆檔案

          相冊

          Documentations

          Groovy & Grails

          友情鏈接

          好友 & 鄰居

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 796531
          • 排名 - 52

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 英德市| 华宁县| 芷江| 和林格尔县| 交口县| 扶风县| 桐柏县| 苍梧县| 连平县| 清水县| 尉犁县| 博乐市| 堆龙德庆县| 准格尔旗| 锡林浩特市| 凤阳县| 汉川市| 富锦市| 鹤岗市| 霍城县| 黄冈市| 武乡县| 宁陕县| 浦江县| 顺义区| 京山县| 临夏县| 贵港市| 静安区| 重庆市| 阿坝| 葫芦岛市| 成都市| 合水县| 乌兰察布市| 黄石市| 南澳县| 合江县| 徐闻县| 栖霞市| 大石桥市|