Advertisement
Guest User

Entropy

a guest
Mar 27th, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function GetEntropy(text)
  2. {
  3.     var result = 0;
  4.     var stat = Array();
  5.     var sum = 0;
  6.     var i;
  7.  
  8.     for(i = 0; i < 208; i++)
  9.     {
  10.         stat[i] = 0;
  11.     }
  12.  
  13.     for(i = 0; i < text.length; i++)
  14.     {
  15.         if((text.charCodeAt(i) >= 0x400 && text.charCodeAt(i) <= 0x44F))
  16.         {
  17.             stat[text.charCodeAt(i) - 0x400]++;
  18.             sum++;
  19.         }
  20.  
  21.         if((text.charCodeAt(i) >= 0x20 && text.charCodeAt(i) <= 0x7F))
  22.         {
  23.             stat[text.charCodeAt(i) + 0x50 - 0x20]++;
  24.             sum++;
  25.         }
  26.     }
  27.  
  28.     if(sum > 0)
  29.     {
  30.         for(i = 0; i < 208; i++)
  31.         {
  32.             var prob = stat[i] / sum;
  33.             if(prob > 0)
  34.                 result -= (prob * Math.log(prob));
  35.         }
  36.         result /= Math.log(2);
  37.     }
  38.  
  39.     return result / 8 * 100;
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement