pragmatic-code-switch-blindspot / rerun_analysis.py
rshahbaz's picture
Upload folder using huggingface_hub
38c9b83 verified
Raw History Blame Contribute Delete
3.34 kB
"""Matched rerun: agreement between two coders and the language effect
with a fully English baseline.
Usage: python rerun_analysis.py [rerun_scores.jsonl]
"""
import json, random, statistics, sys
from collections import defaultdict
D = ["intent_inference", "social_appropriateness",
"unsupported_assumptions", "semantic_preservation"]
rows = [json.loads(l) for l in open(sys.argv[1] if len(sys.argv) > 1 else "rerun_scores.jsonl", encoding="utf-8")]
by = {r["id"]: r for r in rows}
ids = sorted(by)
free = [i for i in ids if not by[i]["no_answer"]]
dirty = {by[i]["scenario_id"] for i in ids if by[i]["no_answer"]}
def wkappa(a, b, k=3):
n = len(a)
O = [[0] * k for _ in range(k)]
for x, y in zip(a, b):
O[x][y] += 1
ra = [sum(r) for r in O]
cb = [sum(O[i][j] for i in range(k)) for j in range(k)]
w = lambda i, j: (i - j) ** 2 / (k - 1) ** 2
obs = sum(w(i, j) * O[i][j] for i in range(k) for j in range(k)) / n
exp = sum(w(i, j) * ra[i] * cb[j] for i in range(k) for j in range(k)) / n**2
return 1 - obs / exp if exp else float("nan")
def ci(v, n=10000, seed=42):
r = random.Random(seed)
m = sorted(statistics.mean(r.choice(v) for _ in v) for _ in range(n))
return m[int(.025 * n)], m[int(.975 * n)]
print(f"Agreement, quadratic-weighted Cohen's kappa, n={len(free)}")
print("(the 4 items with no model answer are excluded: both coders were told to score them 0)")
for d in D:
a = [by[i]["coder1"][d] for i in free]; b = [by[i]["coder2"][d] for i in free]
print(f" {d:26s} kappa {wkappa(a, b):.2f} exact {sum(x==y for x,y in zip(a,b))/len(a):.0%}")
a = [by[i]["coder1"][d] for i in free for d in D]; b = [by[i]["coder2"][d] for i in free for d in D]
print(f" {'pooled':26s} kappa {wkappa(a, b):.2f}")
t1 = [by[i]["coder1"]["total"] for i in free]; t2 = [by[i]["coder2"]["total"] for i in free]
print(f" totals: correlation {statistics.correlation(t1, t2):.2f}, mean absolute difference {statistics.mean(abs(x-y) for x,y in zip(t1,t2)):.2f}")
def effects(label, score):
p = defaultdict(dict)
for i in ids:
p[by[i]["scenario_id"]][by[i]["condition"]] = score(i)
print(f"\n{label}")
for name, x, y, keep in [
("language, context implicit", "roman_urdu_mixed", "implicit_english", None),
("language, context explicit", "explicit_context_added", "explicit_english", None),
("context, English", "explicit_english", "implicit_english", None),
("context, Roman Urdu", "explicit_context_added", "roman_urdu_mixed", None),
("language, implicit, clean only", "roman_urdu_mixed", "implicit_english", lambda s: s not in dirty)]:
v = [q[x] - q[y] for s, q in p.items() if keep is None or keep(s)]
lo, hi = ci(v)
print(f" {name:32s} {statistics.mean(v):+.2f} [{lo:+.2f}, {hi:+.2f}] n={len(v)}")
print(" per scenario, English implicit to Roman Urdu implicit:")
for s in sorted(p):
mark = " (truncated response in this scenario)" if s in dirty else ""
print(f" {s:20s} {p[s]['implicit_english']:.1f} -> {p[s]['roman_urdu_mixed']:.1f}{mark}")
effects("Coder 1", lambda i: by[i]["coder1"]["total"])
effects("Coder 2", lambda i: by[i]["coder2"]["total"])
effects("Mean of both coders", lambda i: (by[i]["coder1"]["total"] + by[i]["coder2"]["total"]) / 2)