Skip to content

Commit

Permalink
🔨 feat: change STT data including time #26
Browse files Browse the repository at this point in the history
  • Loading branch information
noparamin committed Nov 2, 2023
1 parent 226ff9b commit 50a5b02
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/main/java/com/smart/watchboard/service/STTService.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.smart.watchboard.domain.SttData;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -13,6 +14,11 @@
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@RequiredArgsConstructor
@Slf4j
Expand All @@ -23,7 +29,7 @@ public class STTService {
@Value("${clova.stt.invoke-url")
private String clovaInvokeURL;

public String getSTT(String path) throws JsonProcessingException {
public ResponseEntity<String> getSTT(String path) throws JsonProcessingException {
RestTemplate restTemplate = new RestTemplate();
String requestURL = clovaInvokeURL + "/recognizer/url";
HttpHeaders headers = new HttpHeaders();
Expand All @@ -38,10 +44,34 @@ public String getSTT(String path) throws JsonProcessingException {
System.out.println(requestEntity.getBody());
ResponseEntity<String> responseEntity = restTemplate.exchange(requestURL, HttpMethod.POST, requestEntity, String.class);

return responseEntity;
}

public List<SttData> getSTTData(ResponseEntity<String> sttResponseEntity) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode jsonNode = objectMapper.readTree(responseEntity.getBody());
JsonNode rootNode = objectMapper.readTree(sttResponseEntity.getBody());
JsonNode segmentsNode = rootNode.get("segments");
Map<String, List<SttData>> result = new HashMap<>();
List<SttData> sttDatas = new ArrayList<>();
for(JsonNode segment : segmentsNode) {
int start = segment.get("start").asInt();
int end = segment.get("end").asInt();
String text = segment.get("text").asText();

SttData sttData = new SttData(start, end, text);
sttDatas.add(sttData);
}

String text = jsonNode.get("text").asText();
//result.put("data", sttDatas);

//return result;
return sttDatas;
}

public String getText(ResponseEntity<String> sttResponseEntity) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper();
JsonNode rootNode = objectMapper.readTree(sttResponseEntity.getBody());
String text = rootNode.get("text").asText();

return text;
}
Expand Down

0 comments on commit 50a5b02

Please sign in to comment.