diff --git a/.gitignore b/.gitignore index 50af0c3..bae2fd6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ *.iml .idea .project +.vscode target -.vscode \ No newline at end of file diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java new file mode 100644 index 0000000..3f01c76 --- /dev/null +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingCart.java @@ -0,0 +1,92 @@ +package de.hsel.spm.baudas.analysis; + +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; + +/** + * The shopping-cart Analysis. + * + * @author Julian Hinxlage + * @version 0.1 + */ +public class ShoppingCart implements Analysis, List>> { + + private Instances instances; + private Map, List> result; + + @Override + public Map, List> getResult() { + if(result != null) { + return result; + } + + 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..ea22ac1 --- /dev/null +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingCartTest.java @@ -0,0 +1,75 @@ +package de.hsel.spm.baudas.analysis; + +import org.junit.jupiter.api.Test; + +import java.io.File; +import java.net.URL; +import java.time.Duration; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTimeout; + +/** + * Test class for shopping-cart analysis. + * + * @author Johannes Theiner + * @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())); + + AtomicReference, List>> result = new AtomicReference<>(); + assertTimeout(Duration.ofSeconds(4), () -> result.set(cart.getResult())); + + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik"))); + assertEquals(Collections.singletonList("Elektromaterial"), result.get().get(Collections.singletonList("Leuchten"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Schliesstechnik", "Renovierung"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Baustoffe"))); + } + + @Test + void test1000(){ + URL url = getClass().getClassLoader().getResource("kd1000.csv"); + assert url != null; + + ShoppingCart cart = new ShoppingCart(new File(url.getFile())); + + AtomicReference, List>> result = new AtomicReference<>(); + assertTimeout(Duration.ofSeconds(4), () -> result.set(cart.getResult())); + + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Elektromaterial"))); + assertEquals(Collections.singletonList("Elektromaterial"), result.get().get(Arrays.asList("Renovierung", "Leuchten"))); + assertEquals(Collections.singletonList("Sanitaermaterial"), result.get().get(Arrays.asList("Fliesen", "Armaturen"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Renovierung"))); + + } + + @Test + void test10000(){ + URL url = getClass().getClassLoader().getResource("kd10000.csv"); + assert url != null; + + ShoppingCart cart = new ShoppingCart(new File(url.getFile())); + + AtomicReference, List>> result = new AtomicReference<>(); + assertTimeout(Duration.ofSeconds(5), () -> result.set(cart.getResult())); + + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Elektromaterial"))); + assertEquals(Collections.singletonList("Sanitaermaterial"), result.get().get(Arrays.asList("Eisenwaren", "Fliesen", "Armaturen"))); + assertEquals(Collections.singletonList("Elektromaterial"), result.get().get(Arrays.asList("Renovierung", "Leuchten"))); + assertEquals(Collections.singletonList("Eisenwaren"), result.get().get(Arrays.asList("Handwerkzeuge", "Schliesstechnik", "Baustoffe"))); + + } + +}