Skip to content

Instantly share code, notes, and snippets.

@jonasfj
Created April 24, 2021 10:30
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 jonasfj/3bb5b7fa7c8d4999efdf954790e29e03 to your computer and use it in GitHub Desktop.
Save jonasfj/3bb5b7fa7c8d4999efdf954790e29e03 to your computer and use it in GitHub Desktop.
How compute shannon entropy of a string in Dart.
// 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