Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / keyboard / generate_emoji.py
generate_emoji.py127 lines · 4.9 KiBRaw
| 1 | #!/usr/bin/env python3 |
| 2 | """Offline generator: Unicode emoji data -> keyboard/src/main/assets/emoji.txt |
| 3 | |
| 4 | The keyboard shipped with ~135 hardcoded emoji and no names, which is enough to browse |
| 5 | and impossible to search. This builds the full RGI set with the CLDR short name and |
| 6 | search keywords for each one, so the emoji page can offer a search box. |
| 7 | |
| 8 | Output format is `emoji<TAB>group<TAB>name<TAB>keyword|keyword|...`, in the source |
| 9 | file's CLDR order (which is the order emoji palettes are meant to use), one entry per |
| 10 | line. EmojiData.kt parses it. |
| 11 | |
| 12 | Only fully-qualified sequences are kept: minimally-qualified and unqualified entries are |
| 13 | the same emoji missing a variation selector, so keeping them would show visual duplicates. |
| 14 | |
| 15 | Skin-tone and hair-component variants are dropped. They multiply the set roughly fivefold |
| 16 | without adding anything searchable -- every one of them has the same CLDR keywords as its |
| 17 | base emoji, so they would only push real matches out of the results. |
| 18 | |
| 19 | Sources (downloaded at generation time, pinned by version so the asset is reproducible): |
| 20 | https://unicode.org/Public/emoji/15.1/emoji-test.txt |
| 21 | the ordered, grouped, fully-qualified emoji list with CLDR short names |
| 22 | https://raw.githubusercontent.com/unicode-org/cldr/main/common/annotations/en.xml |
| 23 | the English search keywords |
| 24 | |
| 25 | Usage: python3 scripts/keyboard/generate_emoji.py |
| 26 | """ |
| 27 | |
| 28 | import re |
| 29 | import urllib.request |
| 30 | import xml.etree.ElementTree as ET |
| 31 | from pathlib import Path |
| 32 | |
| 33 | REPO = Path(__file__).resolve().parents[2] |
| 34 | OUTPUT = REPO / "keyboard" / "src" / "main" / "assets" / "emoji.txt" |
| 35 | |
| 36 | EMOJI_TEST_URL = "https://unicode.org/Public/emoji/15.1/emoji-test.txt" |
| 37 | CLDR_ANNOTATIONS_URL = ( |
| 38 | "https://raw.githubusercontent.com/unicode-org/cldr/main/common/annotations/en.xml" |
| 39 | ) |
| 40 | |
| 41 | # Skin-tone modifiers (U+1F3FB..U+1F3FF) and the hair components. An emoji carrying any of |
| 42 | # these is a variant of one already in the list. |
| 43 | VARIANT_CODEPOINTS = { |
| 44 | "1F3FB", "1F3FC", "1F3FD", "1F3FE", "1F3FF", # skin tones |
| 45 | "1F9B0", "1F9B1", "1F9B2", "1F9B3", # red/curly/bald/white hair |
| 46 | } |
| 47 | |
| 48 | VARIATION_SELECTOR = "\ufe0f" |
| 49 | |
| 50 | # `1F600 ; fully-qualified # 😀 E1.0 grinning face` |
| 51 | DATA_LINE = re.compile( |
| 52 | r"^(?P<codes>[0-9A-F ]+?)\s*;\s*(?P<status>[a-z-]+)\s*#\s*(?P<emoji>\S+)\s+E[\d.]+\s+(?P<name>.+)$" |
| 53 | ) |
| 54 | |
| 55 | HEADER = """\ |
| 56 | # Emoji palette + search index for the offline keyboard. |
| 57 | # Format: one entry per line, "emoji<TAB>group<TAB>name<TAB>keyword|keyword|...". |
| 58 | # Order is CLDR palette order; groups appear in the order their first entry does. |
| 59 | # |
| 60 | # GENERATED FILE -- do not edit by hand. |
| 61 | # Regenerate with: python3 scripts/keyboard/generate_emoji.py |
| 62 | # Sources: unicode.org emoji-test.txt 15.1 (list + names), CLDR common/annotations/en.xml |
| 63 | # (keywords). Fully-qualified sequences only; skin-tone and hair variants dropped. |
| 64 | """ |
| 65 | |
| 66 | |
| 67 | def fetch(url: str) -> str: |
| 68 | with urllib.request.urlopen(url, timeout=120) as response: |
| 69 | return response.read().decode("utf-8") |
| 70 | |
| 71 | |
| 72 | def load_keywords() -> dict[str, list[str]]: |
| 73 | """CLDR search keywords per emoji. Keys are stripped of variation selectors.""" |
| 74 | root = ET.fromstring(fetch(CLDR_ANNOTATIONS_URL)) |
| 75 | keywords: dict[str, list[str]] = {} |
| 76 | for annotation in root.iter("annotation"): |
| 77 | # type="tts" is the spoken name, which emoji-test.txt already gives us. |
| 78 | if annotation.get("type") == "tts": |
| 79 | continue |
| 80 | cp = (annotation.get("cp") or "").replace(VARIATION_SELECTOR, "") |
| 81 | text = annotation.text or "" |
| 82 | if not cp or not text: |
| 83 | continue |
| 84 | keywords[cp] = [w.strip() for w in text.split("|") if w.strip()] |
| 85 | return keywords |
| 86 | |
| 87 | |
| 88 | def main() -> None: |
| 89 | keywords = load_keywords() |
| 90 | |
| 91 | group = "" |
| 92 | lines = [] |
| 93 | groups: list[str] = [] |
| 94 | for raw in fetch(EMOJI_TEST_URL).splitlines(): |
| 95 | line = raw.strip() |
| 96 | if line.startswith("# group:"): |
| 97 | group = line.removeprefix("# group:").strip() |
| 98 | continue |
| 99 | if not line or line.startswith("#"): |
| 100 | continue |
| 101 | match = DATA_LINE.match(line) |
| 102 | if not match or match["status"] != "fully-qualified": |
| 103 | continue |
| 104 | codes = match["codes"].split() |
| 105 | if VARIANT_CODEPOINTS.intersection(codes): |
| 106 | continue |
| 107 | |
| 108 | emoji = match["emoji"] |
| 109 | name = match["name"] |
| 110 | # CLDR keys have variation selectors stripped; the palette entries do not. |
| 111 | words = keywords.get(emoji.replace(VARIATION_SELECTOR, ""), []) |
| 112 | # The name is already searchable on its own, so it is not repeated as a keyword. |
| 113 | words = [w for w in words if w != name] |
| 114 | |
| 115 | if group not in groups: |
| 116 | groups.append(group) |
| 117 | lines.append(f"{emoji}\t{group}\t{name}\t{'|'.join(words)}") |
| 118 | |
| 119 | OUTPUT.parent.mkdir(parents=True, exist_ok=True) |
| 120 | OUTPUT.write_text(HEADER + "\n".join(lines) + "\n", encoding="utf-8") |
| 121 | |
| 122 | print(f"wrote {OUTPUT.relative_to(REPO)}") |
| 123 | print(f" {len(lines)} emoji across {len(groups)} groups: {', '.join(groups)}") |
| 124 | |
| 125 | |
| 126 | if __name__ == "__main__": |
| 127 | main() |