Wordcount in Scala



import scala.io.Source

object WordCount {
  def main(args: Array[String]) {
    val textContent = Source.fromFile("TEXT_FILE_PATH").mkString
    val countMap = textContent.split("\\s+").groupBy(x => x).mapValues(x => x.length)
    println(countMap)
  }
}


Text File:

This is a test file

This
test
one

Output:

Map(test -> 2, this -> 2, is -> 1, a -> 1, file -> 1, one -> 1)


Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s