Fix for corrupt data in database, Show sentiment icons

This commit is contained in:
vysitor 2025-03-21 13:15:44 +01:00
parent 3a2dce4853
commit 0b1c05acea
5 changed files with 48 additions and 4 deletions

View file

@ -419,6 +419,15 @@ public class MongoDBHandler {
return doc;
}
public static Double getFieldAsDouble(Document doc, String fieldName) {
Object obj = doc.get(fieldName);
if (obj instanceof Double) return (Double) obj;
if (obj instanceof Integer) return ((Integer) obj).doubleValue();
if (obj instanceof String) return Double.valueOf((String) obj);
Logger.error("Wert " + obj + " sollte Double sein, ist aber nicht");
return (Double) obj;
}
/*
* Weitere CRUD Operations
* =======================

View file

@ -1,6 +1,7 @@
package org.texttechnologylab.project.gruppe_05_1.domain.nlp;
import org.bson.Document;
import org.texttechnologylab.project.gruppe_05_1.database.MongoDBHandler;
import java.util.ArrayList;
import java.util.List;
@ -111,10 +112,10 @@ public class Sentiment {
sentiments.add(new Sentiment(
doc.getInteger("begin"),
doc.getInteger("end"),
doc.getDouble("score"),
doc.getDouble("pos"),
doc.getDouble("neu"),
doc.getDouble("neg")
MongoDBHandler.getFieldAsDouble(doc, "score"),
MongoDBHandler.getFieldAsDouble(doc, "pos"),
MongoDBHandler.getFieldAsDouble(doc, "neu"),
MongoDBHandler.getFieldAsDouble(doc, "neg")
));
}
return sentiments;

View file

@ -161,8 +161,13 @@ public class SpeechController {
Sentiment overallSentiment = sentiments.get(0);
attributes.put("overallSentiment", overallSentiment);
sentiments.remove(0);
// Sentiment-Icon
List<String> sentimentIcons = calculateSentimentIcons(sentiments);
attributes.put("sentimentIcons", sentimentIcons);
} else {
attributes.put("overallSentiment", null);
attributes.put("sentimentIcons", null);
}
attributes.put("sentiments", sentiments);
@ -171,6 +176,29 @@ public class SpeechController {
ctx.render("speech.ftl", attributes);
}
private static String POSITIVE_SENTIMENT= "fas fa-thumbs-up sentiment-positive"; // Alternativ: fas fa-smile positive
private static String NEGATIVE_SENTIMENT= "fas fa-thumbs-down sentiment-negative"; // Alternativ: fas fa-frown negative
private static String NEUTRAL_SENTIMENT = "fas fa-meh sentiment-neutral"; // Alternativ: fas fa-circle neutral
/**
* Ordne einem Satz ein Sentiment-Icon
* @param sentiments
* @return Icon Name (als Font Awesome Icon)
*/
private static List<String> calculateSentimentIcons(List<Sentiment> sentiments) {
double threshold = 0.15;
List<String> iconNames = new ArrayList<>();
for (Sentiment s: sentiments) {
if (s.getPositive() - s.getNegative() > threshold) {
iconNames.add(POSITIVE_SENTIMENT);
} else if (s.getNegative() - s.getPositive() > threshold) {
iconNames.add(NEGATIVE_SENTIMENT);
} else iconNames.add(NEUTRAL_SENTIMENT);
}
return iconNames;
}
@OpenApi(
summary = "Liste alle Reden (Filtern ist möglich)",
description = "Liste alle Reden. Man kann nach Freitext (MdB Name, Partei/Fraktion) oder nach Thema (Topic) filtern",

View file

@ -49,6 +49,10 @@
<br>
<#list s.content as c>
<#if sentimentIcons??>
<#assign index = c?index>
<#assign sentimentIcon = sentimentIcons[index]>
</#if>
<#include "speechContent.ftl">
</#list>

View file

@ -1,7 +1,9 @@
<#if c.type == 'SPEAKER'>
<span style="color:Green">Rednerin/Redner: </span> ${c.content}
<#elseif c.type == 'LINE'>
${c.content}
<#elseif c.type == 'COMMENT'>
<span style="color:Blue">Kommentar: </span> ${c.content}
</#if>
<#if sentimentIcon??> <i class="${sentimentIcon}"></i> </#if>
<p>