From a735d2d8f5098ab490f0771629c4c6bb264c4dfd Mon Sep 17 00:00:00 2001 From: nicode3141 Date: Mon, 16 Nov 2026 12:15:34 +0100 Subject: [PATCH] (chore): cleaner code --- .../de/nicode3141/genredct/GenreData.java | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/src/main/java/de/nicode3141/genredct/GenreData.java b/src/main/java/de/nicode3141/genredct/GenreData.java index 5d143cf..428930e 100644 --- a/src/main/java/de/nicode3141/genredct/GenreData.java +++ b/src/main/java/de/nicode3141/genredct/GenreData.java @@ -41,35 +41,6 @@ public class GenreData { } } - /** - * 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: TextCounts - */ - public static Map> countTrainData(Map> texts, Set vocabulary){ - Map> genreWordCounts = new LinkedHashMap<>(); - - // Loop thru each genre - for(Map.Entry> textPaths : texts.entrySet()){ - List genreTextCount = new ArrayList<>(); - // Loop thru all Texts - for(String textPath : textPaths.getValue()){ - 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); - } - - return genreWordCounts; - } - /** * Counts top X words of all textfiles * @param paths source plain textfiles @@ -117,7 +88,6 @@ public class GenreData { IO.println("Couldn't find file " + path + " err: " + e.getLocalizedMessage()); } } - // After that, sort the Map. Simple with ArrayList sort() // The ArrayList could contain the entrySet of the Map // Then loop thru the sorted ArrayList, maximum (limit) and create a new Map @@ -138,6 +108,35 @@ public class GenreData { return topWords; } + /** + * 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: TextCounts + */ + public static Map> countTrainData(Map> texts, Set vocabulary){ + Map> genreWordCounts = new LinkedHashMap<>(); + + // Loop thru each genre + for(Map.Entry> textPaths : texts.entrySet()){ + List genreTextCount = new ArrayList<>(); + // Loop thru all Texts + for(String textPath : textPaths.getValue()){ + 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); + } + + return genreWordCounts; + } + /** * Counts top X words which are in the vocabulary of a textfile * @param scanner source plain textfile/input