fixed bad merge

This commit is contained in:
s5260822 2025-03-24 01:01:08 +01:00
parent 4db18688b5
commit 67e117b647
2 changed files with 72 additions and 2 deletions

View file

@ -111,4 +111,76 @@ public class FrontEndController {
public static void getAboutPage(Context ctx) {
ctx.render("about.ftl");
}
/**
* Aggregiert für alle Reden die NLPErgebnisse (Topics, POS, Named Entities, erste SentimentObjekte)
* und liefert die zusammengefassten Daten an die ChartsAnsicht.
* @param ctx Javalin Context zum Rendern der Seite mit den aggregierten ChartDaten
* Implementiert von Leon
*/
public static void getCharts(Context ctx) {
MongoCollection<Document> col = MongoPprUtils.getSpeechCollection();
List<Bson> topicsPipeline = List.of(
Aggregates.match(Filters.exists("analysisResults.topics.0")),
Aggregates.unwind("$analysisResults.topics"),
Aggregates.group(
"$analysisResults.topics.topic",
Accumulators.sum("totalScore", "$analysisResults.topics.score")
)
);
List<Bson> posPipeline = List.of(
Aggregates.match(Filters.exists("analysisResults.tokens")),
Aggregates.unwind("$analysisResults.tokens"),
Aggregates.group("$analysisResults.tokens.pos", Accumulators.sum("count", 1))
);
List<Bson> nePipeline = List.of(
Aggregates.match(Filters.exists("analysisResults.namedEntities")),
Aggregates.unwind("$analysisResults.namedEntities"),
Aggregates.group("$analysisResults.namedEntities.type", Accumulators.sum("count", 1))
);
List<Bson> sentimentsPipeline = List.of(
Aggregates.match(Filters.exists("analysisResults.sentiments")),
Aggregates.project(Projections.computed("firstSentiment",
new Document("$arrayElemAt", List.of("$analysisResults.sentiments", 0)))),
Aggregates.replaceRoot("$firstSentiment")
);
CompletableFuture<List<Document>> topicsF = CompletableFuture.supplyAsync(() -> col.aggregate(topicsPipeline).into(new ArrayList<>()));
CompletableFuture<List<Document>> posF = CompletableFuture.supplyAsync(() -> col.aggregate(posPipeline).into(new ArrayList<>()));
CompletableFuture<List<Document>> neF = CompletableFuture.supplyAsync(() -> col.aggregate(nePipeline).into(new ArrayList<>()));
CompletableFuture<List<Document>> sentF = CompletableFuture.supplyAsync(() -> col.aggregate(sentimentsPipeline).into(new ArrayList<>()));
CompletableFuture.allOf(topicsF, posF, neF, sentF).join();
List<Topic> aggregatedTopics = topicsF.join().stream()
.map(d -> new Topic(d.getString("_id"), d.getDouble("totalScore"), null))
.collect(Collectors.toList());
List<Token> aggregatedPOS = posF.join().stream()
.map(d -> new Token(d.getString("_id"), String.valueOf(d.getInteger("count")), ""))
.collect(Collectors.toList());
Map<String, Map<String, Integer>> aggregatedNE = new HashMap<>();
neF.join().forEach(d -> {
List<Document> entities = d.getList("entities", Document.class);
Map<String, Integer> typeMap = (entities == null)
? new HashMap<>()
: entities.stream()
.collect(Collectors.toMap(
e -> e.getString("text"),
e -> e.getInteger("count")
));
aggregatedNE.put(d.getString("_id"), typeMap);
});
List<Sentiment> aggregatedSentiments = Sentiment.readSentimentsFromMongo(sentF.join());
ctx.render("charts.ftl", Map.of(
"aggregatedTopics", aggregatedTopics,
"aggregatedPOS", aggregatedPOS,
"aggregatedNE", aggregatedNE,
"aggregatedSentiments", aggregatedSentiments
));
}
}

View file

@ -75,8 +75,6 @@ public class RESTHandler {
// Charts
app.get("/charts", FrontEndController::getCharts);
app.get("/about", FrontEndController::getAbout);
app.get("/export/pdf/speech/{id}", SpeechesLatexExportController::exportSpeech); // exportiere eine Rede als PDF
app.get("/export/pdf/speech", SpeechesLatexExportController::exportSpeech); // exportiere eine Rede als PDF
app.get("/export/pdf/speaker/{id}", SpeechesLatexExportController::exportSpeechesFromSpeaker); // exportiere alle Reden eines Parlamentariers als PDF