(main): genre detection now working

master
nicode3141 2026-11-14 17:57:07 +01:00
parent 9852815267
commit a4fe46bc2a
4 changed files with 54 additions and 10 deletions

View File

@ -1,5 +1,11 @@
package de.nicode3141; package de.nicode3141;
import de.nicode3141.genredct.Detector;
import de.nicode3141.genredct.GenreData;
import java.util.Arrays;
import java.util.Scanner;
/** /**
* Copyright 2026 Nicolas Pfeifer nicodenetworks.com * Copyright 2026 Nicolas Pfeifer nicodenetworks.com
* - * -
@ -7,12 +13,35 @@ package de.nicode3141;
*/ */
public class Main { public class Main {
public static void main(String[] args){ public static void main(String[] args){
String path = "data/labels.csv";
// User Dialog // User Dialog
String userInput = IO.readln("Using " + path + " as standard path for training data. Continue? (ENTER)");
if(!userInput.isEmpty()){
path = IO.readln("Enter your path: ");
}
IO.println("Starting training...");
long start = System.currentTimeMillis();
// Start training // Start training
// List params and weights // List params and weights
Detector genreDetector = new Detector(path);
long stop = System.currentTimeMillis();
IO.println("Training finished! (" + (int) (stop-start) +"ms)");
// User input text // User input text
// Predict genre // Predict genre
IO.println("Please enter your text here: ");
Scanner scanner = new Scanner(System.in);
double[] result = genreDetector.askModel(scanner);
IO.print("");
IO.println("Adventure: " + result[0]);
IO.println("Romance: " + result[1]);
IO.println("Mystery: " + result[2]);
IO.println("Horror: " + result[3]);
IO.println("Sci-Fi: " + result[4]);
} }
} }

View File

@ -1,5 +1,6 @@
package de.nicode3141.genredct; package de.nicode3141.genredct;
import java.io.File;
import java.util.*; import java.util.*;
public class Detector { public class Detector {
@ -59,6 +60,17 @@ public class Detector {
// TODO: use BufferedInputStream // TODO: use BufferedInputStream
} }
/**
* Predicts which genre the text belongs to
* @param scanner file/path/input of text file
* @return array with value for each genre, contains the calculated sum
* Order: adventure, romance, mystery, horror, scifi
*/
public double[] askModel(Scanner scanner){
TextCounts wordCounts = GenreData.countCommonWords(scanner, vocabulary);
return predictGenre(wordCounts);
}
/** /**
* Calculates the sum of weighted word counts for a text * Calculates the sum of weighted word counts for a text
* @param inputText word count to the common vocabulary * @param inputText word count to the common vocabulary

View File

@ -56,8 +56,13 @@ public class GenreData {
List<TextCounts> genreTextCount = new ArrayList<>(); List<TextCounts> genreTextCount = new ArrayList<>();
// Loop thru all Texts // Loop thru all Texts
for(String textPath : textPaths.getValue()){ for(String textPath : textPaths.getValue()){
TextCounts textWordCount = countCommonWords(textPath, vocabulary); try {
Scanner scanner = new Scanner(new File(textPath), "UTF-8");
TextCounts textWordCount = countCommonWords(scanner, vocabulary);
genreTextCount.add(textWordCount); genreTextCount.add(textWordCount);
} catch (FileNotFoundException err){
IO.print(err.getLocalizedMessage());
}
} }
genreWordCounts.put(textPaths.getKey(), genreTextCount); genreWordCounts.put(textPaths.getKey(), genreTextCount);
@ -135,11 +140,11 @@ public class GenreData {
/** /**
* Counts top X words which are in the vocabulary of a textfile * Counts top X words which are in the vocabulary of a textfile
* @param path source plain textfile * @param scanner source plain textfile/input
* @param vocabulary words to count * @param vocabulary words to count
* @return TextCounts -> LinkedMap key: word, value: count; totalWords * @return TextCounts -> LinkedMap key: word, value: count; totalWords
*/ */
public static TextCounts countCommonWords(String path, Set<String> vocabulary){ public static TextCounts countCommonWords(Scanner scanner, Set<String> vocabulary){
// TODO: Scan text line by line and count words from vocabulary. // TODO: Scan text line by line and count words from vocabulary.
Map<String, Integer> vocabCount = new LinkedHashMap<>(); Map<String, Integer> vocabCount = new LinkedHashMap<>();
@ -150,8 +155,6 @@ public class GenreData {
} }
try { try {
Scanner scanner = new Scanner(new File(path), "UTF-8");
while(scanner.hasNext()){ while(scanner.hasNext()){
String word = scanner.next().toLowerCase(); String word = scanner.next().toLowerCase();
@ -170,10 +173,9 @@ public class GenreData {
} }
scanner.close(); scanner.close();
} catch (FileNotFoundException e) { } catch (Exception e){
IO.println("Couldn't find file " + path + " err: " + e.getLocalizedMessage()); IO.println("Couldn't close scanner");
} }
return new TextCounts(vocabCount, totalWords); return new TextCounts(vocabCount, totalWords);
} }
} }

View File

@ -6,6 +6,7 @@ import de.nicode3141.genredct.TextCounts;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -66,7 +67,7 @@ public class DetectorTests {
System.out.println("Total paths: " + allPaths); System.out.println("Total paths: " + allPaths);
Map <String, Integer> vocabulary = GenreData.countWords(allPaths, 500); Map <String, Integer> vocabulary = GenreData.countWords(allPaths, 500);
TextCounts textCounts = GenreData.countCommonWords("data/train/adventure/2154_around-the-world-in-eighty-days-junior-deluxe-edit.txt", vocabulary.keySet()); TextCounts textCounts = GenreData.countCommonWords(new File("data/train/adventure/2154_around-the-world-in-eighty-days-junior-deluxe-edit.txt"), vocabulary.keySet());
} }
@DisplayName("Count training words against Vocabulary") @DisplayName("Count training words against Vocabulary")