Skip to content

Commit

Permalink
[#25] feat: Translate Summarized List Korean to other languages
Browse files Browse the repository at this point in the history
  • Loading branch information
hellouz818 committed Mar 9, 2022
1 parent dd11b9c commit 2b0a8fb
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,16 @@

import com.answer.notinote.Notice.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gcp.vision.CloudVisionTemplate;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;



@RestController
public class NoticeController {

@Autowired
private ResourceLoader resourceLoader;
@Autowired
private CloudVisionTemplate cloudVisionTemplate;
@Autowired
NoticeService noticeService;

Expand All @@ -32,7 +25,8 @@ public String saveImage (@RequestPart MultipartFile uploadfile) throws IOExcepti
Long nid = noticeService.saveImage(uploadfile);
String koreantext = noticeService.detectText(nid);
String transtext = noticeService.transText(nid);
return "Text from image: " + transtext;
String trans_summarizedtext = noticeService.transSumText(nid);
return "Text from image: " + trans_summarizedtext;
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@ public void update_trans_full(String trans_full){
this.trans_full = trans_full;
}

public void update_trans_sum(String trans_sum){
this.trans_sum = trans_sum;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

Expand Down Expand Up @@ -129,5 +130,43 @@ public String transText(Long nid) throws IOException {
return textlist.get(0);
}

public String transSumText(Long nid) throws IOException {

Notice notice = noticeRepository.findByNid(nid);
//추후 nid값 이외에도 인공지능에 넘어온 요약 스트링까지 인자값으로 받아야함
//List<String> summarizedKoreanlist = Arrays.asList(text.split(","));
//System.out.println("Text : "+summarizedKoreanlist.size());
List <String> summarizedKoreanlist = new ArrayList<String>();
summarizedKoreanlist.add("졸업식은 2월 17일 3시입니다.");
summarizedKoreanlist.add("학부모님 모두 참석이 가능합니다.");

String projectId = "notinote-341918";
String targetLanguage = "en"; // 추후 입력받아야함
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
for (String text : summarizedKoreanlist) {
TranslateTextRequest request =
TranslateTextRequest.newBuilder()
.setParent(parent.toString())
.setMimeType("text/plain")
.setTargetLanguageCode(targetLanguage)
.addContents(text)
.build();

TranslateTextResponse response = client.translateText(request);

for (Translation translation : response.getTranslationsList()) {
textlist.add(String.format("%s", translation.getTranslatedText()));
}
}
}
notice.update_trans_sum(textlist.toString());
noticeRepository.save(notice);
return textlist.toString();
}

}

0 comments on commit 2b0a8fb

Please sign in to comment.