# Compares the Word counter figure with the independent counts, and the Hashtag counter with known answers.
# usage: python3 analyze.py inputs.json wc.json counts.jsonl tags.json ht.json
import json, re, sys
inputs = {i["id"]: i for i in json.load(open(sys.argv[1]))}
wc = {r["id"]: r for r in json.load(open(sys.argv[2]))}
cnt = {}
for line in open(sys.argv[3]):
    d = json.loads(line); cnt[d["id"]] = d
tags = {i["id"]: i for i in json.load(open(sys.argv[4]))}
ht = {r["id"]: r for r in json.load(open(sys.argv[5]))}
print("== A. Word counter 'characters' against four independent counts ==")
print("id | tool | utf16 (python) | utf16 (node) | code points | graphemes | utf8 bytes | tool==utf16")
ok = 0
for k, it in inputs.items():
    t = it["text"]
    py16 = len(t.encode("utf-16-le")) // 2
    c = cnt[k]; tool = wc[k]["tool_chars"]
    assert py16 == c["utf16_units"], k
    eq = (tool == py16) or (tool is None and py16 == 0)
    ok += eq
    print(f"{k} | {tool} | {py16} | {c['utf16_units']} | {c['code_points']} | {c['graphemes']} | {c['utf8_bytes']} | {eq}")
print(f"tool equals the Python UTF-16 count on {ok} of {len(inputs)} strings")
print("controls: a2200 ->", wc["a2200"]["tool_chars"], "(expect 2200); a2201 ->", wc["a2201"]["tool_chars"], "(expect 2201); empty ->", wc["empty"]["tool_chars"], "(expect None: no figures shown)")
print("order check (utf16 >= code points >= graphemes) holds on all strings:", all(c["utf16_units"] >= c["code_points"] >= c["graphemes"] for c in cnt.values()))
print()
print("== B. Hashtag counter against a person's count (expect) ==")
print("id | text | expect | tool | unique | over-5 note | listed | tool==expect")
same = 0
for k, it in tags.items():
    r = ht[k]
    eq = r["tool_tags"] == it["expect"] or (r["tool_tags"] is None and it["expect"] == 0)
    same += eq
    print(f"{k} | {it['text']!r} | {it['expect']} | {r['tool_tags']} | {r['tool_unique']} | {r['over_note']} | {r['listed']!r} | {eq}")
print(f"tool equals the by-eye count on {same} of {len(tags)} cases; differences listed above")
print("control: 'five' over-note is", ht["five"]["over_note"], "(expect False) and 'six' over-note is", ht["six"]["over_note"], "(expect True)")
