文字の出現頻度を調べる

1つの文字列に特定の文字の出現頻度を調べます。
例として、円周率における出現頻度を扱います

def char_stat(str){
    table = [:]
    m = (~/(?m)./).matcher(str)
    m.find()
    m.each { word ->
        table[word] = table.get(word,0) + 1
    }
    list = table.keySet().toList().sort()
    list.sort {table[it]}
    statistic = "\n"
    list[-1..-10].each { word->
        println word.padLeft(2) + ':' + table[word] + "\n"
    }
}


pie = "3.141592653589793238462643383279"
char_stat(pie)
3.141592653589793238462643383279
 3:7

 9:4

 2:4

 8:3

 6:3

 5:3

 4:3

 7:2

 1:2

 .:1

===> [3, 9, 2, 8, 6, 5, 4, 7, 1, .]

実行環境