Skip to content

Instantly share code, notes, and snippets.

@aronayne
Created August 25, 2016 21:13
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 aronayne/60df94935e24bb8390f4973e4edab183 to your computer and use it in GitHub Desktop.
Save aronayne/60df94935e24bb8390f4973e4edab183 to your computer and use it in GitHub Desktop.
object MeasureEntropy extends App {
val s = "measure measure here measure measure measure"
def entropyValue(s: String) = {
val m = s.split(" ").toList.groupBy((word: String) => word).mapValues(_.length.toDouble)
var result: Double = 0.0;
val len = s.split(" ").length;
m map {
case (key, value: Double) =>
{
var frequency: Double = value / len;
result -= frequency * (scala.math.log(frequency) / scala.math.log(2));
}
}
result;
}
println(entropyValue(s))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment