File size: 4,658 Bytes
e850536
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# λ°μ΄ν„°μ…‹μ˜ μ‚¬μš© 쑰사 보고 κ·Έλž˜ν”„

import pandas as pd
import json
import re

# 파일 경둜 μ„€μ •
file_path = './data/'

# ν›ˆλ ¨ 라벨 JSON 파일 뢈러였기
with open(file_path + 'training-label.json', 'r', encoding='utf-8') as file:
    training_data_raw = json.load(file)

# ν•„μš”ν•œ λ°μ΄ν„°λ§Œ μΆ”μΆœν•˜μ—¬ λ¦¬μŠ€νŠΈμ— μ €μž₯
extracted_data = []

# λ°μ΄ν„°λŠ” λ¦¬μŠ€νŠΈμ΄λ―€λ‘œ λ°”λ‘œ μˆœνšŒν•©λ‹ˆλ‹€.
for dialogue in training_data_raw:
    try:
        # 1. 감정 라벨 μΆ”μΆœ (emotion ν‚€λŠ” profile μ•ˆμ— μžˆμŠ΅λ‹ˆλ‹€)
        emotion_type = dialogue['profile']['emotion']['type']
        
        # 2. λŒ€ν™” ν…μŠ€νŠΈ μΆ”μΆœ (talk ν‚€ μ•ˆμ— contentκ°€ μžˆμŠ΅λ‹ˆλ‹€)
        dialogue_content = dialogue['talk']['content']
        
        # 3. λ”•μ…”λ„ˆλ¦¬μ˜ valueλ“€(ν…μŠ€νŠΈ)만 μΆ”μΆœν•©λ‹ˆλ‹€.
        texts = list(dialogue_content.values())
        
        # 4. λͺ¨λ“  ν…μŠ€νŠΈλ₯Ό ν•˜λ‚˜μ˜ λ¬Έμžμ—΄λ‘œ ν•©μΉ©λ‹ˆλ‹€.
        # 빈 λ¬Έμžμ—΄μ„ μ œκ±°ν•˜κ³  ν•©μΉ˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.
        full_text = " ".join([text for text in texts if text.strip()])
        
        # 5. 합쳐진 ν…μŠ€νŠΈμ™€ 감정 라벨이 λͺ¨λ‘ μœ νš¨ν•  κ²½μš°μ—λ§Œ μΆ”κ°€ν•©λ‹ˆλ‹€.
        if full_text and emotion_type:
            extracted_data.append({'text': full_text, 'emotion': emotion_type})
            
    except KeyError:
        # 'profile', 'emotion', 'talk', 'content' λ“±μ˜ ν‚€κ°€ μ—†λŠ” ν•­λͺ©μ€ κ±΄λ„ˆλœλ‹ˆλ‹€.
        continue

# μƒˆλ‘œμš΄ λ°μ΄ν„°ν”„λ ˆμž„ 생성
df_train = pd.DataFrame(extracted_data)

# 6. 합쳐진 데이터 확인
print("--- μΆ”μΆœλœ ν›ˆλ ¨ λ°μ΄ν„°ν”„λ ˆμž„μ˜ 첫 5쀄 ---")
print(df_train.head())

print("\n--- λ°μ΄ν„°ν”„λ ˆμž„ 크기 ---")
print(f"ν›ˆλ ¨ 데이터: {df_train.shape}")

# κΈ°μ‘΄ ν›ˆλ ¨ 데이터 λ‘œλ“œ μ½”λ“œ μ•„λž˜μ— μ΄μ–΄μ„œ μž‘μ„±ν•΄ μ£Όμ„Έμš”.
# ------------------------------------------------------------------

# 1. 검증 라벨 JSON 파일 뢈러였기
with open(file_path + 'validation-label.json', 'r', encoding='utf-8') as file:
    validation_data_raw = json.load(file)

# 2. 검증 데이터 μΆ”μΆœ
extracted_val_data = []

