Skip to content

Instantly share code, notes, and snippets.

@Fabszn
Created April 18, 2014 17:19
Show Gist options
  • Save Fabszn/11054896 to your computer and use it in GitHub Desktop.
Save Fabszn/11054896 to your computer and use it in GitHub Desktop.
Analyse of file from data.gouv.fr
import scala.io.Source
import scala.util.parsing.json.{JSONObject, JSONFormat, JSONArray}
/**
* Created by fsznajderman on 16/04/2014.
*/
object main {
case class Accident(lum: Int, agg: Int, int: Int, atm: Int, col: Int, catr: Int, grav: Float)
case class Result(lbl: String, nb: Int, avg: Float) {
override def toString: String = s"{ 'title' : $lbl ,'nb' : $nb, 'avg' : $avg} "
}
def main(args: Array[String]) {
val lines = Source.fromFile("/Users/fsznajderman/Documents/Opendata/DataAccidentsCleaned.csv").getLines().toList
def c(v: String): Int = Option[String](v).filter(s => !s.isEmpty).map(s => s.toInt).getOrElse(0)
def f(v: String): Float = Option[String](v).filter(s => !s.isEmpty).map(s => s.toFloat).getOrElse(0)
val allAccidents = lines.map(line => {
val l = line.split(",")
Accident(c(l(1)), c(l(2)), c(l(3)), c(l(4)), c(l(5)), c(l(8)), f(l(30)))
})
val result = List(('lum, compute(allAccidents.groupBy(i => i.lum))),
('agg, compute(allAccidents.groupBy(i => i.agg))),
('int, compute(allAccidents.groupBy(i => i.int))),
('atm, compute(allAccidents.groupBy(i => i.atm))),
('col, compute(allAccidents.groupBy(i => i.col))),
('catr, compute(allAccidents.groupBy(i => i.catr))))
println("{")
result.foreach(l => formatOutput(l._1, l._2))
println("}")
}
def formatOutput(s: Symbol, l: Iterable[Result]) = {
println(s"$s':[")
l.foreach(item => println(item + ","))
print("],")
}
def avg(data: List[Accident]) = data.map(acc => acc.grav).sum / data.size
def compute(datas: Map[Int, List[Accident]]) = datas.keys.map(k => Result(k.toString, datas(k).size, avg(datas(k))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment