From 4c528ff6e41c4fabc6403b7ffa19936d1c293b04 Mon Sep 17 00:00:00 2001 From: mjh000526 Date: Mon, 24 Jun 2024 13:49:34 +0900 Subject: [PATCH] comment ner add --- .../example/just/Service/CommentService.java | 52 +++++++++++++++++-- 1 file changed, 49 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/example/just/Service/CommentService.java b/src/main/java/com/example/just/Service/CommentService.java index a95beff..344ef49 100644 --- a/src/main/java/com/example/just/Service/CommentService.java +++ b/src/main/java/com/example/just/Service/CommentService.java @@ -29,11 +29,15 @@ import com.example.just.Response.ResponsePostCommentDto; import com.example.just.Response.ResponseCommentDto; +import org.json.simple.JSONArray; +import org.json.simple.JSONObject; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; +import org.springframework.http.*; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; import java.time.LocalDateTime; import java.util.List; @@ -88,7 +92,9 @@ public Comment createComment(Long postId, Long member_id, CommentDto commentDto) Member member = memberRepository.findById(member_id).orElseGet(() -> new Member()); //댓글 객체 생성 Comment comment = new Comment(); - comment.setComment_content(commentDto.getComment_content().strip()); + //비식별화 진행 + List content = getConvertString(commentDto.getComment_content().strip()); + comment.setComment_content(content.get(0)); comment.setPost(post); comment.setMember(member); comment.setParent(parentComment); @@ -319,4 +325,44 @@ public ResponseEntity getMyComment(Long member_id) { public List getAllComments() { return commentRepository.findAll(); } + + public List getConvertString(String str) { // 문자열 변환 + RestTemplate restTemplate = new RestTemplate(); + + String requestBody = "{\"content\": ["; + + requestBody += "\"" + str+"\", "; + requestBody += "]}"; + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + JSONObject parameter = new JSONObject(); + parameter.put("content",str); + HttpEntity request = new HttpEntity<>(parameter.toJSONString(), headers); + System.out.println(parameter.toJSONString()); + ResponseEntity responseEntity = restTemplate.exchange( +// "http://"+server_address+":8081/api/ner/post", + "http://localhost:8081/api/ner/post", + HttpMethod.POST, + request, + String.class); + String responseBody = responseEntity.getBody(); + return parsingJson(responseBody); + } + + public List parsingJson(String json) { // JSON 파싱 + JSONArray jsonArray; + List denyList = new ArrayList<>(); + try { + JSONParser parser = new JSONParser(); + JSONObject elem = (JSONObject) parser.parse(json); + jsonArray = (JSONArray) elem.get("deny_list"); + for (Object obj : jsonArray) { + denyList.add((String) obj); + } + + } catch (ParseException e) { + throw new RuntimeException(e); + } + return denyList; + } }