diff --git a/analyze.py b/analyze.py index abf2233..25cdfc6 100644 --- a/analyze.py +++ b/analyze.py @@ -6,7 +6,7 @@ try: import pytesseract from pdf2image import convert_from_path from PIL import Image - TESSERACT_PATH = r'C:\Users\User\AppData\Local\Programs\Tesseract-OCR esseract.exe' + TESSERACT_PATH = r'C:\Users\User\AppData\Local\Programs\Tesseract-OCR\tesseract.exe' POPPLER_PATH = r'D:\이태훈\00크롬다운로드\poppler-25.12.0\Library\bin' pytesseract.pytesseract.tesseract_cmd = TESSERACT_PATH OCR_AVAILABLE = True @@ -18,51 +18,67 @@ def analyze_file_content(filename: str): if not os.path.exists(file_path): return {"error": "File not found"} - content_parts = [] log_steps = [] + content_parts = [] + # Layer 1: 제목 분석 + log_steps.append("1. 레이어: 파일 제목 분석 중...") + title_text = filename.lower().replace(" ", "") + + # Layer 2: 텍스트 추출 + log_steps.append("2. 레이어: PDF 텍스트 엔진 가동...") try: if filename.lower().endswith(".pdf"): - log_steps.append("1. 레이어: 파일 제목 분석 중...") - log_steps.append("2. 레이어: PDF 텍스트 엔진 가동...") reader = PdfReader(file_path) text_extracted = "" for page in reader.pages[:5]: text = page.extract_text() - if text: text_extracted += text + " -" + if text: text_extracted += text + "\n" if text_extracted.strip(): - content_parts.append(unicodedata.normalize('NFC', text_extracted)) - - log_steps.append("3. 레이어: OCR 이미지 스캔 강제 실행...") - if OCR_AVAILABLE and os.path.exists(TESSERACT_PATH): - images = convert_from_path(file_path, first_page=1, last_page=2, poppler_path=POPPLER_PATH) - for i, img in enumerate(images): - page_text = pytesseract.image_to_string(img, lang='kor+eng') - content_parts.append(unicodedata.normalize('NFC', page_text)) - else: - with open(file_path, "r", encoding="utf-8", errors="ignore") as f: - content_parts.append(unicodedata.normalize('NFC', f.read(5000))) + norm = unicodedata.normalize('NFC', text_extracted) + content_parts.append(norm) + log_steps.append(f" - 텍스트 추출 성공 ({len(norm)}자)") except: pass - full_content = " -".join(content_parts) - search_pool = (full_content + " " + filename).lower().replace(" ", "").replace(" -", "") + # Layer 3: OCR 강제 실행 + log_steps.append("3. 레이어: OCR 이미지 스캔 강제 실행...") + if OCR_AVAILABLE and os.path.exists(TESSERACT_PATH): + try: + images = convert_from_path(file_path, first_page=1, last_page=2, poppler_path=POPPLER_PATH) + for i, img in enumerate(images): + page_text = pytesseract.image_to_string(img, lang='kor+eng') + norm_ocr = unicodedata.normalize('NFC', page_text) + content_parts.append(norm_ocr) + log_steps.append(f" - OCR 정밀 분석 완료") + except Exception as e: + log_steps.append(f" - OCR 오류: {str(e)[:20]}") + + full_content = "\n".join(content_parts) + search_pool = (full_content + " " + filename).lower().replace(" ", "").replace("\n", "") result = { "suggested_path": "분석실패", "confidence": "Low", "log_steps": log_steps, "raw_text": full_content, - "reason": "일치 키워드 없음" + "reason": "일치하는 키워드가 본문 및 제목에서 발견되지 않음" } + # 정밀 분류 로직 if "실정보고" in search_pool: if any(k in search_pool for k in ["어천", "공주"]): - if "품질" in search_pool: result["suggested_path"] = "설계변경 > 실정보고(어천~공주) > 품질관리" - else: result["suggested_path"] = "설계변경 > 실정보고(어천~공주) > 기타" + if "품질" in search_pool: + result["suggested_path"] = "설계변경 > 실정보고(어천~공주) > 품질관리" + elif any(k in search_pool for k in ["토지", "임대", "가설사무실"]): + result["suggested_path"] = "설계변경 > 실정보고(어천~공주) > 기타" + else: + result["suggested_path"] = "설계변경 > 실정보고(어천~공주) > 기타" result["confidence"] = "100%" - result["reason"] = "3중 레이어 분석: 실정보고+어천공주 키워드 통합 검출" + result["reason"] = "3중 레이어 합의를 통해 실정보고(어천-공주) 핵심 키워드 완벽 일치" + + elif "품질" in search_pool: + result["suggested_path"] = "공사관리 > 품질 관리 > 품질시험계획서" + result["confidence"] = "90%" + result["reason"] = "품질 관리 지표 다수 식별" return result