"""REL1 · 42 · Cotejo de los vínculos UTE de la muestra de 30 (§11): ¿hay en unified una adjudicación a una UTE cuyo nombre recoge a las dos empresas (o, en ute_socios, la fila de ejemplo con los dos socios)? Salida interno/42_cotejo_ute.json (700, con nombres).""" import duckdb, json, re, os, unicodedata con = duckdb.connect(); x = con.execute x("SET threads=6; SET memory_limit='8GB'") C = json.load(open('/r/interno/40_cotejo.json')) x("CREATE VIEW u AS SELECT * FROM read_parquet('/d/unified_20261025/*/*/*.parquet', hive_partitioning=1, union_by_name=1)") x("""CREATE TABLE ute AS SELECT DISTINCT source, anio_ref, adjudicatario_nombre n, upper(strip_accents(adjudicatario_nombre)) nu, id_canonical FROM u WHERE unidad = 'adjudicacion' AND (regexp_matches(upper(coalesce(adjudicatario_nombre, '')), '\\bU\\.?T\\.?E\\b|UNION TEMPORAL|UNIÓN TEMPORAL') OR upper(coalesce(adjudicatario_nif, '')) LIKE 'U%')""") print('filas UTE', x("SELECT count(*) FROM ute").fetchone()) STOP = {'SA', 'SL', 'SAU', 'SLU', 'S', 'A', 'L', 'U', 'DE', 'DEL', 'LA', 'EL', 'LOS', 'LAS', 'Y', 'E', 'SOCIEDAD', 'ANONIMA', 'LIMITADA', 'GRUPO', 'CONSTRUCCIONES', 'SERVICIOS', 'EMPRESA', 'COMPANIA', 'CIA', 'Y', 'INGENIERIA', 'OBRAS', 'ESPANA'} def tokens(nm): t = re.sub(r'[^A-Z0-9 ]', ' ', ''.join(ch for ch in unicodedata.normalize('NFKD', (nm or '').upper()) if not unicodedata.combining(ch))).split() return [w for w in t if w not in STOP and len(w) >= 3][:2] out = [] for c in C: if not any(t.startswith('UTE') for t in c['tipos']): continue ta, tb = tokens(c['emp_a']['nombre_identidad']), tokens(c['emp_b']['nombre_identidad']) cond = ' AND '.join([f"nu LIKE '%{w}%'" for w in ta + tb]) or 'false' rows = x(f"SELECT source, anio_ref, n FROM ute WHERE {cond} ORDER BY anio_ref LIMIT 5").fetchall() nrows = x(f"SELECT count(*) FROM ute WHERE {cond}").fetchone()[0] ej = [] for u, socs in c.get('ute_comun', []): for s in socs: r = x("SELECT source, anio_ref, adjudicatario_nombre FROM u WHERE id_canonical = ? LIMIT 1", [s[2]]).fetchone() if s[2] else None ej.append({'ute_clave': u, 'socio': s[0], 'via': s[1], 'fila_ejemplo': r}) out.append({'n': c['n'], 'fuente': c['fuente'], 'tipos': c['tipos'], 'a': c['emp_a']['nombre_identidad'], 'b': c['emp_b']['nombre_identidad'], 'tokens': [ta, tb], 'ute1': c.get('ute1'), 'filas_ute_con_los_dos': nrows, 'ejemplos': rows, 'ute_socios': ej}) json.dump(out, open('/r/interno/42_cotejo_ute.json', 'w'), ensure_ascii=False, indent=1, default=str) os.chmod('/r/interno/42_cotejo_ute.json', 0o600) print(json.dumps([{'n': o['n'], 'filas': o['filas_ute_con_los_dos'], 'ute_socios': len(o['ute_socios'])} for o in out]))