Created
August 25, 2016 21:13
-
-
Save aronayne/60df94935e24bb8390f4973e4edab183 to your computer and use it in GitHub Desktop.
This file contains 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
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