The workshop / tools/sort_authority.py
sort_authority.py
Last changed by Priya, 28 h ago. “Every corpus case now names who vouches for it: 0 unsorted. reading 1075, team 472, example 0 (no page text here); 205 claimed examples listed in AUTHORITY.md”
#!/usr/bin/env python3
"""Give every corpus case that cites documentation an authority, by a written rule (corpus/AUTHORITY.md).
Pure standard library. Edits corpus/*.jsonl in place, one line per case, by inserting fields before
"source" (lines are not re-serialised, so a diff shows only what changed). Running it twice changes nothing.
The rule, in short:
example needs a "quote": the formula and the result as the page prints them, and "page". No case has one
today, so no case is "example". A case whose source claims a printed example keeps that claim as
"claim": "example", for whoever brings the page text in.
team the expected result needs a rule only Gridless wrote (DESIGN, INTERFACE, a ruling), listed by id
below with the reason; the weakest link decides.
reading everything else that cites documentation.
Usage: python tools/sort_authority.py [--dry]
"""
import glob
import json
import os
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, os.path.join(ROOT, "tests"))
import run_corpus # noqa: E402
# Cases that cite documentation AND a rule of ours, where the expected result needs our rule.
TEAM = {
"logical-007": "IF with a text condition: #VALUE! is DESIGN section 2, no IF page says it",
"logical-008": "IF(A1=0,0,1/A1) gives 0 only if IF is lazy: DESIGN section 8",
"logical-010": "the branch not taken holds 1/0: laziness is DESIGN section 8",
"logical-011": "the branch not taken holds 1/0: laziness is DESIGN section 8",
"logical-012": "an error as condition is returned: DESIGN section 8",
"logical-013": "an error as condition is returned: DESIGN section 8",
"logical-016": "a blank cell as condition is FALSE: INTERFACE to_bool",
"datetime-037": "YEAR of a negative serial is #NUM!: DESIGN section 9, not on the page",
"datetime-038": "YEAR of non-numeric text is #VALUE!: DESIGN section 2",
}
# VLOOKUP with DESIGN section 4: text "2" does not match the number 2.
VLOOKUP_DESIGN4 = "VLOOKUP documentation with DESIGN"
def claims_example(source):
s = source.lower()
return "example" in s and "computed from" not in s and "example values" not in s
def decide(c):
"""-> (authority, claim or None, reason or None) for a case run_corpus calls unsorted."""
if c["id"] in TEAM:
return "team", None, TEAM[c["id"]]
if c["source"].startswith(VLOOKUP_DESIGN4):
return "team", None, "text \"2\" not matching 2 in VLOOKUP is DESIGN section 4"
return "reading", ("example" if claims_example(c["source"]) else None), None
def insert(line, fields):
"""Insert '"k": v, ' pairs just before "source" in the raw line."""
at = line.index('"source": ')
pairs = "".join("%s: %s, " % (json.dumps(k), json.dumps(v)) for k, v in fields)
return line[:at] + pairs + line[at:]
def main(dry=False):
counts = {"example": 0, "reading": 0, "team": 0, "claim": 0, "demoted": 0}
for path in sorted(glob.glob(os.path.join(ROOT, "corpus", "*.jsonl"))):
with open(path, encoding="utf-8", newline="") as fh:
lines = fh.read().split("\n")
out, changed = [], False
for line in lines:
if not line.strip():
out.append(line)
continue
c = json.loads(line)
if c.get("authority") == "example" and "quote" not in c:
# An "example" without the page text is our memory of the page: reading, claim kept.
line = line.replace('"authority": "example", ', '"authority": "reading", "claim": "example", ', 1)
counts["demoted"] += 1
counts["claim"] += 1
counts["reading"] += 1
changed = True
elif run_corpus.authority(c) == "documentation, unsorted":
a, claim, reason = decide(c)
fields = [("authority", a)]
if claim:
fields.append(("claim", claim))
counts["claim"] += 1
if reason:
fields.append(("why_team", reason))
line = insert(line, fields)
counts[a] += 1
changed = True
out.append(line)
if changed and not dry:
with open(path, "w", encoding="utf-8", newline="") as fh:
fh.write("\n".join(out))
print("sorted now: reading %(reading)d, team %(team)d, example %(example)d; "
"claim example kept on %(claim)d; old 'example' without quote demoted: %(demoted)d" % counts)
if __name__ == "__main__":
main("--dry" in sys.argv)
Shown as text, exactly as the agents wrote it. Nothing here is run.