Skip to content

Commit

Permalink
[#25] fix: return sentence without eid when requesting OCR
Browse files Browse the repository at this point in the history
  • Loading branch information
raae7742 committed May 31, 2022
1 parent 2cf3098 commit db28cca
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.answer.notinote.Notice.controller;

import com.answer.notinote.Auth.token.provider.JwtTokenProvider;
import com.answer.notinote.Event.domain.Event;
import com.answer.notinote.Event.dto.EventRequestDto;
import com.answer.notinote.Event.service.EventService;
import com.answer.notinote.Notice.dto.NoticeOCRDto;
import com.answer.notinote.Notice.dto.NoticeRequestDto;
import com.answer.notinote.Notice.dto.NoticeSentenceDto;
import com.answer.notinote.Notice.dto.NoticeTitleListDto;
import com.answer.notinote.Notice.dto.*;
import com.answer.notinote.Notice.service.NoticeService;
import com.answer.notinote.User.domain.entity.User;
import com.answer.notinote.User.service.UserService;
Expand All @@ -18,7 +14,6 @@

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


Expand All @@ -45,10 +40,7 @@ public NoticeOCRDto executeOCR (@RequestPart MultipartFile uploadfile, HttpServl
String trans_full = noticeService.transText(korean, targetLanguage); //번역문 추출

List<EventRequestDto> eventWords = noticeService.detectEvent(korean, trans_full, targetLanguage); //이벤트 추출
List<Event> events = new ArrayList<>();
for (EventRequestDto word : eventWords) events.add(eventService.create(word, null));

List<NoticeSentenceDto> fullText = noticeService.extractSentenceFromEvent(trans_full, events);
List<NoticeSentenceDto> fullText = noticeService.extractSentenceFromEventRequestDto(trans_full, eventWords);
return new NoticeOCRDto(korean, trans_full, fullText);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.answer.notinote.Notice.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

import java.time.LocalDate;

@Getter
@AllArgsConstructor
@Builder
public class NoticeEventDto {
int id;
long eid;
String content;
LocalDate date;
boolean highlight;
boolean registered;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
@Builder
public class NoticeSentenceDto {
int id;
long eid = -1;
String content;
LocalDate date;
boolean highlight;
boolean registered;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ public class NoticeTitleListDto {
private String uploadfile;
private String title;
private LocalDate date;
private List<NoticeSentenceDto> fullText = new ArrayList<>();
private List<NoticeEventDto> fullText = new ArrayList<>();
private String korean;
private String trans_full;

public NoticeTitleListDto(Notice entity, List<NoticeSentenceDto> sentences){
public NoticeTitleListDto(Notice entity, List<NoticeEventDto> sentences){
this.uploadfile = entity.getNimageurl() + "/" + entity.getNimagename();
this.title = entity.getTitle();
this.date = entity.getNdate();
this.korean = entity.getOrigin_full();
this.trans_full = entity.getTrans_full();

this.fullText.addAll(sentences);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import com.answer.notinote.Auth.token.provider.JwtTokenProvider;
import com.answer.notinote.Child.service.ChildService;
import com.answer.notinote.Event.domain.Event;
import com.answer.notinote.Notice.dto.NoticeResponseBody;
import com.answer.notinote.Notice.dto.*;
import com.answer.notinote.Event.dto.EventRequestDto;
import com.answer.notinote.Event.service.EventService;
import com.answer.notinote.Exception.CustomException;
import com.answer.notinote.Exception.ErrorCode;
import com.answer.notinote.Notice.domain.entity.Notice;
import com.answer.notinote.Notice.domain.repository.NoticeRepository;
import com.answer.notinote.Notice.dto.NoticeRequestDto;
import com.answer.notinote.Notice.dto.NoticeSentenceDto;
import com.answer.notinote.Notice.dto.NoticeTitleListDto;
import com.answer.notinote.User.domain.entity.User;
import com.answer.notinote.User.domain.repository.UserRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
Expand Down Expand Up @@ -171,24 +168,17 @@ public NoticeTitleListDto saveNotice(MultipartFile uploadfile, NoticeRequestDto
List<Event> events = new ArrayList<>();
for (EventRequestDto dto : eventWords) events.add(eventService.create(dto, notice));

List<NoticeSentenceDto> sentences = extractSentenceFromEvent(notice.getTrans_full(), events);
List<NoticeEventDto> sentences = extractSentenceFromEvent(notice.getTrans_full(), events);

return new NoticeTitleListDto(notice, sentences);
}

public List<NoticeSentenceDto> extractSentenceFromEvent(String text, List<Event> events) {
List<NoticeSentenceDto> sentences = new ArrayList<>();
public List<NoticeEventDto> extractSentenceFromEvent(String text, List<Event> events) {
List<NoticeEventDto> sentences = new ArrayList<>();
int lastIndex = 0, id = 1;

if (events == null) {
NoticeSentenceDto dto = NoticeSentenceDto.builder()
.id(id)
.eid(-1)
.date(null)
.content(text)
.highlight(false)
.registered(false)
.build();
NoticeEventDto dto = new NoticeEventDto(id, -1, text, null, false, false);
sentences.add(dto);
return sentences;
}
Expand All @@ -199,47 +189,65 @@ public List<NoticeSentenceDto> extractSentenceFromEvent(String text, List<Event>
if (lastIndex != event.getIndex_start()) {
// event가 아닌 경우
String sentence = text.substring(lastIndex, event.getIndex_start());
NoticeSentenceDto dto = NoticeSentenceDto.builder()
.id(id++)
.eid(-1)
.content(sentence)
.date(null)
.highlight(false)
.registered(false)
.build();
NoticeEventDto dto = new NoticeEventDto(id++, -1, sentence, null, false, false);
sentences.add(dto);
}

// event인 경우
String sentence = text.substring(event.getIndex_start(), event.getIndex_end());
NoticeSentenceDto dto = NoticeSentenceDto.builder()
.id(id++)
.eid(event.getEid())
.content(sentence)
.date(event.getDate())
.highlight(true)
.registered(event.isRegistered())
.build();
NoticeEventDto dto = new NoticeEventDto(id++, event.getEid(), sentence, event.getDate(), true, event.isRegistered());
sentences.add(dto);

lastIndex = event.getIndex_end();
}

if (lastIndex != text.length() - 1) {
String sentence = text.substring(lastIndex, text.length() - 1);
NoticeEventDto dto = new NoticeEventDto(id, -1, sentence, null, false, false);
sentences.add(dto);
}

return sentences;
}

public List<NoticeSentenceDto> extractSentenceFromEventRequestDto(String text, List<EventRequestDto> events) {
List<NoticeSentenceDto> sentences = new ArrayList<>();
int lastIndex = 0, id = 1;

if (events == null) {
NoticeSentenceDto dto = new NoticeSentenceDto(id, text, null, false);
sentences.add(dto);
return sentences;
}

Collections.sort(events);

for (EventRequestDto event : events) {
if (lastIndex != event.getS_index()) {
// event가 아닌 경우
String sentence = text.substring(lastIndex, event.getS_index());
NoticeSentenceDto dto = new NoticeSentenceDto(id++, sentence, null, false);
sentences.add(dto);
}

// event인 경우
String sentence = text.substring(event.getS_index(), event.getE_index());
NoticeSentenceDto dto = new NoticeSentenceDto(id++, sentence, LocalDate.parse(event.getDate()), true);
sentences.add(dto);

lastIndex = event.getE_index();
}

if (lastIndex != text.length() - 1) {
String sentence = text.substring(lastIndex, text.length() - 1);
NoticeSentenceDto dto = NoticeSentenceDto.builder()
.id(id)
.eid(-1)
.content(sentence)
.date(null)
.highlight(false)
.registered(false)
.build();
NoticeSentenceDto dto = new NoticeSentenceDto(id, sentence, null, false);
sentences.add(dto);
}

return sentences;
}


public Notice findNoticeById(Long id) {
return noticeRepository.findById(id).orElseThrow(
() -> new CustomException(ErrorCode.NOT_FOUND)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.answer.notinote.Search.dto;

import com.answer.notinote.Notice.domain.entity.Notice;
import com.answer.notinote.Notice.dto.NoticeSentenceDto;
import com.answer.notinote.Notice.dto.NoticeEventDto;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -13,11 +12,11 @@
public class SearchResultDetailDto {
private Long id;
private String imageUri;
private List<NoticeSentenceDto> fullText;
private List<NoticeEventDto> fullText;
private String korean;

@Builder
public SearchResultDetailDto(Long id, String imageUri, List<NoticeSentenceDto> fullText, String korean){
public SearchResultDetailDto(Long id, String imageUri, List<NoticeEventDto> fullText, String korean){
this.id = id;
this.imageUri = imageUri;
this.fullText = fullText;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.answer.notinote.Event.service.EventService;
import com.answer.notinote.Notice.domain.entity.Notice;
import com.answer.notinote.Notice.domain.repository.NoticeRepository;
import com.answer.notinote.Notice.dto.NoticeSentenceDto;
import com.answer.notinote.Notice.dto.NoticeEventDto;
import com.answer.notinote.Notice.service.NoticeService;
import com.answer.notinote.Search.dto.SearchDetailDto;
import com.answer.notinote.Search.dto.SearchListDto;
Expand Down Expand Up @@ -88,7 +88,7 @@ public SearchDetailDto searchDetailList(String date, HttpServletRequest request)
Notice notice = notices.get(i);

List<Event> events = eventService.findAllByNotice(notice);
List<NoticeSentenceDto> fullText = noticeService.extractSentenceFromEvent(notice.getTrans_full(), events);
List<NoticeEventDto> fullText = noticeService.extractSentenceFromEvent(notice.getTrans_full(), events);

SearchResultDetailDto searchResultDetailDto = SearchResultDetailDto.builder()
.imageUri(notice.getNimageurl()+"/"+notice.getNimagename())
Expand Down
Binary file modified spring/notinote/tokens/StoredCredential
Binary file not shown.

0 comments on commit db28cca

Please sign in to comment.