Skip to content

Commit

Permalink
comment ner add
Browse files Browse the repository at this point in the history
  • Loading branch information
mjh000526 committed Jun 24, 2024
1 parent c72dede commit 4c528ff
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/main/java/com/example/just/Service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> content = getConvertString(commentDto.getComment_content().strip());
comment.setComment_content(content.get(0));
comment.setPost(post);
comment.setMember(member);
comment.setParent(parentComment);
Expand Down Expand Up @@ -319,4 +325,44 @@ public ResponseEntity getMyComment(Long member_id) {
public List<Comment> getAllComments() {
return commentRepository.findAll();
}

public List<String> 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<String> request = new HttpEntity<>(parameter.toJSONString(), headers);
System.out.println(parameter.toJSONString());
ResponseEntity<String> 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<String> parsingJson(String json) { // JSON 파싱
JSONArray jsonArray;
List<String> 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;
}
}

0 comments on commit 4c528ff

Please sign in to comment.