blob: 699fd918893de191dd8f6ab90d122d8e5921ec7a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package entropy
import "math"
// Shannon measures the Shannon entropy of a string.
// See http://bearcave.com/misl/misl_tech/wavelets/compression/shannon.html for the algorithmic explanation.
func Shannon(value string) (bits int) {
frq := make(map[rune]float64)
//get frequency of characters
for _, i := range value {
frq[i]++
}
var sum float64
for _, v := range frq {
f := v / float64(len(value))
sum += f * math.Log2(f)
}
bits = int(math.Ceil(sum*-1)) * len(value)
return
}
|