Monday, March 3, 2014

How to get the max word-count from a file?

# read strings from a file
scala> val s = Source.fromFile("cover-letter.txt").mkString

# find the max word-count
scala> s.split("\\s+").groupBy(x=>x).mapValues(_.length).toSeq.sortBy(_._2).last
res22: (String, Int) = (and,7)

scala> val (x, v) = lines.split("\\s+").groupBy(x=>x).mapValues(_.size).toList.sortWith(_._2 > _._2).take(1)(0)
x: String = AM):
v: Int = 8

No comments: