Created
April 24, 2021 10:30
-
-
Save jonasfj/3bb5b7fa7c8d4999efdf954790e29e03 to your computer and use it in GitHub Desktop.
How compute shannon entropy of a string in Dart.
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
// Copyright 2021 Google LLC. | |
// SPDX-License-Identifier: Apache-2.0 | |
import 'dart:math'; | |
double entropy(String s) { | |
final length = s.length.toDouble(); | |
// TODO: Explore if this could be done faster without any allocations, since strings are often short. | |
final frequencies = <int, int>{}; | |
for (final rune in s.runes) { | |
frequencies[rune] = (frequencies[rune] ?? 0) + 1; | |
} | |
var sum = 0.0; | |
for (final frequency in frequencies.values) { | |
sum -= frequency / length * (log(frequency / length) / log(2)); | |
} | |
return sum; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment