This commit is contained in:
Johannes Theiner 2019-05-16 13:25:56 +02:00
commit ce5e3a1f27
3 changed files with 168 additions and 1 deletions

2
.gitignore vendored
View File

@ -1,5 +1,5 @@
*.iml
.idea
.project
target
.vscode
target

View File

@ -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<Map<List<String>, List<String>>> {
private Instances instances;
private Map<List<String>, List<String>> result;
@Override
public Map<List<String>, List<String>> 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<String> l1 = new ArrayList<>();
for(Item item : rule.getPremise()){
l1.add(item.getAttribute().name());
}
List<String> 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);
}
}

View File

@ -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<Map<List<String>, List<String>>> 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<Map<List<String>, List<String>>> 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<Map<List<String>, List<String>>> 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")));
}
}