scala本地wordcount的程序编写

主要是在本地编写一个计算文件中单词出现次数的工具,一下是代码:

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)

  }
}
接下来是我的两个外部txt:

newword.txt:

chandler tom
jerry rose
lily jerry
words.txt

tom jerry
tom Chandler 
jerry rose



你可能感兴趣的:(scala,wordcount)