How to merge adjacent lines with scalaz-stream without losing the splitting line

六月ゝ 毕业季﹏ 提交于 2019-12-07 19:44:28

问题


Suppose that my input file myInput.txt looks as follows:

~~~ text1
bla bla
some more text
~~~ text2
lorem ipsum
~~~ othertext
the wikipedia
entry is not
up to date

That is, there are documents separated by ~~~. The desired output is as follows:

text1: bla bla some more text
text2: lorem ipsum 
othertext: the wikipedia entry is not up to date

How do I go about that? The following seems pretty unnatural, plus I lose the titles:

 val converter: Task[Unit] =
    io.linesR("myInput.txt")
      .split(line => line.startsWith("~~~"))
      .intersperse(Vector("\nNew document: "))
      .map(vec => vec.mkString(" "))
      .pipe(text.utf8Encode)
      .to(io.fileChunkW("flawedOutput.txt"))
      .run

  converter.run

回答1:


The following works fine, but it is insanely slow if I run it on more than a toy example (~5 minutes to process 70MB). Is that because I am creating Process's all over the place? Also, it seems to be using only a single core.

  val converter2: Task[Unit] = {
    val docSep = "~~~"
    io.linesR("myInput.txt")
      .flatMap(line => { val words = line.split(" ");
          if (words.length==0 || words(0)!=docSep) Process(line)
          else Process(docSep, words.tail.mkString(" ")) })
      .split(_ == docSep)
      .filter(_ != Vector())
      .map(lines => lines.head + ": " + lines.tail.mkString(" "))
      .intersperse("\n")
      .pipe(text.utf8Encode)
      .to(io.fileChunkW("correctButSlowOutput.txt"))
      .run
  }


来源:https://stackoverflow.com/questions/30053095/how-to-merge-adjacent-lines-with-scalaz-stream-without-losing-the-splitting-line

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!