import json, re
from collections import defaultdict
prog = open('index_main.html', encoding='utf-8').read()
existing = set(re.findall(r'\{id:"([^"]+)"', prog))
todo = json.load(open('todo_upgrade.json', encoding='utf-8'))
# already-en set
existing_en = set()
m = re.findall(r'\{id:"([^"]+)"([\s\S]*?)(?=\{id:"|,?\n\];)', prog)
# simpler: find all en fields
for em in re.finditer(r'\ben:"([^"]+)"', prog):
    existing_en.add(em.group(1))

cands = [t for t in todo if t.get('deg')=='PhD' and t.get('en','') and t.get('en','') not in existing_en]
by_s = defaultdict(list)
for t in cands:
    by_s[t.get('s','')].append(t)

# want 1 per school, diverse schools, CS/ECE/CE
target_schools_order = ['wustl','emory','geo','brown','dartmouth','rice','uva','unc','gatech','umich','cornell','nyu','usc','ucla','penn','jhu','duke','vanderbilt','notre','berkeley','stanford','princeton','harvard','yale','caltech','uchicago','col','cmu','uiuc','illinois','nu','northwestern','mcg','ubc','uw','ut']
picked = []
used_s = set()
for s in target_schools_order:
    if s in used_s: continue
    for t in by_s.get(s, []):
        en = t.get('en','').lower()
        if any(k in en for k in ['computer science','electrical engineering','computer engineering','computer science and engineering']):
            # skip narrow sub-specs that duplicate main PhD
            if 'specialization' in en or 'cognitive' in en or 'bioinformatics' in en or 'applied' in en:
                continue
            picked.append(t)
            used_s.add(s)
            break
    if len(picked) >= 5: break

for t in picked:
    print(t.get('s'), '|', t.get('en'), '|', t.get('cn',''), '|', t.get('deg',''))
print('---')
print('remaining PhD:', len(cands))
