"""FAM1 · lectura de nombres del BORME («APELLIDO1 APELLIDO2 NOMBRE...») en apellidos normalizados. Reglas fijadas antes de cruzar (DEFINICIONES_PREVIAS §3). Sin pandas: los listados del INE se leen con duckdb (o pyarrow en la impl2).""" import re, unicodedata PART = {'DE', 'DEL', 'LA', 'LAS', 'LOS', 'Y', 'I', 'SAN', 'SANTA', 'DA', 'DO', 'DOS', 'DAS', 'VAN', 'VON', 'DER', 'DI', 'LE', 'MAC', 'MC', 'SANT', 'EL', 'AL', 'BEN', 'D'} PJ = re.compile(r'\b(SL|SA|SLU|SAU|SLL|SLP|SC|SCP|SCOOP|COOP|COOPERATIVA|SOCIEDAD|LIMITADA|ANONIMA|AIE|UTE|LTD|LIMITED|GMBH|BV|NV|INC|LLC|AG|SPA|SRL|SARL|SAS|PLC|CORP|' r'FUNDACION|ASOCIACION|AYUNTAMIENTO|BANCO|CAJA|HOLDING|GROUP|GRUPO|INVERSIONES|GESTION|SERVICIOS|CONSULTING|AUDITORES|ASESORES|ABOGADOS|' r'PARTICIPACIONES|CAPITAL|PATRIMONIAL|EMPRESA|COMPANY|COMPANIA|CIA|INTERNACIONAL|SOCIETE|LIMITADA|PROFESIONAL|ESPANA|IBERICA|SPAIN)\b') def nz(s): s = ''.join(c for c in unicodedata.normalize('NFKD', str(s).upper()) if not unicodedata.combining(c)) s = re.sub(r'\s*-\s*', '-', s) return re.sub(r'[^A-Z \-]', ' ', s) def clave(s): """clave de comparación de un apellido: solo letras A-Z (sin espacios ni guiones).""" return re.sub(r'[^A-Z]', '', nz(s)) _INE = {} def ine(con=None): if not _INE: import duckdb c = con or duckdb.connect() _INE['ap'] = {k: (f1, f2) for k, f1, f2 in c.execute("SELECT k, f1, coalesce(f2,0) FROM '/w/interno/datos/ine_apellidos.parquet'").fetchall()} _INE['nom'] = {k for (k,) in c.execute("SELECT k FROM '/w/interno/datos/ine_nombres.parquet'").fetchall()} _INE['nomtok'] = {t for k in _INE['nom'] for t in k.split()} return _INE def unidades(nombre): toks = [t for t in nz(nombre).split() if t] U = []; buf = [] for t in toks: if t in PART: buf.append(t); continue U.append(' '.join(buf + [t])); buf = [] if buf: if U: U[-1] = U[-1] + ' ' + ' '.join(buf) else: U = [' '.join(buf)] return U def leer_nombre(nombre): """-> dict(tipo, ap1, ap2, nom1, nunid). tipo: pj | vacio | un_apellido | nombre_en_apellido | ok""" r = {'tipo': None, 'ap1': None, 'ap2': None, 'nom1': None, 'nunid': 0} s = nz(nombre or '') if not s.strip() or re.search(r'[0-9]', str(nombre or '')): r['tipo'] = 'vacio'; return r if PJ.search(s): r['tipo'] = 'pj'; return r I = ine(); U = unidades(s) # apellido compuesto «X DE Y» (X seguido de unidad con partícula) cuando «X DE Y» está en el INE y quedan >= 3 unidades if len(U) >= 4 and ' ' in U[1] and clave(U[0] + ' ' + U[1]) in I['ap']: U = [U[0] + ' ' + U[1]] + U[2:] if len(U) >= 4 and ' ' in U[2] and clave(U[1] + ' ' + U[2]) in I['ap']: U = [U[0], U[1] + ' ' + U[2]] + U[3:] r['nunid'] = len(U) if len(U) < 3: r['tipo'] = 'un_apellido'; return r a1, a2 = clave(U[0]), clave(U[1]); n1 = U[2].split()[-1] r.update(ap1=a1, ap2=a2, nom1=clave(n1)) def solo_nombre(a): return a in I['nomtok'] and a not in I['ap'] if solo_nombre(a1) or solo_nombre(a2): r['tipo'] = 'nombre_en_apellido'; return r if not any(clave(t) in I['nomtok'] for u in U[2:] for t in u.split()): r['tipo'] = 'sin_nombre_ine'; return r r['tipo'] = 'ok'; return r