Skip to content

Commit a55a5b4

Browse files
committed
fix : dataSet
1 parent 9f86860 commit a55a5b4

File tree

7 files changed

+516
-36
lines changed

7 files changed

+516
-36
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,7 @@ terraform/secrets.tf
5555
### Claude AI ###
5656
CLAUDE.md
5757
.claude/
58+
59+
## dataSet ##
60+
cocktails_ver00.csv
61+
cocktails_ver01.csv

src/main/java/com/back/domain/cocktail/service/CocktailService.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,16 +133,19 @@ private String convertFractions(String ingredient) {
133133
if (ingredient == null) return null;
134134

135135
// 치환 테이블 생성
136-
Map<String, String> fractionMap = Map.of(
137-
"1/2", "½",
138-
"1/3", "⅓",
139-
"2/3", "⅔",
140-
"1/4", "¼",
141-
"3/4", "¾",
142-
"1/8", "⅛",
143-
"3/8", "⅜",
144-
"5/8", "⅝",
145-
"7/8", "⅞"
136+
Map<String, String> fractionMap = Map.ofEntries(
137+
Map.entry("1/2", "½"),
138+
Map.entry("1/3", "⅓"),
139+
Map.entry("2/3", "⅔"),
140+
Map.entry("1/4", "¼"),
141+
Map.entry("3/4", "¾"),
142+
Map.entry("1/8", "⅛"),
143+
Map.entry("3/8", "⅜"),
144+
Map.entry("5/8", "⅝"),
145+
Map.entry("7/8", "⅞"),
146+
Map.entry("1/5", "⅕"),
147+
Map.entry("2/5", "⅖"),
148+
Map.entry("1/6", "⅙")
146149
);
147150

148151
// 테이블 기반 치환
@@ -176,7 +179,7 @@ private List<IngredientDto> parseIngredients(String ingredientStr) {
176179

177180
// (숫자 + 선택적 분수) + (공백) + (단위)
178181
Pattern pattern = Pattern.compile(
179-
"^([0-9]*\\s*[½⅓⅔¼¾⅛⅜⅝⅞]?)\\s*(.*)$",
182+
"^([0-9]*\\s*[½⅓⅔¼¾⅛⅜⅝⅞⅕⅖⅙]?)\\s*(.*)$",
180183
Pattern.UNICODE_CHARACTER_CLASS
181184
);
182185
Matcher matcher = pattern.matcher(amountUnit);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.back.global.standard.util;
2+
3+
import java.io.*;
4+
5+
public class CocktailDataCleaner {
6+
public static void main(String[] args) {
7+
String inputPath = "src/main/resources/cocktails.csv"; // 원본 파일 경로
8+
String outputPath = "src/main/resources/cocktails_clean.csv"; // 정제 후 파일 경로
9+
10+
try (
11+
BufferedReader br = new BufferedReader(new FileReader(inputPath));
12+
BufferedWriter bw = new BufferedWriter(new FileWriter(outputPath))
13+
) {
14+
String line;
15+
boolean isHeader = true;
16+
17+
while ((line = br.readLine()) != null) {
18+
// CSV 안의 쉼표와 큰따옴표를 안전하게 처리
19+
String[] columns = splitCsvLine(line);
20+
21+
if (isHeader) {
22+
bw.write(String.join(",", columns));
23+
bw.newLine();
24+
isHeader = false;
25+
continue;
26+
}
27+
28+
// ✅ 6번째 인덱스(ingredient) 컬럼만 변환
29+
if (columns.length > 6) {
30+
columns[6] = DecimalToFractionConverter.convert(columns[6]);
31+
}
32+
33+
bw.write(String.join(",", columns));
34+
bw.newLine();
35+
}
36+
37+
System.out.println("✅ 칵테일 데이터 정제가 완료되었습니다: " + outputPath);
38+
39+
} catch (IOException e) {
40+
System.err.println("❌ 파일 처리 중 오류 발생: " + e.getMessage());
41+
}
42+
}
43+
44+
/**
45+
* CSV 한 줄을 안전하게 split하는 메서드
46+
* (문장 안에 쉼표가 포함된 경우 대응)
47+
*/
48+
private static String[] splitCsvLine(String line) {
49+
// 따옴표를 기준으로 나누되, 따옴표 안의 콤마는 무시
50+
return line.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)", -1);
51+
}
52+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.back.global.standard.util;
2+
3+
public class DecimalToFractionConverter {
4+
// ingredient 문자열 전체를 처리 (예: "진:4.5 cl, 레몬 주스:1.5 cl")
5+
public static String convert(String value) {
6+
if (value == null || value.isBlank()) return value;
7+
8+
// 쉼표로 구분된 각 재료별로 처리
9+
String[] items = value.split(",");
10+
for (int i = 0; i < items.length; i++) {
11+
items[i] = convertSingleItem(items[i].trim());
12+
}
13+
return String.join(", ", items);
14+
}
15+
16+
// 단일 재료 문자열 처리
17+
private static String convertSingleItem(String item) {
18+
try {
19+
// 숫자만 추출 (정수+소수)
20+
String numericPart = item.replaceAll("[^0-9.]", "");
21+
if (numericPart.isEmpty()) return item;
22+
23+
double number = Double.parseDouble(numericPart);
24+
int intPart = (int) number;
25+
double fracPart = number - intPart;
26+
double tolerance = 0.02;
27+
28+
String fraction = "";
29+
30+
// 소수 -> 분수 변환
31+
if (Math.abs(fracPart - 0.25) < tolerance) fraction = "1/4";
32+
else if (Math.abs(fracPart - 0.33) < tolerance || Math.abs(fracPart - 0.3333) < tolerance)
33+
fraction = "1/3";
34+
else if (Math.abs(fracPart - 0.5) < tolerance) fraction = "1/2";
35+
else if (Math.abs(fracPart - 0.66) < tolerance || Math.abs(fracPart - 0.6666) < tolerance)
36+
fraction = "2/3";
37+
else if (Math.abs(fracPart - 0.75) < tolerance) fraction = "3/4";
38+
39+
// fraction이 있으면 int + fraction, 없으면 정수면 int만, 소수면 그대로
40+
String fractionStr;
41+
if (!fraction.isEmpty()) {
42+
fractionStr = intPart == 0 ? fraction : intPart + " " + fraction;
43+
} else {
44+
fractionStr = (number == intPart) ? String.valueOf(intPart) : String.valueOf(number);
45+
}
46+
47+
// 원본 item에서 숫자 부분만 교체
48+
return item.replace(numericPart, fractionStr);
49+
50+
} catch (NumberFormatException e) {
51+
return item; // 이미 분수 등 숫자가 아닌 경우 그대로 반환
52+
}
53+
}
54+
55+
// 테스트용 main
56+
public static void main(String[] args) {
57+
String test = "진:4.5 cl, 레몬 주스:1.5 cl, 마라스키노 리큐르:1.5 cl, 설탕:1.0 cl, 물:2 cl";
58+
System.out.println("변환 전: " + test);
59+
System.out.println("변환 후: " + convert(test));
60+
61+
String[] testValues = {"4.5", "1.5", "0.5", "2.25", "3.75", "1 1/2", "abc", "2.0"};
62+
for (String val : testValues) {
63+
System.out.printf("%s → %s%n", val, convert(val));
64+
}
65+
}
66+
}

src/main/java/com/back/global/standard/util/FractionFinder.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,45 @@
44
import java.nio.file.Files;
55
import java.nio.file.Path;
66
import java.nio.file.Paths;
7+
import java.util.HashSet;
8+
import java.util.Set;
79
import java.util.regex.Matcher;
810
import java.util.regex.Pattern;
911

1012
public class FractionFinder {
1113

1214
public static void main(String[] args) {
1315
try {
14-
// 1️⃣ SQL 파일 경로 설정
16+
// 1️⃣ CSV 파일 경로 설정
1517
Path path = Paths.get("src/main/resources/cocktails.csv");
1618

1719
// 2️⃣ 파일 읽기
1820
String content = Files.readString(path);
1921

2022
// 3️⃣ 분수 패턴 정의 (예: 1/2, 3/4 등)
21-
Pattern pattern = Pattern.compile("\\d/\\d"); // 공백/문자 붙어 있어도 매칭
23+
Pattern pattern = Pattern.compile("\\d/\\d");
2224
Matcher matcher = pattern.matcher(content);
2325

24-
// 4️⃣ 분수 출력
25-
boolean found = false;
26+
// 4️⃣ 중복 제거용 Set
27+
Set<String> fractions = new HashSet<>();
28+
2629
while (matcher.find()) {
27-
System.out.println("찾은 분수: " + matcher.group());
28-
found = true;
30+
fractions.add(matcher.group()); // Set에 넣으면 자동 중복 제거
2931
}
3032

31-
if (!found) {
32-
System.out.println("SQL 파일에 분수 표현이 없습니다.");
33+
// 5️⃣ 출력
34+
if (fractions.isEmpty()) {
35+
System.out.println("CSV 파일에 분수 표현이 없습니다.");
36+
} else {
37+
System.out.println("찾은 분수 표현 (중복 제거):");
38+
for (String frac : fractions) {
39+
System.out.println(frac);
40+
}
3341
}
3442

3543
} catch (IOException e) {
36-
System.err.println("SQL 파일을 읽는 중 오류 발생: " + e.getMessage());
44+
System.err.println("CSV 파일을 읽는 중 오류 발생: " + e.getMessage());
3745
e.printStackTrace();
3846
}
3947
}
40-
}
48+
}

0 commit comments

Comments
 (0)