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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
ํ
์คํธ ์ฒ๋ฆฌ ์ ํธ๋ฆฌํฐ ํจ์
|
| 3 |
+
"""
|
| 4 |
+
|
| 5 |
+
import re
|
| 6 |
+
from typing import List, Optional
|
| 7 |
+
|
| 8 |
+
from app.core.logger import get_logger
|
| 9 |
+
|
| 10 |
+
logger = get_logger(__name__)
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
def clean_text(text: str) -> str:
|
| 14 |
+
"""
|
| 15 |
+
ํ
์คํธ ์ ๋ฆฌ (๊ณต๋ฐฑ ์ ๊ทํ ๋ฑ)
|
| 16 |
+
|
| 17 |
+
Args:
|
| 18 |
+
text: ์ ๋ฆฌํ ํ
์คํธ
|
| 19 |
+
|
| 20 |
+
Returns:
|
| 21 |
+
์ ๋ฆฌ๋ ํ
์คํธ
|
| 22 |
+
"""
|
| 23 |
+
if not text:
|
| 24 |
+
return ''
|
| 25 |
+
|
| 26 |
+
# ์ฐ์๋ ๊ณต๋ฐฑ ์ ๊ฑฐ
|
| 27 |
+
text = re.sub(r'\s+', ' ', text)
|
| 28 |
+
# ์๋ค ๊ณต๋ฐฑ ์ ๊ฑฐ
|
| 29 |
+
text = text.strip()
|
| 30 |
+
|
| 31 |
+
return text
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
def split_text_into_chunks(
|
| 35 |
+
text: str,
|
| 36 |
+
min_chunk_size: int = 200,
|
| 37 |
+
max_chunk_size: int = 1000,
|
| 38 |
+
overlap: int = 150
|
| 39 |
+
) -> List[str]:
|
| 40 |
+
"""
|
| 41 |
+
์๋ฏธ ๊ธฐ๋ฐ ํ
์คํธ ์ฒญํน (๋ฌธ์ฅ๊ณผ ๋ฌธ๋จ ๊ฒฝ๊ณ๋ฅผ ๊ณ ๋ คํ์ฌ ๋ถํ )
|
| 42 |
+
|
| 43 |
+
Args:
|
| 44 |
+
text: ๋ถํ ํ ํ
์คํธ
|
| 45 |
+
min_chunk_size: ์ต์ ์ฒญํฌ ํฌ๊ธฐ
|
| 46 |
+
max_chunk_size: ์ต๋ ์ฒญํฌ ํฌ๊ธฐ
|
| 47 |
+
overlap: ์ค๋ฒ๋ฉ ํฌ๊ธฐ
|
| 48 |
+
|
| 49 |
+
Returns:
|
| 50 |
+
๋ถํ ๋ ์ฒญํฌ ๋ฆฌ์คํธ
|
| 51 |
+
"""
|
| 52 |
+
if not text or len(text.strip()) == 0:
|
| 53 |
+
return []
|
| 54 |
+
|
| 55 |
+
# 1๋จ๊ณ: ๋ฌธ๋จ ๋จ์๋ก ๋ถํ (๋น ์ค ๊ธฐ์ค)
|
| 56 |
+
paragraphs = re.split(r'\n\s*\n', text.strip())
|
| 57 |
+
paragraphs = [p.strip() for p in paragraphs if p.strip()]
|
| 58 |
+
|
| 59 |
+
if not paragraphs:
|
| 60 |
+
return []
|
| 61 |
+
|
| 62 |
+
# 2๋จ๊ณ: ๊ฐ ๋ฌธ๋จ์ ๋ฌธ์ฅ ๋จ์๋ก ๋ถํ
|
| 63 |
+
sentence_pattern = r'([.!?]+)(?=\s+|$)'
|
| 64 |
+
|
| 65 |
+
all_sentences: List[str] = []
|
| 66 |
+
for para in paragraphs:
|
| 67 |
+
parts = re.split(sentence_pattern, para)
|
| 68 |
+
combined_sentences: List[str] = []
|
| 69 |
+
current_sentence = ""
|
| 70 |
+
|
| 71 |
+
for part in parts:
|
| 72 |
+
if not part.strip():
|
| 73 |
+
continue
|
| 74 |
+
if re.match(r'^[.!?]+$', part):
|
| 75 |
+
# ๊ตฌ๋์ ์ธ ๊ฒฝ์ฐ ํ์ฌ ๋ฌธ์ฅ์ ์ถ๊ฐํ๊ณ ๋ฌธ์ฅ ์์ฑ
|
| 76 |
+
current_sentence += part
|
| 77 |
+
if current_sentence.strip():
|
| 78 |
+
combined_sentences.append(current_sentence.strip())
|
| 79 |
+
current_sentence = ""
|
| 80 |
+
else:
|
| 81 |
+
# ํ
์คํธ์ธ ๊ฒฝ์ฐ ํ์ฌ ๋ฌธ์ฅ์ ์ถ๊ฐ
|
| 82 |
+
current_sentence += part
|
| 83 |
+
|
| 84 |
+
# ๋ง์ง๋ง ๋ฌธ์ฅ ์ฒ๋ฆฌ
|
| 85 |
+
if current_sentence.strip():
|
| 86 |
+
combined_sentences.append(current_sentence.strip())
|
| 87 |
+
|
| 88 |
+
# ๋ฌธ์ฅ์ด ํ๋๋ ์๋ ๊ฒฝ์ฐ
|
| 89 |
+
if not combined_sentences and para.strip():
|
| 90 |
+
combined_sentences.append(para.strip())
|
| 91 |
+
|
| 92 |
+
all_sentences.extend(combined_sentences)
|
| 93 |
+
|
| 94 |
+
if not all_sentences:
|
| 95 |
+
return [text] if text.strip() else []
|
| 96 |
+
|
| 97 |
+
# 3๋จ๊ณ: ๋ฌธ์ฅ๋ค์ ๋ชจ์์ ์๋ฏธ ์๋ ์ฒญํฌ ์์ฑ
|
| 98 |
+
chunks: List[str] = []
|
| 99 |
+
current_chunk: List[str] = []
|
| 100 |
+
current_size = 0
|
| 101 |
+
|
| 102 |
+
for sentence in all_sentences:
|
| 103 |
+
sentence_size = len(sentence)
|
| 104 |
+
|
| 105 |
+
# ํ์ฌ ์ฒญํฌ์ ๋ฌธ์ฅ ์ถ๊ฐ ์ ์ต๋ ํฌ๊ธฐ๋ฅผ ์ด๊ณผํ๋ ๊ฒฝ์ฐ
|
| 106 |
+
if current_size + sentence_size > max_chunk_size and current_chunk:
|
| 107 |
+
# ํ์ฌ ์ฒญํฌ ์ ์ฅ
|
| 108 |
+
chunk_text = '\n'.join(current_chunk)
|
| 109 |
+
if len(chunk_text.strip()) >= min_chunk_size:
|
| 110 |
+
chunks.append(chunk_text)
|
| 111 |
+
else:
|
| 112 |
+
# ์ต์ ํฌ๊ธฐ ๋ฏธ๋ง์ด๋ฉด ๋ค์ ์ฒญํฌ์ ๋ณํฉ
|
| 113 |
+
if chunks:
|
| 114 |
+
chunks[-1] = chunks[-1] + '\n' + chunk_text
|
| 115 |
+
else:
|
| 116 |
+
chunks.append(chunk_text)
|
| 117 |
+
|
| 118 |
+
# ์ค๋ฒ๋ฉ์ ์ํ ๋ฌธ์ฅ ์ ์ง
|
| 119 |
+
overlap_sentences: List[str] = []
|
| 120 |
+
overlap_size = 0
|
| 121 |
+
for s in reversed(current_chunk):
|
| 122 |
+
if overlap_size + len(s) <= overlap:
|
| 123 |
+
overlap_sentences.insert(0, s)
|
| 124 |
+
overlap_size += len(s) + 1
|
| 125 |
+
else:
|
| 126 |
+
break
|
| 127 |
+
|
| 128 |
+
current_chunk = overlap_sentences + [sentence]
|
| 129 |
+
current_size = overlap_size + sentence_size
|
| 130 |
+
else:
|
| 131 |
+
# ํ์ฌ ์ฒญํฌ์ ๋ฌธ์ฅ ์ถ๊ฐ
|
| 132 |
+
current_chunk.append(sentence)
|
| 133 |
+
current_size += sentence_size + 1
|
| 134 |
+
|
| 135 |
+
# ๋ง์ง๋ง ์ฒญํฌ ์ถ๊ฐ
|
| 136 |
+
if current_chunk:
|
| 137 |
+
chunk_text = '\n'.join(current_chunk)
|
| 138 |
+
if chunks and len(chunk_text.strip()) < min_chunk_size:
|
| 139 |
+
chunks[-1] = chunks[-1] + '\n' + chunk_text
|
| 140 |
+
else:
|
| 141 |
+
chunks.append(chunk_text)
|
| 142 |
+
|
| 143 |
+
# ๋น ์ฒญํฌ ์ ๊ฑฐ ๋ฐ ์ต์ ํฌ๊ธฐ ๋ฏธ๋ง ์ฒญํฌ ์ฒ๋ฆฌ
|
| 144 |
+
final_chunks: List[str] = []
|
| 145 |
+
for chunk in chunks:
|
| 146 |
+
chunk = chunk.strip()
|
| 147 |
+
if chunk and len(chunk) >= min_chunk_size:
|
| 148 |
+
final_chunks.append(chunk)
|
| 149 |
+
elif chunk:
|
| 150 |
+
if final_chunks:
|
| 151 |
+
final_chunks[-1] = final_chunks[-1] + '\n' + chunk
|
| 152 |
+
else:
|
| 153 |
+
final_chunks.append(chunk)
|
| 154 |
+
|
| 155 |
+
return final_chunks if final_chunks else [text] if text.strip() else []
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def extract_chapter_number(text: str) -> Optional[int]:
|
| 159 |
+
"""
|
| 160 |
+
ํ
์คํธ์์ ์ฑํฐ ๋ฒํธ ์ถ์ถ
|
| 161 |
+
|
| 162 |
+
Args:
|
| 163 |
+
text: ์ฑํฐ ๋ฒํธ๋ฅผ ์ถ์ถํ ํ
์คํธ
|
| 164 |
+
|
| 165 |
+
Returns:
|
| 166 |
+
์ฑํฐ ๋ฒํธ, ์์ผ๋ฉด None
|
| 167 |
+
"""
|
| 168 |
+
# ๋ค์ํ ์ฑํฐ ํจํด ๋งค์นญ
|
| 169 |
+
patterns = [
|
| 170 |
+
r'์ \s*(\d+)\s*์ฅ', # ์ 1์ฅ, ์ 1 ์ฅ
|
| 171 |
+
r'์ \s*(\d+)\s*ํ', # ์ 1ํ
|
| 172 |
+
r'Chapter\s*(\d+)', # Chapter 1
|
| 173 |
+
r'CHAPTER\s*(\d+)', # CHAPTER 1
|
| 174 |
+
r'Ch\.\s*(\d+)', # Ch. 1
|
| 175 |
+
r'(\d+)\s*์ฅ', # 1์ฅ
|
| 176 |
+
r'(\d+)\s*ํ', # 1ํ
|
| 177 |
+
r'chap\.\s*(\d+)', # chap. 1
|
| 178 |
+
r'ch\s*(\d+)', # ch 1
|
| 179 |
+
r'(\d+)\s*็ซ ', # 1็ซ
|
| 180 |
+
]
|
| 181 |
+
|
| 182 |
+
# ํ
์คํธ์ ์ฒ์ 500์๋ง ๊ฒ์ฌ
|
| 183 |
+
search_text = text[:500]
|
| 184 |
+
|
| 185 |
+
for pattern in patterns:
|
| 186 |
+
match = re.search(pattern, search_text, re.IGNORECASE)
|
| 187 |
+
if match:
|
| 188 |
+
try:
|
| 189 |
+
chapter_num = int(match.group(1))
|
| 190 |
+
return chapter_num
|
| 191 |
+
except (ValueError, AttributeError):
|
| 192 |
+
continue
|
| 193 |
+
|
| 194 |
+
return None
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
|