From e6f0a9ce6f6f73a94b5f632221d59371a53d203b Mon Sep 17 00:00:00 2001 From: Johannes Theiner Date: Tue, 23 Apr 2019 14:12:05 +0200 Subject: [PATCH] =?UTF-8?q?Funktionierende=20Version=20=C3=BCber=20kleinen?= =?UTF-8?q?=20Umweg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Johannes Theiner #SPM-17: add work development 1h 30m funktionierende Version --- .../de/hsel/spm/baudas/analysis/DayHour.java | 4 ++ .../spm/baudas/analysis/ShoppingTimes.java | 49 +++++++------------ .../baudas/analysis/ShoppingTimesTest.java | 9 ++-- 3 files changed, 24 insertions(+), 38 deletions(-) diff --git a/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java b/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java index 054032e..52f7793 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/DayHour.java @@ -1,6 +1,8 @@ package de.hsel.spm.baudas.analysis; +import lombok.AllArgsConstructor; import lombok.Data; +import lombok.EqualsAndHashCode; /** * @author Johannes Theiner @@ -9,6 +11,8 @@ import lombok.Data; **/ @Data +@EqualsAndHashCode +@AllArgsConstructor public class DayHour { private String day; diff --git a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java index bc68fea..3a97155 100644 --- a/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java +++ b/src/main/java/de/hsel/spm/baudas/analysis/ShoppingTimes.java @@ -1,10 +1,13 @@ package de.hsel.spm.baudas.analysis; +import weka.core.AttributeStats; +import weka.core.Instance; import weka.core.Instances; import weka.filters.Filter; import weka.filters.unsupervised.attribute.Remove; import java.io.File; +import java.util.HashMap; import java.util.Map; /** @@ -25,6 +28,7 @@ public class ShoppingTimes implements Analysis> { @Override public Map getResult() { if (result == null) { + result = new HashMap<>(); int[] keepIndexes = new int[]{Attribute.SHOPPING_DAY, Attribute.SHOPPING_HOUR}; int dayIndex = 0; int hourIndex = 1; @@ -39,40 +43,21 @@ public class ShoppingTimes implements Analysis> { ex.printStackTrace(); } - - - /*try { - SubsetByExpression byExpression = new SubsetByExpression(); - byExpression.setExpression("(1 == Montag)"); - byExpression.setInputFormat(instances); - instances = Filter.useFilter(instances, byExpression); - } catch (Exception e) { - e.printStackTrace(); + for (int i = 0; i < instances.attribute(dayIndex).numValues(); i++) { + String day = instances.attribute(dayIndex).value(i); + Instances instancesCopy = new Instances(instances, 0, instances.numInstances()); + for (int j = instancesCopy.numInstances() - 1; j >= 0; j--) { + Instance instance = instancesCopy.get(j); + if(!instance.stringValue(dayIndex).equals(day)) + instancesCopy.delete(j); + } + AttributeStats stats = instancesCopy.attributeStats(hourIndex); + for (int j = 0; j < instancesCopy.attribute(hourIndex).numValues(); j++) { + DayHour dayHour = new DayHour(day, instancesCopy.attribute(hourIndex).value(j)); + result.put(dayHour, stats.nominalCounts[j]); + } } - - Enumeration dayValues = instances.attribute(dayIndex).enumerateValues(); - int i = 0; - while (dayValues.hasMoreElements()) { - System.out.println(dayValues.nextElement() + " " + instances.attributeStats(dayIndex).nominalCounts[i]); - i++; - } - - Enumeration hourValues = instances.attribute(hourIndex).enumerateValues(); - i = 0; - while (hourValues.hasMoreElements()) { - System.out.println(hourValues.nextElement() + " " + instances.attributeStats(hourIndex).nominalCounts[i]); - i++; - }*/ - - - - /* AttributeStats stats = instances.attributeStats(hourIndex); - for (i = 0; i <= instances.attribute(hourIndex).numValues() - 1; i++) { - System.out.println(instances.attribute(hourIndex).value(i) + " " + stats.nominalCounts[i]); - }*/ - - } return result; } diff --git a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java index fdc4d29..14fad53 100644 --- a/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java +++ b/src/test/java/de/hsel/spm/baudas/analysis/ShoppingTimesTest.java @@ -8,6 +8,7 @@ import java.time.Duration; 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; /** @@ -27,13 +28,9 @@ public class ShoppingTimesTest { AtomicReference> results = new AtomicReference<>(); - assertTimeout(Duration.ofMillis(500), () -> results.set(overview.getResult())); - /*for(Pair pair : results.get().keySet()) { - System.out.println(pair); - }*/ + assertTimeout(Duration.ofMillis(1300), () -> results.set(overview.getResult())); - - //assertEquals(21, results.get().get(new Pair<>("Montag", ">17 Uhr"))); + assertEquals(4, results.get().get(new DayHour("Montag", "10-12 Uhr"))); //TODO: mehr Tests }