"""Second reviewer probe: extra targets, macroareas, example forms. Reuses bundle code/colex.py."""
import csv, sys, json
from collections import Counter, defaultdict
sys.path.insert(0, "code")
import colex
lex, fam = colex.load(sys.argv[1], sys.argv[2])
meta = {r["ID"]: r for r in csv.DictReader(open(sys.argv[2], newline="", encoding="utf-8"))}
out = {}
extra = [("HARD", "BE ANGRY"), ("SOFT", "CALM (OF SEA)"), ("SOFT", "PEACE"), ("HEAVY", "GRIEF"), ("HEAVY", "SAD"),
         ("LOW", "BAD"), ("HIGH", "GOOD"), ("UP", "GOOD"), ("DOWN", "BAD"), ("LIGHT (WEIGHT)", "GOOD"),
         ("HARD", "DIFFICULT"), ("SOFT", "EASY"), ("CROOKED", "EVIL"), ("STRAIGHT", "TRUE"), ("TIGHT", "BE ANGRY")]
for a, b in extra:
    r = colex.rates_for(a, lex, fam)
    out[f"{a}|{b}"] = list(r.get(b, (0, 0)))
def fam_area(a, b):
    both, hit = defaultdict(set), defaultdict(set)
    for v, c in lex.items():
        if a in c and b in c:
            ar = meta[v]["Macroarea"] or "?"
            f = fam[v]
            both[ar].add(f)
            if c[a] & c[b]:
                hit[ar].add(f)
    return {ar: [len(hit[ar]), len(both[ar])] for ar in both}
out["macroarea_fam"] = {p: fam_area(*p.split("|")) for p in ["LIGHT (WEIGHT)|EASY", "HEAVY|DIFFICULT", "STRAIGHT|CORRECT (RIGHT)", "STRAIGHT|GOOD", "CROOKED|WRONG"]}
ex = {}
for a, b in [("LIGHT (WEIGHT)", "EASY"), ("HEAVY", "DIFFICULT"), ("STRAIGHT", "GOOD"), ("CROOKED", "WRONG"), ("UP", "HAPPY"), ("SOFT", "QUIET")]:
    rows, seen = [], set()
    for v, c in lex.items():
        if a in c and b in c and c[a] & c[b]:
            f = meta[v]["Family_Name"] or fam[v]
            if f in seen and len(rows) > 3: continue
            seen.add(f)
            rows.append([meta[v]["Name"], f, meta[v]["Contribution_ID"], sorted(c[a] & c[b]), len(c[a]), len(c[b])])
    ex[f"{a}|{b}"] = rows
out["examples_one_per_family"] = ex
json.dump(out, open("/ev/probe2_out.json", "w"), indent=1, ensure_ascii=False)
print(json.dumps(out, ensure_ascii=False, indent=0))
