"""A2 ampliada: una petición por ficha; para cada hecho, ¿su frase (y su comparación) está tal cual y existe su ancla?""" import html, json, re, urllib.request as u, collections M = json.load(open("/w/medidas/a2_todos.json", encoding="utf-8")) def texto(h): h = re.sub(r"(?s)", " ", h); h = re.sub(r"(?s)<(script|style)[^>]*>.*?", " ", h) return html.unescape(re.sub(r"\s+", " ", re.sub(r"<[^>]+>", " ", h))) por = collections.defaultdict(list) for x in M: por[x["enlace"].partition("#")[0]].append(x) res = collections.defaultdict(lambda: {"n": 0, "bien": 0}); malos = [] for k, (ruta, xs) in enumerate(sorted(por.items())): try: h = u.urlopen("http://127.0.0.1:8072" + ruta, timeout=120).read().decode() except Exception as e: h = "" tx = texto(h) for x in xs: a = x["enlace"].partition("#")[2] ok = re.sub(r"\s+", " ", x["frase"]) in tx and (not x["pares"] or re.sub(r"\s+", " ", x["pares"]) in tx) and (not a or f'id="{a}"' in h) r = res[x["tipo"]]; r["n"] += 1; r["bien"] += ok if not ok: malos.append([x["id"], x["enlace"]]) print(json.dumps({"fichas": len(por), "por_tipo": res, "malos": malos[:50], "n_malos": len(malos)}, ensure_ascii=False, indent=1))