0

i'm a beginner in scala. All i've to do now is to parse a file whose content is somewhat like this

  You can't legislate morality... but it seems that morons can be  legislators.

Lottery: A tax on people who are bad at math.

These are questions for action, not speculation, which is idle.
-- Noam Chomsky

If you think education is expensive try Ignorance.
-- Derek Bok, president of Harvard

Photons have neither morals nor visas.
-- Dave Farber

Maturity is not a factor of the games we play but the occasions we play them!

Design a system an idiot can use and only an idiot will want to use it.

It is much more rewarding to do more with less.
-- Donald Knuth

I came up up till this

import scala.io._


object parseFile {
  var sample : Array[String]= new Array[String](20)
  var anyName = List[Map[String,String]]()
  def main(args:Array[String]):Unit = {
    println("Hello, Scala !! ")
  for(line <- Source.fromFile("myFile.txt").getLines())
    //sample  = line.split("--")
    anyName = Map("quote" -> line):: anyName        
    println(anyName)        

 }
}

every line is going to "quote" in the list separately along with the author name as a separate line but i want another entry in the list as "author" which should take the line preceded by "--" and should split it.

Basically i want to separate quote and author and save it in a list.

Thanks in advance

2 Answers2

0

I think you don't need list of maps, but probably list of tuples of (quote, ...), (author, ...) kind.

This can be achieved as follows:

import scala.io._

object Quotes {

    def main(args:Array[String]):Unit = {

       val result = Source.fromFile("myFile.txt")     // read file
                          .getLines()                 // line by line
                          .map(_.trim)                // trim spaces on ends
                          .filter(! _.isEmpty)        // ignore empty lines
                          .map { line =>              

              if (line.startsWith("--")) 
                 "author" -> line.drop(2).trim
              else 
                 "quote" -> line
       }

       // result is an iterator of (String, String) tuple.

       println (result.mkString("\n"))

       // if you want a list of such tuples: result.toList
    }
}

/* output looks like this:
(quote,You can't legislate morality... but it seems that morons can be  legislators.)
(quote,Lottery: A tax on people who are bad at math.)
(quote,These are questions for action, not speculation, which is idle.)
(author,Noam Chomsky)
(quote,If you think education is expensive try Ignorance.)
(author,Derek Bok, president of Harvard)
(quote,Photons have neither morals nor visas.)
(author,Dave Farber)
(quote,Maturity is not a factor of the games we play but the occasions we play them!)
(quote,Design a system an idiot can use and only an idiot will want to use it.)
(quote,It is much more rewarding to do more with less.)
(author,Donald Knuth)
*/
Shyamendra Solanki
  • 8,751
  • 2
  • 31
  • 25
0

Use multiSpan for multiple splitting of a collection (the lines of a text file into a List); needed is these criteria on each line which delimits a quote from empty lines and authors,

def p(line: String) = {
  val tline = line.trim
  tline.nonEmpty && !tline.startsWith("--")
}

Hence

val lines = io.Source.fromFile("myFile.txt").
                      getLines.
                      toList.
                      multiSpan(p)

brings a List[List[String]] with quotes with unknown author (empty second item), such as

List("Lottery: A tax on people who are bad at math.", ""), 

and with quotes and attributed author, such as

List("It is much more rewarding to do more with less.", "-- Donald Knuth")

Note the criteria is adapted to the (guessed) format in the text file, yet p may be adjusted to other formats.

For yielding a Map from quotes onto authors, consider this

(for ( List(a,b,_*) <- lines ) yield a -> b).toMap
Community
  • 1
  • 1
elm
  • 20,117
  • 14
  • 67
  • 113