(chore): cleaner code
parent
f95bb376b9
commit
a735d2d8f5
|
|
@ -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<String, List<TextCounts>> countTrainData(Map<String, List<String>> texts, Set<String> vocabulary){
|
|
||||||
Map<String, List<TextCounts>> genreWordCounts = new LinkedHashMap<>();
|
|
||||||
|
|
||||||
// Loop thru each genre
|
|
||||||
for(Map.Entry<String, List<String>> textPaths : texts.entrySet()){
|
|
||||||
List<TextCounts> 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
|
* Counts top X words of all textfiles
|
||||||
* @param paths source plain textfiles
|
* @param paths source plain textfiles
|
||||||
|
|
@ -117,7 +88,6 @@ public class GenreData {
|
||||||
IO.println("Couldn't find file " + path + " err: " + e.getLocalizedMessage());
|
IO.println("Couldn't find file " + path + " err: " + e.getLocalizedMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// After that, sort the Map. Simple with ArrayList sort()
|
// After that, sort the Map. Simple with ArrayList sort()
|
||||||
// The ArrayList could contain the entrySet of the Map
|
// The ArrayList could contain the entrySet of the Map
|
||||||
// Then loop thru the sorted ArrayList, maximum (limit) and create a new Map
|
// Then loop thru the sorted ArrayList, maximum (limit) and create a new Map
|
||||||
|
|
@ -138,6 +108,35 @@ public class GenreData {
|
||||||
return topWords;
|
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<String, List<TextCounts>> countTrainData(Map<String, List<String>> texts, Set<String> vocabulary){
|
||||||
|
Map<String, List<TextCounts>> genreWordCounts = new LinkedHashMap<>();
|
||||||
|
|
||||||
|
// Loop thru each genre
|
||||||
|
for(Map.Entry<String, List<String>> textPaths : texts.entrySet()){
|
||||||
|
List<TextCounts> 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
|
* Counts top X words which are in the vocabulary of a textfile
|
||||||
* @param scanner source plain textfile/input
|
* @param scanner source plain textfile/input
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue