我有这个字符串输入::
鲍勃不想剪头发。在沙龙里,鲍勃在剪鲍勃的同时做了一个鲍勃。
我需要对字进行计数,然后使用Scala根据计数对输出进行排序。而且尽管某些单词的计数相同,但我需要对具有相同计数的单词进行字典排序。
输出应类似于::
鲍勃:4
a:3
cut:2
at:1
did:1
不是:1
获取:1
头发:1
沙龙:1
the:1
想要:1
时间:1
我已经能够根据字数进行排序,但是无法根据字母进行排序。
我使用了以下代码::
import scala.collection.immutable.ListMap
object wordCount {
def main (args : Array[String]) : Unit ={
var sentence : String = """Bob didn't want a hair cut. At the
salon,
Bob did a bob while getting a bob cut."""
var lines = sentence.replaceAll("""[\p{Punct}&&[^']]""", "")
val words = lines.split(" ").map(a => a.toLowerCase())
var word = words.to[Array]
//count.foreach(println)
var varMap : scala.collection.mutable.Map[String, Int] = scala.collection.mutable.Map()
var b : String = ""
for (b <- words){
if(varMap.contains(b)){
var value = varMap(b)
varMap.remove(b)
varMap(b) = value+1
}
else{
varMap +=(b ->1)
}
}
//var a = ListMap(varMap.toSeq.sortBy(_._2):_*)
var bbq = ListMap(varMap.toSeq.sortWith(_._2 > _._2):_*)
//println(a.getClass)
for ((k,v) <- bbq) println(""+ k+"\t"+": "+v)
//count.foreach(println)
//var words = word.flatMap(line => line.split(" ")).map(a => a.toLowerCase()).map(word => (word,1)).reduceByKey(_+_)
} }
答案 0 :(得分:1)
使用此
val string: String = "Bob didn't want a hair cut. At the salon, Bob did a bob while getting a bob cut."
val words: List[String] = string.toLowerCase.split("[ .,!?\"]").toList.filterNot(_.isEmpty)
val freqMap: List[(String, Int)] = words.groupBy(w => w).mapValues(_.size).toList
def comparator(tup1: (String, Int), tup2: (String, Int)): Boolean = {
if (tup1._2 == tup2._2) tup1._1 <= tup2._1
else tup1._2 > tup2._2
}
val myAnswer: List[(String, Int)] = freqMap.sortWith(comparator)
参考链接