From f1f6014813df9123bbf02d0c9fdb78bb6e637862 Mon Sep 17 00:00:00 2001 From: hellouz818 Date: Wed, 1 Jun 2022 02:30:06 +0900 Subject: [PATCH 1/2] [#25] fix: Add Translated English Text to fastapi --- .../Notice/controller/NoticeController.java | 4 +-- .../Notice/service/NoticeService.java | 34 +++++++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java index ef3941d..1bed0e6 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java @@ -38,8 +38,8 @@ public NoticeOCRDto executeOCR (@RequestPart MultipartFile uploadfile, HttpServl String korean = noticeService.detectText(uploadfile); //원문 추출 String trans_full = noticeService.transText(korean, targetLanguage); //번역문 추출 - - List eventWords = noticeService.detectEvent(korean, trans_full, targetLanguage); //이벤트 추출 + String en_full = noticeService.englishText(korean); // 영어 추출 + List eventWords = noticeService.detectEvent(korean, trans_full, targetLanguage, en_full); //이벤트 추출 List fullText = noticeService.extractSentenceFromEventRequestDto(trans_full, eventWords); return new NoticeOCRDto(korean, trans_full, fullText); } diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java index 78d7d3b..1a8d0b6 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java @@ -113,7 +113,35 @@ public String transText(String korean, String targetLanguage) throws IOException return textlist.get(0); } - public List detectEvent(String korean, String translation, String language) throws JsonProcessingException { + public String englishText(String korean) throws IOException { + String text = korean; + String projectId = "solution-challenge-342914"; + ArrayList textlist = new ArrayList(); + + try (TranslationServiceClient client = TranslationServiceClient.create()) { + LocationName parent = LocationName.of(projectId, "global"); + + // Supported Mime Types: https://cloud.google.com/translate/docs/supported-formats + TranslateTextRequest request = + TranslateTextRequest.newBuilder() + .setParent(parent.toString()) + .setMimeType("text/plain") + .setTargetLanguageCode("en") + .addContents(text) + .build(); + + TranslateTextResponse response = client.translateText(request); + + for (Translation translation : response.getTranslationsList()) { + textlist.add(String.format("%s", translation.getTranslatedText())); + } + //System.out.println("Text : "+textlist.get(0)); + } + + return textlist.get(0); + } + + public List detectEvent(String korean, String translation, String language, String English) throws JsonProcessingException { String url = "https://notinote.herokuapp.com/event-dict"; HttpHeaders headers = new HttpHeaders(); @@ -123,6 +151,7 @@ public List detectEvent(String korean, String translation, Stri body.put("language", language); body.put("kr_text", korean); body.put("translated_text", translation); + body.put("en_text", English); HttpEntity request = new HttpEntity<>(body.toString(), headers); String response = new RestTemplate().postForObject(url, request, String.class); @@ -164,7 +193,8 @@ public NoticeTitleListDto saveNotice(MultipartFile uploadfile, NoticeRequestDto .build(); noticeRepository.save(notice); - List eventWords = detectEvent(notice.getOrigin_full(), notice.getTrans_full(), user.getUlanguage()); + String en_full = englishText(noticeRequestDto.getKorean()); + List eventWords = detectEvent(notice.getOrigin_full(), notice.getTrans_full(), user.getUlanguage(), en_full); List events = new ArrayList<>(); for (EventRequestDto dto : eventWords) events.add(eventService.create(dto, notice)); From 399c8efac7c8b7c8ad6da3281314142d3eff0ccd Mon Sep 17 00:00:00 2001 From: hellouz818 Date: Wed, 1 Jun 2022 03:29:38 +0900 Subject: [PATCH 2/2] [#25] feat: Add Event num & Event List --- .../Notice/controller/NoticeController.java | 4 +++- .../notinote/Notice/dto/NoticeEventListDto.java | 16 ++++++++++++++++ .../answer/notinote/Notice/dto/NoticeOCRDto.java | 6 +++++- .../notinote/Notice/service/NoticeService.java | 11 +++++++++++ 4 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeEventListDto.java diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java index 1bed0e6..0f1b80f 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/controller/NoticeController.java @@ -41,7 +41,9 @@ public NoticeOCRDto executeOCR (@RequestPart MultipartFile uploadfile, HttpServl String en_full = noticeService.englishText(korean); // 영어 추출 List eventWords = noticeService.detectEvent(korean, trans_full, targetLanguage, en_full); //이벤트 추출 List fullText = noticeService.extractSentenceFromEventRequestDto(trans_full, eventWords); - return new NoticeOCRDto(korean, trans_full, fullText); + List events = noticeService.extractEventList(fullText); + Integer event_num = events.size(); + return new NoticeOCRDto(korean, trans_full, fullText, event_num, events); } @RequestMapping(value = "/notice/save", method = RequestMethod.POST) diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeEventListDto.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeEventListDto.java new file mode 100644 index 0000000..05c5ea4 --- /dev/null +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeEventListDto.java @@ -0,0 +1,16 @@ +package com.answer.notinote.Notice.dto; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; + +import java.time.LocalDate; + +@Getter +@AllArgsConstructor +@Builder +public class NoticeEventListDto { + String title; + LocalDate date; + +} diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeOCRDto.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeOCRDto.java index bcec703..cc3b5df 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeOCRDto.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/dto/NoticeOCRDto.java @@ -12,10 +12,14 @@ public class NoticeOCRDto { private String korean; private String trans_full; private List fullText; + private Integer event_num; + private List events; - public NoticeOCRDto(String korean, String trans_full, List fullText){ + public NoticeOCRDto(String korean, String trans_full, List fullText, Integer event_num, List events){ this.korean = korean; this.trans_full = trans_full; this.fullText = fullText; + this.event_num = event_num; + this.events = events; } } diff --git a/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java b/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java index 1a8d0b6..ac049cc 100644 --- a/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java +++ b/spring/notinote/src/main/java/com/answer/notinote/Notice/service/NoticeService.java @@ -277,6 +277,17 @@ public List extractSentenceFromEventRequestDto(String text, L return sentences; } + public List extractEventList(List fullText){ + List events = new ArrayList<>(); + + for (int i = 0; i < fullText.size(); i++){ + if(fullText.get(i).isHighlight()){ + NoticeEventListDto event = new NoticeEventListDto(fullText.get(i).getContent(), fullText.get(i).getDate()); + events.add(event); + } + } + return events; + } public Notice findNoticeById(Long id) { return noticeRepository.findById(id).orElseThrow(