(genreData): finished genreData class
parent
c1fd9e8c4b
commit
c51b656e25
|
|
@ -48,8 +48,22 @@ public class GenreData {
|
|||
* @param vocabulary common top X words to count against
|
||||
* @return Map key: genre, value: (Map: key: word, value: count)
|
||||
*/
|
||||
public static Map<String, List<Map<String, Integer>>> countTrainData(Map<String, List<String>> texts, String[] vocabulary){
|
||||
return null;
|
||||
public static Map<String, List<Map<String, Integer>>> countTrainData(Map<String, List<String>> texts, Set<String> vocabulary){
|
||||
Map<String, List<Map<String, Integer>>> genreWordCounts = new LinkedHashMap<>();
|
||||
|
||||
// Loop thru each genre
|
||||
for(Map.Entry<String, List<String>> textPaths : texts.entrySet()){
|
||||
List<Map<String, Integer>> genreTextCount = new ArrayList<>();
|
||||
// Loop thru all Texts
|
||||
for(String textPath : textPaths.getValue()){
|
||||
Map<String,Integer> textWordCount = countCommonWords(textPath, vocabulary);
|
||||
genreTextCount.add(textWordCount);
|
||||
}
|
||||
|
||||
genreWordCounts.put(textPaths.getKey(), genreTextCount);
|
||||
}
|
||||
|
||||
return genreWordCounts;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -125,9 +139,15 @@ public class GenreData {
|
|||
* @param vocabulary words to count
|
||||
* @return LinkedMap key: word, value: count
|
||||
*/
|
||||
public static Map<String, Integer> countCommonWords(String path, String[] vocabulary){
|
||||
public static Map<String, Integer> countCommonWords(String path, Set<String> vocabulary){
|
||||
// TODO: Scan text line by line and count words from vocabulary.
|
||||
|
||||
Map<String, Integer> vocabCount = new LinkedHashMap<>();
|
||||
|
||||
for(String item : vocabulary){
|
||||
vocabCount.put(item, 0);
|
||||
}
|
||||
|
||||
try {
|
||||
Scanner scanner = new Scanner(new File(path), "UTF-8");
|
||||
|
||||
|
|
@ -140,6 +160,9 @@ public class GenreData {
|
|||
if(word.isEmpty()) continue;
|
||||
|
||||
// If word matches vocabulary
|
||||
if(vocabCount.containsKey(word)){
|
||||
vocabCount.put(word, vocabCount.get(word) +1);
|
||||
}
|
||||
|
||||
}
|
||||
scanner.close();
|
||||
|
|
@ -148,6 +171,6 @@ public class GenreData {
|
|||
IO.println("Couldn't find file " + path + " err: " + e.getLocalizedMessage());
|
||||
}
|
||||
|
||||
return null;
|
||||
return vocabCount;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,4 +50,36 @@ public class DetectorTests {
|
|||
System.out.println("Total paths: " + allPaths);
|
||||
Map <String, Integer> vocabulary = GenreData.countWords(allPaths, 500);
|
||||
}
|
||||
|
||||
@DisplayName("Count Words against Vocabulary")
|
||||
@Test
|
||||
public void testCountWords(){
|
||||
Map<String, List<String>> genreData = GenreData.loadGenres("data/labels.csv");
|
||||
List<String> allPaths = genreData.values()
|
||||
.stream()
|
||||
.flatMap(List::stream)
|
||||
.toList();
|
||||
|
||||
System.out.println("Genres: " + genreData.size());
|
||||
System.out.println("Total paths: " + allPaths);
|
||||
Map <String, Integer> vocabulary = GenreData.countWords(allPaths, 500);
|
||||
|
||||
Map<String, Integer> wordCount = GenreData.countCommonWords("data/train/adventure/2154_around-the-world-in-eighty-days-junior-deluxe-edit.txt", vocabulary.keySet());
|
||||
}
|
||||
|
||||
@DisplayName("Count training words against Vocabulary")
|
||||
@Test
|
||||
public void testCountTrainingWords(){
|
||||
Map<String, List<String>> genreData = GenreData.loadGenres("data/labels.csv");
|
||||
List<String> allPaths = genreData.values()
|
||||
.stream()
|
||||
.flatMap(List::stream)
|
||||
.toList();
|
||||
|
||||
System.out.println("Genres: " + genreData.size());
|
||||
System.out.println("Total paths: " + allPaths);
|
||||
Map <String, Integer> vocabulary = GenreData.countWords(allPaths, 500);
|
||||
|
||||
Map<String, List<Map<String, Integer>>> trainingCounts = GenreData.countTrainData(genreData, vocabulary.keySet());
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,10 +4,7 @@ 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 java.util.*;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
|
|
@ -16,7 +13,13 @@ public class GenreDataTests {
|
|||
@DisplayName("Test Training Word Count against vocabulary")
|
||||
@Test
|
||||
public void testCountTrainInputString(){
|
||||
String[] vocabulary = {"the, suspects, nobody, a, another, after"};
|
||||
Set<String> vocabulary = new HashSet<>();
|
||||
vocabulary.add("the");
|
||||
vocabulary.add("suspects");
|
||||
vocabulary.add("nobody");
|
||||
vocabulary.add("a");
|
||||
vocabulary.add("another");
|
||||
vocabulary.add("after");
|
||||
|
||||
List<String> texts = new ArrayList<>();
|
||||
texts.add("The locked room held three suspects, yet nobody held keys.");
|
||||
|
|
|
|||
Loading…
Reference in New Issue