Skip to content

Instantly share code, notes, and snippets.

@weihsiu
Created August 1, 2017 05:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save weihsiu/b63f02972802803504277d27e3a7712e to your computer and use it in GitHub Desktop.
Save weihsiu/b63f02972802803504277d27e3a7712e to your computer and use it in GitHub Desktop.
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