# -*- coding: utf-8 -*-
import json, io, sys, re, os, html
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
base = r"D:\work buddy\2026-11-05-14-15-52"
raw = {d["title"]: d for d in json.load(open(os.path.join(base,"raw_uchicago.json"),encoding="utf-8"))}

# 学部中文名
UNIT_CN = {
 "Physical Sciences":"物理科学部",
 "Biological Sciences":"生物科学部",
 "Social Sciences":"社会科学部",
 "Arts & Humanities":"人文与艺术学部",
 "Harris":"哈里斯公共政策学院",
 "PME":"普利兹克分子工程学院",
 "Law":"法学院",
 "CFS":"皇冠家庭社会政策实践学院",
 "Divinity":"神学院",
 "Booth":"布斯商学院",
}
def unit_cn(u):
    for k,v in UNIT_CN.items():
        if u.startswith(k): return v
    return u

# 保留硕士映射：en标题 -> (中文名, 规范学位, disc, slug)
KEEP = {
 "Applied Data Science": ("应用数据科学", "MS", "cs", "applied-data-science"),
 "Biomedical Informatics": ("生物医学信息学", "MS", "med", "biomedical-informatics"),
 "Biomedical Sciences": ("生物医学科学", "MS", "sci", "biomedical-sciences"),
 "Chemistry": ("化学", "MS", "sci", "chemistry-ms"),
 "Computational Analysis and Public Policy": ("计算分析与公共政策", "MS", "soc", "computational-analysis-public-policy"),
 "Computational and Applied Mathematics": ("计算与应用数学", "MS", "sci", "computational-applied-math"),
 "Computational Social Science": ("计算社会科学", "MA", "soc", "computational-social-science"),
 "Computer Science": ("计算机科学", "MS", "cs", "cs"),
 "Data Science": ("数据科学", "MS", "cs", "data-science"),
 "Digital Studies of Language, Culture, and History": ("数字语言、文化与历史研究", "MA", "art", "digital-studies"),
 "Economics (SSD)": ("经济学", "MA", "soc", "economics"),
 "Environmental Science": ("环境科学", "MS", "sci", "environmental-science"),
 "Financial Mathematics": ("金融数学", "MS", "sci", "financial-mathematics"),
 "International Relations": ("国际关系", "MA", "soc", "international-relations"),
 "Law/Legal Studies": ("法律研究（LLM）", "LLM", "soc", "legal-studies-llm"),
 "Master of Arts Program in the Humanities": ("人文研究文学硕士（MAPH）", "MA", "art", "maph"),
 "Master of Arts Program in the Social Sciences": ("社会科学研究文学硕士（MAPSS）", "MA", "soc", "mapss"),
 "Master of Engineering": ("工程硕士（分子工程）", "MEng", "eng", "master-engineering"),
 "Master's in Applied AI": ("应用人工智能硕士", "MS", "cs", "applied-ai"),
 "Middle Eastern Studies": ("中东研究", "MA", "art", "middle-eastern-studies"),
 "Precision Health": ("精准健康", "MS", "med", "precision-health"),
 "Public Health": ("公共卫生硕士", "MPH", "med", "public-health-mph"),
 "Public Policy": ("公共政策硕士（MPP）", "MPP", "soc", "public-policy-mpp"),
 "Public Policy with Certificate in Research Methods": ("公共政策硕士（研究方法证书方向）", "MA", "soc", "public-policy-research-methods"),
 "Religious Studies": ("宗教研究文学硕士", "MA", "art", "religious-studies"),
 "Social Sector Leadership and Nonprofit Management": ("社会部门领导力与非营利管理", "MA", "biz", "social-sector-leadership"),
 "Statistics": ("统计学", "MS", "cs", "statistics"),
 "Threat and Response Management": ("威胁与响应管理", "MS", "med", "threat-response-management"),
 "Visual Arts": ("视觉艺术", "MFA", "art", "visual-arts"),
}

rows, pages = [], []
missing = []
for title,(cn,deg,disc,slug) in KEEP.items():
    d = raw.get(title)
    if not d:
        missing.append(title); continue
    det = f"details/uchicago-{slug}.html"
    college = unit_cn(d["unit"])
    rows.append(["uchicago", college, title, cn, deg, d["home"], disc, det])
    pages.append({"det":det,"cn":cn,"en":title,"college":college,"deg":deg,"url":d["home"]})

print("映射命中:", len(rows), " 未命中:", missing)
json.dump(rows, open(os.path.join(base,"pending_uchicago_catd.json"),"w",encoding="utf-8"), ensure_ascii=False, indent=1)
json.dump(pages, open(os.path.join(base,"pending_uchicago_pages.json"),"w",encoding="utf-8"), ensure_ascii=False, indent=1)
from collections import Counter
print("disc 分布:", dict(Counter(r[6] for r in rows)))
print("学位分布:", dict(Counter(r[4] for r in rows)))
for r in rows:
    print(" ", r[4], r[6], r[3], "|", r[2])
