# -*- coding: utf-8 -*-
"""芝大研究型博士补录：raw_uchicago.json 中 phd 非空，剔 Law 职业法学位 / MD·MD-PhD，其余保留。"""
import json, io, sys, os, re, html
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8")
base = r"D:\work buddy\2026-11-05-14-15-52"
raw = 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)
M = {
 "Accounting": ("会计学（博士）", "biz", "accounting-phd"),
 "Anthropology": ("人类学（博士）", "soc", "anthropology-phd"),
 "Anthropology and Sociology of Religion": ("宗教人类学与社会学（博士）", "art", "anthro-sociology-religion-phd"),
 "Art History": ("艺术史（博士）", "art", "art-history-phd"),
 "Astronomy and Astrophysics": ("天文与天体物理（博士）", "sci", "astronomy-astrophysics-phd"),
 "Behavioral Science": ("行为科学（博士）", "biz", "behavioral-science-phd"),
 "Bible (Hebrew Bible, New Testament)": ("圣经研究（希伯来圣经·新约，博士）", "art", "bible-phd"),
 "Biochemistry and Molecular Biophysics": ("生物化学与分子生物物理（博士）", "sci", "biochem-mol-biophysics-phd"),
 "Biophysical Sciences": ("生物物理科学（博士）", "sci", "biophysical-sciences-phd"),
 "Business": ("商学（博士）", "biz", "business-phd"),
 "Cancer Biology": ("癌症生物学（博士）", "sci", "cancer-biology-phd"),
 "Cell and Molecular Biology": ("细胞与分子生物学（博士）", "sci", "cell-molecular-biology-phd"),
 "Chemistry": ("化学（博士）", "sci", "chemistry-phd"),
 "Cinema and Media Studies": ("电影与媒体研究（博士）", "art", "cinema-media-studies-phd"),
 "Classics": ("古典学（博士）", "art", "classics-phd"),
 "Committee on Conceptual and Historical Studies of Science": ("科学概念与历史研究（博士）", "art", "conceptual-historical-studies-science-phd"),
 "Committee on Theater and Performance Studies": ("戏剧与表演研究（博士）", "art", "theater-performance-studies-phd"),
 "Comparative Human Development": ("比较人类发展学（博士）", "soc", "comparative-human-development-phd"),
 "Comparative Literature": ("比较文学（博士）", "art", "comparative-literature-phd"),
 "Computational and Applied Mathematics": ("计算与应用数学（博士）", "sci", "computational-applied-math-phd"),
 "Computational Neuroscience": ("计算神经科学（博士）", "sci", "computational-neuroscience-phd"),
 "Computer Science": ("计算机科学（博士）", "cs", "cs-phd"),
 "Data Science": ("数据科学（博士）", "cs", "data-science-phd"),
 "Development, Regeneration, and Stem Cell Biology": ("发育、再生与干细胞生物学（博士）", "sci", "development-regeneration-stem-cell-phd"),
 "East Asian Languages and Civilizations": ("东亚语言与文明（博士）", "art", "east-asian-languages-civilizations-phd"),
 "Ecology and Evolution": ("生态与进化（博士）", "sci", "ecology-evolution-phd"),
 "Econometrics and Statistics": ("计量经济学与统计学（博士）", "biz", "econometrics-statistics-phd"),
 "Economics (SSD)": ("经济学（博士，社会科学部）", "soc", "economics-ssd-phd"),
 "Economics (Booth)": ("经济学（博士，布斯商学院）", "biz", "economics-booth-phd"),
 "English Language and Literature": ("英语语言文学（博士）", "art", "english-language-literature-phd"),
 "Evolutionary Biology": ("进化生物学（博士）", "sci", "evolutionary-biology-phd"),
 "Finance": ("金融学（博士）", "biz", "finance-phd"),
 "Genetics. Genomics, and Systems Biology": ("遗传学、基因组学与系统生物学（博士）", "sci", "genetics-genomics-systems-biology-phd"),
 "Geophysical Sciences": ("地球物理科学（博士）", "sci", "geophysical-sciences-phd"),
 "Germanic Studies": ("日耳曼研究（博士）", "art", "germanic-studies-phd"),
 "History": ("历史学（博士）", "soc", "history-phd"),
 "History of Christianity": ("基督教史（博士）", "art", "history-christianity-phd"),
 "History of Judaism": ("犹太教史（博士）", "art", "history-judaism-phd"),
 "Human Genetics": ("人类遗传学（博士）", "sci", "human-genetics-phd"),
 "Immunology": ("免疫学（博士）", "sci", "immunology-phd"),
 "Integrative Biology": ("整合生物学（博士）", "sci", "integrative-biology-phd"),
 "International Relations": ("国际关系（博士）", "soc", "international-relations-phd"),
 "Islamic Studies": ("伊斯兰研究（博士）", "art", "islamic-studies-phd"),
 "Linguistics": ("语言学（博士）", "art", "linguistics-phd"),
 "Management Science/Operations Management": ("管理科学/运营管理（博士）", "biz", "management-science-om-phd"),
 "Marketing": ("市场营销（博士）", "biz", "marketing-phd"),
 "Mathematics": ("数学（博士）", "sci", "mathematics-phd"),
 "Medical Physics": ("医学物理（博士）", "sci", "medical-physics-phd"),
 "Microbiology": ("微生物学（博士）", "sci", "microbiology-phd"),
 "Middle Eastern Studies": ("中东研究（博士）", "art", "middle-eastern-studies-phd"),
 "Molecular Engineering": ("分子工程（博士）", "eng", "molecular-engineering-phd"),
 "Molecular Metabolism and Nutrition": ("分子代谢与营养学（博士·边界待复核）", "sci", "molecular-metabolism-nutrition-phd"),
 "Music": ("音乐（博士）", "art", "music-phd"),
 "Neurobiology": ("神经生物学（博士）", "sci", "neurobiology-phd"),
 "Philosophy": ("哲学（博士）", "art", "philosophy-phd"),
 "Philosophy of Religions": ("宗教哲学（博士）", "art", "philosophy-religions-phd"),
 "Physics": ("物理学（博士）", "sci", "physics-phd"),
 "Political Economy": ("政治经济学（博士）", "soc", "political-economy-phd"),
 "Political Science": ("政治学（博士）", "soc", "political-science-phd"),
 "Psychology": ("心理学（博士）", "soc", "psychology-phd"),
 "Public Health": ("公共卫生（博士）", "med", "public-health-phd"),
 "Public Policy": ("公共政策（博士）", "soc", "public-policy-phd"),
 "Quantum Science and Engineering": ("量子科学与工程（博士）", "eng", "quantum-science-engineering-phd"),
 "Religion, Literature, and Visual Culture": ("宗教、文学与视觉文化（博士）", "art", "religion-literature-visual-culture-phd"),
 "Religions in America": ("美国宗教（博士）", "art", "religions-america-phd"),
 "Religious Ethics": ("宗教伦理（博士）", "art", "religious-ethics-phd"),
 "Religious Studies": ("宗教研究（博士）", "art", "religious-studies-phd"),
 "Romance Languages and Literatures": ("罗曼语语言文学（博士）", "art", "romance-languages-literatures-phd"),
 "Slavic Languages and Literatures": ("斯拉夫语言文学（博士）", "art", "slavic-languages-literatures-phd"),
 "Social Thought": ("社会思想（博士）", "soc", "social-thought-phd"),
 "Social Work, Social Policy, and Social Administration": ("社会工作、社会政策与社会行政（博士·边界待复核）", "soc", "social-work-social-policy-phd"),
 "Sociology": ("社会学（博士）", "soc", "sociology-phd"),
 "South Asian Languages and Civilizations": ("南亚语言与文明（博士）", "art", "south-asian-languages-civilizations-phd"),
 "Statistics": ("统计学（博士）", "cs", "statistics-phd"),
 "Theology": ("神学（博士）", "art", "theology-phd"),
}

