Created
August 1, 2017 05:48
-
-
Save weihsiu/b63f02972802803504277d27e3a7712e to your computer and use it in GitHub Desktop.
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
package ngrams | |
import java.io.{ BufferedReader, FileReader } | |
import scala.collection.mutable | |
object NGrams2 extends App { | |
val start = System.currentTimeMillis | |
// download from https://github.com/codygman/faster-command-line-tools-with-haskell/raw/master/ngrams.tsv.tgz | |
val br = new BufferedReader(new FileReader("ngrams.tsv")) | |
val kvs = mutable.Map.empty[String, Int] | |
var line: String = _ | |
var maxKey: String = _ | |
var maxValue = 0 | |
while ({ line = br.readLine; line != null }) { | |
val ps = line.split('\t') | |
val k = ps(1) | |
val v = ps(2).toInt | |
val v1 = kvs.getOrElse(k, 0) + v | |
if (v1 > maxValue) { | |
maxKey = k | |
maxValue = v1 | |
} | |
kvs(k) = v1 | |
} | |
br.close() | |
println(s"key = $maxKey, value = $maxValue, elapsed time = ${System.currentTimeMillis - start}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment