File size: 5,631 Bytes
e513185 6425040 e513185 6425040 e513185 6425040 e513185 d7781fc e513185 6425040 e513185 6425040 e513185 6425040 e513185 6425040 e513185 6425040 d7781fc 6425040 d7781fc 6425040 e513185 6425040 e513185 6425040 d7781fc e513185 6425040 e513185 6425040 e513185 6425040 e513185 6425040 e513185 6425040 e513185 6425040 d7781fc 6425040 e513185 6425040 e513185 6425040 d7781fc 6425040 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 | from __future__ import annotations
import os
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
import pytesseract
from pdf2image import convert_from_path
from src.utils.loader import load_document, normalize_text
@dataclass(frozen=True)
class OcrConfig:
poppler_path: str = r"C:\Program Files\Release-25.12.0-0\poppler-25.12.0\Library\bin"
tesseract_cmd: str = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
lang: str = "vie"
dpi: int = 300
raw_dir: Path = Path("data/raw")
normalized_dir: Path = Path("data/normalized")
min_text_len: int = 50
skip_if_exists_min_bytes: int = 100
def _build_runtime_cfg(cfg: OcrConfig) -> OcrConfig:
return OcrConfig(
poppler_path=os.getenv("POPPLER_PATH", cfg.poppler_path),
tesseract_cmd=os.getenv("TESSERACT_CMD", cfg.tesseract_cmd),
lang=os.getenv("TESSERACT_LANG", cfg.lang),
dpi=int(os.getenv("OCR_DPI", str(cfg.dpi))),
raw_dir=Path(os.getenv("RAW_DIR", str(cfg.raw_dir))),
normalized_dir=Path(os.getenv("NORMALIZED_DIR", str(cfg.normalized_dir))),
min_text_len=int(os.getenv("MIN_TEXT_LEN", str(cfg.min_text_len))),
skip_if_exists_min_bytes=int(
os.getenv("SKIP_IF_EXISTS_MIN_BYTES", str(cfg.skip_if_exists_min_bytes))
),
)
def _to_normalized_txt_path(src_path: Path, raw_dir: Path, normalized_dir: Path) -> Path:
rel = src_path.relative_to(raw_dir)
rel_no_suffix = rel.with_suffix(".txt")
return normalized_dir / rel_no_suffix
def _should_skip_existing(out_txt: Path, min_bytes: int) -> bool:
return out_txt.exists() and out_txt.stat().st_size >= min_bytes
def _ocr_pdf_to_txt(pdf_path: Path, out_txt: Path, cfg: OcrConfig) -> None:
pytesseract.pytesseract.tesseract_cmd = cfg.tesseract_cmd
tessdata_dir = Path(cfg.tesseract_cmd).parent / "tessdata"
os.environ["TESSDATA_PREFIX"] = str(tessdata_dir)
images = convert_from_path(
str(pdf_path),
dpi=cfg.dpi,
poppler_path=cfg.poppler_path,
)
texts: list[str] = []
for i, img in enumerate(images, start=1):
txt = pytesseract.image_to_string(img, lang=cfg.lang)
txt = normalize_text(txt)
texts.append(txt)
print(f"[OCR] {pdf_path.name}: page {i}/{len(images)} done")
final_text = "\n\n".join(t for t in texts if t.strip())
out_txt.parent.mkdir(parents=True, exist_ok=True)
out_txt.write_text(final_text, encoding="utf-8")
print(f"[OCR] wrote -> {out_txt}")
def _handle_txt_file(fp: Path, out_txt: Path) -> None:
out_txt.parent.mkdir(parents=True, exist_ok=True)
raw_text = fp.read_text(encoding="utf-8", errors="ignore")
cleaned = normalize_text(raw_text)
out_txt.write_text(cleaned, encoding="utf-8")
print(f"[NORMALIZE] TXT -> {out_txt}")
def _handle_word_file(fp: Path, out_txt: Path) -> None:
out_txt.parent.mkdir(parents=True, exist_ok=True)
extracted = load_document(fp) # loader.py sẽ tự xử lý .doc/.docx
extracted = normalize_text(extracted)
if not extracted.strip():
raise RuntimeError(f"Word file has no extractable text: {fp}")
out_txt.write_text(extracted, encoding="utf-8")
print(f"[NORMALIZE] WORD -> {out_txt}")
def _handle_pdf_file(fp: Path, out_txt: Path, cfg: OcrConfig) -> None:
out_txt.parent.mkdir(parents=True, exist_ok=True)
try:
extracted = load_document(fp)
except Exception as e:
print(f"[NORMALIZE][WARN] read PDF failed, OCR fallback: {fp} | err={e}")
extracted = ""
if len(extracted.strip()) >= cfg.min_text_len:
out_txt.write_text(extracted, encoding="utf-8")
print(f"[NORMALIZE] TEXT-PDF -> {out_txt}")
return
print(f"[NORMALIZE] OCR start -> {fp}")
_ocr_pdf_to_txt(fp, out_txt, cfg)
print(f"[NORMALIZE] OCR done -> {out_txt}")
def normalize_raw_to_normalized(cfg: Optional[OcrConfig] = None) -> None:
"""
Chuẩn hóa toàn bộ file trong data/raw sang data/normalized
Quy tắc:
- *.txt -> normalize -> normalized/*.txt
- *.pdf -> extract text; nếu không được thì OCR -> normalized/*.txt
- *.docx -> extract text -> normalized/*.txt
- *.doc -> convert/read -> normalized/*.txt
"""
cfg = _build_runtime_cfg(cfg or OcrConfig())
cfg.normalized_dir.mkdir(parents=True, exist_ok=True)
if not cfg.raw_dir.exists():
raise RuntimeError(f"Missing folder: {cfg.raw_dir}")
raw_files = [p for p in cfg.raw_dir.rglob("*") if p.is_file()]
if not raw_files:
print(f"[NORMALIZE] No files found in {cfg.raw_dir}")
return
print(f"[NORMALIZE] raw_dir={cfg.raw_dir}")
print(f"[NORMALIZE] normalized_dir={cfg.normalized_dir}")
print(f"[NORMALIZE] total_files={len(raw_files)}")
for fp in raw_files:
suffix = fp.suffix.lower()
if suffix not in {".pdf", ".txt", ".doc", ".docx"}:
print(f"[NORMALIZE] skip unsupported: {fp}")
continue
out_txt = _to_normalized_txt_path(fp, cfg.raw_dir, cfg.normalized_dir)
if _should_skip_existing(out_txt, cfg.skip_if_exists_min_bytes):
print(f"[NORMALIZE] skip existing: {out_txt}")
continue
try:
if suffix == ".txt":
_handle_txt_file(fp, out_txt)
elif suffix == ".pdf":
_handle_pdf_file(fp, out_txt, cfg)
elif suffix in {".doc", ".docx"}:
_handle_word_file(fp, out_txt)
except Exception as e:
print(f"[NORMALIZE][ERROR] failed: {fp} | err={e}") |