Skip to content
Snippets Groups Projects
Commit 30338928 authored by ywb16155's avatar ywb16155
Browse files

commit #50 - wordcloud improvements

parent 98dda6c9
No related branches found
No related tags found
No related merge requests found
package app.analysis.summarizer;
public class Summarizer {
}
package app.configurations;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
public class WebConfig {
}
......@@ -3,8 +3,10 @@ package app.services;
import app.analysis.sentiment.SentimentAnalyzer;
import app.analysis.sentiment.SentimentResult;
import app.entities.AnswerEntity;
import app.entities.QuestionEntity;
import app.pojo.WordDetails;
import app.repositories.AnswerEntityRepository;
import app.repositories.QuestionEntityRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
......@@ -29,16 +31,23 @@ public class SentimentService {
@Autowired
private AnswerEntityRepository answerRepo;
@Autowired
private QuestionEntityRepository questionRepo;
public List<WordDetails> generateWordCloud(String paperId) {
int id = UtilsService.parseId(paperId);
List<WordDetails> result = new ArrayList<>();
List<AnswerEntity> answers = answerRepo.findAllByPaperIdAndSubmitted(UtilsService.parseId(paperId), true).orElseGet(ArrayList::new);
List<AnswerEntity> answers = answerRepo.findAllByPaperIdAndSubmitted(id, true).orElseGet(ArrayList::new);
if(answers.size() > 0) {
List<String> stopwords = getStopwords();
SentimentAnalyzer analyzer = new SentimentAnalyzer();
Map<AnswerEntity, SentimentResult> sentimentResultMap = new HashMap<>();
Map<String, WordDetails> wordcloud = new HashMap<>();
List<QuestionEntity> questions = questionRepo.findAllByPaperIdOrderByQuestionIdAsc(id).orElseGet(ArrayList::new);
Map<Integer, Map<String,WordDetails>> wordDetailsMap = new HashMap<>();
questions.forEach(question -> wordDetailsMap.put(question.questionId, new HashMap<>()));
Map<AnswerEntity, SentimentResult> sentimentResultMap = new HashMap<>();
answers.forEach(answerEntity -> sentimentResultMap.put(answerEntity, analyzer.analyse(answerEntity.getAnswerText())));
sentimentResultMap.forEach((k,v) -> {
List<String> filteredText = v.getText().stream().filter(word -> !stopwords.contains(word.toLowerCase())).collect(Collectors.toList());
......@@ -48,35 +57,34 @@ public class SentimentService {
sentimentResultMap.forEach((k,v) -> {
v.getText().forEach(word -> {
WordDetails details;
if(wordcloud.get(word) == null) {
details = new WordDetails();
details.setWord(word);
details.setPolarity(v.getScore());
details.setNumberOfOccurrences(1);
details.setQuestionId(k.getQuestionId());
wordcloud.put(word, details);
} else {
details = wordcloud.get(word);
details.setPolarity(calculateScore(details, v.getScore()));
details.setNumberOfOccurrences(details.getNumberOfOccurrences() + 1);
}
if (wordDetailsMap.get(k.getQuestionId()).get(word) == null) {
details = new WordDetails();
details.setWord(word);
details.setNumberOfOccurrences(1);
details.setPolarity(v.getScore());
details.setQuestionId(k.getQuestionId());
wordDetailsMap.get(k.getQuestionId()).put(word, details);
} else {
details = wordDetailsMap.get(k.getQuestionId()).get(word);
details.setPolarity(calculateScore(details.getPolarity(), v.getScore()));
details.setNumberOfOccurrences(details.getNumberOfOccurrences() + 1);
}
});
});
wordcloud.forEach((k,v) -> result.add(v));
wordDetailsMap.forEach((k,v) ->result.addAll(v.values()));
}
return result;
}
private int calculateScore(WordDetails currentWord, int score) {
if(score == currentWord.getPolarity()) {
return score;
private int calculateScore(int currentScore, int score) {
if(score == currentScore) {
return currentScore;
}
if (score < currentWord.getPolarity()) {
return score + 1;
if (score < currentScore) {
return currentScore - 1;
} else {
return score - 1;
return currentScore + 1;
}
}
......@@ -97,7 +105,6 @@ public class SentimentService {
stopwords.add(line);
}
} catch (IOException ioe) {
ioe.printStackTrace();
return new ArrayList<>();
}
return stopwords;
......
......@@ -16,8 +16,3 @@ spring.jpa.show-sql=false
#server.tomcat.additional-tld-skip-patterns='*.jar'
#server.context-path=/ywb16155-root/ompp-api
#server.port=8443
#server.ssl.enabled=true
#server.ssl.key-store-type=PKCS12
#server.ssl.key-store=classpath:server.p12
#server.ssl.key-store-password=certpassword
#server.ssl.key-alias=tomcat
......@@ -4,7 +4,7 @@ import {LecturerInfoService} from 'src/app/services/lecturer-info.service';
import {LecturerDataService} from 'src/app/services/lecturer-data.service';
import {WordDetailsDao} from 'src/app/dao/word-details.dao';
import {QuestionDao} from 'src/app/dao/question.dao';
import {Observable} from 'rxjs';
import {el} from '@angular/platform-browser/testing/src/browser_util';
@Component({
selector: 'app-sentiment-cloud',
......@@ -56,6 +56,9 @@ export class SentimentCloudComponent implements OnInit, OnDestroy {
}
ngOnDestroy(): void {
this.questions = [];
this.wordClouds = [];
this.wordcloudData = [];
}
private sortData(data: WordDetailsDao[]) {
......@@ -74,15 +77,42 @@ export class SentimentCloudComponent implements OnInit, OnDestroy {
private getAllData() {
const data = [];
this.wordClouds.forEach(list => {
list.forEach(elem => {
data.push(elem);
const allWords = new Set();
this.wordClouds.forEach((list: WordDetailsDao[]) => {
list.forEach((word: WordDetailsDao) => {
if (!allWords.has(word.word)) {
allWords.add(word.word);
data.push(word);
} else {
this.updateWord(data, word);
}
});
});
return data;
}
private updateWord(wordDetails: WordDetailsDao[], word: WordDetailsDao): any {
wordDetails.forEach((elem: WordDetailsDao) => {
if (elem.word === word.word) {
elem.numberOfOccurrences = elem.numberOfOccurrences + 1;
elem.polarity = this.calculateScore(elem.polarity, word.polarity);
}
});
}
private calculateScore(currentScore: number, score: number): number {
if (score === currentScore) {
return currentScore;
}
if (score < currentScore) {
return currentScore - 1;
} else {
return currentScore + 1;
}
}
private getColour(polarity: number): String {
switch (polarity) {
case 0: return '#FF0000';
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment