From 221c2aea920e8d2f0c976c680756aa47253376fa Mon Sep 17 00:00:00 2001 From: Julian Hinxlage Date: Sun, 5 May 2019 10:38:12 +0200 Subject: [PATCH] +Shopping cart analysis #SPM-23: add work 2h development --- .../spm/baudas/analysis/ShoppingCart.java | 83 ++++++++++++++++--- .../spm/baudas/analysis/ShoppingCartTest.java | 51 ++++++++++++ 2 files changed, 123 insertions(+), 11 deletions(-) create mode 100644 src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java index f75c786..7153b1e 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java @@ -1,26 +1,87 @@ package de.hsel.spm.baudas.analysis; - -import org.jetbrains.annotations.Nullable; +import weka.associations.Apriori; +import weka.associations.AssociationRule; +import weka.associations.Item; import weka.core.Instances; +import weka.filters.Filter; +import weka.filters.unsupervised.attribute.NumericCleaner; +import weka.filters.unsupervised.attribute.NumericToNominal; import java.io.File; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -public class ShoppingCart implements Analysis>>{ +/** + * The shopping-cart Analysis. + * + * @author Julian Hinxlage + * @version 0.1 + */ +public class ShoppingCart implements Analysis, List>> { - - @Nullable - @Override - public Instances load(File file) { - return null; - } + private Instances instances; @Override - public Map> getResult() { - Map> result = new HashMap<>(); + public Map, List> getResult() { + Map, List> result = new HashMap<>(); + + Apriori model = new Apriori(); + + for (int i = 0; i < 11; i++) { + instances.deleteAttributeAt(0); + } + + //alle artikel anzahlen auf 0 oder 1 setzen + NumericCleaner nc = new NumericCleaner(); + nc.setMaxThreshold(1.0); + nc.setMaxDefault(1.0); + try { + nc.setInputFormat(instances); + instances = Filter.useFilter(instances, nc); + } catch (Exception e) { + e.printStackTrace(); + } + + //zahl werte zu nomial werten umwandeln + NumericToNominal num2nom = new NumericToNominal(); + num2nom.setAttributeIndices("first-last"); + try { + num2nom.setInputFormat(instances); + instances = Filter.useFilter(instances, num2nom); + } catch (Exception e) { + e.printStackTrace(); + } + + model.setTreatZeroAsMissing(true); + try { + model.buildAssociations(instances); + } catch (Exception e) { + e.printStackTrace(); + } + + for(int i = 0; i < model.getAssociationRules().getRules().size();i++){ + AssociationRule rule = model.getAssociationRules().getRules().get(i); + + List l1 = new ArrayList<>(); + for(Item item : rule.getPremise()){ + l1.add(item.getAttribute().name()); + } + List l2 = new ArrayList<>(); + for(Item item : rule.getConsequence()){ + l2.add(item.getAttribute().name()); + } + + result.put(l1,l2); + } + + return result; } + + public ShoppingCart(File file) { + instances = load(file); + } } diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java new file mode 100644 index 0000000..e9e161d --- /dev/null +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java @@ -0,0 +1,51 @@ +package de.hsel.spm.baudas.analysis; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.net.URL; +import java.util.List; +import java.util.Map; + +/** + * Test class for shopping-cart analysis. + * + * @author Julian Hinxlage + * @version 0.1 + */ +class ShoppingCartTest { + + @Test + void test100(){ + URL url = getClass().getClassLoader().getResource("kd100.csv"); + assert url != null; + + ShoppingCart cart = new ShoppingCart(new File(url.getFile())); + Map, List> result = cart.getResult(); + + System.out.println(result); + } + + @Test + void test1000(){ + URL url = getClass().getClassLoader().getResource("kd1000.csv"); + assert url != null; + + ShoppingCart cart = new ShoppingCart(new File(url.getFile())); + Map, List> result = cart.getResult(); + + System.out.println(result); + } + + @Test + void test10000(){ + URL url = getClass().getClassLoader().getResource("kd10000.csv"); + assert url != null; + + ShoppingCart cart = new ShoppingCart(new File(url.getFile())); + Map, List> result = cart.getResult(); + + System.out.println(result); + } + +}