mirror of
https://github.com/wiltodelta/remove-ai-watermarks.git
synced 2026-12-05 03:33:39 +01:00
82 lines
3.5 KiB
Python
82 lines
3.5 KiB
Python
"""Generate the text cleaner's registered variation pairs from pinned Unicode data."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import argparse
|
|
import hashlib
|
|
import sys
|
|
import textwrap
|
|
from collections import defaultdict
|
|
from pathlib import Path
|
|
|
|
UNICODE_DATA_SHA256 = "f55100b2fb11d3d75a37b8c1ab752192dbd1c4b12328c5ec6b38e3807c0ca597"
|
|
EMOJI_DATA_SHA256 = "bb3d09ef03f206012c7532dd52dc0a21c9efddba0135ea4cf0d9201b8b9bba7e"
|
|
IVD_DATA_SHA256 = "2b466659c2bfde1c52e60bd9fc883ab14dfa5c583df024b80352420265d2cd87"
|
|
|
|
|
|
def _groups(raw: bytes) -> dict[int, set[int]]:
|
|
groups: dict[int, set[int]] = defaultdict(set)
|
|
for line in raw.decode("utf-8").splitlines():
|
|
payload = line.split("#", 1)[0].strip()
|
|
if not payload:
|
|
continue
|
|
base, selector = (int(token, 16) for token in payload.split(";", 1)[0].split())
|
|
if 0xFE00 <= selector <= 0xFE0F or 0xE0100 <= selector <= 0xE01EF:
|
|
groups[selector].add(base)
|
|
return groups
|
|
|
|
|
|
def render(raw: bytes, emoji_raw: bytes, ivd_raw: bytes) -> str:
|
|
sources = (
|
|
(raw, UNICODE_DATA_SHA256, "Unicode 17.0 StandardizedVariants.txt", 1293),
|
|
(emoji_raw, EMOJI_DATA_SHA256, "Unicode 17.0 emoji-variation-sequences.txt", 742),
|
|
(ivd_raw, IVD_DATA_SHA256, "Unicode 2026-08-03 IVD_Sequences.txt", 29642),
|
|
)
|
|
groups: dict[int, set[int]] = defaultdict(set)
|
|
for data, expected_digest, name, count in sources:
|
|
if hashlib.sha256(data).hexdigest() != expected_digest:
|
|
raise ValueError(f"Use the exact {name} to regenerate.")
|
|
registered = _groups(data)
|
|
if sum(map(len, registered.values())) != count:
|
|
raise ValueError(f"Unexpected count of registered variation pairs in {name}.")
|
|
for selector, bases in registered.items():
|
|
groups[selector].update(bases)
|
|
out = [
|
|
'"""Registered standardized, emoji presentation, and ideographic variation pairs.',
|
|
"",
|
|
"Source: https://www.unicode.org/Public/17.0.0/ucd/StandardizedVariants.txt",
|
|
f"Source SHA-256: {UNICODE_DATA_SHA256}",
|
|
"Source: https://www.unicode.org/Public/17.0.0/ucd/emoji/emoji-variation-sequences.txt",
|
|
f"Source SHA-256: {EMOJI_DATA_SHA256}",
|
|
"Source: https://www.unicode.org/ivd/data/2026-08-03/IVD_Sequences.txt",
|
|
f"Source SHA-256: {IVD_DATA_SHA256}",
|
|
"Derived from data copyright 2025-2026 Unicode, Inc. under Unicode License V3.",
|
|
"See licenses/unicode-v3.txt. Generated by scripts/generate_text_unicode_variants.py.",
|
|
'"""',
|
|
"",
|
|
"",
|
|
"def _bases(hex_codepoints: str) -> frozenset[str]:",
|
|
" return frozenset(chr(int(value, 16)) for value in hex_codepoints.split())",
|
|
"",
|
|
"",
|
|
"STANDARDIZED_VARIATION_BASES = {",
|
|
]
|
|
for selector, bases in sorted(groups.items()):
|
|
content = " ".join(f"{base:X}" for base in sorted(bases))
|
|
if len(content) <= 96:
|
|
out.append(f' 0x{selector:04X}: _bases("{content}"),')
|
|
else:
|
|
out.append(f" 0x{selector:04X}: _bases(")
|
|
out.extend(f' "{line} "' for line in textwrap.wrap(content, width=100))
|
|
out.append(" ),")
|
|
return "\n".join([*out, "}", ""])
|
|
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("data", type=Path)
|
|
parser.add_argument("emoji_data", type=Path)
|
|
parser.add_argument("ivd_data", type=Path)
|
|
args = parser.parse_args()
|
|
sys.stdout.write(render(args.data.read_bytes(), args.emoji_data.read_bytes(), args.ivd_data.read_bytes()))
|