From ac068094a45f781db2d80d6579c78de2bab3132d Mon Sep 17 00:00:00 2001 From: IGI-111 Date: Sun, 5 Apr 2015 18:57:56 +0200 Subject: [PATCH] phase2 finished --- DataModel.hs | 7 ++++--- FreqParser.hs | 11 +++++++++++ phase1.hs | 17 +++++++++++++---- phase2.hs | 7 +++---- 4 files changed, 31 insertions(+), 11 deletions(-) create mode 100644 FreqParser.hs diff --git a/DataModel.hs b/DataModel.hs index ae175a1..7ce2e48 100644 --- a/DataModel.hs +++ b/DataModel.hs @@ -13,13 +13,14 @@ class Freq a where data Item = Item String deriving (Eq, Ord) instance Show Item where - show (Item a) = show a + show (Item a) = a + data ItemSet = ItemSet (Set Item) deriving (Eq, Ord) instance Show ItemSet where show (ItemSet x) = - foldr ((\y old -> y ++ " " ++ old).show) "" (Set.toList x) + init $ foldr ((\y old -> y ++ " " ++ old).show) "" (Set.toList x) instance Freq ItemSet where frequency table (ItemSet set) = @@ -40,7 +41,7 @@ empty = ItemSet (Set.fromList []) data Rule = Rule ItemSet ItemSet deriving (Eq) instance Show Rule where - show (Rule a b) = show a ++ "-> " ++ show b + show (Rule a b) = show a ++ "->" ++ show b instance Freq Rule where frequency table (Rule (ItemSet set1) (ItemSet set2)) = frequency table $ diff --git a/FreqParser.hs b/FreqParser.hs new file mode 100644 index 0000000..62a8a1d --- /dev/null +++ b/FreqParser.hs @@ -0,0 +1,11 @@ +module ItemsParser ( + parseItems, + parseItemsWithFreq + )where +import qualified Data.Map as Map +import Data.Map(Map) +import DataModel + +parseItems :: String -> Either ParseError [ItemSet] + +parseItemsWithFreq :: String -> Either ParseError Map ItemSet Frequency diff --git a/phase1.hs b/phase1.hs index 628ae72..afb0f5a 100644 --- a/phase1.hs +++ b/phase1.hs @@ -8,11 +8,20 @@ import Control.Monad(when) main :: IO() main = do args <- getArgs - when (2 /= length args) (error "Usage: Main ") + when (2 > length args) + (error "Usage: Main ") let filename = head args - let threshold = read $ last args + let threshold = read $ args !! 1 file <- readFile filename case parseCSV file of Left _ -> error "Could not parse out.csv" - Right val -> mapM_ (\x -> putStrLn (show x ++ "(" ++ show (count table x) ++ ")")) (concat (frequentPatterns threshold table)) where - table = map (ItemSet. Set.fromList .map Item) val + Right val -> do + let table = map (ItemSet. Set.fromList .map Item) val + let freqPats = concat (frequentPatterns threshold table) + mapM_ (\x -> putStrLn (show x ++ "(" ++ show (count table x) ++ ")")) freqPats + when (length args > 2) $ + writeFile (args !! 2) $ formatToCSV freqPats + +formatToCSV :: [ItemSet] -> String +formatToCSV = foldr (\x old -> old ++ formatRow x ++ "\n") "" where + formatRow (ItemSet set) = init $ Set.foldr (\x old -> old ++ show x ++ ",") "" set diff --git a/phase2.hs b/phase2.hs index 2226135..7240594 100644 --- a/phase2.hs +++ b/phase2.hs @@ -1,5 +1,4 @@ import CSVParser -import FrequentPatterns import DataModel import ExtractRules import qualified Data.Set as Set @@ -15,6 +14,6 @@ main = do file <- readFile filename case parseCSV file of Left _ -> error "Could not parse out.csv" - Right val -> print $ extractRules threshold $ - filter (/= empty) $ concat $ frequentPatterns 0.01 table where - table = map (ItemSet. Set.fromList .map Item) val + Right val -> do + let table = map (ItemSet. Set.fromList .map Item) val + print $ extractRules threshold table