parsing-test / src /template /template_parser.py
jaehyun-vyarth's picture
Update src/template/template_parser.py
ebc093f verified
Raw History Blame Contribute Delete
95.8 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] = {} self._pdf = None # pdfplumber 객체 μ°Έμ‘° 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, float, list]]: """μ„Ήμ…˜ ν…Œμ΄λΈ”μ„ (page_num, table_y, table_data) νŠœν”Œ 리슀트둜 λ°˜ν™˜ κ΅κ³Όν•™μŠ΅λ°œλ‹¬μƒν™© νŒŒμ‹± μ‹œ νŽ˜μ΄μ§€λ³„ ν•™λ…„ λ§€ν•‘ 및 Yμ’Œν‘œ 기반 마컀 μ μš©μ— μ‚¬μš© """ 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_y, 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: """창의적 μ²΄ν—˜ν™œλ™ νŒŒμ‹± - Yμ’Œν‘œ 기반 μ…€ bbox 방식 κΈ°μ‘΄ prefix ν…μŠ€νŠΈ 검색 λ°©μ‹μ˜ 문제점(μ˜μ—­ 경계 ν˜Όλ™, 헀더 ν˜Όμž…) 해결을 μœ„ν•΄ μ…€ bbox 기반 ν•™λ…„/μ˜μ—­ λ§€ν•‘ + chars 기반 λ‚΄μš© μΆ”μΆœ λ°©μ‹μœΌλ‘œ μ „ν™˜ """ result = { "autonomous": [], # μžμœ¨ν™œλ™ "club": [], # λ™μ•„λ¦¬ν™œλ™ "career": [], # μ§„λ‘œν™œλ™ "volunteer": [], # λ΄‰μ‚¬ν™œλ™ } # (grade, area) -> entry λ§€ν•‘ (νŽ˜μ΄μ§€ λ„˜κΉ€ λ³‘ν•©μš©) entries = {} boundary = self.section_boundaries.get("창체") if not boundary: return result with pdfplumber.open(self.pdf_path) as pdf: for page_num in range(boundary.start_page, min(boundary.end_page + 1, len(pdf.pages))): page = pdf.pages[page_num] tables = page.find_tables() words = page.extract_words() chars = page.chars for table in tables: table_data = table.extract() table_text = ' '.join(str(c) for row in table_data[: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_data)) continue # 창체 ν…Œμ΄λΈ” 확인 (띄어쓰기 λ³€ν˜• 처리) normalized = table_text.replace(' ', '') is_creative = ('창의적' in normalized or 'μžμœ¨ν™œλ™' in normalized or 'λ™μ•„λ¦¬ν™œλ™' in normalized or 'μ§„λ‘œν™œλ™' in normalized) is_volunteer = 'λ΄‰μ‚¬ν™œλ™' in normalized or 'λˆ„κ³„μ‹œκ°„' in normalized if not is_creative or is_volunteer: continue bbox = table.bbox cells = table.cells # 1. ν•™λ…„ μ…€ μ°ΎκΈ° (첫 번째 컬럼, 1/2/3 κ°’) grade_cells = [] # [(grade, y0, y1)] for cell in cells: x0, y0, x1, y1 = cell if x0 < bbox[0] + 50: # 첫 번째 컬럼 try: cropped = page.within_bbox(cell) text = (cropped.extract_text() or '').strip() if text in ['1', '2', '3']: grade_cells.append((int(text), y0, y1)) except Exception: pass # 2. μ˜μ—­ μ…€κ³Ό λ‚΄μš© μ…€ μ°ΎκΈ° area_markers = [] for cell in cells: x0, y0, x1, y1 = cell try: cropped = page.within_bbox(cell) text = (cropped.extract_text() or '').strip() if text in ['μžμœ¨ν™œλ™', 'λ™μ•„λ¦¬ν™œλ™', 'μ§„λ‘œν™œλ™']: # λ‚΄μš© μ…€ X μ‹œμž‘ μœ„μΉ˜ μ°ΎκΈ° content_cell_x0 = None for cell2 in cells: cx0, cy0, cx1, cy1 = cell2 if (cy1 > y0 + 5 and cy0 < y1 - 5 and cx0 > x1 and cx1 > bbox[2] - 50): if content_cell_x0 is None or cx0 < content_cell_x0: content_cell_x0 = cx0 if content_cell_x0 is None: content_cell_x0 = x1 + 30 # ν•™λ…„ λ§€ν•‘ (μ˜μ—­ μ…€ 쀑심점 κΈ°μ€€) area_center_y = (y0 + y1) / 2 matched_grade = None for grade, gy0, gy1 in grade_cells: if gy0 <= area_center_y <= gy1: matched_grade = grade break # μ‹œκ°„ μ°ΎκΈ° hours = 0 for w2 in words: if (y0 <= w2['top'] <= y1 and w2['text'].isdigit() and 5 <= int(w2['text']) <= 200): hours = int(w2['text']) break # 희망뢄야 μ°ΎκΈ° (μ§„λ‘œν™œλ™) desired_field = None content_start_y = y0 if 'μ§„λ‘œ' in text: for cell2 in cells: cx0, cy0, cx1, cy1 = cell2 if cy0 >= y0 and cy1 <= y1: try: cropped2 = page.within_bbox(cell2) cell_text = (cropped2.extract_text() or '').strip() if cell_text == '희망뢄야': for cell3 in cells: dx0, dy0, dx1, dy1 = cell3 if (abs(dy0 - cy0) < 5 and dx0 >= cx1 and abs(dy1 - cy1) < 5 and dx0 != cx0): cropped3 = page.within_bbox(cell3) desired_field = (cropped3.extract_text() or '').strip() break content_start_y = cy1 break except Exception: pass area_markers.append({ 'area': text.replace('ν™œλ™', ''), 'grade': matched_grade, 'y0': y0, 'y1': y1, 'content_start_y': content_start_y, 'content_x': content_cell_x0, 'hours': hours, 'desired_field': desired_field, }) except Exception: pass area_markers.sort(key=lambda x: x['y0']) # 3. 각 μ˜μ—­μ˜ λ‚΄μš© μΆ”μΆœ (chars 기반) for marker in area_markers: if marker['grade'] is None: continue start_y = marker['content_start_y'] end_y = marker['y1'] content_x = marker['content_x'] content_chars = [ c for c in chars if start_y <= c['top'] < end_y and c['x0'] >= content_x and bbox[0] <= c['x0'] <= bbox[2] ] # Y별 κ·Έλ£Ήν™” β†’ X순 μ •λ ¬ β†’ ν…μŠ€νŠΈ μ‘°ν•© lines = {} for c in content_chars: y = round(c['top'], 0) if y not in lines: lines[y] = [] lines[y].append(c) content_parts = [] for y in sorted(lines.keys()): line_text = ''.join(c['text'] for c in sorted(lines[y], key=lambda x: x['x0'])) content_parts.append(line_text) content = ' '.join(content_parts).strip() # κΈ°μ‘΄ ν•­λͺ©κ³Ό 병합 (νŽ˜μ΄μ§€ λ„˜κΉ€ 처리) key = (marker['grade'], marker['area']) if key in entries: entries[key]['details'] += ' ' + content else: entries[key] = { 'grade': marker['grade'], 'area': marker['area'], 'hours': marker['hours'], 'details': content, 'desired_field': marker['desired_field'], } # κ²°κ³Ό 정리 for (grade, area), entry in entries.items(): details = entry['details'].strip() restricted = 'κ³΅κ³΅κΈ°κ΄€μ˜ μ •λ³΄κ³΅κ°œμ— κ΄€ν•œ 법λ₯ ' in details if restricted: details = "λΉ„κ³΅κ°œ" if area == '자율': result['autonomous'].append({ 'grade': entry['grade'], 'hours': entry['hours'], 'details': details, 'restricted': restricted, }) elif area == '동아리': club_name, cleaned_details = self._extract_club_name(details) result['club'].append({ 'grade': entry['grade'], 'hours': entry['hours'], 'club_name': club_name if not restricted else None, 'details': cleaned_details if not restricted else "λΉ„κ³΅κ°œ", 'restricted': restricted, }) elif area == 'μ§„λ‘œ': desired_field = entry['desired_field'] if desired_field and '곡곡기관' in desired_field: desired_field = None result['career'].append({ 'grade': entry['grade'], 'hours': entry['hours'], 'desired_field': desired_field if not restricted else None, 'details': details, 'restricted': restricted, }) # ν•™λ…„μˆœ μ •λ ¬ for key in ['autonomous', 'club', 'career']: result[key].sort(key=lambda x: x['grade']) return result def _extract_club_name(self, content: str) -> tuple: """동아리λͺ… μΆ”μΆœ - (동아리λͺ…)(μ‹œκ°„) νŒ¨ν„΄ Returns: (club_name, cleaned_content) """ match = re.match(r'^\(([^)]+(?:\([^)]+\))?[^)]*)\)\s*\((\d+)μ‹œκ°„\)\s*', content) if match: club_name = match.group(1) cleaned = content[match.end():].strip() return club_name, cleaned return None, content def _normalize_for_matching(self, text: str) -> str: """ν…μŠ€νŠΈ μ •κ·œν™” (특수문자 차이 ν•΄κ²°) PDF의 ν…Œμ΄λΈ” μΆ”μΆœκ³Ό ν…μŠ€νŠΈ μΆ”μΆœμ—μ„œ νŠΉμˆ˜λ¬Έμžκ°€ λ‹€λ₯΄κ²Œ λ‚˜μ˜¬ 수 있음: - κ΄„ν˜Έ: () vs οΌˆοΌ‰ vs {} - 점: . vs . vs ・ - 곡백 차이 ν•œκΈ€κ³Ό 숫자만 μΆ”μΆœν•˜μ—¬ 비ꡐ """ # ν•œκΈ€, 영문, 숫자만 μΆ”μΆœ normalized = re.sub(r'[^κ°€-힣a-zA-Z0-9]', '', text) return normalized def _find_prefix_in_text(self, prefix: str, full_text: str) -> int: """prefix의 μœ„μΉ˜λ₯Ό full_textμ—μ„œ μ°ΎκΈ° (μ •κ·œν™” 비ꡐ) Returns: μ‹œμž‘ μœ„μΉ˜ (μ°Ύμ§€ λͺ»ν•˜λ©΄ -1) """ if not prefix: return -1 # μ •κ·œν™”λœ prefix (ν•œκΈ€+숫자만) norm_prefix = self._normalize_for_matching(prefix[:20]) if len(norm_prefix) < 5: return -1 # full_textλ₯Ό μŠ€μΊ”ν•˜λ©΄μ„œ μ •κ·œν™” 비ꡐ # μŠ¬λΌμ΄λ”© μœˆλ„μš°λ‘œ 검색 for i in range(len(full_text) - 10): # ν˜„μž¬ μœ„μΉ˜λΆ€ν„° 40자 정도 μΆ”μΆœν•˜μ—¬ μ •κ·œν™” window = full_text[i:i + 40] norm_window = self._normalize_for_matching(window) # prefixκ°€ window에 ν¬ν•¨λ˜λŠ”μ§€ 확인 if norm_prefix[:10] in norm_window: return i return -1 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 _get_page_markers_with_y(self, page_num: int) -> list[tuple[float, str, Any]]: """νŽ˜μ΄μ§€μ—μ„œ ν•™λ…„/μΉ΄ν…Œκ³ λ¦¬ λ§ˆμ»€μ™€ Yμ’Œν‘œ μΆ”μΆœ Returns: [(y_coord, marker_type, marker_value), ...] marker_type: "grade" or "category" marker_value: 1/2/3 for grade, "일반"/"μ§„λ‘œμ„ νƒ"/"체윑예술" for category """ if page_num >= len(self.pages): return [] markers = [] # pdfplumber둜 νŽ˜μ΄μ§€ λ‹€μ‹œ μ—΄μ–΄μ„œ chars μ ‘κ·Ό with pdfplumber.open(self.pdf_path) as pdf: if page_num >= len(pdf.pages): return [] page = pdf.pages[page_num] chars = page.chars if not chars: return [] current_y = None current_text = "" for char in chars: # Yμ’Œν‘œκ°€ λ°”λ€Œλ©΄ 이전 쀄 처리 if current_y is None or abs(char['top'] - current_y) > 5: if current_text.strip(): # ν•™λ…„ 마컀 확인 if "[1ν•™λ…„]" in current_text: markers.append((current_y, "grade", 1)) elif "[2ν•™λ…„]" in current_text: markers.append((current_y, "grade", 2)) elif "[3ν•™λ…„]" in current_text: markers.append((current_y, "grade", 3)) # μΉ΄ν…Œκ³ λ¦¬ 마컀 확인 if "<μ§„λ‘œ 선택" in current_text or "<μ§„λ‘œμ„ νƒ" in current_text or "μ§„λ‘œ 선택 κ³Όλͺ©" in current_text: markers.append((current_y, "category", "μ§„λ‘œμ„ νƒ")) elif "<체윑" in current_text or "μ²΄μœ‘γ†μ˜ˆμˆ " in current_text or "체윑·예술" in current_text: markers.append((current_y, "category", "체윑예술")) current_y = char['top'] current_text = char['text'] else: current_text += char['text'] # λ§ˆμ§€λ§‰ 쀄 처리 if current_text.strip(): if "[1ν•™λ…„]" in current_text: markers.append((current_y, "grade", 1)) elif "[2ν•™λ…„]" in current_text: markers.append((current_y, "grade", 2)) elif "[3ν•™λ…„]" in current_text: markers.append((current_y, "grade", 3)) if "<μ§„λ‘œ 선택" in current_text or "<μ§„λ‘œμ„ νƒ" in current_text or "μ§„λ‘œ 선택 κ³Όλͺ©" in current_text: markers.append((current_y, "category", "μ§„λ‘œμ„ νƒ")) elif "<체윑" in current_text or "μ²΄μœ‘γ†μ˜ˆμˆ " in current_text or "체윑·예술" in current_text: markers.append((current_y, "category", "체윑예술")) return sorted(markers, key=lambda x: x[0]) def _find_personal_remarks_by_y(self) -> list[dict]: """Yμ’Œν‘œ 기반 κ°œμΈμ„ΈνŠΉ 감지 2λ°° 쀄간격 ν›„ κ³Όλͺ©λͺ…: νŒ¨ν„΄ 없이 μ‹œμž‘ν•˜λŠ” ν…μŠ€νŠΈλ₯Ό κ°œμΈμ„ΈνŠΉμœΌλ‘œ 감지 Returns: [{"page": int, "y": float, "text": str, "grade": int}, ...] """ results = [] boundary = self.section_boundaries.get("ꡐ과") if not boundary: return results with pdfplumber.open(self.pdf_path) as pdf: # ν˜„μž¬ ν•™λ…„ 좔적 (νŽ˜μ΄μ§€ κ°„ μœ μ§€) current_grade = 1 for page_num in range(boundary.start_page, boundary.end_page + 1): if page_num >= len(pdf.pages): continue page = pdf.pages[page_num] chars = page.chars if not chars: continue # 라인별 κ·Έλ£Ήν™” lines_by_y = {} for char in chars: y = round(char['top'], 1) if y not in lines_by_y: lines_by_y[y] = '' lines_by_y[y] += char['text'] sorted_ys = sorted(lines_by_y.keys()) if len(sorted_ys) < 3: continue # 평균 쀄간격 계산 (일반적인 λ²”μœ„: 8-20) gaps = [sorted_ys[i] - sorted_ys[i-1] for i in range(1, len(sorted_ys))] normal_gaps = [g for g in gaps if 8 < g < 20] if not normal_gaps: continue avg_gap = sum(normal_gaps) / len(normal_gaps) # ν•™λ…„ 마컀 확인 및 κ°œμΈμ„ΈνŠΉ 감지 prev_y = sorted_ys[0] for y in sorted_ys[1:]: text = lines_by_y[y].strip() # ν•™λ…„ 마컀 μ—…λ°μ΄νŠΈ if "[1ν•™λ…„]" in text: current_grade = 1 elif "[2ν•™λ…„]" in text: current_grade = 2 elif "[3ν•™λ…„]" in text: current_grade = 3 gap = y - prev_y # 2λ°° 간격 감지 (1.7λ°° 이상) if gap > avg_gap * 1.7: # κ³Όλͺ©λͺ…: νŒ¨ν„΄μœΌλ‘œ μ‹œμž‘ν•˜λŠ”μ§€ 확인 if not self._starts_with_subject_pattern(text): # λ…Έμ΄μ¦ˆ 필터링 if self._is_valid_personal_remark(text): results.append({ "page": page_num, "y": y, "text": text, "grade": current_grade, }) prev_y = y return results def _starts_with_subject_pattern(self, text: str) -> bool: """ν…μŠ€νŠΈκ°€ 'κ³Όλͺ©λͺ…:' νŒ¨ν„΄μœΌλ‘œ μ‹œμž‘ν•˜λŠ”μ§€ 확인""" import re text = text.strip() # (1ν•™κΈ°), (2ν•™κΈ°) 접두사 제거 text = re.sub(r'^\(?\dν•™κΈ°\)?\s*', '', text) # 콜둠 μ•žμ˜ ν…μŠ€νŠΈκ°€ κ³Όλͺ©λͺ…인지 확인 colon_match = re.match(r'^([^::]{1,25})[::]', text) if colon_match: potential_subject = colon_match.group(1).strip() # κ³Όλͺ©λͺ… νŠΉμ„±: ν•œκΈ€ μœ„μ£Ό, 둜마숫자 포함 κ°€λŠ₯ if re.match(r'^[κ°€-힣\sΒ·β… β…‘]+$', potential_subject): return True return False def _is_valid_personal_remark(self, text: str) -> bool: """κ°œμΈμ„ΈνŠΉμœΌλ‘œ μœ νš¨ν•œ ν…μŠ€νŠΈμΈμ§€ 확인 (λ…Έμ΄μ¦ˆ 필터링)""" import re if len(text) < 30: return False # λ…Έμ΄μ¦ˆ νŒ¨ν„΄ noise_patterns = [ r'^κ³Όλͺ©\s+μ„Έ\s*λΆ€', # ν…Œμ΄λΈ” 헀더 r'고등학ꡐ.*\d{4}λ…„', # νŽ˜μ΄μ§€ ν‘Έν„° r'^\d+\s*/\s*\d+', # νŽ˜μ΄μ§€ 번호 (1 / 21) r'^ν•™κΈ°\s+ꡐ과', # 성적 ν…Œμ΄λΈ” 헀더 r'^<μ§„λ‘œ\s*선택', # μΉ΄ν…Œκ³ λ¦¬ 마컀 r'^<체윑', # μΉ΄ν…Œκ³ λ¦¬ 마컀 ] for pattern in noise_patterns: if re.search(pattern, text): return False return True def _get_full_text_from_y(self, page_num: int, start_y: float) -> str: """νŠΉμ • Yμ’Œν‘œλΆ€ν„° λ‹€μŒ 2λ°° 간격 λ˜λŠ” κ³Όλͺ© μ‹œμž‘κΉŒμ§€μ˜ 전체 ν…μŠ€νŠΈ μΆ”μΆœ""" with pdfplumber.open(self.pdf_path) as pdf: if page_num >= len(pdf.pages): return "" page = pdf.pages[page_num] chars = page.chars if not chars: return "" # 라인별 κ·Έλ£Ήν™” lines_by_y = {} for char in chars: y = round(char['top'], 1) if y not in lines_by_y: lines_by_y[y] = '' lines_by_y[y] += char['text'] sorted_ys = sorted(lines_by_y.keys()) # 평균 쀄간격 계산 gaps = [sorted_ys[i] - sorted_ys[i-1] for i in range(1, len(sorted_ys))] normal_gaps = [g for g in gaps if 8 < g < 20] if not normal_gaps: return lines_by_y.get(start_y, "") avg_gap = sum(normal_gaps) / len(normal_gaps) # start_yλΆ€ν„° λ‹€μŒ κ΅¬λΆ„μ κΉŒμ§€ ν…μŠ€νŠΈ μˆ˜μ§‘ collecting = False result_lines = [] prev_y = None for y in sorted_ys: if abs(y - start_y) < 2: # start_y μ°ΎκΈ° collecting = True if collecting: text = lines_by_y[y].strip() # μ’…λ£Œ 쑰건: 2λ°° 간격 + κ³Όλͺ© νŒ¨ν„΄ λ˜λŠ” λ…Έμ΄μ¦ˆ if prev_y is not None: gap = y - prev_y if gap > avg_gap * 1.7: # λ‹€μŒ 블둝이 κ³Όλͺ©μ΄λ©΄ μ’…λ£Œ if self._starts_with_subject_pattern(text): break # λ…Έμ΄μ¦ˆλ©΄ μ’…λ£Œ if not self._is_valid_personal_remark(text): break # νŽ˜μ΄μ§€ ν‘Έν„° μ œμ™Έ if "고등학ꡐ" in text and "νŽ˜μ΄μ§€" not in text: if len(text) < 50: break result_lines.append(text) prev_y = y return " ".join(result_lines) def _get_table_y_start(self, page_num: int, table_idx: int) -> float: """ν…Œμ΄λΈ”μ˜ μ‹œμž‘ Yμ’Œν‘œ λ°˜ν™˜""" if page_num >= len(self.pdf.pages): return 0 page = self.pdf.pages[page_num] tables = page.find_tables() if table_idx < len(tables): return tables[table_idx].bbox[1] # top Y coordinate return 0 def _parse_academics(self) -> dict: """κ΅κ³Όν•™μŠ΅λ°œλ‹¬μƒν™© νŒŒμ‹± - Yμ’Œν‘œ 기반 마컀 좔적""" text = self._get_section_text("ꡐ과") tables_with_page = self._get_section_tables_with_page("ꡐ과") result = { "by_year": [], "subject_remarks": [], } # νŽ˜μ΄μ§€λ³„ 마컀 μΊμ‹œ (Yμ’Œν‘œ 포함) page_markers_cache = {} boundary = self.section_boundaries.get("ꡐ과") if boundary: for page_num in range(boundary.start_page, boundary.end_page + 1): page_markers_cache[page_num] = self._get_page_markers_with_y(page_num) # μ „μ—­ μƒνƒœ (νŽ˜μ΄μ§€/ν…Œμ΄λΈ” κ°„ μœ μ§€) 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_y, table in tables_with_page: if not table: continue # Yμ’Œν‘œ 기반 마컀 적용 (ν…Œμ΄λΈ” μœ„μ— μžˆλŠ” λ§ˆμ»€λ“€μ˜ Yμ’Œν‘œ μˆœμ„œλ₯Ό κ³ λ €) page_markers = page_markers_cache.get(page_num, []) # ν…Œμ΄λΈ” μœ„μ— μžˆλŠ” λ§ˆμ»€λ“€ 쀑 λ§ˆμ§€λ§‰ κ²ƒλ“€λ§Œ 적용 last_grade_y = None last_grade_value = None last_category_y = None last_category_value = None for marker_y, marker_type, marker_value in page_markers: if marker_y < table_y: # λ§ˆμ»€κ°€ ν…Œμ΄λΈ” μœ„μ— 있음 if marker_type == "grade": last_grade_y = marker_y last_grade_value = marker_value elif marker_type == "category": last_category_y = marker_y last_category_value = marker_value # ν•™λ…„ 마컀 적용 if last_grade_value is not None and current_grade != last_grade_value: current_grade = last_grade_value # 학년이 λ°”λ€Œλ©΄ μΉ΄ν…Œκ³ λ¦¬λ₯Ό 일반으둜 리셋 (μΉ΄ν…Œκ³ λ¦¬ λ§ˆμ»€κ°€ ν•™λ…„ 마컀 이후에 μ—†μœΌλ©΄) if last_category_y is None or last_category_y < last_grade_y: current_category = "일반" # μΉ΄ν…Œκ³ λ¦¬ λ§ˆμ»€κ°€ ν•™λ…„ λ§ˆμ»€λ³΄λ‹€ 뒀에 있으면 적용 if last_category_value is not None: if last_grade_y is None or last_category_y > last_grade_y: current_category = last_category_value # 헀더 ν–‰λ“€μ—μ„œ 정보 μΆ”μΆœ (ν…Œμ΄λΈ” μžμ²΄μ— λ§ˆμ»€κ°€ μžˆλŠ” 경우) 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: # Yμ’Œν‘œ 기반으둜 κ²°μ •λœ μΉ΄ν…Œκ³ λ¦¬ μ‚¬μš© 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, }) # νŽ˜μ΄μ§€ λ„˜κΉ€μœΌλ‘œ 끊긴 μ„ΈνŠΉ 병합 result["subject_remarks"] = self._merge_orphan_remarks(result["subject_remarks"]) # Yμ’Œν‘œ 기반 κ°œμΈμ„ΈνŠΉ 감지 및 μΆ”κ°€ personal_remarks_by_y = self._find_personal_remarks_by_y() existing_contents = {r.get("content", "")[:50] for r in result["subject_remarks"]} for pr in personal_remarks_by_y: # 쀑볡 λ°©μ§€: 이미 νŒŒμ‹±λœ λ‚΄μš©κ³Ό κ²ΉμΉ˜μ§€ μ•ŠλŠ”μ§€ 확인 pr_content_prefix = pr["text"][:50] if pr_content_prefix not in existing_contents: # 전체 ν…μŠ€νŠΈ κ°€μ Έμ˜€κΈ° (Yμ’Œν‘œ μ΄ν›„μ˜ 연속 ν…μŠ€νŠΈ) full_text = self._get_full_text_from_y(pr["page"], pr["y"]) if full_text and len(full_text) > 50: result["subject_remarks"].append({ "grade": pr["grade"], "semester": None, "subject": None, # κ°œμΈμ„ΈνŠΉ "content": full_text.strip(), "restricted": False, }) existing_contents.add(full_text[:50]) 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 _merge_orphan_remarks(self, remarks: list[dict]) -> list[dict]: """νŽ˜μ΄μ§€ λ„˜κΉ€μœΌλ‘œ 끊긴 μ„ΈνŠΉ 병합 subject=None인 ν•­λͺ© 쀑 연속 νŒ¨ν„΄(예: "μ—μ„œ", "κ³ ", "ν•˜λ©°" λ“±μœΌλ‘œ μ‹œμž‘)을 κ°μ§€ν•˜μ—¬ 이전 κ³Όλͺ©μ˜ λ‚΄μš©μ— λ³‘ν•©ν•©λ‹ˆλ‹€. κ°œμ„ λœ 둜직: - orphan 이전에 λ‚˜μ˜¨ κ³Όλͺ© 쀑 λΆˆμ™„μ „ν•˜κ²Œ λλ‚˜λŠ” λ§ˆμ§€λ§‰ κ³Όλͺ©μ— 병합 - 학년이 λ‹€λ₯΄λ”라도 νŽ˜μ΄μ§€ μˆœμ„œμƒ κ°€κΉŒμš΄ 것에 병합 """ if not remarks: return remarks # 연속 νŒ¨ν„΄: λ‚΄μš©μ΄ 쑰사/μ ‘μ†μ‚¬λ‘œ μ‹œμž‘ν•˜λ©΄ 이전 λ‚΄μš©μ˜ 연속 continuation_patterns = [ "μ—μ„œ ", "μ—μ„œλŠ” ", "κ³  ", "λ©° ", "ν•˜λ©° ", "으며 ", "κ³Ό ", "와 ", "λ₯Ό ", "을 ", "λŠ” ", "은 ", "의 ", "둜 ", "으둜 ", "λ‹€λŠ” ", "λΌλŠ” ", "ν•΄ ", "ν•˜μ—¬ ", "ν•˜κ³  ", "게 ", "도둝 ", ] result = [] last_subject_idx = -1 # λ§ˆμ§€λ§‰ κ³Όλͺ© ν•­λͺ©μ˜ 인덱슀 for remark in remarks: is_orphan_continuation = False if remark.get("subject") is None and not remark.get("restricted", False): content = remark.get("content", "").strip() # 연속 νŒ¨ν„΄μœΌλ‘œ μ‹œμž‘ν•˜λŠ”μ§€ 확인 is_orphan_continuation = any(content.startswith(p) for p in continuation_patterns) if is_orphan_continuation and last_subject_idx >= 0: # λ§ˆμ§€λ§‰ κ³Όλͺ©μ˜ λ‚΄μš©μ΄ λΆˆμ™„μ „ν•˜κ²Œ λλ‚˜λŠ”μ§€ 확인 last_remark = result[last_subject_idx] last_content = last_remark.get("content", "").strip() if last_content and last_content[-1] not in ".!?。": # 병합 result[last_subject_idx]["content"] = last_content + " " + content continue result.append(remark) # κ³Όλͺ©λͺ…이 μžˆλŠ” ν•­λͺ©μ΄λ©΄ λ§ˆμ§€λ§‰ 인덱슀 μ—…λ°μ΄νŠΈ if remark.get("subject") and not remark.get("restricted", False): last_subject_idx = len(result) - 1 return result 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*\([^)]+\))', r'(?:^|\s)(자율적\s*κ΅μœ‘κ³Όμ •\s*\([^)]+\))', # λ‚ μ§œλ‘œ μ‹œμž‘ν•˜λŠ” ν™œλ™ 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]: """ν–‰λ™νŠΉμ„± 및 μ’…ν•©μ˜κ²¬ νŒŒμ‹± (νŽ˜μ΄μ§€ λ„˜κΉ€ 병합 지원) νŽ˜μ΄μ§€ λ„˜κΉ€μœΌλ‘œ 같은 ν•™λ…„ λ‚΄μš©μ΄ μ—¬λŸ¬ ν…Œμ΄λΈ”μ— λΆ„λ¦¬λœ 경우 병합 """ tables = self._get_section_tables("행동") # 학년별 λ‚΄μš© λ³‘ν•©μš© λ”•μ…”λ„ˆλ¦¬ behavior_by_grade = {} # {grade: content_list} 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 # μ’…ν•©μ˜κ²¬ (두 번째 μ»¬λŸΌλΆ€ν„°, κ°œν–‰ 제거) content = ' '.join(str(c).replace('\n', ' ') for c in row[1:] if c) content = ' '.join(content.split()) # 연속 곡백 정리 # 빈 λ‚΄μš© μ œμ™Έ if not content: continue # ν•™λ…„λ³„λ‘œ λ‚΄μš© λˆ„μ  (νŽ˜μ΄μ§€ λ„˜κΉ€ 병합) if grade not in behavior_by_grade: behavior_by_grade[grade] = [] behavior_by_grade[grade].append(content) # κ²°κ³Ό μ‘°ν•© behavior = [] for grade in sorted(behavior_by_grade.keys()): # 같은 ν•™λ…„μ˜ μ—¬λŸ¬ λ‚΄μš©μ„ 곡백으둜 μ—°κ²° combined_content = ' '.join(behavior_by_grade[grade]) combined_content = ' '.join(combined_content.split()) # 연속 곡백 정리 behavior.append({ "grade": grade, "content": combined_content, "restricted": "μ œκ³΅ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€" in combined_content, }) 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μ™Έκ΅­μ–΄/ν•œλ¬Έ/ꡐ양",
}
@dataclass