from pathlib import Path
from re import sub
from unicodedata import normalize

from pypdf import PdfReader


def main():
    srs_dirs = [dir for dir in Path("data").iterdir() if dir.is_dir()]

    for srs_dir in srs_dirs:
        pdf_path = srs_dir / "original.pdf"
        assert pdf_path.is_file(), f"Not found: {pdf_path}"

        reader = PdfReader(pdf_path)
        text_fragments = []
        for page in reader.pages:
            extracted_text = page.extract_text()
            text_fragments.append(extracted_text)

        text = "\n".join(text_fragments)
        text = normalize("NFKC", text)
        text = normalize_whitespaces_except_newline(text)
        text = remove_excess_newlines(text)

        output_path = srs_dir / "extracted.txt"
        output_path.write_text(text, encoding="utf-8")


def normalize_whitespaces_except_newline(txt: str) -> str:
    return sub(r"([^\S\n])\1", r"\1", txt)


def remove_excess_newlines(txt: str) -> str:
    return sub(r"(\s*\n){3,}", r"\n\n", txt)


if __name__ == "__main__":
    main()
