User Tools

Site Tools


shannon_entropy_calculator

Shannon Entropy Calculator

<PRELOAD> ./lib/scripts/jquery/jquery.min.js </PRELOAD> <CSS> span.entropy_small { font-size: 0.7em; color: #888888; } pre.entropy { font-size: 1em; font-family: “Lucida Console”, Monaco, monospace; } pre.entropy_smaller { font-size: 0.8em; font-family: “Lucida Console”, Monaco, monospace; } input.entropy, textarea.entropy { font-size: 1.1em; font-family: “Lucida Grande”, sans-serif; width: 80%; } </CSS> <HTML> <textarea type=“text” name=“expr” id=“expr” rows=“3” class=“entropy”></textarea> <br> <span class=“entropy_small”>* use STRING SPACE COUNT, separated by comma, eg. abc 3, def 4</span> <p> <pre id=“result” name=“result” class=“entropy”>H = …</pre> <pre id=“detail1” name=“detail1” class=“entropy_smaller”>…</pre> <pre id=“detail2” name=“detail2” class=“entropy_smaller”>…</pre> </pre> </HTML> <JS> function setCookie(cname, cvalue, exdays) {

var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + "; " + expires;

} function getCookie(cname) {

var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1);
    if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
}
return "";

} function unrtrim (str, max) {

str = str.toString();
return str.length < max ? unrtrim(str + " ", max) : str;

} function unltrim (str, max) {

str = str.toString();
return str.length < max ? unltrim(" " + str, max) : str;

} var re = /(.*) ([0-9]+)/; $( document ).ready(function() {

  $("#expr").keyup(function () {
      var parts = $(this).val().split(',');
      var values = {};
      var sum = 0;
      var num_values = 0;
      $('#result').text("");
      $('#detail1').text("");
      $('#detail2').text("");
      $.each(parts, function (index, value) {
          value = value.trim();
          var matches = re.exec(value);
          if (matches) {
              if (values[matches[1]]) {
                  $('#detail2').append(unltrim((index + 1), 3) + ". ERROR! Value \"" + matches[1] + "\" was already used.\n");
              } else {
                  $('#detail2').append(unltrim((index + 1), 3) + ". " + unrtrim(matches[1], 12) + ' * ' + unltrim(matches[2], 3) + "\n");
                  values[matches[1]] = matches[2];
                  sum += parseInt(matches[2]);
                  num_values++;
              }
          } else {
              $('#detail2').append(unltrim((index + 1), 3) + ". ERROR! Value has invalid format.\n");
          }
      });
      var h = 0;
      $.each(values, function(index, value) {
          var p = value / sum;
          var l = Math.log(p) / Math.log(2);
          h -= (p * l);
      });
      $('#result').append("H = " + h);
      $('#detail1').append(num_values + " valid values");
      if (parts.length != num_values) {
          $('#detail1').append(", " + (parts.length - num_values) + " ERRORs");
      }
      setCookie("expr", $("#expr").val(), 365);
  });
  if ($("#expr").val() == "") {
      $("#expr").val(getCookie("expr"));
  }
  $("#expr").trigger('keyup');

}); </JS>

shannon_entropy_calculator.txt · Last modified: by 127.0.0.1