File size: 1,817 Bytes
24aaa76 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | """rc1 prep, step misc: what the detector's training pages are (counts by page kind), for the detector card.
Run with CODE/.venv_kraken/bin/python -I. Reads the page lists of the detector (train and validation only; never a test list).
usage: rc1_prep_misc.py REPORT_JSON PAGES_GEN_TRAIN PAGES_GEN_VAL PAGES_SCHOOL_TRAIN PAGES_SCHOOL_VAL SCHOOL_BUILD_SUMMARY
"""
import csv
import json
import sys
from collections import Counter
csv.field_size_limit(10 ** 9)
def scan(path):
assert "test" not in path.lower().split("/")[-1]
kinds, notes, n, lines = Counter(), Counter(), 0, 0
langs = Counter()
with open(path, encoding="utf-8", newline="") as f:
rd = csv.DictReader(f, delimiter="\t", quoting=csv.QUOTE_NONE)
for r in rd:
n += 1
parts = r["id"].split("_")
kinds["_".join(parts[2:-1]) if r["id"].startswith("pg_") else parts[0] + "_" + parts[1]] += 1
if r["id"].startswith("pg_") and len(parts) >= 4:
langs[parts[3]] += 1
notes[(r.get("note") or "")[:120]] += 1
lines += int(r["n_lines"])
return {"pages": n, "lines": lines, "id_kind_counts": dict(kinds.most_common(40)), "lang_token_counts": dict(langs.most_common(40)),
"note_values_top": dict(notes.most_common(8))}
def main():
rep_path = sys.argv[1]
out = {}
for name, p in zip(("gen_train", "gen_val", "school_train", "school_val"), sys.argv[2:6]):
out[name] = scan(p)
out["school_build_summary"] = json.load(open(sys.argv[6], encoding="utf-8"))
with open(rep_path, "w", encoding="utf-8") as f:
json.dump(out, f, indent=1, ensure_ascii=False)
print(json.dumps({k: (v.get("pages") if isinstance(v, dict) else None) for k, v in out.items()}))
if __name__ == "__main__":
main()
|