Guest User

Untitled

a guest
Jul 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. import java.io._
  2. import scala.io._
  3.  
  4. def time(f: => Unit) = {
  5. val t1 = System.currentTimeMillis
  6. f
  7. ((System.currentTimeMillis - t1)/1000.0)
  8. }
  9.  
  10. def processNewsgroups(rootDir: File): Unit = {
  11. def write(map: Map[String, Int], file: String)(sort: (Tuple2[String, Int], Tuple2[String, Int]) => Boolean) {
  12. using(new PrintWriter(new FileWriter(file))) { out =>
  13. map.toList.sort(sort).foreach { pair => out.println(pair._1 + "\t" + pair._2) }
  14. }
  15. }
  16.  
  17. def using[Closeable <: {def close(): Unit}, B](closeable: Closeable)(getB: Closeable => B): B =
  18. try {
  19. getB(closeable)
  20. } finally {
  21. closeable.close()
  22. }
  23.  
  24. implicit def file2String(file: File): String = Source.fromFile(file, "ISO-8859-1").getLines.mkString("\n")
  25.  
  26. var counts = (Map.empty[String, Int].withDefaultValue(0) /:
  27. rootDir.listFiles.filter(_.isDirectory).flatMap(_.listFiles).flatMap(_.toLowerCase.split("""\W+"""))) (
  28. (c,word) => c + (word -> (1 + c(word))) )
  29. write(counts, "counts-descreasing-scala.txt") {_._2 > _._2}
  30. write(counts, "counts-alphabetical-scala.txt") {_._1 < _._1}
  31. }
  32.  
  33. time(processNewsgroups(new File("/Users/danny/Downloads/mini_newsgroups")))
Add Comment
Please, Sign In to add comment