view tools/parse.scala @ 2:5cf8de487ed6

Adding sizes analysis
author Joshua Garnett <josh.garnett@gmail.com>
date Thu, 08 Aug 2013 23:06:56 -0400
parents cdfc5e2de435
children
line wrap: on
line source

//
// How to run:  scala -cp json-smart-1.0.9-1.jar parse.scala
//
import net.minidev.json._
import scala.collection.JavaConversions._
import scala.collection.mutable._

val file = scala.io.Source.fromFile("../data/myproblems.json").mkString
val json = JSONValue.parse(file).asInstanceOf[JSONArray]

var totalProblems = json.size
var totalSize = 0
val operatorHash = HashMap[String, Int]()
val allSizes = HashMap[Int, Int]()

for(p <- json) {
  val problem = p.asInstanceOf[JSONObject]
  val size = problem.get("size").asInstanceOf[java.lang.Integer]
  totalSize += size
  val operators = problem.get("operators").asInstanceOf[JSONArray]

  allSizes(size) = allSizes.get(size) match {
    case Some(count) => count + 1
    case None        => 1
  }

  for(o <- operators) {
    val operator = o.asInstanceOf[String]
    operatorHash(operator) = operatorHash.get(operator) match {
      case Some(count) => count + 1
      case None        => 1
    }
  }
}

println("Total problems: " + totalProblems)
println("Total size: " + totalSize)
println("Average size: " + totalSize / totalProblems)

println("Operator Count: ")
for((key, value) <- operatorHash) {
  println("  " + key + ":" + value)
}

println("Size Count: ")
for(key <- allSizes.keys.toList.sorted) {
  println("  " + key + ":" + allSizes(key))
}