various improvements, corrected logic errors
This commit is contained in:
@@ -6,8 +6,8 @@ import qualified Data.Set as Set
|
|||||||
import qualified Data.Map as Map
|
import qualified Data.Map as Map
|
||||||
import Data.Map(Map)
|
import Data.Map(Map)
|
||||||
|
|
||||||
extractRules :: Confidence -> [ItemSet] -> [Rule]
|
extractRules :: Confidence -> [ItemSet] -> [ItemSet] -> [Rule]
|
||||||
extractRules threshold patterns = filter (\x -> threshold <= confidence patterns x) rules where
|
extractRules threshold table patterns = filter (\x -> threshold <= confidence table x) rules where
|
||||||
rules = Map.foldrWithKey (\k v old -> ruleFromSubset v k : old) [] subsets
|
rules = Map.foldrWithKey (\k v old -> ruleFromSubset v k : old) [] subsets
|
||||||
subsets = foldr (\x old -> insertMultiple (filteredPowerset x) x old) Map.empty patterns
|
subsets = foldr (\x old -> insertMultiple (filteredPowerset x) x old) Map.empty patterns
|
||||||
filteredPowerset (ItemSet set) = map (ItemSet . Set.fromList) $
|
filteredPowerset (ItemSet set) = map (ItemSet . Set.fromList) $
|
||||||
|
|||||||
25
phase2.hs
25
phase2.hs
@@ -8,12 +8,21 @@ import Control.Monad(when)
|
|||||||
main :: IO()
|
main :: IO()
|
||||||
main = do
|
main = do
|
||||||
args <- getArgs
|
args <- getArgs
|
||||||
when (2 /= length args) (error "Usage: phase2 <file.csv> <threshold>")
|
when (3 /= length args) (error "Usage: phase2 <table.csv> <frequents.csv> <threshold>")
|
||||||
let filename = head args
|
|
||||||
let threshold = read $ last args
|
let threshold = read $ last args
|
||||||
file <- readFile filename
|
tableFile <- readFile $ head args
|
||||||
case parseCSV file of
|
freqFile <- readFile $ args !! 1
|
||||||
Left _ -> error "Could not parse out.csv"
|
case parseCSV tableFile of
|
||||||
Right val -> do
|
Left _ -> error "Could not parse table"
|
||||||
let table = map (ItemSet. Set.fromList .map Item) val
|
Right tableFileContent ->
|
||||||
print $ extractRules threshold table
|
case parseCSV freqFile of
|
||||||
|
Left _ -> error "Could not parse frequent patterns"
|
||||||
|
Right freqFileContent -> do
|
||||||
|
putStrLn output
|
||||||
|
when (length args > 2) $
|
||||||
|
writeFile (args !! 2) $ output
|
||||||
|
where
|
||||||
|
freqPats = map (ItemSet. Set.fromList .map Item) (map tail freqFileContent)
|
||||||
|
table = map (ItemSet. Set.fromList .map Item) tableFileContent
|
||||||
|
rules = extractRules threshold table freqPats
|
||||||
|
output = init $ foldr (\x old -> old++x++"\n") "" $ map show rules
|
||||||
|
|||||||
Reference in New Issue
Block a user