summaryrefslogtreecommitdiffhomepage
path: root/Entropy.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2016-08-07 13:58:41 -0400
committerJoey Hess <joeyh@joeyh.name>2016-08-07 13:58:41 -0400
commit460edfad8ed45412050822dfdf84f2d54015fb04 (patch)
treec356183a9d1952386355d9e861819dae9ad62989 /Entropy.hs
parent6205bbac7abc919371a5e7af9e4ba1a8d70de85e (diff)
downloadkeysafe-460edfad8ed45412050822dfdf84f2d54015fb04.tar.gz
refacor
Diffstat (limited to 'Entropy.hs')
-rw-r--r--Entropy.hs21
1 files changed, 18 insertions, 3 deletions
diff --git a/Entropy.hs b/Entropy.hs
index 2ff28b2..d6779fe 100644
--- a/Entropy.hs
+++ b/Entropy.hs
@@ -1,9 +1,24 @@
+{-# LANGUAGE FlexibleInstances #-}
+
module Entropy where
import Data.List
+import qualified Data.ByteString.UTF8 as B
+
+class ToChars t where
+ toChars :: t -> [Char]
+
+instance ToChars String where
+ toChars = id
-entropy :: String -> Double
-entropy = sum . map lg' . fq' . map (fromIntegral.length) . group . sort
+instance ToChars B.ByteString where
+ toChars = B.toString
+
+shannonEntropy :: ToChars s => s -> Double
+shannonEntropy = sum . map lg' . fq' . map (fromIntegral.length) . group . sort . toChars
where
lg' c = (c * ) . logBase 2 $ 1.0 / c
- fq' c = let sc = sum c in map (/ sc) c
+ fq' c = let sc = sum c in map (/ sc) c
+
+totalEntropy :: ToChars s => s -> Double
+totalEntropy s = shannonEntropy s * fromIntegral (length (toChars s))