phase2 finished

This commit is contained in:
IGI-111
2015-04-05 18:57:56 +02:00
parent c108e32c89
commit ac068094a4
4 changed files with 31 additions and 11 deletions

View File

@@ -13,13 +13,14 @@ class Freq a where
data Item = Item String deriving (Eq, Ord) data Item = Item String deriving (Eq, Ord)
instance Show Item where instance Show Item where
show (Item a) = show a show (Item a) = a
data ItemSet = ItemSet (Set Item) deriving (Eq, Ord) data ItemSet = ItemSet (Set Item) deriving (Eq, Ord)
instance Show ItemSet where instance Show ItemSet where
show (ItemSet x) = 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 instance Freq ItemSet where
frequency table (ItemSet set) = frequency table (ItemSet set) =
@@ -40,7 +41,7 @@ empty = ItemSet (Set.fromList [])
data Rule = Rule ItemSet ItemSet deriving (Eq) data Rule = Rule ItemSet ItemSet deriving (Eq)
instance Show Rule where instance Show Rule where
show (Rule a b) = show a ++ "-> " ++ show b show (Rule a b) = show a ++ "->" ++ show b
instance Freq Rule where instance Freq Rule where
frequency table (Rule (ItemSet set1) (ItemSet set2)) = frequency table $ frequency table (Rule (ItemSet set1) (ItemSet set2)) = frequency table $

11
FreqParser.hs Normal file
View File

@@ -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

View File

@@ -8,11 +8,20 @@ import Control.Monad(when)
main :: IO() main :: IO()
main = do main = do
args <- getArgs args <- getArgs
when (2 /= length args) (error "Usage: Main <file.csv> <threshold>") when (2 > length args)
(error "Usage: Main <table.csv> <threshold> <outfile.csv>")
let filename = head args let filename = head args
let threshold = read $ last args let threshold = read $ args !! 1
file <- readFile filename file <- readFile filename
case parseCSV file of case parseCSV file of
Left _ -> error "Could not parse out.csv" Left _ -> error "Could not parse out.csv"
Right val -> mapM_ (\x -> putStrLn (show x ++ "(" ++ show (count table x) ++ ")")) (concat (frequentPatterns threshold table)) where Right val -> do
table = map (ItemSet. Set.fromList .map Item) val 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

View File

@@ -1,5 +1,4 @@
import CSVParser import CSVParser
import FrequentPatterns
import DataModel import DataModel
import ExtractRules import ExtractRules
import qualified Data.Set as Set import qualified Data.Set as Set
@@ -15,6 +14,6 @@ main = do
file <- readFile filename file <- readFile filename
case parseCSV file of case parseCSV file of
Left _ -> error "Could not parse out.csv" Left _ -> error "Could not parse out.csv"
Right val -> print $ extractRules threshold $ Right val -> do
filter (/= empty) $ concat $ frequentPatterns 0.01 table where let table = map (ItemSet. Set.fromList .map Item) val
table = map (ItemSet. Set.fromList .map Item) val print $ extractRules threshold table