"""Single-reader coding of the frozen random 50+50 English review packet.

B: explicitly discusses build/configuration systems, choices, classes or skill trees.
E: explicitly discusses hands-on execution/control or evaluates the underlying
   activity separately from progression. Merely naming an arcade genre is insufficient.
P: evaluates/describes audiovisual presentation or terminal aesthetic.
F: technical/usability friction, including interface information or visual overload.
R: explicitly discusses replay, repeat runs, variety or longevity.
U: references the developer/public presence; not proof of following or prior purchase.
Labels are topics voiced in current review text, not inferred motives or personalities.
"""
from pathlib import Path
import json,hashlib
import pandas as pd
R=Path(__file__).resolve().parent
labels=[
'BPF','BPRF','F','','F','PF','PF','BRF','BPRF','BF',
'BER','BR','','BP','BRF','','','','B','BP',
'BRF','BEPRF','BP','BRP','PF',
'B','E','','BP','BPF','','BR','B','','',
'BP','R','BPRF','','B','F','BF','PF','BF','',
'BEPF','','','','BP',
'B','','','','','','','PR','P','',
'','F','','R','','','U','','R','B',
'','BR','','BPF','',
'P','','','','','','R','','BR','',
'PR','','P','','','','','','','',
'U','E','','BR',''
]
raw=json.loads((R/'own_review_pilot.json').read_text());assert len(raw)==len(labels)==100
rows=[]
for i,(r,code) in enumerate(zip(raw,labels)):
 rows.append(dict(index=i,appid=r['appid'],game=r['game'],review_id=r['review_id'],codes=code,words=len(r['text'].split()),**{k:k in code for k in 'BEPFRU'}))
pd.DataFrame(rows).to_csv(R/'own_review_pilot_labels.csv',index=False)
result=[]
for appid,g in pd.DataFrame(rows).groupby('appid'):
 result.append(dict(appid=int(appid),game=g.game.iloc[0],n=len(g),median_words=float(g.words.median()),**{k:int(g[k].sum()) for k in 'BEPFRU'}))
(R/'own_review_pilot_results.json').write_text(json.dumps(result,indent=2))
(R/'own_review_coding_manifest.json').write_text(json.dumps(dict(pilot_sha256=hashlib.sha256((R/'own_review_pilot.json').read_bytes()).hexdigest(),labels_sha256=hashlib.sha256((R/'own_review_pilot_labels.csv').read_bytes()).hexdigest(),
 note='Single reader, multi-label topic coding. Generic praise and genre analogies are not converted into inferred motives. Different review lengths prevent treating cross-game mention-rate differences as preference differences.'),indent=2))
print(pd.DataFrame(result).to_string(index=False))
