Skip to content

Commit

Permalink
feat: 파싱을 위한 GptScheduleParser 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
yo0oni committed Mar 9, 2024
1 parent df048b3 commit fd069c5
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.isp.backend.domain.gpt.constant;

import java.util.List;

public class ParsingConstants {
public static final String DATE_REGEX = "(\\d{4}-\\d{2}-\\d{2})";
public static final String NEW_LINE_REGEX = "\n";
public static final String CURRENT_DATE = "";
public static final int BEGIN_INDEX = 3;
public static final List<String> FILTER_STRINGS = List.of(
"Message(role=assistant, content=", ")"
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.isp.backend.domain.gpt.entity;

import com.isp.backend.domain.gpt.constant.ParsingConstants;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RequiredArgsConstructor
@Component
public class GptScheduleParser {

public List<GptSchedule> parseScheduleText(String scheduleText) {
List<GptSchedule> schedules = new ArrayList<>();
Pattern pattern = Pattern.compile(ParsingConstants.DATE_REGEX);

List<String> lines = List.of(scheduleText.split(ParsingConstants.NEW_LINE_REGEX));
List<String> currentScheduleDetail = new ArrayList<>();
String currentDate = ParsingConstants.CURRENT_DATE;

for (String line : lines) {
processLine(line, pattern, schedules, currentScheduleDetail, currentDate);
}

addScheduleIfNotEmpty(currentDate, schedules, currentScheduleDetail);

return schedules;
}

private void processLine(String line, Pattern pattern, List<GptSchedule> schedules, List<String> currentScheduleDetail, String currentDate) {
Matcher dateMatcher = pattern.matcher(line);

if (dateMatcher.find()) {
handleNewDate(currentDate, schedules, currentScheduleDetail, dateMatcher.group(1));
} else {
handleNonDateLine(line, currentScheduleDetail);
}
}

private void handleNewDate(String currentDate, List<GptSchedule> schedules, List<String> currentScheduleDetail, String newDate) {
if (!currentDate.isEmpty()) {
schedules.add(new GptSchedule(currentDate, currentScheduleDetail));
}
}

private void handleNonDateLine(String line, List<String> currentScheduleDetail) {
if (!line.trim().isEmpty() && ParsingConstants.FILTER_STRINGS.stream().noneMatch(line::contains)) {
currentScheduleDetail.add(line.trim().substring(ParsingConstants.BEGIN_INDEX)); // Remove leading index
}
}

private void addScheduleIfNotEmpty(String currentDate, List<GptSchedule> schedules, List<String> currentScheduleDetail) {
if (!currentDate.isEmpty()) {
schedules.add(new GptSchedule(currentDate, currentScheduleDetail));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.isp.backend.domain.gpt.dto.GptScheduleResponseDto;
import com.isp.backend.domain.gpt.entity.GptMessage;
import com.isp.backend.domain.gpt.entity.GptSchedule;
import com.isp.backend.domain.gpt.mapper.JsonMapper;
import com.isp.backend.domain.gpt.entity.GptScheduleParser;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -27,7 +27,7 @@
@Service
public class GptService {
private final RestTemplate restTemplate;
private final JsonMapper jsonMapper;
private final GptScheduleParser gptScheduleParser;

@Value("${api-key.chat-gpt}")
private String apiKey;
Expand All @@ -50,7 +50,7 @@ public GptScheduleResponseDto getResponse(HttpEntity<GptRequestDTO> chatGptReque
GptConfig.CHAT_URL,
chatGptRequestHttpEntity,
GptResponseDTO.class);
List<GptSchedule> gptSchedules = jsonMapper.parseScheduleText(getScheduleText(responseEntity));
List<GptSchedule> gptSchedules = gptScheduleParser.parseScheduleText(getScheduleText(responseEntity));
return new GptScheduleResponseDto(gptSchedules);
}

Expand Down

0 comments on commit fd069c5

Please sign in to comment.