<commit_before>{-# LANGUAGE RecordWildCards #-}

module DecisionTreeSpec
    ( main
    , spec
    ) where

import           Ch03DecisionTrees.DecisionTree
import           Data.Binary
import qualified Data.ByteString.Lazy.Char8 as C8
import qualified Data.Map as M
import           MLUtil
import           MLUtil.Graphics
import           MLUtil.Test
import           Test.Hspec

-- Arrow: Eq a, Ord a
newtype Feature = F { unFeature :: Int } deriving (Eq, Ord, Show)
instance ArrowLabel Feature where alLabel = show . unFeature

-- Leaf: Eq l, Ord l
newtype Class = C { unClass :: String } deriving (Eq, Ord, Show)
instance LeafLabel Class where llLabel = unClass

-- Node: Eq n
newtype Label = L { unLabel :: String } deriving (Eq, Show)
instance NodeLabel Label where nlLabel = unLabel

dataSet :: [Record Feature Class]
dataSet =
    [ (F <$> [1, 1], C "yes")
    , (F <$> [1, 1], C "yes")
    , (F <$> [1, 0], C "no")
    , (F <$> [0, 1], C "no")
    , (F <$> [0, 1], C "no")
    ]

labels :: [Label]
labels = L <$> ["no surfacing", "flippers"]

leaf :: String -> Tree Feature Class Label
leaf = Leaf . C

node :: String -> [Arrow Feature Class Label] -> Tree Feature Class Label
node = Node . L

spec :: Spec
spec = do
    describe "calculateShannonEntropy" $ do
        it "should return correct value" $
            calculateShannonEntropy dataSet `shouldRoundTo` 0.97095
<commit_msg>Fix up failing tests<commit_after>{-# LANGUAGE RecordWildCards #-}

module DecisionTreeSpec
    ( main
    , spec
    ) where

import           Ch03DecisionTrees.DecisionTree
import           Data.Binary
import qualified Data.ByteString.Lazy.Char8 as C8
import qualified Data.Map as M
import           MLUtil
import           MLUtil.Graphics
import           MLUtil.Test
import           Test.Hspec

newtype Feature = F Int deriving (Eq, Ord, Show)

newtype Class = C String deriving (Eq, Ord, Show)

newtype Label = L String deriving (Eq, Show)

mkRecord :: ([Int], String) -> Record Feature Class
mkRecord (fs, c) = (F <$> fs, C c)

dataSet :: [Record Feature Class]
dataSet = mkRecord <$>
    [ ([1, 1], "yes")
    , ([1, 1], "yes")
    , ([1, 0], "no")
    , ([0, 1], "no")
    , ([0, 1], "no")
    ]

labels :: [Label]
labels = L <$> ["no surfacing", "flippers"]

leaf :: String -> Tree Feature Class Label
leaf = Leaf . C

node :: String -> [Branch Feature Class Label] -> Tree Feature Class Label
node = Node . L

spec :: Spec
spec = do
    describe "calculateShannonEntropy" $ do
        it "should return correct value" $
            calculateShannonEntropy dataSet `shouldRoundTo` 0.97095

{'fraction_non_alphanumeric': 0.12422839506172839, 'fraction_numerical': 0.015432098765432098, 'mean_word_length': 2.7579710144927536, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4929306', 'n_tokens_mistral': 870, 'n_tokens_neox': 832, 'n_words': 298}