Skip to content

Commit

Permalink
Merge pull request #220 from twenty-three-23/feature/TT-268-fix-stt-p…
Browse files Browse the repository at this point in the history
…rocess
  • Loading branch information
ch8930 authored Jul 17, 2024
2 parents c4af5a8 + ffd365f commit f008786
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
import com.twentythree.peech.script.domain.SentenceEntity;
import com.twentythree.peech.script.dto.paragraphIdToExpectedTime;
import com.twentythree.peech.script.repository.SentenceRepository;
import com.twentythree.peech.script.stt.dto.EditClovaSpeechSentenceVO;
import com.twentythree.peech.script.stt.dto.SentenceVO;
import com.twentythree.peech.script.stt.dto.AddSentenceInformationVO;
import com.twentythree.peech.script.stt.utils.RealTimeUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -48,23 +47,22 @@ public List<Long> saveInputSentencesByParagraphs(ScriptEntity scriptEntity, Stri
}

@Transactional
public List<SentenceVO> saveSTTSentences(ScriptEntity scriptEntity, List<EditClovaSpeechSentenceVO> sentenceAndRealTimeList, List<List<Integer>> sentenceSpan) {
public List<SentenceEntity> saveSTTSentences(ScriptEntity scriptEntity, List<AddSentenceInformationVO> sentenceAndRealTimeList, List<List<Integer>> sentenceSpan) {

Long paragraphId = 0L;
List<SentenceVO> sentenceVOList = new ArrayList<>();
List<SentenceEntity> sentenceEntityList = new ArrayList<>();

for(List<Integer> paragraph : sentenceSpan) {
for (Integer index : paragraph) {
EditClovaSpeechSentenceVO sentence = sentenceAndRealTimeList.get(index);
AddSentenceInformationVO sentence = sentenceAndRealTimeList.get(index);
SentenceEntity sentenceEntity = SentenceEntity.ofCreateSTTSentence(scriptEntity, paragraphId, sentence.sentenceContent(), sentence.sentenceOrder(), RealTimeUtils.convertMsToTimeFormat(sentence.sentenceDuration()));
sentenceRepository.save(sentenceEntity);
SentenceVO sentenceVO = new SentenceVO(sentenceEntity);
sentenceVOList.add(sentenceVO);
sentenceEntityList.add(sentenceEntity);
}
paragraphId++;
}

return sentenceVOList;
return sentenceEntityList;
}

public List<paragraphIdToExpectedTime> getParagraphExpectedTime(Long scriptId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import java.time.LocalTime;

public record EditClovaSpeechSentenceVO(
public record AddSentenceInformationVO(
Long sentenceOrder,
String sentenceContent,
int sentenceDuration
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class ParagraphDivideResponseDto {
@JsonIgnoreProperties(ignoreUnknown = true)
public static class Result {

@JsonProperty("paragraphs")
@JsonProperty("paragraphList")
@JsonAlias("topicSeg")
private List<List<String>> paragraphs;
private List<List<String>> paragraphList;

@JsonProperty("span")
private List<List<Integer>> span;
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.twentythree.peech.script.stt.service;

import com.twentythree.peech.script.stt.dto.AddSentenceInformationVO;
import com.twentythree.peech.script.stt.dto.response.ClovaResponseDto;
import com.twentythree.peech.script.stt.dto.response.ParagraphDivideResponseDto;
import com.twentythree.peech.script.stt.utils.MergeWordListUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class AddRealTimeToSentenceService {
public List<AddSentenceInformationVO> addRealTimeToSentence(ClovaResponseDto clovaResponseDto, ParagraphDivideResponseDto paragraphDivideResponseDto) {

// AddSentenceInformationVO 객체 리스트를 만들어야 함
List<AddSentenceInformationVO> AddSentenceInformationVOList = new ArrayList<>();

// 문단 나누기 결과 가져오기
List<String> sentenceList = paragraphDivideResponseDto.getResult().getParagraphList().stream().flatMap(List::stream).toList();

// clovaResponseDto에서 wordList 가져오기
List<List<Object>> wordList = MergeWordListUtils.mergeWordList(clovaResponseDto);

int startIndex = 0;
int endIndex = 0;
int startStamp = 0;
int endStamp = 0;
long sentenceOrder = 0L;

// 문장의 단어 개수를 기반으로 문장의 시작 시간과 종료 시간을 계산
for(String sentence : sentenceList){
startStamp = (int) wordList.get(startIndex).get(0);
// 해당 문장의 단어 개수를 알아야함
int wordCount = sentence.split(" ").length;
endIndex = startIndex + wordCount - 1;
endStamp = (int) wordList.get(endIndex).get(1);
int duration = endStamp - startStamp;

AddSentenceInformationVO AddSentenceInformationVO = new AddSentenceInformationVO(++sentenceOrder, sentence, duration);
AddSentenceInformationVOList.add(AddSentenceInformationVO);
startIndex = endIndex + 1;
}

return AddSentenceInformationVOList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@
package com.twentythree.peech.script.stt.service;

import com.twentythree.peech.script.cache.RedisTemplateImpl;
import com.twentythree.peech.script.domain.SentenceEntity;
import com.twentythree.peech.script.dto.NowStatus;
import com.twentythree.peech.script.dto.RedisSentenceDTO;
import com.twentythree.peech.script.stt.dto.EditClovaSpeechSentenceVO;
import com.twentythree.peech.script.stt.dto.STTResultSentenceDto;
import com.twentythree.peech.script.stt.dto.SentenceVO;
import com.twentythree.peech.script.stt.dto.response.*;
import com.twentythree.peech.script.stt.utils.RealTimeUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// STT 결과를 클라이언트에게 전달할 VO 생성
@Service
Expand All @@ -23,58 +21,37 @@ public class CreateSTTResultService {

private final RedisTemplateImpl redisTemplateImpl;

public STTScriptResponseDTO createSTTResultResponseDto(ClovaResponseDto clovaResponseDto, List<EditClovaSpeechSentenceVO> sentenceAndRealTimeList, List<SentenceVO> sentenceVOList, ParagraphDivideResponseDto paragraphDivideResponseDto) {
public STTScriptResponseDTO createSTTResultResponseDto(ClovaResponseDto clovaResponseDto, List<SentenceEntity> sentenceEntityList) {

int timestamp;
Long paragraphId = 0L;
Long paragraphOrder = 0L;

List<STTResultSentenceDto> sttResultSentenceDtoList = new ArrayList<>();
// 문단별로 그룹핑
Map<Long, List<SentenceEntity>> sentenceListGroupByParagraph = sentenceEntityList.stream().collect(Collectors.groupingBy(SentenceEntity::getParagraphId));

// 전체 대본 소요 시간
LocalTime totalRealTime = clovaResponseDto.getTotalRealTime();

// sttSentenceDto 리스트에 있는 타임 스탬프들을 가져옴
List<Integer> sentencesRealTimeList = sentenceAndRealTimeList
.stream().map(EditClovaSpeechSentenceVO::sentenceDuration)
.toList();

for(int i = 0; i < sentencesRealTimeList.size(); i++){
SentenceVO sentenceVO = sentenceVOList.get(i);

Long id = sentenceVO.sentenceEntity().getSentenceId();
Long order = sentenceVO.sentenceEntity().getSentenceOrder();
String content = sentenceVO.sentenceEntity().getSentenceContent();

STTResultSentenceDto sttResultSentenceDto = new STTResultSentenceDto(id, order, content, sentencesRealTimeList.get(i));
sttResultSentenceDtoList.add(sttResultSentenceDto);
}
List<STTParagraphDTO> paragraphList = new ArrayList<>();

// paragraphDevideResponseDto에서 문단별로 나눈 index를 가져옴
List<List<Integer>> paragraphNumber = paragraphDivideResponseDto.getResult().getSpan();
for(Map.Entry<Long, List<SentenceEntity>> sentenceEntry : sentenceListGroupByParagraph.entrySet()) {

// 문단별 측정된 시간 저장
List<LocalTime> paragraphRealTime = new ArrayList<>();
List<SentenceDTO> sentenceDTOList = new ArrayList<>();

List<STTParagraphDTO> paragraphList = new ArrayList<>();
for(SentenceEntity sentenceEntity : sentenceEntry.getValue()) {
SentenceDTO sentenceDTO = new SentenceDTO(sentenceEntity.getSentenceId(), sentenceEntity.getSentenceOrder(), sentenceEntity.getSentenceContent());
sentenceDTOList.add(sentenceDTO);
}

for (List<Integer> paragraph : paragraphNumber) {
List<SentenceDTO> sentenceList = new ArrayList<>();
timestamp = 0;
for (Integer index : paragraph) {
List<SentenceEntity> sentenceList = sentenceEntry.getValue();
List<String> sentenceContentList = sentenceEntry.getValue().stream().map(SentenceEntity::getSentenceContent).toList();

Long sentenceId = sttResultSentenceDtoList.get(index).getSentenceId();
Long sentenceOrder = sttResultSentenceDtoList.get(index).getSentenceOrder();
String sentenceContent = sttResultSentenceDtoList.get(index).getContent();
// 문단에 해당하는 시간 합산
LocalTime realTimePerParagraph = sentenceList.stream()
.map(SentenceEntity::getSentenceRealTime)
.reduce(LocalTime.of(0,0,0),
((localTime, localTime2) -> localTime.plusHours(localTime2.getHour()).plusMinutes(localTime2.getMinute()).plusSeconds(localTime2.getSecond())));

timestamp += sttResultSentenceDtoList.get(index).getRealTime();
sentenceList.add(new SentenceDTO(sentenceId, sentenceOrder, sentenceContent));
redisTemplateImpl.saveSentenceInformation(sentenceId, new RedisSentenceDTO(paragraphId, paragraphOrder, Long.valueOf(index), sentenceContent, RealTimeUtils.convertMsToTimeFormat(timestamp), NowStatus.RealTime));
}
paragraphList.add(new STTParagraphDTO(paragraphId++, paragraphOrder++, RealTimeUtils.convertMsToTimeFormat(timestamp), NowStatus.RealTime, sentenceList));
STTParagraphDTO sttParagraphDTO = new STTParagraphDTO(sentenceEntry.getKey(), sentenceEntry.getKey(), realTimePerParagraph, NowStatus.RealTime, sentenceDTOList);
paragraphList.add(sttParagraphDTO);
}

return new STTScriptResponseDTO(paragraphList);
return new STTScriptResponseDTO(paragraphList);// return new STTScriptResponseDTO(paragraphList);
}

}
Expand Down

This file was deleted.

Loading

0 comments on commit f008786

Please sign in to comment.