"""CPV1-v2 · paso 42 (Python puro): la línea base de la v1 (§7), «la división más frecuente del órgano» (si no, la de tipo × menor; si no, la global), con su confianza, entrenada con E ≤ 2025 (≤ 2025 sin cada fuente S para S-2026), para E 2026. Escribe interno/pred/base__E2026.tsv y base__E2026_.tsv (id, pred, conf). Uso: python 42_base.py FUENTES_LOSO(coma)""" import sys, collections sys.path.insert(0, '/w/py') import cpvtexto as ct D = '/w/interno/datos'; P = '/w/interno/pred' LOSO = sys.argv[1].split(',') co = collections.Counter(); ct_ = collections.Counter(); ev = [] with open(f'{D}/E.tsv', encoding='utf-8') as f: cab = f.readline().rstrip('\n').split('\t') I = {c: i for i, c in enumerate(cab)} for l in f: r = l.rstrip('\n').split('\t') if not ct.terminos(r[I['objeto']]): continue a = int(r[I['anio_ref']]); src = r[I['source']]; org = r[I['organo_clave']] tm = (r[I['tipo_contrato']], r[I['es_menor']]); y = r[I['div']] if a <= 2025: if org: co[(src, org, y)] += 1 ct_[(src, tm, y)] += 1 elif a == 2026: ev.append((r[I['id_canonical']], src, org, tm)) def tabla(cont, excl=None): o = collections.defaultdict(collections.Counter) for (src, key, y), n in cont.items(): if src != excl: o[key][y] += n return o def moda(c): tot = sum(c.values()) y = min(c, key=lambda d: (-c[d], d)) return y, c[y] / tot def predice(nombre, excl=None): to = tabla(co, excl); tt = tabla(ct_, excl) glob = collections.Counter() for c in tt.values(): glob.update(c) mo = {k: moda(c) for k, c in to.items()}; mt = {k: moda(c) for k, c in tt.items()}; mg = moda(glob) n = 0 with open(f'{P}/base__{nombre}.tsv', 'w', encoding='utf-8') as fo: fo.write('id\tpred\tconf\n') for idc, src, org, tm in ev: if excl and src != excl: continue y, c = mo.get(org) or mt.get(tm) or mg fo.write(f'{idc}\t{y}\t{c!r}\n'); n += 1 print(nombre, n, flush=True) predice('E2026') for s in LOSO: predice(f'E2026_{s}', excl=s)