Created
March 16, 2010 15:48
-
-
Save codahale/334119 to your computer and use it in GitHub Desktop.
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
def time(f: => Unit) = { | |
val t1 = System.currentTimeMillis | |
f | |
((System.currentTimeMillis - t1)/1000.0) | |
} | |
def write(values: Array[(String, Int)], file: String) { | |
val writer = new PrintWriter(new FileWriter(file)) | |
try { | |
for ((x, y) <- values) { | |
writer.println(x + "\t" + y) | |
} | |
} finally { | |
writer.close() | |
} | |
} | |
def processNewsgroups(rootDir: File): Unit = { | |
val counts = Map.empty[String, Int] | |
val pattern = """\W+""".r | |
for (dir <- rootDir.listFiles if dir.isDirectory; | |
file <- dir.listFiles; | |
line <- Source.fromFile(file)(Codec.ISO8859).getLines(); | |
word <- pattern.split(line.toLowerCase)) { | |
counts(word) = counts.getOrElse(word, 0) + 1 | |
} | |
val array = counts.toArray | |
write(array.sortWith { _._2 > _._2 }, "counts-descreasing-scala.txt") | |
write(array.sortWith { _._1 < _._1 }, "counts-alphabetical-scala.txt") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment