Created
September 4, 2026 07:52
-
-
Save raeq/85cf246818fed9706ca1c45577ed2ab4 to your computer and use it in GitHub Desktop.
raeq/disarm#972: every disarm surface on the Emoji Attack construction (emoji inside a word) — the meta-benchmark ladder, reproduction
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """Every disarm surface on the Emoji Attack construction: an Emoji_Presentation code point inside a word. | |
| The ladder is the meta-benchmark's (`benchmarks/meta/suites/academic.py`, EmojiDelimiterSegmentation): | |
| rejoined (emoji gone, word restored) > survives (still two runs) > widened (a name or gloss: | |
| extra runs) / substituted (became letters) / destroyed (carrier lost). Reads emoji-data.txt from | |
| $DISARM_META_EMOJI_DATA, else downloads it from unicode.org. Needs disarm importable; run from a | |
| checkout for the profile names. Expected output on disarm 0.15.0 at 43e24b6b: | |
| disarm 0.15.0; Emoji_Presentation code points: 1219 | |
| surface rejoined survives widened subst destroyed aa🔥bb -> | |
| profile:code_context 0 1219 0 0 0 'aa🔥bb' | |
| profile:library_catalog_key_eu 1206 1 12 0 0 'aabb' | |
| profile:llm_guardrail 0 1206 0 13 0 'aa🔥bb' | |
| profile:ml_corpus_normalize 26 0 1180 13 0 'aa fire bb' | |
| profile:normalize_web_input 0 1206 0 13 0 'aa🔥bb' | |
| profile:rag_ingest 1206 1 12 0 0 'aabb' | |
| profile:scholarly_cyrillic_iso9 1206 1 12 0 0 'aabb' | |
| profile:search_index 1206 1 12 0 0 'aabb' | |
| canonicalize 0 1206 0 13 0 'aa🔥bb' | |
| strip_obfuscation 0 1206 0 13 0 'aa🔥bb' | |
| normalize_confusables 0 1219 0 0 0 'aa🔥bb' | |
| ml_normalize 26 0 1180 13 0 'aa fire bb' | |
| strip_format 0 1219 0 0 0 'aa🔥bb' | |
| demojize 0 26 1193 0 0 'aa fire bb' | |
| TextPipeline(demojize=True) 26 0 1193 0 0 'aa fire bb' | |
| TextPipeline(transliterate=True) 1206 0 0 13 0 'aabb' | |
| """ | |
| from __future__ import annotations | |
| import os | |
| import urllib.request | |
| import disarm | |
| URL = "https://www.unicode.org/Public/UCD/latest/ucd/emoji/emoji-data.txt" | |
| LEFT, RIGHT = "aa", "bb" | |
| def emoji_presentation() -> list[int]: | |
| path = os.environ.get("DISARM_META_EMOJI_DATA") | |
| text = open(path, encoding="utf-8").read() if path else urllib.request.urlopen(URL, timeout=30).read().decode() | |
| cps: set[int] = set() | |
| for raw in text.splitlines(): | |
| line = raw.split("#")[0].strip() | |
| if not line or ";" not in line: | |
| continue | |
| parts = [p.strip() for p in line.split(";")] | |
| if len(parts) < 2 or parts[1] != "Emoji_Presentation": | |
| continue | |
| if ".." in parts[0]: | |
| lo, hi = parts[0].split("..") | |
| cps.update(range(int(lo, 16), int(hi, 16) + 1)) | |
| else: | |
| cps.add(int(parts[0], 16)) | |
| return sorted(cps) | |
| def runs(text: str) -> int: | |
| n, inside = 0, False | |
| for ch in text: | |
| if ch.isalnum(): | |
| if not inside: | |
| n += 1 | |
| inside = True | |
| else: | |
| inside = False | |
| return n | |
| def ladder(fn, cps): | |
| c = dict(rejoined=0, survives=0, widened=0, substituted=0, destroyed=0) | |
| clean = LEFT + RIGHT | |
| for cp in cps: | |
| attacked = LEFT + chr(cp) + RIGHT | |
| try: | |
| out = fn(attacked) | |
| except Exception: # noqa: BLE001 — a refusal leaves the text alone, as the harness does | |
| out = attacked | |
| r = runs(out) | |
| if out == clean: | |
| c["rejoined"] += 1 | |
| elif LEFT not in out or RIGHT not in out: | |
| c["destroyed"] += 1 | |
| elif r <= 1: | |
| c["substituted"] += 1 | |
| elif r == 2: | |
| c["survives"] += 1 | |
| else: | |
| c["widened"] += 1 | |
| return c | |
| def main() -> None: | |
| cps = emoji_presentation() | |
| print(f"disarm {disarm.__version__}; Emoji_Presentation code points: {len(cps)}") | |
| surfaces = {f"profile:{p}": disarm.get_pipeline(p) for p in disarm.list_profiles()} | |
| for name in ("canonicalize", "strip_obfuscation", "normalize_confusables", "ml_normalize", "strip_format"): | |
| surfaces[name] = getattr(disarm, name) | |
| surfaces["demojize"] = disarm.demojize | |
| surfaces["TextPipeline(demojize=True)"] = disarm.TextPipeline(demojize=True) | |
| surfaces["TextPipeline(transliterate=True)"] = disarm.TextPipeline(transliterate=True) | |
| print(f"{'surface':36s} {'rejoined':>9s} {'survives':>9s} {'widened':>8s} {'subst':>6s} {'destroyed':>9s} aa🔥bb ->") | |
| for name, fn in surfaces.items(): | |
| c = ladder(fn, cps) | |
| try: | |
| sample = repr(fn("aa\U0001F525bb")) | |
| except Exception as exc: # noqa: BLE001 | |
| sample = "err " + type(exc).__name__ | |
| print(f"{name:36s} {c['rejoined']:>9d} {c['survives']:>9d} {c['widened']:>8d} {c['substituted']:>6d} {c['destroyed']:>9d} {sample}") | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment