#!/usr/bin/env python3
"""Test-set structure. Prints counts only, never text. argv: <test_set.json> <id.json> <ood.json> <example_test_set.json>"""
import hashlib, json, sys
from collections import Counter

p, pid, pood, pex = sys.argv[1:5]
raw = open(p, 'rb').read()
print('test_set sha256', hashlib.sha256(raw).hexdigest(), 'bytes', len(raw))
T = json.loads(raw)
ID = json.load(open(pid))
OOD = json.load(open(pood))
EX = json.load(open(pex))
print('entries', len(T), 'id', len(ID), 'ood', len(OOD), 'example', len(EX))
print('top-level fields', Counter(k for t in T for k in t).most_common())
key = lambda c: f"{c['book']}-{c['plot']['i_p']}-{c['i_c']}"
kT = [key(c) for c in T]
print('unique keys', len(set(kT)))
print('tag values', Counter(str(c.get('tag')) for c in T))
print('id ∪ ood == test_set', set(key(c) for c in ID) | set(key(c) for c in OOD) == set(kT), 'id∩ood', len(set(key(c) for c in ID) & set(key(c) for c in OOD)))
print('test_set order == id + ood', [key(c) for c in ID] + [key(c) for c in OOD] == kT)
print('first100 are id-file', set(kT[:100]) == set(key(c) for c in ID))
print('content equal to id/ood entries', sum(json.dumps(a, sort_keys=True) == json.dumps(b, sort_keys=True) for a, b in zip(T, ID + OOD)))
print('books total', len(set(c['book'] for c in T)), 'books id', len(set(c['book'] for c in ID)), 'books ood', len(set(c['book'] for c in OOD)), 'book overlap', len(set(c['book'] for c in ID) & set(c['book'] for c in OOD)))
print('key_characters fields', Counter(f for c in T for ch in c['key_characters'] for f in ch).most_common())
print('example file key_characters fields', Counter(f for c in EX for ch in c['key_characters'] for f in ch).most_common())
print('id file key_characters fields', Counter(f for c in ID for ch in c['key_characters'] for f in ch).most_common())
nt = sum(1 for c in T for ch in c['key_characters'] if ch.get('thought'))
print('key characters with non-empty thought', nt, 'of', sum(len(c['key_characters']) for c in T))
print('conversations where a speaking character has no key_characters entry', sum(any(s != 'Environment' and s not in {x.get('name') for x in c['key_characters']} for s in c['speaking_characters_w_env']) for c in T))
print('speaking lists containing Environment', sum('Environment' in c['speaking_characters_w_env'] for c in T))
print('speaking[0]==dialogues[0].character', sum(c['speaking_characters_w_env'][0] == c['dialogues'][0]['character'] for c in T))
print('speaking[0]==Environment', sum(c['speaking_characters_w_env'][0] == 'Environment' for c in T))
print('dialogues length min/mean/max', min(len(c['dialogues']) for c in T), sum(len(c['dialogues']) for c in T) / len(T), max(len(c['dialogues']) for c in T))
print('dialogue longer than 20 lines', sum(len(c['dialogues']) > 20 for c in T))
print('reference messages with [..]', sum('[' in d['message'] for c in T for d in c['dialogues']), 'of', sum(len(c['dialogues']) for c in T))
print('major characters mean', sum(len(c['major_characters']) for c in T) / len(T))
print('major_characters containing Environment', sum('Environment' in c['major_characters'] for c in T))
print('speaking mean', sum(len(c['speaking_characters_w_env']) for c in T) / len(T))
nonascii = sum(1 for c in T for d in c['dialogues'] for ch in d['message'] if '一' <= ch <= '鿿')
print('CJK characters in dialogues', nonascii)
print('profiles empty for a speaking character', sum(1 for c in T for s in c['speaking_characters_w_env'] if s != 'Environment' and not (c['character_profiles'].get(s) or '').strip()))
print('plot.key_characters description field present', Counter('description' in x for c in T for x in c['plot']['key_characters']))
print('plot summary empty', sum(not c['plot'].get('summary') for c in T))
print('i_p fields equal', sum(c.get('i_p') == c['plot']['i_p'] for c in T))
