scala本地wordcount的程式編寫
阿新 • • 發佈:2019-01-10
主要是在本地編寫一個計算檔案中單詞出現次數的工具,一下是程式碼:
接下來是我的兩個外部txt:import scala.actors.{Future, Actor} import scala.collection.mutable import scala.collection.mutable.{ListBuffer, ArrayBuffer} import scala.io.Source /** * mhc * Created by Administrator on 2016/5/12. */ case class startTask(fileName: String) case class stopTask() case class resultSet(result: Map[String, Int]) class Task extends Actor { override def act(): Unit = { loop { react { case startTask(fileName: String) => { // 開始任務 val lines = Source.fromFile(fileName).mkString val arr = lines.split("\r\n") val result = arr.flatMap(_.split(" ")).map((_, 1)).groupBy(_._1).mapValues(_.length) sender ! resultSet(result) } case stopTask => { exit() } } } } } object Task { def main(args: Array[String]) { val replySet = new mutable.HashSet[Future[Any]] var list = new ListBuffer[resultSet] val files = Array("D://words.txt", "D://newword.txt") val task = new Task task.start() for (s <- 0 until files.length) { replySet += (task !! startTask(files(s))) // 傳送訊息 } for (set <- replySet) { list += set.apply().asInstanceOf[resultSet] } val result = list.map(_.result).flatten.groupBy(_._1).mapValues(x => x.foldLeft(0)(_ + _._2)) println(result) } }
newword.txt:
chandler tom
jerry rose
lily jerry
words.txt
tom jerry
tom Chandler
jerry rose