Download analyze_results.py from rshahbaz/pragmatic-code-switch-blindspot: direct link, hf CLI and curl.
- Browser
- Download file 2.66 kB
-
https://huggingface.co/datasets/rshahbaz/pragmatic-code-switch-blindspot/resolve/a1200bc746bf2e556f9caeed89f9cecaede5986f/analyze_results.py
- Command line
-
hf download hf://datasets/rshahbaz/pragmatic-code-switch-blindspot@a1200bc746bf2e556f9caeed89f9cecaede5986f/analyze_results.py
-
curl -L -o analyze_results.py https://huggingface.co/datasets/rshahbaz/pragmatic-code-switch-blindspot/resolve/a1200bc746bf2e556f9caeed89f9cecaede5986f/analyze_results.py
2.66 kB
| import json, random, statistics, sys | |
| from collections import defaultdict | |
| PATH = sys.argv[1] if len(sys.argv) > 1 else "results_scored.jsonl" | |
| DIMS = ["intent_inference", "social_appropriateness", | |
| "unsupported_assumptions", "semantic_preservation"] | |
| rows = [json.loads(x) for x in open(PATH, encoding="utf-8") if x.strip()] | |
| missing = [r["id"] for r in rows if not all(d in r for d in DIMS)] | |
| if missing: | |
| sys.exit(f"missing score fields on: {missing}") | |
| bad = [r["id"] for r in rows if not all(int(r[d]) in (0, 1, 2) for d in DIMS)] | |
| if bad: | |
| sys.exit(f"scores outside 0-2 on: {bad}") | |
| for r in rows: | |
| r["total"] = sum(int(r[d]) for d in DIMS) | |
| pairs = defaultdict(dict) | |
| by_cond = defaultdict(list) | |
| for r in rows: | |
| pairs[r["scenario_id"]][r["condition"]] = r["total"] | |
| by_cond[r["condition"]].append(r["total"]) | |
| ORDER = ["implicit_english", "roman_urdu_mixed", | |
| "explicit_english", "explicit_context_added"] | |
| def ci(values, n=10000, seed=42): | |
| rng = random.Random(seed) | |
| means = sorted(statistics.mean([rng.choice(values) for _ in values]) | |
| for _ in range(n)) | |
| return means[int(.025 * n)], means[int(.975 * n)] | |
| def diffs(a, b): | |
| return [p[a] - p[b] for p in pairs.values() if a in p and b in p] | |
| def report(label, a, b): | |
| d = diffs(a, b) | |
| if not d: | |
| return | |
| lo, hi = ci(d) | |
| print(f"{label:34s} {statistics.mean(d):+.3f} " | |
| f"95% CI [{lo:+.3f}, {hi:+.3f}] n={len(d)}") | |
| print("Mean total score out of 8, by condition") | |
| for c in ORDER: | |
| v = by_cond.get(c, []) | |
| if v: | |
| print(f" {c:26s} {statistics.mean(v):.3f} n={len(v)}") | |
| print("\nPaired differences") | |
| report("language, context implicit", "roman_urdu_mixed", "implicit_english") | |
| report("language, context explicit", "explicit_context_added", "explicit_english") | |
| report("context, English", "explicit_english", "implicit_english") | |
| report("context, Roman Urdu", "explicit_context_added", "roman_urdu_mixed") | |
| inter = [(p["explicit_context_added"] - p["roman_urdu_mixed"]) | |
| - (p["explicit_english"] - p["implicit_english"]) | |
| for p in pairs.values() if len(p) == 4] | |
| if inter: | |
| lo, hi = ci(inter) | |
| print(f"\n{'interaction, recovery in RU vs EN':34s} {statistics.mean(inter):+.3f} " | |
| f"95% CI [{lo:+.3f}, {hi:+.3f}] n={len(inter)}") | |
| print("\nPer-dimension means by condition") | |
| hdr = " " + " ".join(f"{d[:12]:>13s}" for d in DIMS) | |
| print(f" {'':26s}{hdr}") | |
| for c in ORDER: | |
| sel = [r for r in rows if r["condition"] == c] | |
| if not sel: | |
| continue | |
| cells = " ".join(f"{statistics.mean([int(r[d]) for r in sel]):>13.2f}" for d in DIMS) | |
| print(f" {c:26s} {cells}") | |