首页 > 解决方案 > 使用 Akka 演员计算不同单词的数量

问题描述

我的练习是检查给定文本中有多少不同的单词。我必须使用 Akka 演员来做到这一点。我必须创建动态数量workers,其中每个都worker查看一行并检查一行中有多少不同的单词,然后它应该将它添加到一些累加器中 - 这是我的主要问题 - 当我得到“Akka dead letter”时m 试图将号码发送到累加器。

import Main.{dane, tekst}
import akka.actor.{Actor, ActorSystem, Props}
case class Init(numberOfWorkers: Int)
case class Mission(text: List[String])
case class Result(liczba: Int)
case class Do(y: Int)
case class Sum(z: Int)



class Supervisor extends Actor {
  def receive: Receive = {
    case Init(n) =>
      for (x <- 0 to n) {
        val worker= context.actorOf(Props[Worker]())
        worker ! Mission(dane().map(y => List(y))(x))
      }
    
  }

  def Summing(acc: List[Int]): Receive = {
    case Sum(y: Int) =>

      println("Current sum: " + (acc:+y).sum)
      context.become(Summing((acc:+y)))
  }




}



class Worker extends Actor {
  def receive: Receive = {
    case Mission(line) =>
      val amountOfWords = line.flatMap(x => x.split(" ").toList).groupBy(identity).size

      println("Amount of different words: " + amountOfWords)
      context.sender() ! Summing(amountOfWords)
  }
}


object Main {

  def dane(): List[String] = {
    
    scala.io.Source.fromResource("ogniem_i_mieczem.txt").getLines.toList.map(x => x.replaceAll("[.,:;-]", ""))
  }
  val text = dane()

  def main(args: Array[String]): Unit = {

    println(dane())
    val systemNapisow = ActorSystem("SystemNapisow")
    val mySupervisor = systemNapisow.actorOf(Props[Supervisor], "supervisor")

    mySupervisor ! Init(dane().length-1)
  }

}

标签: scalaakka

解决方案


推荐阅读