Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 组合分隔符,使用Akka Streams从文本文件创建字流_Scala_Akka Stream - Fatal编程技术网

Scala 组合分隔符,使用Akka Streams从文本文件创建字流

Scala 组合分隔符,使用Akka Streams从文本文件创建字流,scala,akka-stream,Scala,Akka Stream,我有计算文本文件中单词频率的下一个代码: implicit val system: ActorSystem = ActorSystem("words-count") implicit val mat = ActorMaterializer() implicit val ec: ExecutionContextExecutor = system.dispatcher val sink = Sink.fold[Map[String, Int], String](Map.empty)({ c

我有计算文本文件中单词频率的下一个代码:

implicit val system: ActorSystem = ActorSystem("words-count")
implicit val mat = ActorMaterializer()
implicit val ec: ExecutionContextExecutor = system.dispatcher

val sink = Sink.fold[Map[String, Int], String](Map.empty)({
    case (count, word) => count + (word -> (count.getOrElse(word, 0) + 1))
  })    

FileIO.fromPath(Paths.get("/file.txt"))
        .via(Framing.delimiter(ByteString(" "), 256, true).map(_.utf8String))
        .toMat(sink)((_, right) => right)
        .run()
        .map(println(_))
        .onComplete(_ => system.terminate())

目前,它使用空格作为分隔符,但忽略换行符(
“\n”
)。我是否可以在同一个流中同时使用空格和换行符作为分隔符,即是否有方法将它们组合在一起

您可以将分隔符设置为
\n
,然后使用
flatMapConcat
按空格拆分行:

FileIO
.fromPath(path.get(“file.txt”))
.via(Framing.delimiter(ByteString(“\n”)、256,true).map(uu0.utf8String))
.flatMapConcat(s=>Source(s.split(“”.toList))//按空间分割行
.toMat(水槽)((右)=>右)
.run()
.map(println(u))
.onComplete(=>system.terminate())