-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'backend/Feature/137-gpt-add-price' into dev_backend
- Loading branch information
Showing
7 changed files
with
140 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 31 additions & 10 deletions
41
backend/src/main/java/com/isp/backend/domain/gpt/constant/ParsingConstants.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,34 @@ | ||
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 String COMMA = ", "; | ||
public static final List<String> FILTER_STRINGS = List.of( | ||
"Message(role=assistant, content=", ")" | ||
); | ||
import lombok.Getter; | ||
|
||
import javax.swing.*; | ||
|
||
@Getter | ||
public enum ParsingConstants { | ||
COMMA(", "), | ||
ENTRY_SEPARATOR("<"), | ||
LINE_SEPARATOR("\n"), | ||
DATE_SUFFIX(">"), | ||
PRICE_PREFIX("("), | ||
PRICE_SUFFIX(")"), | ||
DETAIL_PREFIX("- "), | ||
PRICE_FREE("무료"), | ||
PRICE_VAR("변동"), | ||
DEFAULT_PRICE(0.0), | ||
DETAIL_SUFFIX(":"), | ||
DEFAULT_COORDINATE(0.0); | ||
private final String stringValue; | ||
private final Double doubleValue; | ||
|
||
ParsingConstants(String stringValue) { | ||
this.stringValue = stringValue; | ||
this.doubleValue = null; | ||
} | ||
|
||
ParsingConstants(Double doubleValue) { | ||
this.stringValue = null; | ||
this.doubleValue = doubleValue; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 59 additions & 29 deletions
88
backend/src/main/java/com/isp/backend/domain/gpt/entity/GptScheduleParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,95 @@ | ||
package com.isp.backend.domain.gpt.entity; | ||
|
||
import com.isp.backend.domain.gpt.constant.ParsingConstants; | ||
import com.isp.backend.global.exception.gpt.NonValidatedParsingException; | ||
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 { | ||
|
||
private static final Pattern LINE_PATTERN = Pattern.compile("-\\s*(.*?):\\s*([\\d.]+,\\s*[\\d.]+)?\\s*\\((.*?)\\)"); | ||
|
||
public List<GptSchedule> parseScheduleText(String scheduleText) { | ||
System.out.println(scheduleText); | ||
List<GptSchedule> schedules = new ArrayList<>(); | ||
|
||
String[] entries = scheduleText.split("<"); | ||
String[] entries = scheduleText.split(ParsingConstants.ENTRY_SEPARATOR.getStringValue()); | ||
|
||
for (String entry : entries) { | ||
if (entry.trim().isEmpty()) continue; | ||
|
||
String[] lines = entry.split("\n"); | ||
String date = lines[0].trim().replace(">", ""); | ||
String[] lines = entry.split(ParsingConstants.LINE_SEPARATOR.getStringValue()); | ||
String date = parseDate(lines[0]); | ||
|
||
List<GptScheduleDetail> scheduleDetails = new ArrayList<>(); | ||
|
||
for (int i = 1; i < lines.length; i++) { | ||
String line = lines[i].trim(); | ||
if (line.isEmpty()) continue; | ||
|
||
String[] parts = line.split(" "); | ||
StringBuilder detail = new StringBuilder(); | ||
double latitude = 0.0; | ||
double longitude = 0.0; | ||
|
||
for (int j = 0; j < parts.length; j++) { | ||
try { | ||
if (j == parts.length - 2) { | ||
latitude = Double.parseDouble(parts[j].replace(",", "")); | ||
} else if (j == parts.length - 1) { | ||
longitude = Double.parseDouble(parts[j]); | ||
} else { | ||
if (!detail.isEmpty()) detail.append(" "); | ||
detail.append(parts[j]); | ||
} | ||
} catch (NumberFormatException e) { | ||
if (!detail.isEmpty()) detail.append(" "); | ||
detail.append(parts[j]); | ||
} | ||
} | ||
Matcher matcher = LINE_PATTERN.matcher(line); | ||
if (matcher.find()) { | ||
String detail = matcher.group(1); | ||
String coordinatesStr = matcher.group(2); | ||
String priceStr = matcher.group(3); | ||
|
||
String detailString = detail.toString().trim(); | ||
if (detailString.startsWith("-")) { | ||
detailString = detailString.substring(1).trim(); | ||
} | ||
Coordinate coordinate = parseCoordinates(coordinatesStr); | ||
double price = parsePriceString(priceStr); | ||
|
||
scheduleDetails.add(new GptScheduleDetail(detailString, new Coordinate(latitude, longitude))); | ||
scheduleDetails.add(new GptScheduleDetail(detail, price, coordinate)); | ||
} else { | ||
System.out.println("767867889"); | ||
throw new NonValidatedParsingException(); | ||
} | ||
} | ||
|
||
schedules.add(new GptSchedule(date, scheduleDetails)); | ||
} | ||
|
||
return schedules; | ||
} | ||
|
||
private String parseDate(String line) { | ||
return line.trim().replace(ParsingConstants.DATE_SUFFIX.getStringValue(), ""); | ||
} | ||
|
||
private Coordinate parseCoordinates(String coordinatesStr) { | ||
if (coordinatesStr == null || coordinatesStr.isEmpty()) { | ||
return new Coordinate(ParsingConstants.DEFAULT_COORDINATE.getDoubleValue(), ParsingConstants.DEFAULT_COORDINATE.getDoubleValue()); | ||
} | ||
|
||
String[] parts = coordinatesStr.split(","); | ||
if (parts.length != 2) { | ||
System.out.println("1546351"); | ||
throw new NonValidatedParsingException(); | ||
} | ||
|
||
try { | ||
double latitude = Double.parseDouble(parts[0].trim()); | ||
double longitude = Double.parseDouble(parts[1].trim()); | ||
return new Coordinate(latitude, longitude); | ||
} catch (NumberFormatException e) { | ||
System.out.println("1234"); | ||
throw new NonValidatedParsingException(); | ||
} | ||
} | ||
|
||
private double parsePriceString(String priceStr) { | ||
if (ParsingConstants.PRICE_FREE.getStringValue().equals(priceStr)) { | ||
return 0.0; | ||
} | ||
|
||
try { | ||
return Double.parseDouble(priceStr.replaceAll("[^\\d.]", "")); | ||
} catch (NumberFormatException e) { | ||
System.out.println("5678"); | ||
return ParsingConstants.DEFAULT_PRICE.getDoubleValue(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
backend/src/main/java/com/isp/backend/global/exception/gpt/NonValidatedParsingException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.isp.backend.global.exception.gpt; | ||
|
||
import com.isp.backend.global.exception.CustomException; | ||
import com.isp.backend.global.exception.ErrorCode; | ||
|
||
public class NonValidatedParsingException extends CustomException { | ||
public NonValidatedParsingException() { | ||
super(ErrorCode.PARSING_IS_NOT_VALIDATED); | ||
} | ||
} |