This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
**/ | |
No comments:
Post a Comment