Created
December 30, 2009 07:22
-
-
Save virasak/265899 to your computer and use it in GitHub Desktop.
Remove projection if you don't want to println(dir)
This file contains hidden or 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
import java.io.{Closeable, File, FileWriter, PrintWriter} | |
val rootDir = new File("/home/virasak/.vim") | |
if (!rootDir.exists) throw new IllegalArgumentException(rootDir + " does not exist") | |
val counts = scala.collection.mutable.Map.empty[String, Int] | |
for { | |
dir <- rootDir.listFiles.projection if dir.isDirectory && println("Processing " + dir) == () | |
file <- dir.listFiles if file.isFile | |
line <- scala.io.Source.fromFile(file, "ISO-8859-1").getLines | |
word <- line.split("""\W+""").map(_.toLowerCase) | |
} counts(word) = counts.getOrElse(word, 0) + 1 | |
val countList = counts.toList | |
println("Writing counts in decreasing order") | |
write(countList.sort(_._2 > _._2), "counts-descreasing-scala") | |
println("Writing counts in alphabetical order") | |
write(countList.sort(_._1 < _._1), "counts-alphabetical-scala") | |
def write(counts: List[(String,Int)], fileName: String) { | |
using(new PrintWriter(new FileWriter(fileName))) { out => | |
for ((word, count) <- counts) out.println(word + "\t" + count) | |
} | |
} | |
def using[A <: Closeable, B](a: A)(f: A => B) = try { f(a) } finally { a.close } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment