2

假设我的输入文件myInput.txt如下所示:

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

也就是说,有文档以 . 分隔~~~。所需的输出如下:

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

我该怎么做?以下似乎很不自然,而且我失去了标题:

 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
4

1 回答 1

0

以下工作正常,但如果我在不止一个玩具示例上运行它(处理 70MB 约 5 分钟),它会非常慢。那是因为我Process到处都在创作吗?此外,它似乎只使用一个核心。

  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
  }
于 2015-05-06T13:12:09.453 回答