유튜브 댓글 분석 서비스 프로젝트를 진행하던 중, 싱글톤 방식의 설계가 잘못되어 분석을 계속해서 실행할 때마다 이전 분석 값이 누적되어 출력되는 현상이 발생하였다.
Flask 서버에서 분석된 결과를 JSON 형태로 받아와 통계를 추출하여 Front 서버에 전달하는 형식인데 Service 단에서 문제가 발생한 것 같았다.
ResponseJsonController.java
public class ResponseJsonController {
private final CommentService commentService;
...
public KeywordDTO getKeyword(@PathVariable String url, Model model) {
String KeywordBaseUrl = "http://localhost:5000/keywords?url=" + url;
RestTemplate KeywordRestTemplate = new RestTemplate();
ResponseEntity<KeywordDTO> KeywordResponse = KeywordRestTemplate.getForEntity(KeywordBaseUrl, KeywordDTO.class);
...
HashMap<String, List> commentMap = commentService.classifyComment(comments);
HashMap<String, Double> positiveNegativePercentMap = commentService.positiveNegativePercent();
HashMap<String, Double> sentimentPercentMap = commentService.sentimentPercent();
...
}
CommentService.java
@Service
public class CommentService {
private List<CommentDTO> positiveComments = new ArrayList<>(); // json 구분 인덱스 : 1
private List<CommentDTO> negativeComments = new ArrayList<>(); // json 구분 인덱스 : 0
private List<CommentDTO> fearComments = new ArrayList<>(); // json 구분 인덱스 : 2
private List<CommentDTO> surprisedComments = new ArrayList<>(); // json 구분 인덱스 : 3
private List<CommentDTO> angerComments = new ArrayList<>(); // json 구분 인덱스 : 4
private List<CommentDTO> sadnessComments = new ArrayList<>(); // json 구분 인덱스 : 5
private List<CommentDTO> neutralComments = new ArrayList<>(); // json 구분 인덱스 : 6
private List<CommentDTO> happyComments = new ArrayList<>(); // json 구분 인덱스 : 7
private List<CommentDTO> disgustComments = new ArrayList<>(); // json 구분 인덱스 : 8
public HashMap<String, List> classifyComment(CommentDTO[] comments){
HashMap<String, List> commentMap = new HashMap<>();
for(int i = 0; i< comments.length; i++){ //인덱스 번호를 통해서 긍정, 부정 , 감정 댓글 분류
String index = comments[i].getIndex();
switch (index){
case "0" :
negativeComments.add(comments[i]);
break;
case "1" :
positiveComments.add(comments[i]);
break;
case "2" :
fearComments.add(comments[i]);
break;
case "3" :
surprisedComments.add(comments[i]);
break;
case "4" :
angerComments.add(comments[i]);
break;
case "5" :
sadnessComments.add(comments[i]);
break;
case "6" :
neutralComments.add(comments[i]);
break;
case "7" :
happyComments.add(comments[i]);
break;
case "8" :
disgustComments.add(comments[i]);
break;
}
commentMap.put("positiveComments",positiveComments);
commentMap.put("negativeComments",negativeComments);
commentMap.put("fearComments",fearComments);
commentMap.put("surprisedComments",surprisedComments);
commentMap.put("angerComments",angerComments);
commentMap.put("sadnessComments",sadnessComments);
commentMap.put("neutralComments",neutralComments);
commentMap.put("happyComments",happyComments);
commentMap.put("disgustComments",disgustComments);
return commentMap;
}
public HashMap<String, Double> positiveNegativePercent(){
HashMap<String, Double> positiveNegativePercentMap = new HashMap<>();
List positiveComments = map.get("positiveComments");
List negativeComments = map.get("negativeComments");
double positivePercent = ((double)positiveComments.size() / ((double)positiveComments.size()+(double)negativeComments.size()))*100;
double negativePercent = ((double)negativeComments.size() / ((double)positiveComments.size()+(double)negativeComments.size()))*100;
}
public HashMap<String, Double> sentimentPercent(){
HashMap<String, Double> sentimentPercentMap = new HashMap<>();
double emTotalSize = fearComments.size()+ surprisedComments.size()+ angerComments.size()+ sadnessComments.size()+ neutralComments.size()+ happyComments.size()+ disgustComments.size();
double fearPercent = (((double)fearComments.size()/emTotalSize)*100);
double surprisedPercent = ((double)surprisedComments.size()/emTotalSize)*100;
}
...
====================수정 후 코드=================
ResponseJsonController.java
public class ResponseJsonController {
private final CommentService commentService;
...
public KeywordDTO getKeyword(@PathVariable String url, Model model) {
String KeywordBaseUrl = "http://localhost:5000/keywords?url=" + url;
RestTemplate KeywordRestTemplate = new RestTemplate();
ResponseEntity<KeywordDTO> KeywordResponse = KeywordRestTemplate.getForEntity(KeywordBaseUrl, KeywordDTO.class);
...
HashMap<String, List> commentMap = commentService.classifyComment(comments);
HashMap<String, Double> positiveNegativePercentMap = commentService.positiveNegativePercent(commentMap);
HashMap<String, Double> sentimentPercentMap = commentService.sentimentPercent(commentMap);
...
}
CommentService.java
@Service
public class CommentService {
public HashMap<String, List> classifyComment(CommentDTO[] comments){
HashMap<String, List> commentMap = new HashMap<>();
List<CommentDTO> positiveComments = new ArrayList<>(); // json 구분 인덱스 : 1
List<CommentDTO> negativeComments = new ArrayList<>(); // json 구분 인덱스 : 0
List<CommentDTO> fearComments = new ArrayList<>(); // json 구분 인덱스 : 2
List<CommentDTO> surprisedComments = new ArrayList<>(); // json 구분 인덱스 : 3
List<CommentDTO> angerComments = new ArrayList<>(); // json 구분 인덱스 : 4
List<CommentDTO> sadnessComments = new ArrayList<>(); // json 구분 인덱스 : 5
List<CommentDTO> neutralComments = new ArrayList<>(); // json 구분 인덱스 : 6
List<CommentDTO> happyComments = new ArrayList<>(); // json 구분 인덱스 : 7
List<CommentDTO> disgustComments = new ArrayList<>(); // json 구분 인덱스 : 8
for(int i = 0; i< comments.length; i++){ //인덱스 번호를 통해서 긍정, 부정 , 감정 댓글 분류
String index = comments[i].getIndex();
switch (index){
case "0" :
negativeComments.add(comments[i]);
break;
case "1" :
positiveComments.add(comments[i]);
break;
case "2" :
fearComments.add(comments[i]);
break;
case "3" :
surprisedComments.add(comments[i]);
break;
case "4" :
angerComments.add(comments[i]);
break;
case "5" :
sadnessComments.add(comments[i]);
break;
case "6" :
neutralComments.add(comments[i]);
break;
case "7" :
happyComments.add(comments[i]);
break;
case "8" :
disgustComments.add(comments[i]);
break;
}
commentMap.put("positiveComments",positiveComments);
commentMap.put("negativeComments",negativeComments);
commentMap.put("fearComments",fearComments);
commentMap.put("surprisedComments",surprisedComments);
commentMap.put("angerComments",angerComments);
commentMap.put("sadnessComments",sadnessComments);
commentMap.put("neutralComments",neutralComments);
commentMap.put("happyComments",happyComments);
commentMap.put("disgustComments",disgustComments);
return commentMap;
}
public HashMap<String, Double> positiveNegativePercent(HashMap<String, List> map){
HashMap<String, Double> positiveNegativePercentMap = new HashMap<>();
List positiveComments = map.get("positiveComments");
List negativeComments = map.get("negativeComments");
double positivePercent = ((double)positiveComments.size() / ((double)positiveComments.size()+(double)negativeComments.size()))*100;
double negativePercent = ((double)negativeComments.size() / ((double)positiveComments.size()+(double)negativeComments.size()))*100;
}
public HashMap<String, Double> sentimentPercent(HashMap<String, List> map){
HashMap<String, Double> sentimentPercentMap = new HashMap<>();
double emTotalSize = fearComments.size()+ surprisedComments.size()+ angerComments.size()+ sadnessComments.size()+ neutralComments.size()+ happyComments.size()+ disgustComments.size();
double fearPercent = (((double)fearComments.size()/emTotalSize)*100);
double surprisedPercent = ((double)surprisedComments.size()/emTotalSize)*100;
}
...