"""CPV1 · paso 51 (Python puro): cruza las etiquetas a ciegas (interno/cotejo/etiquetas.tsv: n, principal, aceptables (coma), grupo, nota) con la clave (interno/cotejo/clave.tsv). Pre-registro §9 y A4 (≥ 42/50 correctas entre las emitidas). Escribe salidas/51_cotejo.json (cifras, sin textos) e interno/cotejo/cruce.tsv (caso a caso).""" import json, math C = '/w/interno/cotejo' def lee(p): with open(p, encoding='utf-8') as f: cab = f.readline().rstrip('\n').split('\t') return {r[0]: dict(zip(cab, r)) for r in (l.rstrip('\n').split('\t') for l in f)} et = lee(f'{C}/etiquetas.tsv'); cl = lee(f'{C}/clave.tsv'); cg = lee(f'{C}/ciega.tsv') assert set(et) == set(cl), 'faltan etiquetas' def wilson(k, n, z=1.96): p = k / n; d = 1 + z * z / n; c = (p + z * z / (2 * n)) / d; h = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / d return [round(c - h, 3), round(c + h, 3)] res = {} with open(f'{C}/cruce.tsv', 'w', encoding='utf-8') as fo: fo.write('n\tmuestra\tfuente\tobjeto\tprincipal\taceptables\testimada\tp1\tcorrecta\testricta\tgrupo_lector\tgrupo_emitido\tgrupo_ok\n') for n in sorted(et, key=int): e, k = et[n], cl[n] acept = {e['principal']} | set(filter(None, e['aceptables'].split(','))) ok = k['d1'] in acept; est = k['d1'] == e['principal'] gem = k['g1'] if k['grupo_emitido'] == '1' else '' gok = '' if not gem or not e['grupo'] else str(gem == e['grupo']) r = res.setdefault(k['muestra'], {'n': 0, 'correctas': 0, 'estrictas': 0, 'grupos_emitidos': 0, 'grupos_juzgables': 0, 'grupos_ok': 0, 'por_fuente': {}}) r['n'] += 1; r['correctas'] += ok; r['estrictas'] += est pf = r['por_fuente'].setdefault(cg[n]['fuente'], [0, 0]); pf[0] += 1; pf[1] += ok if gem: r['grupos_emitidos'] += 1 if e['grupo']: r['grupos_juzgables'] += 1; r['grupos_ok'] += (gem == e['grupo']) fo.write('\t'.join([n, k['muestra'], cg[n]['fuente'], cg[n]['objeto'][:160], e['principal'], e['aceptables'], k['d1'], k['p1'][:6], str(ok), str(est), e['grupo'], gem, gok]) + '\n') for m, r in res.items(): r['ic_correctas'] = wilson(r['correctas'], r['n']) res['A4'] = res['E']['correctas'] >= 42 gj = res['E']['grupos_juzgables'] res['G3'] = None if res['E']['grupos_emitidos'] < 20 else (res['E']['grupos_ok'] / gj >= 0.80 if gj else None) json.dump(res, open('/w/salidas/51_cotejo.json', 'w'), ensure_ascii=False, indent=1) print(json.dumps(res, ensure_ascii=False, indent=1))