scala - 使用 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)
}
}
解决方案
推荐阅读
- javascript - 在 reactjs 中构建后,条件样式导入捆绑在一起
- python - Python - pip install - 错误:必须指定 GDAL API 版本
- sql - 如何使用 xpath 查询从 xml 内容中过滤数据以创建临时表
- javascript - 你如何使这种淡入淡出效果正常工作?
- php - 通过 jQuery 显示活动类别的帖子
- javascript - 如何调整在 HTML、CSS 和 JavaScript 中创建的图像滑块的大小
- python - 我无法从用户输入中获取 False 布尔值
- symfony - 会话在 MockArraySessionStorage.php:127 开始后无法设置会话 ID
- c++ - 意外的 gcc 警告:函数返回局部变量的地址 - 是编译器错误吗?
- asp.net - 如何在 Visual Studio For Mac 中将 Web API 发布到部署槽?