def remove_reason(x):
    p = str(x.get("phd") or ""); u = str(x.get("unit") or "")
    if re.search(r"M\.D\.|MD/PhD", p): return "MD/MD-PhD"
    if u.startswith("Law"): return "Law 职业法学位"
    return None

def phd_url(x):
    ph = str(x.get("phd") or "")
    for name, url in x.get("links", []):
        if name and ("Ph" in name or "JSD" in name or "Comp" in name):
            return url
    return x.get("home")

rows, pages, rem, miss = [], [], [], []
for x in raw:
    if not x.get("phd"): continue
    rsn = remove_reason(x)
    if rsn:
        rem.append((rsn, x["title"], x["phd"])); continue
    t = x["title"]
    if t not in M:
        miss.append(t); continue
    cn, disc, slug = M[t]
    det = f"details/uchicago-{slug}.html"
    college = unit_cn(x["unit"])
    url = phd_url(x)
    rows.append(["uchicago", college, t, cn, "PhD", url, disc, det])
    pages.append({"det":det,"cn":cn,"en":t,"college":college,"deg":"PhD","url":url})

print("保留博士:", len(rows), " 剔除:", len(rem), " 未映射:", miss)
for r in rem: print("  剔:", r)
from collections import Counter
print("disc:", dict(Counter(r[6] for r in rows)))
json.dump(rows, open(os.path.join(base,"pending_uchicago_phd_catd.json"),"w",encoding="utf-8"), ensure_ascii=False, indent=1)
json.dump(pages, open(os.path.join(base,"pending_uchicago_phd_pages.json"),"w",encoding="utf-8"), ensure_ascii=False, indent=1)
for r in rows: print(" ", r[4], r[6], r[3], "|", r[2])
