From 1496af9135ad5cd7e18f884bad7b5012f275887e Mon Sep 17 00:00:00 2001 From: IGI-111 Date: Thu, 2 Apr 2015 10:16:04 +0200 Subject: [PATCH] added formatting --- apriori.hs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/apriori.hs b/apriori.hs index 6fb55d4..8bdfb49 100644 --- a/apriori.hs +++ b/apriori.hs @@ -48,7 +48,7 @@ parseCSV :: String -> Either ParseError [ItemSet] parseCSV input = parse csvFile ("unknown") input -- frequent items -frequentItems :: Integral a => [ItemSet] -> Support -> Map Item a +frequentItems :: [ItemSet] -> Support -> Map Item Integer frequentItems x y = pi where pi = Map.filter (>= frequency) m where frequency = ceiling(y * (fromIntegral $ length x)) where @@ -57,8 +57,19 @@ frequentItems x y = pi where f m i = Map.insertWith acc i 1 m where acc new old = old + 1 --- frequent itemsets --- frequentItemSets :: [ItemSet] -> [Item] -> Support -> [ItemSet] +displayFrequentItems :: Map Item Integer -> String +displayFrequentItems i = Map.foldrWithKey (\key val old -> old ++ (itemToString key val) ++ "\n" ) "" i where + itemToString (Item str) freq = str++" ("++(show freq)++")" + +-- frequentItemSets :: [ItemSet] -> Support -> Map ItemSet Integer +-- frequentItemSets x y = pi where +-- pi = Map.filter (>= frequency) candidates where +-- frequency = ceiling(y * (fromIntegral $ length x)) where +-- candidates = foldl count Map.empty x where +-- count m (ItemSet is) = Set.foldl f m is where +-- f m i = Map.insertWith acc i 1 m where +-- acc new old = old + 1 + main :: IO () main = do @@ -70,21 +81,10 @@ main = do print e Right r -> do let i = frequentItems r (read s) - print $ i + putStrLn $ displayFrequentItems i -- helper functions defined here -trim :: [Char] -> [Char] -trim = trimFront . trimBack - -trimFront :: [Char] -> [Char] -trimFront (x:xs) = case x of - ' ' -> trim xs - _ -> (x:xs) - -trimBack :: [Char] -> [Char] -trimBack x = reverse $ trimFront $ reverse x - allItems :: [ItemSet] -> [Item] allItems iss = Set.toList $ foldl add Set.empty iss where add s (ItemSet is) = Set.union s is