(main): genre detection now working
parent
9852815267
commit
a4fe46bc2a
|
|
@ -1,5 +1,11 @@
|
|||
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
|
||||
* -
|
||||
|
|
@ -7,12 +13,35 @@ package de.nicode3141;
|
|||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args){
|
||||
String path = "data/labels.csv";
|
||||
// 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
|
||||
// List params and weights
|
||||
Detector genreDetector = new Detector(path);
|
||||
|
||||
long stop = System.currentTimeMillis();
|
||||
IO.println("Training finished! (" + (int) (stop-start) +"ms)");
|
||||
|
||||
// User input text
|
||||
// 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]);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
package de.nicode3141.genredct;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.*;
|
||||
|
||||
public class Detector {
|
||||
|
|
@ -59,6 +60,17 @@ public class Detector {
|
|||
// 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
|
||||
* @param inputText word count to the common vocabulary
|
||||
|
|
|
|||
|
|
@ -56,8 +56,13 @@ public class GenreData {
|
|||
List<TextCounts> genreTextCount = new ArrayList<>();
|
||||
// Loop thru all Texts
|
||||
for(String textPath : textPaths.getValue()){
|
||||
TextCounts textWordCount = countCommonWords(textPath, vocabulary);
|
||||
genreTextCount.add(textWordCount);
|
||||
try {
|
||||
Scanner scanner = new Scanner(new File(textPath), "UTF-8");
|
||||
TextCounts textWordCount = countCommonWords(scanner, vocabulary);
|
||||
genreTextCount.add(textWordCount);
|
||||
} catch (FileNotFoundException err){
|
||||
IO.print(err.getLocalizedMessage());
|
||||
}
|
||||
}
|
||||
|
||||
genreWordCounts.put(textPaths.getKey(), genreTextCount);
|
||||
|
|
@ -135,11 +140,11 @@ public class GenreData {
|
|||
|
||||
/**
|
||||
* 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
|
||||
* @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.
|
||||
|
||||
Map<String, Integer> vocabCount = new LinkedHashMap<>();
|
||||
|
|
@ -150,8 +155,6 @@ public class GenreData {
|
|||
}
|
||||
|
||||
try {
|
||||
Scanner scanner = new Scanner(new File(path), "UTF-8");
|
||||
|
||||
while(scanner.hasNext()){
|
||||
String word = scanner.next().toLowerCase();
|
||||
|
||||
|
|
@ -170,10 +173,9 @@ public class GenreData {
|
|||
}
|
||||
scanner.close();
|
||||
|
||||
} catch (FileNotFoundException e) {
|
||||
IO.println("Couldn't find file " + path + " err: " + e.getLocalizedMessage());
|
||||
} catch (Exception e){
|
||||
IO.println("Couldn't close scanner");
|
||||
}
|
||||
|
||||
return new TextCounts(vocabCount, totalWords);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import de.nicode3141.genredct.TextCounts;
|
|||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
|
@ -66,7 +67,7 @@ public class DetectorTests {
|
|||
System.out.println("Total paths: " + allPaths);
|
||||
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")
|
||||
|
|
|
|||
Loading…
Reference in New Issue