Advertisement
Lusien_Lashans

Entropy

Mar 13th, 2017
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. WSH.echo('Type str in console');
  2. var str = WScript.StdIn.ReadLine();
  3. var alf = new Array();
  4. /*Initialization*/
  5. for (var i = 0; i < str.length; i++){
  6. alf[str.charAt(i)] = 0;
  7. }
  8. /*frq calc*/
  9. for (var i = 0; i < str.length; i++){
  10. alf[str.charAt(i)]++;
  11. }
  12.  
  13. var sum = 0.0;
  14. var entropy = 0.0;
  15. var n = 0;
  16. for (i in alf){
  17.     sum += alf[i];
  18.     n++;
  19. }
  20. //WSH.echo(alf.length);
  21. for (i in alf){
  22.     alf[i] = alf[i]/sum;
  23. }
  24. if (n>1)
  25. for (i in alf){
  26.     entropy += (alf[i])*((Math.log(alf[i]))/(Math.log(n)));
  27. }
  28.  
  29. else
  30.     entropy = -1;
  31. entropy *= -1;
  32. if (str.length == 0)
  33.     WSH.echo("str is null");
  34. else
  35. WSH.echo(entropy);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement