# -*- coding: utf-8 -*- """Validate and merge the hand-written scores/comments.""" import json, os, re, sys, pickle sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import roles BASE = os.path.dirname(os.path.abspath(__file__)) DATA = os.path.join(BASE, 'data') def load_comments(): recs = {} for fn in ['comments_w.txt', 'comments_u.txt', 'comments_b.txt', 'comments_r.txt', 'comments_g.txt', 'comments_m.txt', 'comments_c.txt']: path = os.path.join(DATA, fn) for ln, line in enumerate(open(path, encoding='utf-8'), 1): line = line.rstrip('\n') if not line.strip(): continue parts = line.split('|') if len(parts) != 3: raise SystemExit(f'{fn}:{ln} bad fields: {line[:80]}') idx, score, comment = parts idx = int(idx); score = float(score) if idx in recs: raise SystemExit(f'duplicate idx {idx} in {fn}:{ln}') recs[idx] = {'score': score, 'comment': comment, 'file': fn, 'line': ln} return recs def main(): cards = json.load(open(os.path.join(DATA, 'cards.json'), encoding='utf-8')) recs = load_comments() missing = [c['idx'] for c in cards if c['idx'] not in recs] extra = [i for i in recs if i not in {c['idx'] for c in cards}] print('cards', len(cards), 'comments', len(recs), 'missing', missing, 'extra', extra[:20]) idx = pickle.load(open(os.path.join(BASE, 'pool.pkl'), 'rb')) pool = {c['name'].lower(): c for c in idx['pool']} trk = {c['name'].lower(): c for c in cards} unresolved = [] for c in cards: r = recs.get(c['idx']) if not r: continue c['score'] = r['score'] c['comment'] = r['comment'] for name in re.findall(r'\[\[([^\]]+)\]\]', r['comment']): key = name.lower() if key not in pool and key not in trk: unresolved.append((c['idx'], c['name'], name)) print('unresolved refs', len(unresolved)) for u in unresolved: print(' ', u) # score distribution from collections import Counter print('score dist', sorted(Counter(round(c.get('score', 0), 1) for c in cards).items())) # comment length lens = [len(c.get('comment', '')) for c in cards] print('comment len min/avg/max', min(lens), sum(lens) // len(lens), max(lens)) # save merged with open(os.path.join(DATA, 'cards_scored.json'), 'w', encoding='utf-8') as f: json.dump(cards, f, ensure_ascii=False, indent=1) print('saved cards_scored.json') if __name__ == '__main__': main()