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?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é)果:



?Collections:?2


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