"""E1c · cotejo A: clasificación final (DEFINICIONES_PREVIAS §7.2) y aceptación C-A0…C-A4 con lo leído por 07_leer_A.py (A_anuncios_lectura.csv, A_parecidas_lectura.csv). Adaptado de e1b/py/16_evaluar_A_r2.py: la comprobación e es el resultado (desierto ↔ adjudicado = E-DES; desistimiento o renuncia = E-RES) y el número de ofertas solo se anota. Escribe cotejo/A_resultado.csv y salidas/09_cotejo_A.json.""" import collections, csv, datetime as dt, json C = '/e1c/cotejo/' an = list(csv.DictReader(open(C + 'A_anuncios_lectura.csv'))) ps = list(csv.DictReader(open(C + 'A_parecidas_lectura.csv'))) out = [] for p in ps: r = dict(p); err = [] if p.get('estado') != 'leida': r['clase'] = 'NL'; out.append(r); continue if p.get('organo') in ('ERROR', 'anuncio_no_legible'): err.append('E-ORG' if p.get('organo') == 'ERROR' else 'E-ORG?') if p.get('fuente_cpv_nota'): r['clase'] = 'NL'; r['nota_nl'] = 'CPV del lote no legible'; out.append(r); continue cpvs = [c for c in (p.get('fuente_cpvs') or '').split(';') if c] if not cpvs or any(c[:2] != p['nuestro_cpv'] for c in cpvs): err.append('E-CPV') fa = dt.date.fromisoformat(p['nuestro_fecha_adj']) if p['nuestro_fecha_adj'] else None if p.get('fuente_award'): if p['fuente_award'] != p['nuestro_fecha_adj']: err.append('E-AÑO') else: pd_ = (p.get('primer_doc_resultado') or '')[:10] if not (fa and pd_ and fa <= dt.date.fromisoformat(pd_) <= fa + dt.timedelta(days=365)): err.append('E-AÑO') if not ('2023' <= p['nuestro_anio'] <= '2025'): err.append('E-AÑO') fr = p.get('fuente_resultado') if fr in ('desistido', 'otro', None, ''): err.append('E-RES') elif fr != p['nuestro_resultado']: err.append('E-DES') r['errores'] = ' '.join(sorted(set(err))) r['clase'] = 'verificada' out.append(r) ver = [r for r in out if r['clase'] == 'verificada'] leg_an = [a for a in an if a['legible'] == 'True'] cnt = collections.Counter(e for r in ver for e in r['errores'].split()) an_ok = [a for a in leg_an if a['organo_ok'] == 'True' and a['cpv_ok'] == 'True' and a['fin_ok'] == 'True'] n = max(1, len(ver)) res = {'parecidas': len(out), 'verificadas': len(ver), 'no_legibles': len(out) - len(ver), 'verificadas_desiertas_nuestras': sum(1 for r in ver if r['nuestro_resultado'] == 'desierto'), 'anuncios_legibles': len(leg_an), 'errores': dict(cnt), 'resultado_igual': sum(1 for r in ver if not ({'E-DES', 'E-RES'} & set(r['errores'].split()))), 'correctas_todo': sum(1 for r in ver if not r['errores']), 'organo_tipos': dict(collections.Counter(r.get('organo') for r in ver)), 'fuente_resultado': dict(collections.Counter(r.get('fuente_res') for r in ver)), 'anuncios_ok': len(an_ok), 'anuncios_detalle': [{k: a.get(k) for k in ('anuncio', 'organo_ok', 'cpv_ok', 'fin_ok', 'fin_fuente', 'fin_nuestro', 'rectificaciones_posteriores')} for a in leg_an if a not in an_ok], 'parecidas_con_error': [{k: r.get(k) for k in ('anuncio', 'k', 'licitacion_id', 'lote', 'nuestro_resultado', 'fuente_resultado', 'fuente_codigos', 'fuente_texto', 'nuestro_fecha_adj', 'fuente_award', 'primer_doc_resultado', 'errores', 'docs_posteriores_corte')} for r in ver if r['errores']]} res['C_A0_legibilidad'] = len(ver) >= 36 and len(leg_an) >= 19 res['C_A1_resultado'] = res['resultado_igual'] / n >= 0.95 res['C_A2_organo'] = cnt.get('E-ORG', 0) == 0 and cnt.get('E-ORG?', 0) == 0 and all(a['organo_ok'] == 'True' for a in leg_an) res['C_A3_todo'] = res['correctas_todo'] / n >= 0.95 res['C_A4_anuncios'] = len(an_ok) / max(1, len(leg_an)) >= 0.95 res['PASA_A'] = all(res[k] for k in ('C_A0_legibilidad', 'C_A1_resultado', 'C_A2_organo', 'C_A3_todo', 'C_A4_anuncios')) campos = [] for f in out: for c in f: if c not in campos: campos.append(c) with open(C + 'A_resultado.csv', 'w', newline='') as fh: w = csv.DictWriter(fh, fieldnames=campos); w.writeheader(); w.writerows(out) json.dump(res, open('/e1c/salidas/09_cotejo_A.json', 'w'), indent=1, ensure_ascii=False) print(json.dumps(res, indent=1, ensure_ascii=False))