for dialogue in validation_data_raw:
    try:
        emotion_type = dialogue['profile']['emotion']['type']
        dialogue_content = dialogue['talk']['content']
        texts = list(dialogue_content.values())
        full_text = " ".join([text for text in texts if text.strip()])

        if full_text and emotion_type:
            extracted_val_data.append({'text': full_text, 'emotion': emotion_type})
            
    except KeyError:
        continue

# 3. μƒˆλ‘œμš΄ λ°μ΄ν„°ν”„λ ˆμž„ 생성
df_val = pd.DataFrame(extracted_val_data)

# 4. 검증 데이터 확인
print("\n--- μΆ”μΆœλœ 검증 λ°μ΄ν„°ν”„λ ˆμž„μ˜ 첫 5쀄 ---")
print(df_val.head())

print("\n--- 검증 λ°μ΄ν„°ν”„λ ˆμž„ 크기 ---")
print(f"검증 데이터: {df_val.shape}")

# main.py의 κΈ°μ‘΄ μ½”λ“œ 맨 μ•„λž˜μ— μ΄μ–΄μ„œ μž‘μ„±ν•©λ‹ˆλ‹€.
# -----------------------------------------------------------
# --- [Phase 1] 데이터 탐색 및 μ „μ²˜λ¦¬ ---
# -----------------------------------------------------------
import matplotlib.pyplot as plt
import seaborn as sns

# 1. 데이터 탐색 및 μ‹œκ°ν™”
print("\n--- [Phase 1-1] 데이터 탐색 및 μ‹œκ°ν™” μ‹œμž‘ ---")

# ν•œκΈ€ 폰트 μ„€μ • (Windows: Malgun Gothic, Mac: AppleGothic)
plt.rcParams['font.family'] = 'Malgun Gothic'
plt.rcParams['axes.unicode_minus'] = False # λ§ˆμ΄λ„ˆμŠ€ 기호 깨짐 λ°©μ§€

# ν›ˆλ ¨ λ°μ΄ν„°μ˜ 감정 뢄포 확인
print("\n--- ν›ˆλ ¨ 데이터 감정 뢄포 ---")
print(df_train['emotion'].value_counts())

# 감정 뢄포 μ‹œκ°ν™”
plt.figure(figsize=(10, 6))
sns.countplot(data=df_train, y='emotion', order=df_train['emotion'].value_counts().index)
plt.title('ν›ˆλ ¨ 데이터 감정 뢄포 μ‹œκ°ν™”', fontsize=15)
plt.xlabel('개수', fontsize=12)
plt.ylabel('감정', fontsize=12)
plt.grid(axis='x', linestyle='--', alpha=0.7)
plt.show() # κ·Έλž˜ν”„ μ°½ 보여주기

print("\nμ‹œκ°ν™” μ™„λ£Œ. κ·Έλž˜ν”„ 창을 λ‹«μœΌλ©΄ λ‹€μŒ 단계가 μ§„ν–‰λ©λ‹ˆλ‹€.")

# 2. ν…μŠ€νŠΈ μ •μ œ
print("\n--- [Phase 1-2] ν…μŠ€νŠΈ μ •μ œ μ‹œμž‘ ---")
# 이미 re λͺ¨λ“ˆμ€ μœ„μ—μ„œ import ν–ˆμŠ΅λ‹ˆλ‹€.

def clean_text(text):
    # μ •κ·œν‘œν˜„μ‹μ„ μ‚¬μš©ν•˜μ—¬ ν•œκΈ€, μ˜μ–΄, 숫자, 곡백을 μ œμ™Έν•œ λͺ¨λ“  문자 제거
    return re.sub(r'[^κ°€-힣a-zA-Z0-9 ]', '', text)

# ν›ˆλ ¨/검증 데이터에 μ •μ œ ν•¨μˆ˜ 적용
df_train['cleaned_text'] = df_train['text'].apply(clean_text)
df_val['cleaned_text'] = df_val['text'].apply(clean_text)

print("ν…μŠ€νŠΈ μ •μ œ μ™„λ£Œ.")
print(df_train[['text', 'cleaned_text']].head())