#!/usr/bin/env python3 """Compare the two manual audits of the machine syntax layer with each other. The first audit *corrected* the parser's trees token by token; the second annotated the same 100 sentences from scratch, without seeing the parser's output or the first audit. Their agreement is the inter-annotator agreement of one annotator (Claude Fable 5.1) in two conditions, and the difference between the parser's score against each is the anchoring effect of correcting rather than annotating: a corrector who leaves a parser decision alone unless it is clearly wrong will agree with the parser more than an independent annotator does. Writes one JSON report with, for every pair among {parser, corrected, scratch}: UAS, LAS, label accuracy, UPOS accuracy, FEATS micro-F1 and the per-label agreement, plus the three-way counts (all agree, parser agrees with one audit only, the audits agree against the parser, all differ) for heads. """ from __future__ import annotations import argparse import json from collections import Counter from itertools import combinations from pathlib import Path try: from scripts.machine_syntax_audit import feats_set, label_scores, load_jsonl, set_scores except ModuleNotFoundError: # Direct execution from the scripts directory. from machine_syntax_audit import feats_set, label_scores, load_jsonl, set_scores def pairwise(a: dict[str, dict], b: dict[str, dict]) -> dict: heads = deprels = both = tokens = 0 upos_pairs, deprel_pairs, feats_pairs = [], [], [] for sentence_id in sorted(set(a) & set(b)): ta, tb = a[sentence_id]["tokens"], b[sentence_id]["tokens"] assert [t["form"] for t in ta] == [t["form"] for t in tb], sentence_id for x, y in zip(ta, tb): tokens += 1 head_ok = x["head"] == y["head"] deprel_ok = x["deprel"] == y["deprel"] heads += head_ok deprels += deprel_ok both += head_ok and deprel_ok upos_pairs.append((x["upos"], y["upos"])) deprel_pairs.append((x["deprel"], y["deprel"])) feats_pairs.append((feats_set(x["feats"]), feats_set(y["feats"]))) return { "tokens": tokens, "uas": round(heads / tokens, 4), "las": round(both / tokens, 4), "label_accuracy": round(deprels / tokens, 4), "upos_accuracy": round(label_scores(upos_pairs)["accuracy"], 4), "upos_macro_f1": label_scores(upos_pairs)["macro"].get("f1"), "feats_micro_f1": set_scores(feats_pairs)["micro"]["f1"], "feats_exact_set_accuracy": set_scores(feats_pairs)["exact_set_accuracy"], "deprel_per_label_f1": { label: values["f1"] for label, values in label_scores(deprel_pairs)["per_label"].items() }, } def three_way(parser: dict, corrected: dict, scratch: dict, field: str) -> dict: counts: Counter = Counter() for sentence_id in sorted(set(parser) & set(corrected) & set(scratch)): for p, c, s in zip(parser[sentence_id]["tokens"], corrected[sentence_id]["tokens"], scratch[sentence_id]["tokens"]): pc, ps, cs = p[field] == c[field], p[field] == s[field], c[field] == s[field] if pc and ps: counts["all_three_agree"] += 1 elif cs: counts["audits_agree_against_parser"] += 1 elif pc: counts["parser_agrees_with_corrected_only"] += 1 elif ps: counts["parser_agrees_with_scratch_only"] += 1 else: counts["all_three_differ"] += 1 total = sum(counts.values()) return {key: {"count": value, "share": round(value / total, 4)} for key, value in sorted(counts.items())} def main() -> None: parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("--machine", type=Path, required=True) parser.add_argument("--corrected", type=Path, required=True) parser.add_argument("--scratch", type=Path, required=True) parser.add_argument("--output", type=Path, required=True) args = parser.parse_args() machine = load_jsonl(args.machine) corrected = load_jsonl(args.corrected) scratch = load_jsonl(args.scratch) assert set(machine) == set(corrected) == set(scratch), "the three files must cover the same sentences" sets = {"parser": machine, "corrected": corrected, "scratch": scratch} report = { "annotators": { "corrected": "Claude Fable 5.1 correcting the parser's trees token by token (2026-09-18)", "scratch": "Claude Fable 5.1 annotating the same sentences from forms alone, without the " "parser's trees or the first audit (2026-09-22)", }, "sentences": len(machine), "pairs": { f"{a}_vs_{b}": pairwise(sets[a], sets[b]) for a, b in combinations(sets, 2) }, "three_way": { "head": three_way(machine, corrected, scratch, "head"), "deprel": three_way(machine, corrected, scratch, "deprel"), "upos": three_way(machine, corrected, scratch, "upos"), }, } p_c = report["pairs"]["parser_vs_corrected"] p_s = report["pairs"]["parser_vs_scratch"] report["anchoring"] = { "description": "how much more the parser agrees with the audit that started from its " "trees than with the one that did not", "uas": round(p_c["uas"] - p_s["uas"], 4), "las": round(p_c["las"] - p_s["las"], 4), "upos_accuracy": round(p_c["upos_accuracy"] - p_s["upos_accuracy"], 4), "feats_micro_f1": round(p_c["feats_micro_f1"] - p_s["feats_micro_f1"], 4), } args.output.write_text(json.dumps(report, indent=2, ensure_ascii=False) + "\n", encoding="utf-8") summary = { key: {k: v for k, v in values.items() if k != "deprel_per_label_f1"} for key, values in report["pairs"].items() } print(json.dumps({"pairs": summary, "anchoring": report["anchoring"], "three_way_head": report["three_way"]["head"]}, indent=2)) if __name__ == "__main__": main()