# -*- coding: utf-8 -*-
import sys, io, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
BASE = r"D:\work buddy\2026-11-05-14-15-52\cache"
CODE = re.compile(r"\b[A-Z]{2,8}[\s\-]?\d{3,4}[A-Z]?\b")
for name in ["r347-emory-ms-req", "r347-rice-mcs", "r347-rice-mds", "r347-dart-grad"]:
    t = open(BASE + "\\" + name + ".html", encoding="utf-8", errors="replace").read()
    t = re.sub(r"<script.*?</script>|<style.*?</style>", " ", t, flags=re.S)
    txt = re.sub(r"<[^>]+>", "\n", t)
    txt = re.sub(r"&nbsp;", " ", txt)
    txt = re.sub(r"\n{2,}", "\n", txt)
    codes = CODE.findall(txt)
    uniq = sorted(set(c.strip() for c in codes))
    print("=== ", name, "codes", len(uniq), uniq[:30])
    # print body segments around 'require' / 'curriculum' / 'course'
    keep = [ln.strip() for ln in txt.split("\n") if len(ln.strip()) > 30]
    body = "\n".join(keep)
    idx = body.lower().find("curriculum")
    print(body[:120].replace("\n", " "))
    print("--- excerpts")
    for kw in ["degree requirement", "required course", "core course", "curriculum", "credit"]:
        i = body.lower().find(kw)
        if i > 0:
            print(f"[{kw}]", body[max(0,i-180):i+500].replace("\n", " ")[:700])
            print()
