diff --git a/src/main/java/de/nicode3141/genredct/Detector.java b/src/main/java/de/nicode3141/genredct/Detector.java index e48836a..180ef57 100644 --- a/src/main/java/de/nicode3141/genredct/Detector.java +++ b/src/main/java/de/nicode3141/genredct/Detector.java @@ -47,7 +47,7 @@ public class Detector { * @param bias constant to adjust the perceptron * @return sum of all weighted word counts */ - double calcSum(Map inputText, double[] weights, double bias){ + public static double calcSum(Map inputText, double[] weights, double bias){ // TODO: loop thru the map and add the (word count / total words) * weight to it // After the loop, increment the sum by the bias return 0.0d; diff --git a/src/main/java/de/nicode3141/genredct/GenreData.java b/src/main/java/de/nicode3141/genredct/GenreData.java index 0770420..c44f99a 100644 --- a/src/main/java/de/nicode3141/genredct/GenreData.java +++ b/src/main/java/de/nicode3141/genredct/GenreData.java @@ -1,5 +1,6 @@ package de.nicode3141.genredct; +import java.util.List; import java.util.Map; public class GenreData { @@ -7,19 +8,20 @@ public class GenreData { /** * This method gets all books and their corresponding label * @param csvPath Path of the labels.csv file - * @return Map key: genre, value: path + * @return Map key: genre, value: paths */ - public static Map loadGenres(String csvPath){ - // TODO: Loop thru all paths and return a Map of + public static Map> loadGenres(String csvPath){ + // TODO: Loop thru all paths and return a Map of return null; } /** * Counts all words against the common vocab and categorizes them to their genre. * @param texts label, path of the genre texts + * @param vocabulary common top X words to count against * @return Map key: genre, value: (Map: key: word, value: count) */ - public static Map> countTrainData(Map texts){ + public static Map>> countTrainData(Map> texts, String[] vocabulary){ return null; } @@ -29,7 +31,7 @@ public class GenreData { * @param limit only counts the top (limit) words * @return LinkedMap key: word, value: count */ - public static Map countWords(String paths[], int limit){ + public static Map countWords(String[] paths, int limit){ // TODO: Scan texts line by line and count already added Words. // If word was not present, add it to the Map. // After that, sort the Map. Simple with ArrayList sort() diff --git a/src/test/java/AuthorTests.java b/src/test/java/AuthorTests.java deleted file mode 100644 index a6fe164..0000000 --- a/src/test/java/AuthorTests.java +++ /dev/null @@ -1,10 +0,0 @@ -import de.nicode3141.Main; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -public class AuthorTests { - -} diff --git a/src/test/java/genredct/DetectorTests.java b/src/test/java/genredct/DetectorTests.java new file mode 100644 index 0000000..c2dc9ba --- /dev/null +++ b/src/test/java/genredct/DetectorTests.java @@ -0,0 +1,31 @@ +package genredct; + +import de.nicode3141.genredct.Detector; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class DetectorTests { + @DisplayName("Calculate Sum") + @Test + public void testCalcSum(){ + // For the text: The locked room held three suspects, yet nobody held keys. + int totalWords = 10; + Map wordCounts = new HashMap<>(); + wordCounts.put("the", 1); + wordCounts.put("suspects", 1); + wordCounts.put("nobody", 1); + wordCounts.put("a", 0); + wordCounts.put("another", 0); + wordCounts.put("after", 0); + + double weigths[] = {0.1d, 0.0d, -1.2d, 3.4d, 5,9d, 0.0d}; + double bias = 0.0d; + double sum = -0.11d; + assertEquals(sum, Detector.calcSum(wordCounts, weigths, bias)); + } +} diff --git a/src/test/java/genredct/GenreDataTests.java b/src/test/java/genredct/GenreDataTests.java new file mode 100644 index 0000000..ac0d9be --- /dev/null +++ b/src/test/java/genredct/GenreDataTests.java @@ -0,0 +1,41 @@ +package genredct; + +import de.nicode3141.genredct.GenreData; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class GenreDataTests { + + @DisplayName("Test Training Word Count against vocabulary") + @Test + public void testCountTrainInputString(){ + String[] vocabulary = {"the, suspects, nobody, a, another, after"}; + + List texts = new ArrayList<>(); + texts.add("The locked room held three suspects, yet nobody held keys."); + Map> textGenres = new HashMap<>(); + textGenres.put("mystery", texts); + + Map expectedWordCount = new HashMap<>(); + expectedWordCount.put("the", 1); + expectedWordCount.put("suspects", 1); + expectedWordCount.put("nobody", 1); + expectedWordCount.put("a", 0); + expectedWordCount.put("another", 0); + expectedWordCount.put("after", 0); + + List> expectedWordCounts = new ArrayList<>(); + expectedWordCounts.add(expectedWordCount); + Map>> expected = new HashMap<>(); + expected.put("mystery", expectedWordCounts); + + assertEquals(expected, GenreData.countTrainData(textGenres, vocabulary)); + } +}