#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""从 todo_upgrade.json 挑下一批 5 条热门硕士未核验项目（按学校+英文名去重）"""
import json
import re

WORKDIR = r"D:\work buddy\2026-11-05-14-15-52"

# 读 PROGRAMS
with open(WORKDIR + r"\index_main.html", encoding="utf-8") as f:
    html = f.read()

# 提取 PROGRAMS 里所有 (s, en) 对
prog_start = html.index("const PROGRAMS=[")
depth = 0
j = prog_start + len("const PROGRAMS=")
while j < len(html):
    ch = html[j]
    if ch == "[":
        depth += 1
    elif ch == "]":
        depth -= 1
        if depth == 0:
            break
    j += 1
prog_block = html[prog_start:j+1]

prog_records = re.findall(r'\{id:"[^"]+",s:"([^"]+)",n:"[^"]*",en:"([^"]*)"', prog_block)
prog_pairs = set((s, en.strip().lower()) for s, en in prog_records)
print(f"PROGRAMS 已有 (s,en) 对数: {len(prog_pairs)}")

# 读 todo
with open(WORKDIR + r"\todo_upgrade.json", encoding="utf-8") as f:
    todo = json.load(f)

# 已定性排除的项目
excluded = [
    ("duke", "quantitative financial economics"),
    ("wustl", "statistics and data science"),
    ("notre", "data science"),
    ("ucla", "data science in biomedicine"),
    ("unc", "applied data science"),
    ("geo", "cybersecurity risk management"),
    ("up", "electrical engineering"),
    ("up", "biomedical informatics"),
    ("jhu", "bioinformatics"),
    ("jhu", "financial economics"),
]

# 上两轮已落库的（避免重复）
already_added = [
    ("emory", "computer science"),
    ("emory", "data science"),
    ("uva", "data science"),
    ("unc", "statistics, analytics, and data science"),
    ("ucla", "business analytics"),
    ("ucla", "applied statistics and data science"),
    ("cornell", "applied economics and management"),
    ("notre", "computer science and engineering"),
    ("unc", "biomedical and health informatics"),
    ("jhu", "geographic information systems"),
]

# 筛选未核验的、硕士、非排除项
pending = []
for t in todo:
    s = t.get("s", "")
    en = t.get("en", "").strip().lower()
    deg = t.get("deg", "").upper()
    
    if "PHD" in deg:
        continue
    
    if (s, en) in prog_pairs:
        continue
    
    is_excluded = False
    for es, een in excluded:
        if s == es and een in en:
            is_excluded = True
            break
    if is_excluded:
        continue
    
    # 跳过已落库的
    for as_, aen in already_added:
        if s == as_ and aen in en:
            is_excluded = True
            break
    if is_excluded:
        continue
    
    pending.append(t)

print(f"未核验硕士 todo 数: {len(pending)}")

# 按热门优先级打分 + 低覆盖学校加权
def score(item):
    en = item.get("en", "").lower()
    cn = item.get("cn", "").lower()
    disc = item.get("disc", "").lower()
    col = item.get("col", "").lower()
    s = item.get("s", "")
    text = en + " " + cn + " " + disc
    
    sc = 0
    disc_weights = {"cs": 10, "eng": 8, "biz": 7, "sci": 5, "med": 6, "soc": 4, "art": 1, "law": 3, "edu": 2}
    sc += disc_weights.get(disc, 3)
    
    hot_kw = [
        ("computer", 5), ("data science", 5), ("machine learning", 5), ("artificial intelligence", 5),
        ("electrical", 4), ("computer engineering", 4),
        ("finance", 4), ("financial", 4), ("quantitative", 4),
        ("economics", 3), ("economic", 3),
        ("business analytics", 4), ("analytics", 3),
        ("statistics", 3), ("biostatistics", 4),
        ("mechanical", 3),
        ("bioinformatics", 4), ("biomedical", 4), ("biotechnology", 4),
        ("management", 2), ("MPA", 2), ("MPP", 2), ("public policy", 2),
        ("information system", 3), ("information science", 3),
    ]
    for kw, w in hot_kw:
        if kw.lower() in text:
            sc += w
    
    if "business" in col or "merrill" in col or "kellogg" in col:
        sc += 2
    if "engineering" in col or "viterbi" in col or "seas" in col:
        sc += 2
    
    # 低覆盖学校加权（CMU/Rice/Vanderbilt/WashU/Emory 等）
    low_coverage = ["cmu", "rice", "vanderbilt", "wustl", "emory", "geo", "notre", "uva", "dartmouth", "brown", "caltech", "mit", "princeton", "harvard", "yale", "stanford", "col", "duke"]
    if s in low_coverage:
        sc += 3
    
    return sc

pending_scored = [(score(t), t) for t in pending]
pending_scored.sort(key=lambda x: -x[0])

print("\n=== 前 30 条热门候选（硕士，低覆盖校加权） ===")
for i, (sc, t) in enumerate(pending_scored[:30]):
    print(f"  {i} [score={sc}] {t['s']} | {t['cn'][:20]} | {t['en'][:50]} | {t['deg']}")
