If you're interested in functional programming, you might also want to checkout my second blog which i'm actively working on!!

Monday, July 8, 2013

Simple Word Counter in Scala

package pelssers.io
import scala.collection.mutable.Map
class WordCounter(file: String, ignoredChars: List[Char]) {
override def toString = s"WordCounter(File=$file, ignoredChars=$ignoredChars)"
def count:Map[String, Int] = {
import scala.io.Source
val result: Map[String, Int] = new scala.collection.mutable.HashMap[String, Int]
val source = Source.fromFile(file);
for (line <- source.getLines) {
for (word <- prepareLine(line).split(" ")) {
result.update(word, result.getOrElseUpdate(word, 0) + 1)
}
}
result
}
def top(number: Int): Seq[(String,Int)] = {
count.toList.sortBy(r => r._2).reverse.take(number)
}
private def prepareLine(line: String): String = {
val sb = new StringBuffer();
for (c <- line) {
sb.append(if (ignoredChars.contains(c)) " " else c)
}
sb.toString()
}
}
/** contents of sample.txt
Working together with leaders of the Play Framework, Akka, and Scala open source communities,
Typesafe seeks to give developers the modern tools they need to build the next generation of
software applications for the era of multicore hardware and cloud computing workloads.
**/
/** worksheet test
val wordCounter = new WordCounter("c:/scalademo/sample.txt", List('.', '!', '?'))
//> wordCounter : pelssers.io.WordCounter = WordCounter(File=c:/scalademo/sample.txt, ignoredChars=List(., !, ?))
val top3words = wordCounter.top(3)
//> top3words : Seq[(String, Int)] = List((the,4), (of,3), (and,2))
**/
view raw gistfile1.scala hosted with ❤ by GitHub

No comments:

Post a Comment