Spaces:
Sleeping
Sleeping
Download src/template_parser.py from vyarth/parsing-test: direct link, hf CLI and curl.
- Browser
- Download file 76 kB
-
https://huggingface.co/spaces/vyarth/parsing-test/resolve/main/src/template_parser.py
- Command line
-
hf download hf://spaces/vyarth/parsing-test/src/template_parser.py
-
curl -L -o template_parser.py https://huggingface.co/spaces/vyarth/parsing-test/resolve/main/src/template_parser.py
76 kB
"""
ν
νλ¦Ώ κΈ°λ° μνκΈ°λ‘λΆ νμ
ν΅μ¬ λ‘μ§:
1. PDFμμ μ 체 ν
μ€νΈ μΆμΆ (νμ΄μ§λ³)
2. λ¬Έμ λ²μ κ°μ§ (ꡬλ²μ /μ λ²μ /λμ
μ ν)
3. κ³ μ νμ΄ν λ§€μΉμΌλ‘ μΉμ
κ²½κ³ μ°ΎκΈ°
4. μΉμ
λ³ νμλ‘ λ΄μ© μΆμΆ
"""
import re
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Optional
import pdfplumber
from .section_template import (
SECTIONS,
SECTION_BY_ID,
CREATIVE_ACTIVITY_AREAS,
ACADEMIC_CATEGORIES,
ACADEMIC_CATEGORY_BY_ID,
DocVersion,
normalize_text,
detect_doc_version,
)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# κ³Όλͺ©λͺ
λͺ©λ‘ (μΈνΉ νμ±μ©)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
SUBJECT_NAMES = [
# κ΅μ΄
"κ΅μ΄", "λ¬Έν", "λ
μ", "νλ²κ³Ό μλ¬Έ", "μΈμ΄μ 맀체", "μ¬ν κ΅μ΄",
# μν
"μν", "μνβ
", "μνβ
‘", "λ―Έμ λΆ", "νλ₯ κ³Ό ν΅κ³", "κΈ°ν",
"μ¬ν μνβ
", "μ¬ν μνβ
‘", "μνμ μ¬κ³ μ μ λΆ", "κ³ κΈ μνβ
", "κ³ κΈ μνβ
‘",
# μμ΄
"μμ΄", "μμ΄β
", "μμ΄β
‘", "μμ΄ λ
ν΄μ μλ¬Έ", "μμ΄ νν", "μ€μ© μμ΄",
# νκ΅μ¬/μ¬ν
"νκ΅μ¬", "ν΅ν©μ¬ν", "νκ΅μ§λ¦¬", "μΈκ³μ§λ¦¬", "λμμμμ¬", "μΈκ³μ¬",
"κ²½μ ", "μ μΉμ λ²", "μ¬νΒ·λ¬Έν", "μ€λ¦¬μ μ¬μ", "μνκ³Ό μ€λ¦¬", "μ¬νμ§λ¦¬",
# κ³Όν
"ν΅ν©κ³Όν", "κ³Όννꡬμ€ν", "물리νβ
", "물리νβ
‘", "ννβ
", "ννβ
‘",
"μλͺ
κ³Όνβ
", "μλͺ
κ³Όνβ
‘", "μ§κ΅¬κ³Όνβ
", "μ§κ΅¬κ³Όνβ
‘",
"κ³ κΈ λ¬Όλ¦¬ν", "κ³ κΈ νν", "κ³ κΈ μλͺ
κ³Όν", "κ³ κΈ μ§κ΅¬κ³Όν",
"물리ν μ€ν", "νν μ€ν", "μλͺ
κ³Όν μ€ν", "μ§κ΅¬κ³Όν μ€ν",
# 체μ‘/μμ
"체μ‘", "μ΄λκ³Ό 건κ°", "μ€ν¬μΈ μν", "μ€ν¬μΈ λ¬Έν", "μ€ν¬μΈ κ³Όν",
"μμ
", "λ―Έμ ", "μ°κ·Ή", "μμ
μ°μ£Ό", "μμ
κ°μκ³Ό λΉν", "λ―Έμ μ°½μ", "λ―Έμ κ°μκ³Ό λΉν",
# κΈ°μ /κ°μ /μ 2μΈκ΅μ΄/κ΅μ
"μ 보", "κΈ°μ Β·κ°μ ", "μΌλ³Έμ΄β
", "μΌλ³Έμ΄β
‘", "μ€κ΅μ΄β
", "μ€κ΅μ΄β
‘",
"νλ¬Έβ
", "νλ¬Έβ
‘", "μ¬λ¦¬ν", "보건", "κ΅μ‘ν", "μ² ν", "λ
Όλ¦¬ν",
]
# κ³Όλͺ©λͺ
β κ΅κ³Όμμ λ§€ν
SUBJECT_TO_AREA = {
# κ΅μ΄
"κ΅μ΄": "κ΅μ΄", "λ¬Έν": "κ΅μ΄", "λ
μ": "κ΅μ΄", "νλ²κ³Ό μλ¬Έ": "κ΅μ΄",
"μΈμ΄μ 맀체": "κ΅μ΄", "μ¬ν κ΅μ΄": "κ΅μ΄",
# μν
"μν": "μν", "μνβ
": "μν", "μνβ
‘": "μν", "λ―Έμ λΆ": "μν",
"νλ₯ κ³Ό ν΅κ³": "μν", "κΈ°ν": "μν", "μ¬ν μνβ
": "μν", "μ¬ν μνβ
‘": "μν",
# μμ΄
"μμ΄": "μμ΄", "μμ΄β
": "μμ΄", "μμ΄β
‘": "μμ΄", "μμ΄ λ
ν΄μ μλ¬Έ": "μμ΄",
"μμ΄ νν": "μμ΄", "μ€μ© μμ΄": "μμ΄",
# νκ΅μ¬
"νκ΅μ¬": "νκ΅μ¬",
# μ¬ν
"ν΅ν©μ¬ν": "μ¬ν(μμ¬/λλν¬ν¨)", "νκ΅μ§λ¦¬": "μ¬ν(μμ¬/λλν¬ν¨)",
"μΈκ³μ§λ¦¬": "μ¬ν(μμ¬/λλν¬ν¨)", "λμμμμ¬": "μ¬ν(μμ¬/λλν¬ν¨)",
"μΈκ³μ¬": "μ¬ν(μμ¬/λλν¬ν¨)", "κ²½μ ": "μ¬ν(μμ¬/λλν¬ν¨)",
"μ μΉμ λ²": "μ¬ν(μμ¬/λλν¬ν¨)", "μ¬νΒ·λ¬Έν": "μ¬ν(μμ¬/λλν¬ν¨)",
"μ€λ¦¬μ μ¬μ": "μ¬ν(μμ¬/λλν¬ν¨)", "μνκ³Ό μ€λ¦¬": "μ¬ν(μμ¬/λλν¬ν¨)",
"μ¬νμ§λ¦¬": "μ¬ν(μμ¬/λλν¬ν¨)",
# κ³Όν
"ν΅ν©κ³Όν": "κ³Όν", "κ³Όννꡬμ€ν": "κ³Όν",
"물리νβ
": "κ³Όν", "물리νβ
‘": "κ³Όν",
"ννβ
": "κ³Όν", "ννβ
‘": "κ³Όν",
"μλͺ
κ³Όνβ
": "κ³Όν", "μλͺ
κ³Όνβ
‘": "κ³Όν",
"μ§κ΅¬κ³Όνβ
": "κ³Όν", "μ§κ΅¬κ³Όνβ
‘": "κ³Όν",
"κ³ κΈ λ¬Όλ¦¬ν": "κ³Όν", "κ³ κΈ νν": "κ³Όν",
"κ³ κΈ μλͺ
κ³Όν": "κ³Όν", "κ³ κΈ μ§κ΅¬κ³Όν": "κ³Όν",
# 체μ‘
"체μ‘": "체μ‘", "μ΄λκ³Ό 건κ°": "체μ‘", "μ€ν¬μΈ μν": "체μ‘",
"μ€ν¬μΈ λ¬Έν": "체μ‘", "μ€ν¬μΈ κ³Όν": "체μ‘",
# μμ
"μμ
": "μμ ", "λ―Έμ ": "μμ ", "μ°κ·Ή": "μμ ",
"μμ
μ°μ£Ό": "μμ ", "μμ
κ°μκ³Ό λΉν": "μμ ",
"λ―Έμ μ°½μ": "μμ ", "λ―Έμ κ°μκ³Ό λΉν": "μμ ", "맀체 λ―Έμ ": "μμ ",
# κΈ°μ Β·κ°μ /κ΅μ
"μ 보": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ", "κΈ°μ Β·κ°μ ": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ",
"μΌλ³Έμ΄β
": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ", "μ€κ΅μ΄β
": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ",
"νλ¬Έβ
": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ", "μ¬λ¦¬ν": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ",
}
@dataclass
class SectionBoundary:
"""μΉμ
κ²½κ³ μ 보 (Yμ’ν κΈ°λ°)"""
section_id: str
start_page: int # 0-indexed
start_y: float # μΉμ
μμ Yμ’ν (νμ΄μ§ λ΄)
end_page: int
end_y: float # μΉμ
λ Yμ’ν (νμ΄μ§ λ΄)
title_found: str # μ€μ μ°Ύμ νμ΄ν
@dataclass
class TableInfo:
"""ν
μ΄λΈ μ 보 (bbox ν¬ν¨)"""
page_num: int # 0-indexed
bbox: tuple # (x0, top, x1, bottom)
data: list # μΆμΆλ ν
μ΄λΈ λ°μ΄ν°
@property
def top_y(self) -> float:
"""ν
μ΄λΈ μλ¨ Yμ’ν"""
return self.bbox[1]
@property
def bottom_y(self) -> float:
"""ν
μ΄λΈ νλ¨ Yμ’ν"""
return self.bbox[3]
@dataclass
class PageText:
"""νμ΄μ§λ³ ν
μ€νΈ"""
page_num: int # 0-indexed
text: str
lines: list[str] = field(default_factory=list)
tables: list[TableInfo] = field(default_factory=list) # TableInfo 리μ€νΈ
page_height: float = 0.0 # νμ΄μ§ λμ΄ (Yμ’ν κ³μ°μ©)
class TemplateParser:
"""ν
νλ¦Ώ κΈ°λ° μνκΈ°λ‘λΆ νμ"""
def __init__(self, pdf_path: str):
self.pdf_path = Path(pdf_path)
if not self.pdf_path.exists():
raise FileNotFoundError(f"PDF νμΌμ μ°Ύμ μ μμ΅λλ€: {pdf_path}")
self.pages: list[PageText] = []
self.full_text: str = ""
self.doc_version: DocVersion = DocVersion.V1_8_SECTIONS
self.section_boundaries: dict[str, SectionBoundary] = {}
def parse(self) -> dict[str, Any]:
"""μ 체 νμ± μν"""
# 1. PDF λ‘λ λ° ν
μ€νΈ μΆμΆ
self._load_pdf()
# 2. λ¬Έμ λ²μ κ°μ§
self.doc_version = detect_doc_version(self.full_text)
# 3. μΉμ
κ²½κ³ μ°ΎκΈ°
self._find_section_boundaries()
# 4. κ° μΉμ
νμ±
result = {
"metadata": self._extract_metadata(),
"doc_version": self.doc_version.value,
"sections_found": {sid: bool(b) for sid, b in self.section_boundaries.items()},
}
# μΉμ
λ³ νμ±
result["student"] = self._parse_student_info()
result["class_history"] = self._parse_class_history()
result["academic_history"] = self._parse_academic_history()
result["attendance"] = self._parse_attendance()
result["awards"] = self._parse_awards()
result["creative_activities"] = self._parse_creative_activities()
result["academics"] = self._parse_academics()
result["reading_activities"] = self._parse_reading()
result["behavior_and_opinion"] = self._parse_behavior()
return result
def _load_pdf(self):
"""PDF λ‘λ λ° νμ΄μ§λ³ ν
μ€νΈ/ν
μ΄λΈ μΆμΆ (bbox ν¬ν¨)"""
with pdfplumber.open(self.pdf_path) as pdf:
self.pdf_metadata = pdf.metadata
for i, page in enumerate(pdf.pages):
text = page.extract_text() or ""
# ν
μ΄λΈμ TableInfoλ‘ λ³ν (bbox ν¬ν¨)
table_infos = []
for table in page.find_tables():
table_infos.append(TableInfo(
page_num=i,
bbox=table.bbox, # (x0, top, x1, bottom)
data=table.extract(),
))
page_text = PageText(
page_num=i,
text=text,
lines=text.split('\n') if text else [],
tables=table_infos,
page_height=page.height,
)
self.pages.append(page_text)
self.full_text += text + "\n"
# μΉμ
νμ΄ν Yμ’ν μΆμΆμ μν΄ words μ 보 μ μ₯
self._extract_title_positions(pdf)
def _extract_title_positions(self, pdf):
"""μΉμ
νμ΄νμ Yμ’ν μΆμΆ (pdfplumber words μ¬μ©)"""
self.title_positions = {} # {section_id: (page_num, y_top)}
for page_num, page in enumerate(pdf.pages):
words = page.extract_words()
# μΉμ
λ²νΈ ν¨ν΄ ("1.", "2.", ...) μ°ΎκΈ°
for i, word in enumerate(words):
text = word['text']
# "λ²νΈ." ν¨ν΄ νμΈ (μ: "1.", "7.")
num_match = re.match(r'^(\d+)\.$', text)
if not num_match:
continue
section_num = int(num_match.group(1))
y_top = word['top']
# λ€μ λ¨μ΄λ€μ ν©μ³μ νμ΄ν νμΈ
title_words = []
for j in range(i + 1, min(i + 5, len(words))): # μ΅λ 4λ¨μ΄ νμΈ
next_word = words[j]
# Yμ’νκ° λΉμ·νλ©΄ κ°μ μ€
if abs(next_word['top'] - y_top) < 5:
title_words.append(next_word['text'])
else:
break
title_text = ''.join(title_words)
norm_title = normalize_text(title_text)
# μΉμ
λ§€μΉ
for section in SECTIONS:
if section.id in self.title_positions:
continue # μ΄λ―Έ μ°Ύμ μΉμ
for title in section.titles:
if normalize_text(title) in norm_title or norm_title.startswith(normalize_text(title)[:4]):
self.title_positions[section.id] = (page_num, y_top)
break
else:
continue
break
def _find_section_boundaries(self):
"""Yμ’ν κΈ°λ° μΉμ
κ²½κ³ μ€μ
title_positionsμμ μΆμΆν Yμ’νλ₯Ό μ¬μ©νμ¬
κ° μΉμ
μ μμ/λ κ²½κ³λ₯Ό μ νν μ€μ
"""
if not hasattr(self, 'title_positions') or not self.title_positions:
return
# νμ΄μ§/Yμ’ν μμΌλ‘ μ λ ¬
sorted_sections = sorted(
self.title_positions.items(),
key=lambda x: (x[1][0], x[1][1]) # (page_num, y_top)
)
# κ²½κ³ μ€μ
for i, (section_id, (page_num, y_top)) in enumerate(sorted_sections):
# λ€μ μΉμ
μμΉ (λ κ²½κ³)
if i + 1 < len(sorted_sections):
next_section_id, (next_page, next_y) = sorted_sections[i + 1]
end_page = next_page
end_y = next_y
else:
# λ§μ§λ§ μΉμ
- λ¬Έμ λκΉμ§
end_page = len(self.pages) - 1
end_y = self.pages[-1].page_height if self.pages else 1000.0
self.section_boundaries[section_id] = SectionBoundary(
section_id=section_id,
start_page=page_num,
start_y=y_top,
end_page=end_page,
end_y=end_y,
title_found=f"{section_id}",
)
def _get_section_text(self, section_id: str) -> str:
"""μΉμ
IDλ‘ ν΄λΉ μΉμ
ν
μ€νΈ μΆμΆ
μ°Έκ³ : Yμ’ν κΈ°λ° μ λ° νν°λ§μ ν
μ΄λΈμλ§ μ μ©.
ν
μ€νΈλ νμ΄μ§ λ²μ λ΄ μ 체 ν
μ€νΈ λ°ν (보쑰 μ©λ)
"""
boundary = self.section_boundaries.get(section_id)
if not boundary:
return ""
lines = []
for page_num in range(boundary.start_page, boundary.end_page + 1):
if page_num >= len(self.pages):
break
page = self.pages[page_num]
lines.extend(page.lines)
return '\n'.join(lines)
def _get_section_tables(self, section_id: str) -> list:
"""μΉμ
IDλ‘ ν΄λΉ μΉμ
ν
μ΄λΈ μΆμΆ (Yμ’ν κΈ°λ° νν°λ§)
ν
μ΄λΈμ top_yκ° μΉμ
μ Y λ²μ λ΄μ μλ κ²½μ°λ§ λ°ν
- 첫 λ²μ§Έ μΉμ
: νμ΄μ§ μμ(Y=0)λΆν° ν¬ν¨ (λ¬Έμ ν€λ ν¬ν¨)
- νμ΄ν λ§μ§: νμ΄νμ΄ ν
μ΄λΈ λ΄λΆμ μλ κ²½μ° μ²λ¦¬
"""
boundary = self.section_boundaries.get(section_id)
if not boundary:
return []
# νμ΄νμ΄ ν
μ΄λΈ λ΄λΆμ μμ μ μμΌλ―λ‘ λ§μ§ μ μ©
MARGIN = 20.0
# 첫 λ²μ§Έ μΉμ
μΈμ§ νμΈ (μΈμ νμ )
is_first_section = section_id == "μΈμ νμ "
tables = []
for page_num in range(boundary.start_page, boundary.end_page + 1):
if page_num >= len(self.pages):
break
for table_info in self.pages[page_num].tables:
table_y = table_info.top_y
# μμ νμ΄μ§ νν°λ§
if page_num == boundary.start_page:
# 첫 λ²μ§Έ μΉμ
: νμ΄μ§ μμλΆν° ν¬ν¨
if is_first_section:
pass # μμ Y μ ν μμ
elif table_y < (boundary.start_y - MARGIN):
continue
# λ νμ΄μ§ νν°λ§: end_y - MARGIN λ―Έλ§λ§
if page_num == boundary.end_page and table_y >= (boundary.end_y - MARGIN):
continue
tables.append(table_info.data)
return tables
def _get_section_tables_with_page(self, section_id: str) -> list[tuple[int, list]]:
"""μΉμ
ν
μ΄λΈμ (page_num, table_data) νν 리μ€νΈλ‘ λ°ν
κ΅κ³Όνμ΅λ°λ¬μν© νμ± μ νμ΄μ§λ³ νλ
λ§€νμ μ¬μ©
"""
boundary = self.section_boundaries.get(section_id)
if not boundary:
return []
MARGIN = 20.0
is_first_section = section_id == "μΈμ νμ "
tables = []
for page_num in range(boundary.start_page, boundary.end_page + 1):
if page_num >= len(self.pages):
break
for table_info in self.pages[page_num].tables:
table_y = table_info.top_y
if page_num == boundary.start_page:
if is_first_section:
pass
elif table_y < (boundary.start_y - MARGIN):
continue
if page_num == boundary.end_page and table_y >= (boundary.end_y - MARGIN):
continue
tables.append((page_num, table_info.data))
return tables
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# λ©νλ°μ΄ν° μΆμΆ
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _extract_metadata(self) -> dict:
"""λ¬Έμ λ©νλ°μ΄ν° μΆμΆ"""
metadata = {
"document_number": None,
"school_name": None,
"issue_date": None,
"total_pages": len(self.pages),
"pdf_metadata": self.pdf_metadata if hasattr(self, 'pdf_metadata') else {},
}
if self.pages:
first_text = self.pages[0].text
# λ¬ΈμνμΈλ²νΈ
doc_match = re.search(r"λ¬ΈμνμΈλ²νΈ\s*[:οΌ]?\s*([A-Za-z0-9\-]+)", first_text)
if doc_match:
metadata["document_number"] = doc_match.group(1)
# νκ΅λͺ
school_match = re.search(r"([κ°-ν£]+κ³ λ±νκ΅)", first_text)
if school_match:
metadata["school_name"] = school_match.group(1)
# λ°κΈμΌ
date_match = re.search(r"(\d{4}λ
\s*\d{1,2}μ\s*\d{1,2}μΌ)", first_text)
if date_match:
metadata["issue_date"] = date_match.group(1)
return metadata
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# μΈμ Β·νμ μ¬ν νμ±
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _parse_student_info(self) -> dict:
"""νμ κΈ°λ³Έ μ 보 νμ±"""
text = self._get_section_text("μΈμ νμ ")
student = {
"name": None,
"gender": None,
"resident_number": None,
"address": None,
}
# μ±λͺ
- μ½λ‘ μ΄ μλ ν¨ν΄λ§ λ§€μΉ (λ΄μμ±λͺ
vs μ±λͺ
: ꡬλΆ)
name_match = re.search(r"μ±λͺ
\s*[:οΌ]\s*([κ°-ν£]+)", text)
if name_match:
student["name"] = name_match.group(1)
# μ±λ³
gender_match = re.search(r"μ±λ³\s*[:οΌ]?\s*([λ¨μ¬])", text)
if gender_match:
student["gender"] = gender_match.group(1)
# μ£Όλ―Όλ±λ‘λ²νΈ
rrn_match = re.search(r"μ£Όλ―Όλ±λ‘λ²νΈ\s*[:οΌ]?\s*(\d{6}[-\s]*[\d*]+)", text)
if rrn_match:
student["resident_number"] = rrn_match.group(1)
# μ£Όμ
addr_match = re.search(r"μ£Όμ\s*[:οΌ]?\s*(.+?)(?=\n\d{4}λ
|\nνμ |$)", text, re.DOTALL)
if addr_match:
student["address"] = addr_match.group(1).strip().replace('\n', ' ')
return student
def _parse_class_history(self) -> list[dict]:
"""νλ° μ 보 νμ± (Yμ’ν κΈ°λ° νν°λ§μΌλ‘ κ°μν)"""
tables = self._get_section_tables("μΈμ νμ ")
class_history = []
for table in tables:
if not table:
continue
# νλ
, λ°, λ²νΈ μ»¬λΌ μ°ΎκΈ°
header_row = None
for row in table:
if row:
row_text = ''.join(str(c) for c in row if c)
# νλ° ν
μ΄λΈ: "νλ
" AND ("λ°" OR "λ²νΈ" λλ "λ΄μ") νμ
has_grade = "νλ
" in row_text
has_class_info = "λ°" in row_text or "λ²νΈ" in row_text or "λ΄μ" in row_text
if has_grade and has_class_info:
header_row = row
break
if not header_row:
continue
# ν€λ μΈλ±μ€ μ°ΎκΈ°
indices = {}
for i, cell in enumerate(header_row):
if not cell:
continue
cell_str = str(cell)
if "νλ
" in cell_str:
indices["grade"] = i
elif "λ°" in cell_str and "λ²" not in cell_str:
indices["class"] = i
elif "λ²νΈ" in cell_str or ("λ²" in cell_str and "λ°" not in cell_str):
indices["number"] = i
elif "λ΄μ" in cell_str:
indices["teacher"] = i
elif "νκ³Ό" in cell_str:
indices["department"] = i
# λ°μ΄ν° ν νμ±
for row in table:
if row == header_row or not row:
continue
try:
grade_val = row[indices.get("grade", 0)]
if not grade_val or not str(grade_val).strip().isdigit():
continue
entry = {
"grade": int(grade_val),
"department": None,
"class_number": None,
"student_number": None,
"homeroom_teacher": None,
}
if "department" in indices and indices["department"] < len(row):
entry["department"] = row[indices["department"]] or None
if "class" in indices and indices["class"] < len(row):
val = row[indices["class"]]
if val and str(val).strip().isdigit():
entry["class_number"] = int(val)
if "number" in indices and indices["number"] < len(row):
val = row[indices["number"]]
if val and str(val).strip().isdigit():
entry["student_number"] = int(val)
if "teacher" in indices and indices["teacher"] < len(row):
entry["homeroom_teacher"] = row[indices["teacher"]] or None
class_history.append(entry)
except (IndexError, ValueError, TypeError):
continue
return class_history
def _parse_academic_history(self) -> dict:
"""νμ μ¬ν νμ±"""
text = self._get_section_text("μΈμ νμ ")
entries = []
# YYYYλ
MMμ DDμΌ ν¨ν΄
matches = re.findall(
r"(\d{4}λ
\s*\d{1,2}μ\s*\d{1,2}μΌ)\s*([κ°-ν£]+(?:νκ΅|κ³ λ±νκ΅))\s*(.+?)(?=\d{4}λ
|$)",
text, re.DOTALL
)
for date, school, content in matches:
entries.append(f"{date} {school} {content.strip()}")
return {
"entries": entries,
"special_notes": None,
}
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# μΆκ²°μν© νμ±
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _parse_attendance(self) -> list[dict]:
"""μΆκ²°μν© νμ± (Yμ’ν κΈ°λ° νν°λ§μΌλ‘ κ°μν)"""
tables = self._get_section_tables("μΆκ²°")
attendance = []
for table in tables:
if not table or len(table) < 2:
continue
# ν€λ ν μ°ΎκΈ° (κ²°μ, μ§κ° λ± ν¬ν¨)
header_idx = 0
for i, row in enumerate(table):
if row and any("κ²°μ" in str(c) or "μ§κ°" in str(c) for c in row if c):
header_idx = i
break
# λ°μ΄ν° ν νμ± (νλ
1, 2, 3)
for row in table[header_idx + 1:]:
if not row or len(row) < 2:
continue
grade_val = row[0]
if not grade_val or not str(grade_val).strip().isdigit():
continue
grade = int(grade_val)
if grade not in [1, 2, 3]:
continue
entry = {
"grade": grade,
"school_days": self._safe_int(row[1] if len(row) > 1 else None),
"absence": {"illness": 0, "unauthorized": 0, "other": 0},
"late": {"illness": 0, "unauthorized": 0, "other": 0},
"early_leave": {"illness": 0, "unauthorized": 0, "other": 0},
"class_absence": {"illness": 0, "unauthorized": 0, "other": 0},
"special_notes": None,
}
# μ»¬λΌ λ§€ν (κ²°μ 3κ° + μ§κ° 3κ° + μ‘°ν΄ 3κ° + κ²°κ³Ό 3κ° + νΉκΈ°μ¬ν)
idx = 2
for category in ["absence", "late", "early_leave", "class_absence"]:
for sub in ["illness", "unauthorized", "other"]:
if idx < len(row):
entry[category][sub] = self._safe_int(row[idx])
idx += 1
# νΉκΈ°μ¬ν (λ§μ§λ§ 컬λΌ)
if idx < len(row) and row[idx]:
entry["special_notes"] = str(row[idx]).strip() or None
attendance.append(entry)
return attendance
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# μμκ²½λ ₯ νμ±
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _parse_awards(self) -> list[dict]:
"""μμκ²½λ ₯ νμ±"""
tables = self._get_section_tables("μμ")
awards = []
current_grade = None # λ³ν©λ μ
μ²λ¦¬μ©
current_semester = None # λ³ν©λ νκΈ° μ
μ²λ¦¬μ©
for table in tables:
if not table:
continue
# μμλͺ
μ»¬λΌ νμΈ
has_award = any(
row and any("μμ" in str(c) or "μλͺ
" in str(c) for c in row if c)
for row in table
)
if not has_award:
continue
for row in table:
if not row or len(row) < 4:
continue
# ν€λ ν μ€ν΅ (μμλͺ
, νλ
λ± ν¬ν¨)
row_text = ' '.join(str(c) for c in row if c)
if "μμλͺ
" in row_text or "μ μ λͺ
" in row_text or "μμκ²½λ ₯" in row_text:
continue
# 첫 컬λΌμμ νλ
νμΈ (λ³ν©λ μ
μ NoneμΌ μ μμ)
grade_val = row[0]
if grade_val and str(grade_val).strip().isdigit():
new_grade = int(grade_val)
if new_grade in [1, 2, 3]:
current_grade = new_grade
# νλ
μ΄ μμΌλ©΄ μ€ν΅ (첫 νμ΄κ±°λ ν€λ)
if current_grade is None:
continue
grade = current_grade
# νκΈ° νμΈ (λ λ²μ§Έ 컬λΌ, λ³ν© μ None)
semester_val = row[1] if len(row) > 1 else None
if semester_val and str(semester_val).strip() in ["1", "2"]:
current_semester = int(semester_val)
elif semester_val is None or str(semester_val).strip() == "":
# λ³ν©λ μ
- μ΄μ νκΈ° μ μ§ (current_semester κ·Έλλ‘)
pass
entry = {
"grade": grade,
"semester": current_semester,
"name": None,
"rank": None,
"date": None,
"organization": None,
"participants": None,
}
# λλ¨Έμ§ μ»¬λΌ (κ°ν λ¬Έμ μ κ±°)
# ν
μ΄λΈ ꡬ쑰: [νλ
, νκΈ°, μμλͺ
, λ±κΈ, μμμΌ, μμ¬κΈ°κ΄, μ°Έκ°λμ]
def clean_cell(val):
if not val:
return None
return str(val).replace('\n', '').strip() or None
if len(row) > 2:
entry["name"] = clean_cell(row[2])
if len(row) > 3:
entry["rank"] = clean_cell(row[3])
if len(row) > 4:
entry["date"] = clean_cell(row[4])
if len(row) > 5:
entry["organization"] = clean_cell(row[5])
if len(row) > 6:
entry["participants"] = clean_cell(row[6])
if entry["name"]:
awards.append(entry)
return awards
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# μ°½μμ 체ννλ νμ±
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _parse_creative_activities(self) -> dict:
"""μ°½μμ 체ννλ νμ± - ν
μ΄λΈ ꡬ쑰 κΈ°λ°"""
text = self._get_section_text("창체")
tables = self._get_section_tables("창체")
result = {
"autonomous": [], # μμ¨νλ
"club": [], # λμ리νλ
"career": [], # μ§λ‘νλ
"volunteer": [], # λ΄μ¬νλ
}
# 1λ¨κ³: ν
μ΄λΈμμ μνΈλ¦¬ ꡬ쑰 μΆμΆ
entries = [] # [(grade, area_id, hours, desired_field, detail_prefix)]
current_grade = 1
for table in tables:
if not table:
continue
# 창체 νλ ν
μ΄λΈ νμΈ (λ΄μ¬νλ ν
μ΄λΈ μ μΈ)
table_text = ' '.join(str(c) for row in table[:4] for c in row if c)
if "λ΄μ¬νλ" in table_text or "λκ³μκ°" in table_text or "μΌμ λλ κΈ°κ°" in table_text:
result["volunteer"].extend(self._parse_volunteer_table(table))
continue
if "μ°½μμ " not in table_text and "μμ" not in table_text:
continue
# ν
μ΄λΈ ν νμ±
for row in table:
if not row:
continue
# μ
κ° μΆμΆ (None μ²λ¦¬)
def safe_cell(idx):
if len(row) <= idx:
return ""
val = row[idx]
if val is None or val == '':
return ""
return str(val).strip()
grade_val = safe_cell(0)
area_val = safe_cell(1)
hours_val = safe_cell(2)
col3 = safe_cell(3)
col4 = safe_cell(4)
# ν€λ ν μ€ν΅ (λ ꡬ체μ μΈ ν¨ν΄)
row_str = ''.join(str(c) for c in row if c)
# "μ°½ μ μ 체 ν" λλ "μ°½μμ 체ν" ν¨ν΄ (λ΄μ©μ "μ°½μμ "μ΄ ν¬ν¨λ μ μμ)
is_header = False
if "μ°½ μ μ 체 ν" in row_str or "μ°½μμ 체ν" in row_str or "μ°½μμ 체ν" in row_str:
is_header = True
# "μμ", "μκ°", "νΉκΈ°μ¬ν"μ΄ λͺ¨λ μμΌλ©΄ ν€λ
if "μμ" in row_str and "μκ°" in row_str and "νΉκΈ°μ¬ν" in row_str:
is_header = True
# μ§§μ ν€λ ν
μ€νΈ
if row_str.strip() in ["νλ
", "μμ", "μκ°", "νΉκΈ°μ¬ν"]:
is_header = True
if is_header:
continue
# νλ
μ
λ°μ΄νΈ
if grade_val and grade_val.isdigit() and 1 <= int(grade_val) <= 3:
current_grade = int(grade_val)
# μμ ν κ°μ§ (μ μνΈλ¦¬)
area_id = self._get_creative_area_id(area_val)
if area_id and hours_val.isdigit():
entry = {
"grade": current_grade,
"area_id": area_id,
"hours": int(hours_val),
"desired_field": None,
"detail_prefix": "", # ν
μ΄λΈμ 첫 μμΈ λ΄μ© (κ²μμ©)
}
# ν¬λ§λΆμΌ (μ§λ‘νλ)
if col3 == "ν¬λ§λΆμΌ" and col4 and "곡곡기κ΄" not in col4:
entry["desired_field"] = col4
entries.append(entry)
# μμΈ λ΄μ© ν (μμ μμ, col3μ λ΄μ© μμ)
elif not area_val and col3 and len(col3) > 10 and entries:
# λ§μ§λ§ μνΈλ¦¬μ prefix μ μ₯ (첫 λ²μ§Έ μμΈ νλ§)
if not entries[-1]["detail_prefix"]:
# μ€λ°κΏ μ κ±°νκ³ μ²« 20μ μ μ₯
prefix = col3.replace('\n', ' ')[:30]
entries[-1]["detail_prefix"] = prefix
# 2λ¨κ³: ν
μ€νΈμμ μ 체 μμΈ λ΄μ© μΆμΆ
full_text = text.replace('\n', ' ')
full_text = re.sub(r'\s+', ' ', full_text)
for i, entry in enumerate(entries):
prefix = entry["detail_prefix"]
if not prefix:
# prefixκ° μμΌλ©΄ restricted λ΄μ©
entry["details"] = ""
entry["restricted"] = True
continue
# prefix μμΉ μ°ΎκΈ°
# νΉμλ¬Έμ μ΄μ€μΌμ΄ν
prefix_pattern = re.escape(prefix[:20])
match = re.search(prefix_pattern, full_text)
if match:
start_pos = match.start()
# λ€μ μνΈλ¦¬ prefix μμΉ μ°ΎκΈ°
end_pos = len(full_text)
for j in range(i + 1, len(entries)):
next_prefix = entries[j]["detail_prefix"]
if next_prefix:
next_match = re.search(re.escape(next_prefix[:20]), full_text[start_pos + 1:])
if next_match:
end_pos = start_pos + 1 + next_match.start()
break
details = full_text[start_pos:end_pos].strip()
# μ 리: νμ΄μ§ ν€λ/νΈν° μ κ±°
details = re.sub(r'μ λκ³ λ±νκ΅\s+\d{4}λ
\s+\d+μ\s+\d+μΌ\s+\d+/\d+\s+λ°\s+\d+\s+λ²νΈ\s+\d+\s+μ΄λ¦\s+\S+', ' ', details)
details = re.sub(r'μ°½\s*μ\s*μ \s*체\s*ν\s*ν\s*λ\s*μ\s*ν©', ' ', details)
details = re.sub(r'νλ
\s+μμ\s+μκ°\s+νΉκΈ°μ¬ν', ' ', details)
details = re.sub(r'\s+', ' ', details).strip()
entry["details"] = details
entry["restricted"] = "곡곡기κ΄μ μ 보곡κ°μ κ΄ν λ²λ₯ " in details
else:
entry["details"] = ""
entry["restricted"] = True
# 3λ¨κ³: κ°μ (νλ
, μμ, μκ°) μ‘°ν© λ³ν© (νμ΄μ§ λκΉμΌλ‘ λΆλ¦¬λ νλͺ© ν©μΉκΈ°)
merged_entries = {} # key: (grade, area_id, hours) -> entry with combined details
for entry in entries:
key = (entry["grade"], entry["area_id"], entry["hours"])
details = entry.get("details", "")
if key in merged_entries:
# κΈ°μ‘΄ νλͺ©μ λ΄μ© μΆκ°
if details and not entry.get("restricted", False):
existing = merged_entries[key]
if existing.get("details") and not existing.get("restricted", False):
# λ λ΄μ© λͺ¨λ μμΌλ©΄ ν©μΉ¨
existing["details"] = existing["details"] + " " + details
elif not existing.get("details") or existing.get("restricted", False):
# κΈ°μ‘΄μ΄ λΉμ΄μκ±°λ restrictedλ©΄ μ λ΄μ©μΌλ‘ λ체
existing["details"] = details
existing["restricted"] = False
else:
merged_entries[key] = entry.copy()
# 4λ¨κ³: κ²°κ³Όμ μΆκ°
for entry in merged_entries.values():
details = entry.get("details", "")
# restricted λ΄μ© μ 리 (λ§μ»€ ν
μ€νΈ μ κ±°)
if entry.get("restricted", False):
# νμ€ restricted ν
μ€νΈλ‘ λ체
details = "ν΄λΉλ΄μ©μ γ곡곡기κ΄μ μ 보곡κ°μ κ΄ν λ²λ₯ γ μ 9μ‘°μ 1νμ 5νΈμ λ°λΌ λ΄λΆκ²ν μ€μΈ μ¬νμΌλ‘ λΉν΄νλ
λμλ μ 곡νμ§ μμ΅λλ€."
# λ§μ»€ ν¨ν΄ μ κ±° (λ΄μ©μ νΌμ
λ κ²½μ°)
details = re.sub(r'\d+\s*(μμ¨νλ|λμ리νλ|μ§λ‘νλ)\s+\d+', '', details)
details = re.sub(r'\s+', ' ', details).strip()
self._add_creative_entry(
result,
entry["area_id"],
entry["grade"],
entry["hours"],
details,
entry["desired_field"],
entry.get("restricted", False)
)
return result
def _get_creative_area_id(self, area_text: str) -> str:
"""μμ ν
μ€νΈμμ μμ ID λ°ν"""
area_text = area_text.strip()
if "μμ¨" in area_text:
return "μμ¨"
elif "λμ리" in area_text:
return "λμ리"
elif "μ§λ‘" in area_text:
return "μ§λ‘"
return ""
def _add_creative_entry(self, result: dict, area_id: str, grade: int,
hours: int, details: str, desired_field: str = None,
restricted: bool = False):
"""창체 νλͺ© μΆκ°"""
if area_id == "μμ¨":
result["autonomous"].append({
"grade": grade,
"hours": hours,
"details": details,
"restricted": restricted,
})
elif area_id == "λμ리":
result["club"].append({
"grade": grade,
"club_name": None, # detailsμμ μΆμΆ κ°λ₯
"hours": hours,
"details": details,
"restricted": restricted,
})
elif area_id == "μ§λ‘":
result["career"].append({
"grade": grade,
"hours": hours,
"desired_field": desired_field,
"details": details,
"restricted": restricted,
})
def _parse_volunteer_table(self, table: list) -> list[dict]:
"""λ΄μ¬νλ ν
μ΄λΈ νμ±"""
volunteers = []
current_grade = None
for row in table:
if not row or len(row) < 2:
continue
# μ
κ° μΆμΆ (None μ²λ¦¬)
def safe_cell(idx):
if len(row) <= idx:
return ""
val = row[idx]
if val is None or val == '':
return ""
return str(val).strip()
grade_val = safe_cell(0)
data_col = safe_cell(1)
# ν€λ ν μ€ν΅ (λ ꡬ체μ μΈ μ‘°κ±΄)
# 1) 첫 λ²μ§Έ μ΄μ΄ "νλ
" λλ "ν λ
" (νλ
μ΄ ν€λ)
# 2) λ λ²μ§Έ μ΄μ΄ "μΌμ" λλ "λ΄ μ¬" μμ (μ΄ ν€λ)
is_header = False
if grade_val and ("νλ
" in grade_val or "ν λ
" in grade_val):
is_header = True
if data_col and (data_col.startswith("μΌμ") or data_col.startswith("λ΄ μ¬")):
is_header = True
if is_header:
continue
# νλ
μΆμΆ (첫 λ²μ§Έ μ΄)
if grade_val and grade_val.isdigit() and 1 <= int(grade_val) <= 3:
current_grade = int(grade_val)
# λ΄μ¬νλ λ°μ΄ν° (λ μ§ ν¨ν΄μΌλ‘ κ°μ§)
# νμ: νλ
| μΌμ λλ κΈ°κ° + μ₯μ + νλλ΄μ© (λ³ν©) | ...
date_match = re.search(r'(\d{4}\.\d{2}\.\d{2})', data_col)
if date_match and current_grade:
entry = {
"grade": current_grade,
"date": date_match.group(1),
"location": None,
"activity": data_col.replace('\n', ' '), # κ°ν μ κ±°
"hours": None,
"cumulative_hours": None,
}
# μκ°/λκ³μκ° μΆμΆ (μλ κ²½μ°)
if len(row) > 4:
entry["hours"] = self._safe_int(row[4])
if len(row) > 5:
entry["cumulative_hours"] = self._safe_int(row[5])
volunteers.append(entry)
return volunteers
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# κ΅κ³Όνμ΅λ°λ¬μν© νμ±
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _parse_academics(self) -> dict:
"""κ΅κ³Όνμ΅λ°λ¬μν© νμ± - academics.py λ‘μ§ ν΅ν©"""
text = self._get_section_text("κ΅κ³Ό")
tables_with_page = self._get_section_tables_with_page("κ΅κ³Ό")
result = {
"by_year": [],
"subject_remarks": [],
}
# νμ΄μ§λ³ νλ
λ§€ν κ΅¬μΆ (ν
μ€νΈμμ [nνλ
] λ§μ»€ μ°ΎκΈ°)
page_to_grade = {}
boundary = self.section_boundaries.get("κ΅κ³Ό")
if boundary:
current_grade_for_page = None
for page_num in range(boundary.start_page, boundary.end_page + 1):
if page_num >= len(self.pages):
break
page_text = self.pages[page_num].text
if "[1νλ
]" in page_text:
current_grade_for_page = 1
elif "[2νλ
]" in page_text:
current_grade_for_page = 2
elif "[3νλ
]" in page_text:
current_grade_for_page = 3
page_to_grade[page_num] = current_grade_for_page
current_grade = None
current_category = "μΌλ°" # κΈ°λ³Έκ°
# (grade, semester, category) -> records λ³ν©μ©
by_year_dict = {}
# μΈλΆλ₯λ ₯νΉκΈ°μ¬νμ νλ
λ³λ‘ λμ
remarks_by_grade = {1: [], 2: [], 3: []}
seen_contents = set() # μ€λ³΅ λ°©μ§μ©
for page_num, table in tables_with_page:
if not table:
continue
# νμ΄μ§ κΈ°λ° νλ
μ€μ
page_grade = page_to_grade.get(page_num)
if page_grade and page_grade != current_grade:
current_grade = page_grade
current_category = "μΌλ°"
# ν€λ νλ€μμ μ 보 μΆμΆ
header_rows_text = ""
for row in table[:5]:
if row:
header_rows_text += " " + " ".join(str(cell) for cell in row if cell)
header_rows_no_space = header_rows_text.replace(" ", "")
first_row_text = " ".join(str(cell) for cell in table[0] if cell) if table[0] else ""
# ν
μ΄λΈ ν€λμμλ νλ
νμΈ (λ°±μ
)
if "[1νλ
]" in header_rows_text or "[1νλ
]" in header_rows_no_space:
if current_grade != 1:
current_grade = 1
current_category = "μΌλ°"
elif "[2νλ
]" in header_rows_text or "[2νλ
]" in header_rows_no_space:
if current_grade != 2:
current_grade = 2
current_category = "μΌλ°"
elif "[3νλ
]" in header_rows_text or "[3νλ
]" in header_rows_no_space:
if current_grade != 3:
current_grade = 3
current_category = "μΌλ°"
# μΉ΄ν
κ³ λ¦¬ μ 보 μΆμΆ
if "μ§λ‘μ ν" in header_rows_text or "μ§λ‘ μ ν" in header_rows_text:
current_category = "μ§λ‘μ ν"
elif "체μ‘Β·μμ " in header_rows_text or "체μ‘γμμ " in header_rows_text or "체μ‘μμ " in header_rows_no_space:
current_category = "체μ‘μμ "
# μ±μ ν
μ΄λΈ νμ±
if "κ΅κ³Ό" in header_rows_text and ("κ³Όλͺ©" in header_rows_text or "λ¨μ" in header_rows_text):
records_by_sem = self._parse_grade_table(table, current_grade, 1, current_category)
for sem, records in records_by_sem.items():
if records:
key = (current_grade, sem, current_category)
if key not in by_year_dict:
by_year_dict[key] = []
by_year_dict[key].extend(records)
# μΈλΆλ₯λ ₯νΉκΈ°μ¬ν ν
μ΄λΈ νμ±
first_row_no_space = first_row_text.replace(" ", "")
# 창체 ν
μ΄λΈ μ μΈ (μ°½μμ 체ννλμν© ν
μ΄λΈμ κ΅κ³Ό μΈνΉμ΄ μλ)
is_changche_table = "μ°½μμ 체ννλ" in header_rows_no_space or "μ°½μμ 체ννλ" in first_row_no_space
if ("μΈλΆλ₯λ ₯" in first_row_no_space or "νΉκΈ°μ¬ν" in first_row_no_space) and not is_changche_table:
remarks_text = self._extract_remarks_text(table)
if remarks_text:
# μ€λ³΅ κ°μ§: (νλ
, λ΄μ© ν΄μ) μ‘°ν©μΌλ‘ 체ν¬
# λ€λ₯Έ νλ
μ λμΌ λ΄μ©μ νμ©, κ°μ νλ
λ΄ μ€λ³΅μ νμ© (νκΈ°λ³ λΉκ³΅κ° λ±)
grade_key = current_grade if current_grade in [1, 2, 3] else 1
content_hash = hash((grade_key, remarks_text[:100]))
# λΉκ³΅κ° λ΄μ©μ μ€λ³΅ νμ© (νκΈ°λ³λ‘ κ°μ λ΄μ©μ΄ λ°λ³΅λ¨)
is_restricted = "μ 곡νμ§ μμ΅λλ€" in remarks_text
if not is_restricted and content_hash in seen_contents:
continue
seen_contents.add(content_hash)
remarks_by_grade[grade_key].append(remarks_text)
# by_year_dictλ₯Ό μ λ ¬λ 리μ€νΈλ‘ λ³ν
if by_year_dict:
categories = ["μΌλ°", "μ§λ‘μ ν", "체μ‘μμ "]
for grade in [1, 2, 3]:
for category in categories:
for semester in [1, 2]:
key = (grade, semester, category)
records = by_year_dict.get(key, [])
result["by_year"].append({
"grade": grade,
"semester": semester,
"category": category,
"records": records,
"total_credits": sum(r.get("credits", 0) or 0 for r in records),
})
# νλ
λ³ λμ λ ν
μ€νΈλ₯Ό νμ±
for grade, texts in remarks_by_grade.items():
if texts:
# λΉκ³΅κ° ν
μ€νΈμ μΌλ° ν
μ€νΈ λΆλ¦¬
restricted_texts = []
normal_texts = []
for t in texts:
if "μ 곡νμ§ μμ΅λλ€" in t:
restricted_texts.append(t)
else:
normal_texts.append(t)
# μΌλ° ν
μ€νΈλ ν©μ³μ νμ± (κ³Όλͺ©μ΄ μ¬λ¬ ν
μ΄λΈμ κ±Έμ³μμ μ μμ)
if normal_texts:
combined_text = " ".join(normal_texts)
parsed = self._split_by_subjects(combined_text, grade)
result["subject_remarks"].extend(parsed)
# λΉκ³΅κ° ν
μ€νΈλ κ°λ³μ μΌλ‘ μ²λ¦¬ (κ°κ° λ³λ νλͺ©)
for restricted_text in restricted_texts:
result["subject_remarks"].append({
"grade": grade,
"semester": None,
"subject": None,
"content": restricted_text.strip(),
"restricted": True,
})
return result
def _expand_merged_rows(self, table: list, data_start_idx: int) -> list[list]:
"""λ³ν©λ μ
μ κ°λ³ νμΌλ‘ νμ₯
pdfplumberλ λ³ν© μ
λ΄ μ€λ°κΏμ \\nμΌλ‘ νν.
κ³Όλͺ©λͺ
μ΄μ κΈ°μ€μΌλ‘ ν μλ₯Ό κ²°μ νλ, μλ €μ§ κ³Όλͺ©λͺ
λͺ©λ‘μ νμ©.
"""
expanded_rows = []
for row in table[data_start_idx:]:
if not row or all(cell is None or str(cell).strip() == "" for cell in row):
continue
# "μ΄μνμ ν©κ³" νμ μ€ν΅
row_text = " ".join(str(c) for c in row if c)
if "μ΄μνμ " in row_text or "ν©κ³" in row_text:
continue
# κ³Όλͺ©λͺ
μ΄ μ°ΎκΈ° (λ³΄ν΅ μ΄ 2, μμΌλ©΄ μ΄ 1)
subject_col_idx = 2
if len(row) <= 2 or not row[2]:
subject_col_idx = 1
subject_cell = str(row[subject_col_idx]) if len(row) > subject_col_idx and row[subject_col_idx] else ""
# κ³Όλͺ©λͺ
μΆμΆ - μλ €μ§ κ³Όλͺ©λͺ
λͺ©λ‘μΌλ‘ λ§€μΉ
subjects = self._extract_subjects_from_cell(subject_cell)
# λ³ν©λ νμ΄ μλλ©΄ κ·Έλλ‘ μΆκ°
if len(subjects) <= 1:
cleaned_row = []
for c in row:
cell_str = str(c) if c else ""
# κ°ν μ κ±° (λ―Έμ κ°μκ³Ό λΉ\nν β λ―Έμ κ°μκ³Ό λΉν)
cell_str = cell_str.replace("\n", "").strip()
cleaned_row.append(cell_str)
expanded_rows.append(cleaned_row)
continue
# κ° μ
μ \nμΌλ‘ split
split_cells = []
for cell in row:
cell_str = str(cell) if cell else ""
parts = cell_str.split("\n")
split_cells.append(parts)
num_subjects = len(subjects)
# λ³ν©λ ν: κ³Όλͺ© κ°μλ§νΌ ν μμ±
# rank μ΄(μ΄ 6)μ μ€ν κ³Όλͺ©μ μ€ν΅νλ©΄μ λ§€νν΄μΌ ν¨
rank_col_idx = 6
rank_parts = split_cells[rank_col_idx] if len(split_cells) > rank_col_idx else []
rank_offset = 0 # μ€ν κ³Όλͺ©μ μ€ν΅ν νμ
for i in range(num_subjects):
subject = subjects[i] if i < len(subjects) else ""
is_no_rank_subject = "μ€ν" in subject or "νꡬμ€ν" in subject
new_row = []
for col_idx, parts in enumerate(split_cells):
if col_idx == 0:
# νκΈ° μ΄: 첫 λ²μ§Έ κ°λ§ μ¬μ©
new_row.append(parts[0].strip() if parts else "")
elif col_idx == subject_col_idx:
# κ³Όλͺ©λͺ
μ΄: μΆμΆλ κ³Όλͺ©λͺ
μ¬μ©
new_row.append(subject)
elif col_idx == subject_col_idx - 1:
# κ΅κ³Ό μμ μ΄: κ³Όλͺ©μμ μΆλ‘
area = SUBJECT_TO_AREA.get(subject, "")
new_row.append(area)
elif col_idx == rank_col_idx:
# rank μ΄: μ€ν κ³Όλͺ©μ λΉ κ°, μλλ©΄ μ€νμ
μ μ©
if is_no_rank_subject:
new_row.append("")
else:
adjusted_idx = i - rank_offset
if adjusted_idx < len(rank_parts):
new_row.append(rank_parts[adjusted_idx].strip())
else:
new_row.append("")
else:
# λλ¨Έμ§ μ΄: ν΄λΉ μΈλ±μ€ κ°
if i < len(parts):
new_row.append(parts[i].strip())
else:
new_row.append("")
# μ€ν κ³Όλͺ© μΉ΄μ΄νΈ μ¦κ°
if is_no_rank_subject:
rank_offset += 1
if any(c.strip() for c in new_row[1:]):
expanded_rows.append(new_row)
return expanded_rows
def _extract_subjects_from_cell(self, cell_text: str) -> list[str]:
"""μ
ν
μ€νΈμμ μλ €μ§ κ³Όλͺ©λͺ
λͺ©λ‘ μΆμΆ
pdfplumberκ° λ³ν© μ
μ μΆμΆν λ μ€λ°κΏμΌλ‘ λΆλ¦¬νμ§λ§,
κ³Όλͺ©λͺ
μ체μ μ€λ°κΏμ΄ ν¬ν¨λ μ μμ (λ―Έμ κ°μκ³Ό λΉ\nν).
μλ €μ§ κ³Όλͺ©λͺ
λͺ©λ‘μ μ¬μ©νμ¬ μ ννκ² μΆμΆ.
"""
if not cell_text:
return []
# μ€λ°κΏμ λΉ λ¬Έμμ΄λ‘ λ³ν (λ―Έμ κ°μκ³Ό λΉ\nν β λ―Έμ κ°μκ³Ό λΉν)
full_text = cell_text.replace("\n", "")
# μ°μ 곡백 μ κ±°
full_text = re.sub(r'\s+', ' ', full_text).strip()
# μλ €μ§ κ³Όλͺ©λͺ
μ€ κΈ΄ κ²λΆν° λ§€μΉ (λΆλΆ λ§€μΉ λ°©μ§)
sorted_subjects = sorted(SUBJECT_NAMES, key=len, reverse=True)
found_subjects = []
remaining = full_text
while remaining.strip():
matched = False
remaining_stripped = remaining.strip()
for subject in sorted_subjects:
# 곡백 μ κ·ν ν λΉκ΅
norm_subject = subject.replace(" ", "")
norm_remaining = remaining_stripped.replace(" ", "")
if norm_remaining.startswith(norm_subject):
found_subjects.append(subject)
# μλ³Έ ν
μ€νΈμμ ν΄λΉ λΆλΆ μ κ±°
# 곡백 ν¬ν¨λ κΈΈμ΄λ§νΌ μ κ±°
idx = 0
matched_len = 0
while idx < len(remaining_stripped) and matched_len < len(norm_subject):
if remaining_stripped[idx] != " ":
matched_len += 1
idx += 1
remaining = remaining_stripped[idx:].strip()
matched = True
break
if not matched:
# μλ €μ§ κ³Όλͺ©λͺ
μ΄ μμΌλ©΄ 첫 λ¨μ΄ μ€ν΅
parts = remaining.strip().split(None, 1)
if len(parts) > 1:
remaining = parts[1]
else:
break
# λͺ»μ°ΎμΌλ©΄ μλ λ°©μμΌλ‘ μ€λ°κΏ λΆλ¦¬
if not found_subjects:
raw_parts = cell_text.split("\n")
merged_parts = []
for part in raw_parts:
part = part.strip()
if not part:
continue
# μ§§μ λ¨μ΄(3μ λ―Έλ§)κ° μ΄μ κ³Όλͺ©λͺ
μ μΌλΆμΈ κ²½μ° ν©μΉ¨
if merged_parts and len(part) < 3 and not any(part == s for s in SUBJECT_NAMES):
merged_parts[-1] = merged_parts[-1] + part
else:
merged_parts.append(part)
return merged_parts
return found_subjects
def _parse_grade_table(self, table: list, grade: int, semester: int, category: str) -> dict[int, list[dict]]:
"""μ±μ ν
μ΄λΈ νμ± - νκΈ°λ³λ‘ λΆλ¦¬λ κ²°κ³Ό λ°ν"""
result_by_semester = {1: [], 2: []}
# ν€λ μ°ΎκΈ°
data_start_idx = 0
for i, row in enumerate(table):
row_text = " ".join(str(cell) for cell in row if cell)
if "μμ μ" in row_text or "μ±μ·¨λ" in row_text or "μμ°¨λ±κΈ" in row_text:
data_start_idx = i + 1
break
if data_start_idx == 0:
data_start_idx = 2
# λ³ν© μ
νμ₯
expanded_rows = self._expand_merged_rows(table, data_start_idx)
current_subject_area = None
current_semester = semester
cat_info = ACADEMIC_CATEGORY_BY_ID.get(category, ACADEMIC_CATEGORY_BY_ID["μΌλ°"])
for values in expanded_rows:
values = [self._clean_text(v) for v in values]
# 첫 λ²μ§Έ μ΄μμ νκΈ° μ 보 μΆμΆ
if values[0] and values[0].isdigit() and int(values[0]) in [1, 2]:
current_semester = int(values[0])
record = {
"subject_area": None,
"subject": None,
"credits": None,
"score": None,
"average": None,
"std_dev": None,
"achievement": None,
"students": None,
"rank": None,
"distribution": None,
}
# κ³Όλͺ©λͺ
μΆμΆ - μ΄ 2λ₯Ό μ°μ νμΈ (ν
μ΄λΈ ꡬ쑰: νκΈ°, κ΅κ³Ό, κ³Όλͺ©, ...)
subject_found = False
# μ΄ 2κ° μλ €μ§ κ³Όλͺ©λͺ
μΈμ§ νμΈ
if len(values) > 2 and values[2]:
subject_candidate = values[2].replace("\n", "").strip()
if subject_candidate in SUBJECT_NAMES or (
subject_candidate and not subject_candidate.isdigit()
and subject_candidate not in ["A", "B", "C", "D", "E", "P"]
and len(subject_candidate) >= 2
):
record["subject"] = subject_candidate
subject_found = True
# μ΄ 2μμ λͺ»μ°ΎμΌλ©΄ μ΄ 1-3 λ²μμμ μ°ΎκΈ°
if not subject_found:
for val in values[1:4]:
if val and not val.isdigit() and len(val) >= 2:
if val not in ["A", "B", "C", "D", "E", "P"]:
subject_name = val.replace("\n", "").strip()
record["subject"] = subject_name
break
# κ΅κ³Ό μμ μΆλ‘ - μ΄ 1 λλ κ³Όλͺ©λͺ
μμ
if record["subject"]:
inferred_area = SUBJECT_TO_AREA.get(record["subject"])
if inferred_area:
current_subject_area = inferred_area
elif len(values) > 1 and values[1]:
# μ΄ 1μ΄ κ΅κ³Ό μμμΌ μ μμ
area_candidate = values[1].replace("\n", "").strip()
# μλ €μ§ κ΅κ³Ό μμ ν¨ν΄ νμΈ
if any(area in area_candidate for area in ["κ΅μ΄", "μν", "μμ΄", "μ¬ν", "κ³Όν", "체μ‘", "μμ ", "κΈ°μ "]):
current_subject_area = area_candidate
record["subject_area"] = current_subject_area
# μ«μ κ° μΆμΆμ μν λ³μλ€
found_credits = False
found_score_avg = False
for idx, val in enumerate(values):
if idx == 0:
continue
if not val:
continue
# λ¨μ/νμ (1-9 μ¬μ΄ λ¨μΌ μ«μ)
if val.isdigit() and not found_credits:
num = int(val)
if 1 <= num <= 9:
record["credits"] = num
found_credits = True
continue
# μμ μ/νκ· (νμ€νΈμ°¨) νμ: 78/76.2(14.1)
score_match = re.match(r"^(\d+)/(\d+\.?\d*)\((\d+\.?\d*)\)$", val)
if score_match:
record["score"] = float(score_match.group(1))
record["average"] = float(score_match.group(2))
record["std_dev"] = float(score_match.group(3))
found_score_avg = True
continue
# μ§λ‘μ ν κ³Όλͺ©: μμ μ/νκ· νμ (νμ€νΈμ°¨ μμ): 78/82.8
score_avg_match = re.match(r"^(\d+)/(\d+\.?\d*)$", val)
if score_avg_match and category == "μ§λ‘μ ν":
record["score"] = float(score_avg_match.group(1))
record["average"] = float(score_avg_match.group(2))
found_score_avg = True
continue
# μ±μ·¨λ(μκ°μμ) νμ: A(215), P(100)
achievement_match = re.match(r"^([A-EP])\((\d+)\)$", val)
if achievement_match:
record["achievement"] = achievement_match.group(1)
record["students"] = int(achievement_match.group(2))
continue
# λ¨λ
μ±μ·¨λ: A, B, C, D, E, P
if val in ["A", "B", "C", "D", "E", "P"]:
if record["achievement"] is None:
record["achievement"] = val
continue
# μ±μ·¨λλ³ λΆν¬: A(62.9) B(36.0) C(1.0)
if re.search(r"[A-E]\(\d+\.?\d*\)", val) and not re.match(r"^[A-EP]\(\d+\)$", val):
record["distribution"] = val
continue
# μκ°μμ (3μ리 μ΄μ μ«μ)
if val.isdigit() and len(val) >= 3 and record["students"] is None:
record["students"] = int(val)
continue
# μμ°¨λ±κΈ μΆμΆ (μΌλ° κ³Όλͺ©λ§)
# "μ€ν" κ³Όλͺ©μ μμ°¨λ±κΈμ΄ μμ
subject_name = record.get("subject", "")
is_no_rank_subject = "μ€ν" in subject_name or "νꡬμ€ν" in subject_name
if cat_info.has_rank and not is_no_rank_subject:
# ν
μ΄λΈ ꡬ쑰: νκΈ°, κ΅κ³Ό, κ³Όλͺ©, νμ μ, μμ μ/νκ· , μ±μ·¨λ, μμ°¨λ±κΈ, λΉκ³
# μμ°¨λ±κΈμ μ΄ 6 μμΉ (0-indexed)
rank_col_idx = 6
if len(values) > rank_col_idx:
rank_val = values[rank_col_idx].strip() if values[rank_col_idx] else ""
if rank_val.isdigit() and len(rank_val) == 1:
num = int(rank_val)
if 1 <= num <= 9:
record["rank"] = num
elif rank_val == "P":
record["rank"] = "P"
if record["subject"]:
result_by_semester[current_semester].append(record)
return result_by_semester
def _extract_remarks_text(self, table: list) -> str:
"""μΈλΆλ₯λ ₯νΉκΈ°μ¬ν ν
μ΄λΈμμ ν
μ€νΈ μΆμΆ"""
text_parts = []
for row in table[1:]:
if not row or all(cell is None or str(cell).strip() == "" for cell in row):
continue
for cell in row:
if cell:
cell_text = self._clean_text(str(cell))
if cell_text and cell_text not in ["κ³Όλͺ©", "μΈλΆλ₯λ ₯", "νΉκΈ°μ¬ν", "ν΄λΉ μ¬ν μμ"]:
text_parts.append(cell_text)
return " ".join(text_parts).strip()
def _split_by_subjects(self, text: str, grade: int) -> list[dict]:
"""ν
μ€νΈλ₯Ό κ³Όλͺ©λ³λ‘ λΆλ¦¬
κ°μ μ¬ν:
- κ³Όλͺ©λͺ
μλ κ°μΈ μΈνΉ (subject=None) μ§μ
- λΉκ³΅κ° λ΄μ© (restricted=True) κ°μ§
"""
result = []
# λΉκ³΅κ° μ¬λΆ νμΈ
is_restricted = "μ 곡νμ§ μμ΅λλ€" in text or "μ 보곡κ°μ κ΄ν λ²λ₯ " in text
# κ³Όλͺ©λͺ
μ κΈΈμ΄μμΌλ‘ μ λ ¬ (κΈ΄ κ² λ¨Όμ λ§€μΉ)
sorted_subjects = sorted(SUBJECT_NAMES, key=len, reverse=True)
subject_pattern = "|".join(re.escape(s) for s in sorted_subjects)
# ν¨ν΄: "(νκΈ°)" + κ³Όλͺ©λͺ
+ ":"
pattern = re.compile(
rf"(?:^|\s)(\(?\d?νκΈ°\)?)?({subject_pattern})\s*[:οΌ]"
)
matches = list(pattern.finditer(text))
if not matches:
# ν¨ν΄ λ§€μΉ μ€ν¨ μ - μ 체λ₯Ό κ°μΈ μΈνΉ λλ λΉκ³΅κ° λ΄μ©μΌλ‘ μ²λ¦¬
content = text.strip()
if content and content != "ν΄λΉ μ¬ν μμ":
# 첫 μ½λ‘ μ΄μ μ κ³Όλͺ©λͺ
μΌλ‘ μΆμ μλ
colon_pos = text.find(":")
if 0 < colon_pos < 30 and not is_restricted:
subject = text[:colon_pos].strip()
content = text[colon_pos + 1:].strip()
if subject and content:
result.append({
"grade": grade,
"semester": None,
"subject": subject,
"content": content,
"restricted": False,
})
else:
# κ³Όλͺ©λͺ
μλ κ°μΈ μΈνΉ λλ λΉκ³΅κ° λ΄μ©
result.append({
"grade": grade,
"semester": None,
"subject": None, # κ°μΈ μΈνΉ
"content": content,
"restricted": is_restricted,
})
return result
# 첫 λ§€μΉ μ΄μ μ ν
μ€νΈ νμΈ (κ°μΈ μΈνΉ)
if matches[0].start() > 0:
pre_text = text[:matches[0].start()].strip()
# 50μ μ΄μμ΄λ©΄ κ°μΈ μΈνΉμΌλ‘ κ°μ£Ό
if len(pre_text) >= 50:
result.append({
"grade": grade,
"semester": None,
"subject": None, # κ°μΈ μΈνΉ
"content": pre_text,
"restricted": False,
})
# κ° λ§€μΉμ λν΄ κ³Όλͺ©κ³Ό λ΄μ© μΆμΆ
for i, match in enumerate(matches):
semester_str = match.group(1) or ""
subject_name = match.group(2)
semester = None
if "1νκΈ°" in semester_str:
semester = 1
elif "2νκΈ°" in semester_str:
semester = 2
content_start = match.end()
content_end = matches[i + 1].start() if i + 1 < len(matches) else len(text)
content = text[content_start:content_end].strip()
if content:
result.append({
"grade": grade,
"semester": semester,
"subject": subject_name,
"content": content,
"restricted": False,
})
# λͺ¨λ κ³Όλͺ© λ΄μ©μμ κ°μΈ μΈνΉ λΆλ¦¬ μλ
# (κ°μΈ μΈνΉμ΄ λ§μ§λ§ κ³Όλͺ©μ΄ μλ μ€κ° κ³Όλͺ©μ λΆμ΄μμ μ μμ)
personal_remarks_to_add = []
for i, item in enumerate(result):
if item["subject"] is None:
continue # μ΄λ―Έ κ°μΈ μΈνΉμΈ κ²½μ° μ€ν΅
content = item["content"]
personal_remark = self._extract_personal_remark_from_content(content)
if personal_remark:
# κ³Όλͺ© λ΄μ©μμ κ°μΈ μΈνΉ λΆλΆ μ κ±°
result[i]["content"] = content[:content.find(personal_remark)].strip()
# κ°μΈ μΈνΉ μΆκ° (λμ€μ resultμ μΆκ°)
personal_remarks_to_add.append({
"grade": grade,
"semester": None,
"subject": None,
"content": personal_remark.strip(),
"restricted": False,
})
# κ°μΈ μΈνΉ μΆκ°
result.extend(personal_remarks_to_add)
return result
def _extract_personal_remark_from_content(self, content: str) -> Optional[str]:
"""λ§μ§λ§ κ³Όλͺ© λ΄μ©μμ κ°μΈ μΈνΉ λΆλ¦¬
κ°μΈ μΈνΉ μμ ν¨ν΄:
- λ μ§ ν¨ν΄: (2024.07.17.), (2024λ
)
- νλ‘μ νΈ/νλ: ~νλ‘μ νΈ, ~νλμ μ°Έμ¬
- λ
립μ μ£Όμ μμ (λ§μΉ¨ν ν μ λ¬Έμ₯μ΄ κ³Όλͺ©κ³Ό 무κ΄ν κ²½μ°)
"""
if len(content) < 100:
return None
# κ°μΈ μΈνΉ μμ ν€μλ ν¨ν΄
personal_patterns = [
# λ μ§λ‘ μμνλ νλ
r'(?<=\.\s)([κ°-ν£]+\s*νλ‘μ νΈ\s*\(\d{4}\.\d{2}\.\d{2}\.\))',
r'(?<=\.\s)([κ°-ν£]+\s*νλ‘μ νΈ\s*\(\d{4}λ
)',
# νΉμ μ£Όμ λ‘ μμ (μλ―Όμ§, μμ¬ λ± νκ΅ νλ)
r'(?<=\.\s)(μμ 리\s+μλ―Όμ§μ¬)',
r'(?<=\.\s)(κ°μΉμ±μ₯\s+νλ‘μ νΈ)',
# μΌλ°μ μΈ κ°μΈ μΈνΉ ν¨ν΄ (λ λμ λ²μ)
r'(?<=\.\s)([κ°-ν£]+(?:νλ|νλ‘μ νΈ)\s*\(\d{4})',
]
for pattern in personal_patterns:
match = re.search(pattern, content)
if match:
# λ§€μΉλ μμΉλΆν° λκΉμ§κ° κ°μΈ μΈνΉ
start_idx = match.start(1)
personal_content = content[start_idx:].strip()
# μ΅μ 50μ μ΄μμ΄μ΄μΌ κ°μΈ μΈνΉμΌλ‘ μΈμ
if len(personal_content) >= 50:
return personal_content
return None
def _clean_text(self, text: str) -> str:
"""ν
μ€νΈ μ 리"""
if not text:
return ""
# 곡백 μ κ·ν
text = re.sub(r'\s+', ' ', text.strip())
return text
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# λ
μνλμν© νμ±
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _parse_reading(self) -> list[dict]:
"""λ
μνλμν© νμ± (Yμ’ν κΈ°λ° νν°λ§μΌλ‘ κ°μν)"""
tables = self._get_section_tables("λ
μ")
reading = []
current_grade = None # λ³ν©λ μ
μ²λ¦¬μ©
current_subject = "곡ν΅" # λ³ν©λ μ
μ²λ¦¬μ©
for table in tables:
if not table:
continue
for row in table:
if not row or len(row) < 2:
continue
# ν€λ ν μ€ν΅
row_text = ' '.join(str(c) for c in row if c)
if "λ
μνλ" in row_text or "κ³Όλͺ© λλ μμ" in row_text:
continue
# νλ
νμΈ (λ³ν©λ μ
μ None)
grade_val = row[0]
if grade_val and str(grade_val).strip().isdigit():
new_grade = int(grade_val)
if new_grade in [1, 2, 3]:
current_grade = new_grade
# νλ
μ΄ μμΌλ©΄ μ€ν΅ (첫 νμ΄κ±°λ ν€λ)
if current_grade is None:
continue
# κ³Όλͺ©/μμ νμΈ (λ³ν©λ μ
μ None)
subject_val = row[1] if len(row) > 1 else None
if subject_val and str(subject_val).strip():
current_subject = str(subject_val).strip()
# λμ λͺ©λ‘ (λ§μ§λ§ 컬λΌ, κ°ν μ κ±°)
books_text = ""
if len(row) > 2 and row[2]:
books_text = str(row[2]).replace('\n', ' ').strip()
elif len(row) > 1 and row[1] and not subject_val:
# κ³Όλͺ© μμ΄ λ΄μ©λ§ μλ κ²½μ° (λ³ν©λ ν)
books_text = str(row[1]).replace('\n', ' ').strip()
# λΉ λ΄μ© μ μΈ
if not books_text:
continue
reading.append({
"grade": current_grade,
"subject_or_area": current_subject,
"content": books_text,
})
return reading
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# νλνΉμ± λ° μ’
ν©μ견 νμ±
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _parse_behavior(self) -> list[dict]:
"""νλνΉμ± λ° μ’
ν©μ견 νμ± (Yμ’ν κΈ°λ° νν°λ§μΌλ‘ κ°μν)"""
tables = self._get_section_tables("νλ")
behavior = []
seen_grades = set()
for table in tables:
if not table:
continue
for row in table:
if not row or len(row) < 2:
continue
grade_val = row[0]
if not grade_val or not str(grade_val).strip().isdigit():
continue
grade = int(grade_val)
if grade not in [1, 2, 3]:
continue
# μ΄λ―Έ ν΄λΉ νλ
λ°μ΄ν°κ° μμΌλ©΄ μ€ν΅ (μ€λ³΅ λ°©μ§)
if grade in seen_grades:
continue
# μ’
ν©μ견 (λ λ²μ§Έ 컬λΌλΆν°, κ°ν μ κ±°)
content = ' '.join(str(c).replace('\n', ' ') for c in row[1:] if c)
content = ' '.join(content.split()) # μ°μ 곡백 μ 리
# λΉ λ΄μ© μ μΈ
if not content:
continue
seen_grades.add(grade)
behavior.append({
"grade": grade,
"content": content,
"restricted": "μ 곡νμ§ μμ΅λλ€" in content,
})
# νλ
μ μ λ ¬
behavior.sort(key=lambda x: x["grade"])
return behavior
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# μ νΈλ¦¬ν°
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _safe_int(self, val) -> int:
"""μμ ν μ μ λ³ν"""
if val is None:
return 0
try:
# . μ 0μΌλ‘ μ²λ¦¬
val_str = str(val).strip()
if val_str in ['.', '', '-']:
return 0
return int(val_str)
except (ValueError, TypeError):
return 0
def parse_with_template(pdf_path: str) -> dict[str, Any]:
"""ν
νλ¦Ώ κΈ°λ° νμ± νΈμ ν¨μ"""
parser = TemplateParser(pdf_path)
return parser.parse()
"""
ν
νλ¦Ώ κΈ°λ° μνκΈ°λ‘λΆ νμ
ν΅μ¬ λ‘μ§:
1. PDFμμ μ 체 ν
μ€νΈ μΆμΆ (νμ΄μ§λ³)
2. λ¬Έμ λ²μ κ°μ§ (ꡬλ²μ /μ λ²μ /λμ
μ ν)
3. κ³ μ νμ΄ν λ§€μΉμΌλ‘ μΉμ
κ²½κ³ μ°ΎκΈ°
4. μΉμ
λ³ νμλ‘ λ΄μ© μΆμΆ
"""
import re
from dataclasses import dataclass, field
from pathlib import Path
from typing import Any, Optional
import pdfplumber
from .section_template import (
SECTIONS,
SECTION_BY_ID,
CREATIVE_ACTIVITY_AREAS,
ACADEMIC_CATEGORIES,
ACADEMIC_CATEGORY_BY_ID,
DocVersion,
normalize_text,
detect_doc_version,
)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# κ³Όλͺ©λͺ
λͺ©λ‘ (μΈνΉ νμ±μ©)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
SUBJECT_NAMES = [
# κ΅μ΄
"κ΅μ΄", "λ¬Έν", "λ
μ", "νλ²κ³Ό μλ¬Έ", "μΈμ΄μ 맀체", "μ¬ν κ΅μ΄",
# μν
"μν", "μνβ
", "μνβ
‘", "λ―Έμ λΆ", "νλ₯ κ³Ό ν΅κ³", "κΈ°ν",
"μ¬ν μνβ
", "μ¬ν μνβ
‘", "μνμ μ¬κ³ μ μ λΆ", "κ³ κΈ μνβ
", "κ³ κΈ μνβ
‘",
# μμ΄
"μμ΄", "μμ΄β
", "μμ΄β
‘", "μμ΄ λ
ν΄μ μλ¬Έ", "μμ΄ νν", "μ€μ© μμ΄",
# νκ΅μ¬/μ¬ν
"νκ΅μ¬", "ν΅ν©μ¬ν", "νκ΅μ§λ¦¬", "μΈκ³μ§λ¦¬", "λμμμμ¬", "μΈκ³μ¬",
"κ²½μ ", "μ μΉμ λ²", "μ¬νΒ·λ¬Έν", "μ€λ¦¬μ μ¬μ", "μνκ³Ό μ€λ¦¬", "μ¬νμ§λ¦¬",
# κ³Όν
"ν΅ν©κ³Όν", "κ³Όννꡬμ€ν", "물리νβ
", "물리νβ
‘", "ννβ
", "ννβ
‘",
"μλͺ
κ³Όνβ
", "μλͺ
κ³Όνβ
‘", "μ§κ΅¬κ³Όνβ
", "μ§κ΅¬κ³Όνβ
‘",
"κ³ κΈ λ¬Όλ¦¬ν", "κ³ κΈ νν", "κ³ κΈ μλͺ
κ³Όν", "κ³ κΈ μ§κ΅¬κ³Όν",
"물리ν μ€ν", "νν μ€ν", "μλͺ
κ³Όν μ€ν", "μ§κ΅¬κ³Όν μ€ν",
# 체μ‘/μμ
"체μ‘", "μ΄λκ³Ό 건κ°", "μ€ν¬μΈ μν", "μ€ν¬μΈ λ¬Έν", "μ€ν¬μΈ κ³Όν",
"μμ
", "λ―Έμ ", "μ°κ·Ή", "μμ
μ°μ£Ό", "μμ
κ°μκ³Ό λΉν", "λ―Έμ μ°½μ", "λ―Έμ κ°μκ³Ό λΉν",
# κΈ°μ /κ°μ /μ 2μΈκ΅μ΄/κ΅μ
"μ 보", "κΈ°μ Β·κ°μ ", "μΌλ³Έμ΄β
", "μΌλ³Έμ΄β
‘", "μ€κ΅μ΄β
", "μ€κ΅μ΄β
‘",
"νλ¬Έβ
", "νλ¬Έβ
‘", "μ¬λ¦¬ν", "보건", "κ΅μ‘ν", "μ² ν", "λ
Όλ¦¬ν",
]
# κ³Όλͺ©λͺ
β κ΅κ³Όμμ λ§€ν
SUBJECT_TO_AREA = {
# κ΅μ΄
"κ΅μ΄": "κ΅μ΄", "λ¬Έν": "κ΅μ΄", "λ
μ": "κ΅μ΄", "νλ²κ³Ό μλ¬Έ": "κ΅μ΄",
"μΈμ΄μ 맀체": "κ΅μ΄", "μ¬ν κ΅μ΄": "κ΅μ΄",
# μν
"μν": "μν", "μνβ
": "μν", "μνβ
‘": "μν", "λ―Έμ λΆ": "μν",
"νλ₯ κ³Ό ν΅κ³": "μν", "κΈ°ν": "μν", "μ¬ν μνβ
": "μν", "μ¬ν μνβ
‘": "μν",
# μμ΄
"μμ΄": "μμ΄", "μμ΄β
": "μμ΄", "μμ΄β
‘": "μμ΄", "μμ΄ λ
ν΄μ μλ¬Έ": "μμ΄",
"μμ΄ νν": "μμ΄", "μ€μ© μμ΄": "μμ΄",
# νκ΅μ¬
"νκ΅μ¬": "νκ΅μ¬",
# μ¬ν
"ν΅ν©μ¬ν": "μ¬ν(μμ¬/λλν¬ν¨)", "νκ΅μ§λ¦¬": "μ¬ν(μμ¬/λλν¬ν¨)",
"μΈκ³μ§λ¦¬": "μ¬ν(μμ¬/λλν¬ν¨)", "λμμμμ¬": "μ¬ν(μμ¬/λλν¬ν¨)",
"μΈκ³μ¬": "μ¬ν(μμ¬/λλν¬ν¨)", "κ²½μ ": "μ¬ν(μμ¬/λλν¬ν¨)",
"μ μΉμ λ²": "μ¬ν(μμ¬/λλν¬ν¨)", "μ¬νΒ·λ¬Έν": "μ¬ν(μμ¬/λλν¬ν¨)",
"μ€λ¦¬μ μ¬μ": "μ¬ν(μμ¬/λλν¬ν¨)", "μνκ³Ό μ€λ¦¬": "μ¬ν(μμ¬/λλν¬ν¨)",
"μ¬νμ§λ¦¬": "μ¬ν(μμ¬/λλν¬ν¨)",
# κ³Όν
"ν΅ν©κ³Όν": "κ³Όν", "κ³Όννꡬμ€ν": "κ³Όν",
"물리νβ
": "κ³Όν", "물리νβ
‘": "κ³Όν",
"ννβ
": "κ³Όν", "ννβ
‘": "κ³Όν",
"μλͺ
κ³Όνβ
": "κ³Όν", "μλͺ
κ³Όνβ
‘": "κ³Όν",
"μ§κ΅¬κ³Όνβ
": "κ³Όν", "μ§κ΅¬κ³Όνβ
‘": "κ³Όν",
"κ³ κΈ λ¬Όλ¦¬ν": "κ³Όν", "κ³ κΈ νν": "κ³Όν",
"κ³ κΈ μλͺ
κ³Όν": "κ³Όν", "κ³ κΈ μ§κ΅¬κ³Όν": "κ³Όν",
# 체μ‘
"체μ‘": "체μ‘", "μ΄λκ³Ό 건κ°": "체μ‘", "μ€ν¬μΈ μν": "체μ‘",
"μ€ν¬μΈ λ¬Έν": "체μ‘", "μ€ν¬μΈ κ³Όν": "체μ‘",
# μμ
"μμ
": "μμ ", "λ―Έμ ": "μμ ", "μ°κ·Ή": "μμ ",
"μμ
μ°μ£Ό": "μμ ", "μμ
κ°μκ³Ό λΉν": "μμ ",
"λ―Έμ μ°½μ": "μμ ", "λ―Έμ κ°μκ³Ό λΉν": "μμ ", "맀체 λ―Έμ ": "μμ ",
# κΈ°μ Β·κ°μ /κ΅μ
"μ 보": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ", "κΈ°μ Β·κ°μ ": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ",
"μΌλ³Έμ΄β
": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ", "μ€κ΅μ΄β
": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ",
"νλ¬Έβ
": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ", "μ¬λ¦¬ν": "κΈ°μ Β·κ°μ /μ 2μΈκ΅μ΄/νλ¬Έ/κ΅μ",
}