How to combine adjacent lines with fantastic flow without losing the split line
Let's assume my input file myInput.txt
looks like this:
~~~ text1
bla bla
some more text
~~~ text2
lorem ipsum
~~~ othertext
the wikipedia
entry is not
up to date
That is, the documents are split ~~~
. The required output looks like this:
text1: bla bla some more text
text2: lorem ipsum
othertext: the wikipedia entry is not up to date
How should I do it? The following looks pretty unnatural, plus I'm losing the name:
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
+2
source to share
1 answer
The following works great, but is insanely slow if I run it more than the toys example (~ 5 minutes to handle 70MB). Is it because I'm creating Process
all over the place? Also, it looks like only one core is being used.
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
}
0
source to share