Spaces:
Sleeping
Sleeping
File size: 9,193 Bytes
82c1146 | 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | """
ํฌ๋งทํฐ ์ ํธ๋ฆฌํฐ ํจ์ ๋ชจ์.
ํฌ๋งทํ
๊ท์น ์ ์ฉ, ์ ํ์ง ์ ๊ทํ, ์๊ฐ ์๋ฃ ์ค๋ช
๋ณํฉ ๋ฑ
ํต์ฌ ํ์ฒ๋ฆฌ ๋ก์ง์ ํ ๊ณณ์ ๋ชจ์๋๋ค.
"""
from __future__ import annotations
import re
import unicodedata
from dataclasses import dataclass
from typing import Dict, Iterable, List, Optional, Tuple
from .mock_models import MockElement
from .formatter_rules import RuleConfig
AI_PRIORITY_CLASSES = {"figure", "table", "flowchart"}
# ---------------------------------------------------------------------------
# ๋ฐ์ดํฐ ๋ณํ ํฌํผ
# ---------------------------------------------------------------------------
def ocr_inputs_to_dict(ocr_texts) -> Dict[int, str]:
"""
OCR ์
๋ ฅ์ element_id โ text ๋์
๋๋ฆฌ๋ก ๋ณํ.
"""
if isinstance(ocr_texts, dict):
return {int(k): (v or "").strip() for k, v in ocr_texts.items()}
ocr_dict: Dict[int, str] = {}
for item in ocr_texts or []:
try:
element_id = int(getattr(item, "element_id"))
text = getattr(item, "ocr_text", "") or ""
except AttributeError:
continue
cleaned = text.strip()
if cleaned:
ocr_dict[element_id] = cleaned
return ocr_dict
def normalize_ai_descriptions(
ai_descriptions: Optional[Dict[int, str]],
) -> Dict[int, str]:
"""
AI ์ค๋ช
๋์
๋๋ฆฌ๋ฅผ ์ ๋ฆฌํฉ๋๋ค.
"""
if not ai_descriptions:
return {}
return {
int(k): (v or "").strip()
for k, v in ai_descriptions.items()
if (v or "").strip()
}
def split_first_line(text: str) -> Tuple[str, str]:
"""
๋ฌธ์์ด์ ์ฒซ ์ค๊ณผ ๋๋จธ์ง๋ก ๋ถ๋ฆฌํ๋ค.
"""
if not text:
return "", ""
lines = text.splitlines()
first = lines[0]
remainder = "\n".join(lines[1:]).strip()
return first, remainder
# ---------------------------------------------------------------------------
# ์ฝํ
์ธ ํ์ฒ๋ฆฌ
# ---------------------------------------------------------------------------
CHOICE_PATTERN = re.compile(
r"^(\(?\d{1,2}[\).]|[โ -โณ]|[A-Z][\).]|[๊ฐ-ํ]\.|[๊ฐ-ํ]\))\s*(.+)$"
)
def normalize_choices(text: str) -> str:
"""
์ ํ์ง ํ
์คํธ๋ฅผ ํ์คํํ๋ค.
- ํจํด์ด ๋ช
ํํ๋ฉด ๊ทธ๋๋ก ์ฌ์ฉ.
- ๊ทธ๋ ์ง ์์ผ๋ฉด 'โข ' ๋ถ๋ฆฟ์ ๋ถ์ธ๋ค.
"""
lines = [line.strip() for line in text.splitlines() if line.strip()]
normalized: List[str] = []
for line in lines:
match = CHOICE_PATTERN.match(line)
if match:
label, body = match.groups()
normalized.append(f"{label} {body.strip()}")
else:
normalized.append(f"โข {line}")
return "\n".join(normalized)
LIST_PATTERN = re.compile(r"^([\-โข]|\d+\.)\s*(.+)$")
def normalize_list(text: str) -> str:
"""
์ผ๋ฐ ๋ฆฌ์คํธ ํ
์คํธ๋ฅผ ์ ๊ทํ.
"""
lines = [line.strip() for line in text.splitlines() if line.strip()]
normalized: List[str] = []
for line in lines:
match = LIST_PATTERN.match(line)
if match:
normalized.append(f"- {match.group(2).strip()}")
else:
normalized.append(f"- {line}")
return "\n".join(normalized)
def normalize_reading_list(text: str) -> str:
"""
์ผ๋ฐ ๋ฌธ์์ฉ ๋ฆฌ์คํธ ์ ๊ทํ (๋ถ๋ฆฟ ๊ธฐํธ ์ ์ง).
"""
lines = [line.strip() for line in text.splitlines() if line.strip()]
normalized: List[str] = []
for line in lines:
match = LIST_PATTERN.match(line)
if match:
normalized.append(f"โข {match.group(2).strip()}")
else:
normalized.append(f"โข {line}")
return "\n".join(normalized)
def merge_visual_description(text: str, ai_text: Optional[str]) -> str:
"""
๊ทธ๋ฆผ/ํ/์์๋ ์ค๋ช
์ ๊ฒฐํฉํ๋ค.
AI ์ค๋ช
์ด ์์ผ๋ฉด ์ฐ์ ์ฌ์ฉํ๊ณ , OCR ํ
์คํธ๊ฐ ์์ผ๋ฉด ๋ค์ ์ค์ ์ถ๊ฐํ๋ค.
"""
if ai_text and text:
return f"{ai_text}\n{text}"
return ai_text or text
def isolate_formula(text: str) -> str:
"""
์์์ ์ฃผ์ด์ง ํ
์คํธ๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๋ ์๋ค ๊ณต๋ฐฑ์ ์ ๋ํ๋ค.
"""
return text.strip()
def uppercase_title(text: str) -> str:
return text.strip()
def normalize_question_type(text: str) -> str:
"""
question type OCR ๊ฒฐ๊ณผ์ ์ค ์ ๋ ฌ/๋
ธ์ด์ฆ ์ ๊ฑฐ.
- ์ค๋ฐ๊ฟ์ ๊ณต๋ฐฑ์ผ๋ก ์นํํ์ฌ ํ ์ค๋ก ์ ๋ฆฌ
(๊ทธ ์ธ ๋ฌธ์/๊ณต๋ฐฑ์ ์๋ณธ์ ์ต๋ํ ์ ์ง)
"""
normalized = unicodedata.normalize("NFKC", text or "")
normalized = normalized.replace("\r\n", "\n").replace("\r", "\n")
return normalized.replace("\n", " ")
TRANSFORM_DISPATCH = {
"normalize_choices": normalize_choices,
"normalize_list": normalize_list,
"normalize_reading_list": normalize_reading_list,
"merge_visual_description": merge_visual_description,
"isolate_formula": isolate_formula,
"uppercase_title": uppercase_title,
"normalize_question_type": normalize_question_type,
}
# ---------------------------------------------------------------------------
# ๊ท์น ์ ์ฉ ๋ฐ ์ถ๋ ฅ ์ ๋ฆฌ
# ---------------------------------------------------------------------------
def apply_rule(rule: RuleConfig, content: str) -> str:
"""
๊ท์น์ ๋ฐ๋ผ ์ฝํ
์ธ ์ ์ ๋์ฌ, ๋ค์ฌ์ฐ๊ธฐ, ์ ๋ฏธ์ฌ๋ฅผ ์ ์ฉํ๋ค.
"""
if not content and not rule.allow_empty:
return ""
working = content
if rule.indent > 0:
indent_str = " " * rule.indent
indented_lines: List[str] = []
for line in working.splitlines():
if not line.strip():
indented_lines.append("")
else:
indented_lines.append(f"{indent_str}{line}")
working = "\n".join(indented_lines)
if not working and not rule.keep_suffix_on_empty:
return rule.prefix if rule.prefix else ""
return f"{rule.prefix}{working}{rule.suffix}"
def clean_output(text: str) -> str:
"""
์ต์ข
์ถ๋ ฅ ๋ฌธ์์ด์์ ์ฐ์ ๋น ์ค ๋ฐ ํํ ๊ณต๋ฐฑ์ ์ ๋ฆฌํ๋ค.
"""
lines = text.splitlines()
cleaned: List[str] = []
empty_streak = 0
for line in lines:
stripped = line.rstrip()
if stripped == "":
empty_streak += 1
if empty_streak > 2:
continue
else:
empty_streak = 0
cleaned.append(stripped)
result = "\n".join(cleaned).strip()
return result
# ---------------------------------------------------------------------------
# ๋ ๋๋ง ์ปจํ
์คํธ
# ---------------------------------------------------------------------------
@dataclass
class RenderContext:
"""
๋ ๋๋ง ์ ํ์ํ ์ปจํ
์คํธ.
"""
ocr_texts: Dict[int, str]
ai_texts: Dict[int, str]
rules: Dict[str, RuleConfig]
def get_texts(self, element: MockElement) -> Tuple[str, str]:
element_id = getattr(element, "element_id", None)
base_text = self.ocr_texts.get(element_id, "").strip()
ai_text = self.ai_texts.get(element_id, "").strip()
return base_text, ai_text
def apply_transform(
self,
element: MockElement,
text: str,
*,
base_text: str,
ai_text: str,
) -> str:
rule = self.rules.get(element.class_name)
if not rule or not rule.transform:
return text.strip()
transform = TRANSFORM_DISPATCH.get(rule.transform)
if not transform:
return text.strip()
if rule.transform == "merge_visual_description":
return transform(base_text.strip(), ai_text.strip())
return transform(text.strip())
def format_element(
self, element: MockElement, content_override: Optional[str] = None
) -> str:
"""
๊ฐ๋ณ ์์๋ฅผ ๊ท์น์ ๋ฐ๋ผ ๋ฌธ์์ด๋ก ๋ณํํ๋ค.
"""
element_id = getattr(element, "element_id", None)
base_text = (
content_override
if content_override is not None
else self.ocr_texts.get(element_id, "")
).strip()
ai_text = self.ai_texts.get(element_id, "").strip()
# ๊ทธ๋ฆผ/ํ/์์๋๋ transform ํจ์์์ ๋ณํฉ ์ฒ๋ฆฌ
if element.class_name in AI_PRIORITY_CLASSES:
# merge_visual_description transform์ ๋งก๊น
working = base_text
else:
working = base_text or ai_text
# Transform ์ ์ฉ (merge_visual_description์ด ai_text์ ๋ณํฉ)
working = self.apply_transform(
element,
working,
base_text=base_text,
ai_text=ai_text,
)
# Transform ํ์๋ ๋น์ด์๊ณ AI ์ค๋ช
์ด ์์ผ๋ฉด AI ์ค๋ช
์ฌ์ฉ
if not working and ai_text and element.class_name in AI_PRIORITY_CLASSES:
working = ai_text
# ๊ท์น ์ ์ฉ (prefix, suffix, indent)
rule = self.rules.get(element.class_name)
if rule:
return apply_rule(rule, working)
# ๊ท์น์ด ์์ผ๋ฉด ๊ธฐ๋ณธ์ ์ผ๋ก ํ ์ค ์ถ๋ ฅ
return f"{working}\n" if working else ""
|