Work on Sentiment, Bug with empty topic list fixed

This commit is contained in:
vysitor 2025-03-20 15:30:43 +01:00
parent b795314716
commit e65abe88bc
4 changed files with 70 additions and 31 deletions

View file

@ -4,6 +4,7 @@ import org.bson.Document;
import org.texttechnologylab.project.gruppe_05_1.database.MongoDBHandler;
import org.texttechnologylab.project.gruppe_05_1.database.MongoPprUtils;
import org.texttechnologylab.project.gruppe_05_1.domain.nlp.NlpInfo;
import org.texttechnologylab.project.gruppe_05_1.domain.nlp.Sentiment;
import org.texttechnologylab.project.gruppe_05_1.domain.nlp.Token;
import org.texttechnologylab.project.gruppe_05_1.domain.nlp.Topic;
import org.texttechnologylab.project.gruppe_05_1.domain.speech.SpeechMetaData;
@ -70,6 +71,7 @@ public class HtmlSpeech {
List<Document> namedEntitiesDocs = nlpDoc.get("namedEntities", MongoDBHandler.DOC_LIST_CLASS);
List<Document> sentimentsDocs = nlpDoc.get("sentiments", MongoDBHandler.DOC_LIST_CLASS);
nlp.setSentiments(Sentiment.readSentimentsFromMongo(sentimentsDocs));
List<Document> topicsDocs = nlpDoc.get("topics", MongoDBHandler.DOC_LIST_CLASS);
nlp.setTopics(Topic.readTopicsFromMongo(topicsDocs));

View file

@ -1,5 +1,9 @@
package org.texttechnologylab.project.gruppe_05_1.domain.nlp;
import org.bson.Document;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
@ -94,4 +98,25 @@ public class Sentiment {
.add("positive=" + positive)
.toString();
}
/**
*
* @param sentimentDocs Die Sentiment-Dokumente (Speech --> analysisResults --> sentiment) aus der MongoDB lesen.
* Das erste Dokument ist für die gesamte Rede, Sentiments 1..n entsprechen Sentences 0..n-1
* @return
*/
public static List<Sentiment> readSentimentsFromMongo(List<Document> sentimentDocs) {
List<Sentiment> sentiments = new ArrayList<>();
for (Document doc : sentimentDocs) {
sentiments.add(new Sentiment(
doc.getInteger("begin"),
doc.getInteger("end"),
doc.getDouble("score"),
doc.getDouble("pos"),
doc.getDouble("neu"),
doc.getDouble("neg")
));
}
return sentiments;
}
}

View file

@ -78,38 +78,48 @@ public class SpeechController {
String picture = MongoPprUtils.getParlamentarierPictureByID(parlamentarierId);
attributes.put("picture", picture);
// NLP: Topic
if ((speech.getNlp() != null) && (speech.getNlp().getTopics() != null)) {
Map<String, Double> topics = Topic.condenseTopicInformation(speech.getNlp().getTopics()); // Daten "verdichten"...
// ... und ersetzen
speech.getNlp().setTopics(
topics.entrySet().stream()
.map(me -> new Topic(me.getKey(), me.getValue(), null))
.collect(Collectors.toList()));
// NLP
if (speech.getNlp() != null) {
// NLP: Topic
if ((speech.getNlp().getTopics() != null) && (speech.getNlp().getTopics().size() > 0)) {
Map<String, Double> topics = Topic.condenseTopicInformation(speech.getNlp().getTopics()); // Daten "verdichten"...
// ... und ersetzen
speech.getNlp().setTopics(
topics.entrySet().stream()
.map(me -> new Topic(me.getKey(), me.getValue(), null))
.collect(Collectors.toList()));
} else {
speech.getNlp().setTopics(null);
}
// NLP: POS
if (speech.getNlp().getTokens() != null) {
List<Token> tokens = speech.getNlp().getTokens();
Map<String, Integer> posCounts = Token.countPOS(tokens);
List<Token> posList = posCounts.entrySet().stream()
.map(entry -> new Token(entry.getKey(), String.valueOf(entry.getValue()), "")) // Lemma remains empty
.collect(Collectors.toList());
Logger.debug("Sending POS List to NLP - " + posList);
speech.getNlp().setPosList((List) posList);
} else {
Logger.debug("POS List is EMPTY");
speech.getNlp().setPosList((List) new ArrayList<Token>()); // Ensure it's never null
}
// TODO: Token wird momentan etwas komisch abgespeichert, da im Attribut text die POS art steht, und in pos die Anzahl dieser POS arten. Umstrukturieren damit keine Verwirrung herrscht
// NLP: Sentiments
if (speech.getNlp().getSentiments() != null) {
}
}
// NLP: POS
if (speech.getNlp() != null && speech.getNlp().getTokens() != null) {
List<Token> tokens = speech.getNlp().getTokens();
Map<String, Integer> posCounts = Token.countPOS(tokens);
List<Token> posList = posCounts.entrySet().stream()
.map(entry -> new Token(entry.getKey(), String.valueOf(entry.getValue()), "")) // Lemma remains empty
.collect(Collectors.toList());
Logger.debug("Sending POS List to NLP - " + posList);
speech.getNlp().setPosList((List) posList);
} else {
Logger.debug("POS List is EMPTY");
speech.getNlp().setPosList((List) new ArrayList<Token>()); // Ensure it's never null
}
// TODO: Token wird momentan etwas komisch abgespeichert, da im Attribut text die POS art steht, und in pos die Anzahl dieser POS arten. Umstrukturieren damit keine Verwirrung herrscht
ctx.render("speech.ftl", attributes);
}

View file

@ -17,7 +17,9 @@
<#if s.nlp.sentiments??>
<h3>SentimentsInformation (als Radar Chart)</h3>
<#assign overallSentiment = s.nlp.overallSentiment>
<#if s.nlp.overallSentiment??>
<#assign overallSentiment = s.nlp.overallSentiment>
</#if>
<#assign sentiments = s.nlp.sentiments>
<#include "sentimentsRadarChart.ftl">
<#else>