Skip to content

Commit

Permalink
Merge pull request #95 from dsc-sookmyung/feature/translate-backend
Browse files Browse the repository at this point in the history
[#25] fix: Add Translated English Text to fastapi
  • Loading branch information
raae7742 authored Jun 1, 2022
2 parents 548fe2f + 399c8ef commit 3c55c20
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ public NoticeOCRDto executeOCR (@RequestPart MultipartFile uploadfile, HttpServl

String korean = noticeService.detectText(uploadfile); //원문 추출
String trans_full = noticeService.transText(korean, targetLanguage); //번역문 추출

List<EventRequestDto> eventWords = noticeService.detectEvent(korean, trans_full, targetLanguage); //이벤트 추출
String en_full = noticeService.englishText(korean); // 영어 추출
List<EventRequestDto> eventWords = noticeService.detectEvent(korean, trans_full, targetLanguage, en_full); //이벤트 추출
List<NoticeSentenceDto> fullText = noticeService.extractSentenceFromEventRequestDto(trans_full, eventWords);
return new NoticeOCRDto(korean, trans_full, fullText);
List<NoticeEventListDto> 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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ public class NoticeOCRDto {
private String korean;
private String trans_full;
private List<NoticeSentenceDto> fullText;
private Integer event_num;
private List<NoticeEventListDto> events;

public NoticeOCRDto(String korean, String trans_full, List<NoticeSentenceDto> fullText){
public NoticeOCRDto(String korean, String trans_full, List<NoticeSentenceDto> fullText, Integer event_num, List<NoticeEventListDto> events){
this.korean = korean;
this.trans_full = trans_full;
this.fullText = fullText;
this.event_num = event_num;
this.events = events;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,35 @@ public String transText(String korean, String targetLanguage) throws IOException
return textlist.get(0);
}

public List<EventRequestDto> 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 <String> textlist = new ArrayList<String>();

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<EventRequestDto> detectEvent(String korean, String translation, String language, String English) throws JsonProcessingException {
String url = "https://notinote.herokuapp.com/event-dict";

HttpHeaders headers = new HttpHeaders();
Expand All @@ -123,6 +151,7 @@ public List<EventRequestDto> 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<String> request = new HttpEntity<>(body.toString(), headers);

String response = new RestTemplate().postForObject(url, request, String.class);
Expand Down Expand Up @@ -164,7 +193,8 @@ public NoticeTitleListDto saveNotice(MultipartFile uploadfile, NoticeRequestDto
.build();
noticeRepository.save(notice);

List<EventRequestDto> eventWords = detectEvent(notice.getOrigin_full(), notice.getTrans_full(), user.getUlanguage());
String en_full = englishText(noticeRequestDto.getKorean());
List<EventRequestDto> eventWords = detectEvent(notice.getOrigin_full(), notice.getTrans_full(), user.getUlanguage(), en_full);
List<Event> events = new ArrayList<>();
for (EventRequestDto dto : eventWords) events.add(eventService.create(dto, notice));

Expand Down Expand Up @@ -247,6 +277,17 @@ public List<NoticeSentenceDto> extractSentenceFromEventRequestDto(String text, L
return sentences;
}

public List<NoticeEventListDto> extractEventList(List<NoticeSentenceDto> fullText){
List<NoticeEventListDto> 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(
Expand Down

0 comments on commit 3c55c20

Please sign in to comment.