hex/android/scripts/build-emoji-suggestions.py

86 lines
3.8 KiB
Python
Executable file

#!/usr/bin/env python3
"""Builds the Android keyboard's English emoji suggestion list.
The iPhone keyboard's emoji index (ios/HEXShared/FullKeyboard/Emoji/emoji-index-en.txt)
is generated by ios/scripts/build-emoji-data.py from Unicode emoji-test.txt 18.0,
CLDR 49 annotations, and Hex's own chat words. The Android keyboard only needs a
whole word's best emoji, so this keeps the same 0.85 strong matches the iPhone strip
uses (EmojiSuggester.strongWeight), drops the phrase and search tables, and applies a
small reviewed extras and exclusion list.
python3 android/scripts/build-emoji-suggestions.py
Writes android/app/src/main/assets/emoji/suggestions-en.txt: one "word<TAB>emoji"
line per word, sorted. The word is already folded (lowercase, unaccented) by the
iOS index, so the keyboard only lowercases what is being typed.
"""
import pathlib
HERE = pathlib.Path(__file__).resolve().parent
REPO = HERE.parents[1]
SOURCE = REPO / "ios" / "HEXShared" / "FullKeyboard" / "Emoji" / "emoji-index-en.txt"
OUT = HERE.parent / "app" / "src" / "main" / "assets" / "emoji" / "suggestions-en.txt"
# The iPhone strip's own strong-match line (EmojiSuggester.strongWeight): a whole
# name scores 1.0 and Hex's chat words 0.9 to 1.2. Anything below that is a keyword
# that only shares a word with the emoji, which made common verbs and function words
# suggest unrelated pictures (see -> 🙈, well -> ❤‍🩹).
STRONG = 0.85
# Reviewed words the 0.85 line misses but the Android keyboard wants. "joy" is a
# CLDR keyword of 😂 (0.452) and reads as the emoji, unlike the rest of that tier.
EXTRAS = {"joy": "😂"}
# 0.85 whole-name matches that are ordinary words in another, much commoner sense.
# Reviewed against the English typing model's top 5,000 words.
EXCLUDE = {
# Number words name clock faces, not numbers ("two" is 🕑).
"two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirty",
# Ordinary words that only name the emoji's object by another sense.
"bat", "button", "cancer", "circled", "fly", "match", "mate", "pick",
"present", "pretty", "ray", "registered", "ring", "rip", "room", "sake",
"seal", "ship", "shipped", "show", "turkey", "watch",
}
# The examples the feature promises, checked after filtering so a source update
# cannot quietly drop one.
REQUIRED = {"smile": "😊", "joy": "😂", "haha": "😂", "heart": "❤️"}
HEADER = (
"# Generated by android/scripts/build-emoji-suggestions.py; do not edit.\n"
"# From the iOS keyboard's emoji-index-en.txt: Unicode emoji-test.txt 18.0 and\n"
"# CLDR 49 annotations (Unicode License v3, assets/licenses/unicode.txt), and\n"
"# Hex's own chat words (ios/scripts/emoji-aliases-en.txt).\n"
"# 0.85 strong matches, plus reviewed extras, minus reviewed exclusions.\n"
"# word<TAB>emoji\n"
)
def main():
emoji = []
rows = {}
for raw in SOURCE.read_text(encoding="utf-8").splitlines():
parts = raw.split("\t")
if parts[0] == "E":
emoji.append(parts[1])
elif parts[0] == "W":
word, postings = parts[1], parts[2]
entry, weight = postings.split(",", 1)[0].rsplit(":", 1)
if float(weight) >= STRONG:
rows[word] = emoji[int(entry)]
rows.update(EXTRAS)
for word in EXCLUDE:
rows.pop(word, None)
for word, expected in REQUIRED.items():
if rows.get(word) != expected:
raise SystemExit(f"{word}: expected {expected}, got {rows.get(word)}")
ordered = sorted(rows.items())
OUT.parent.mkdir(parents=True, exist_ok=True)
OUT.write_text(HEADER + "".join(f"{word}\t{e}\n" for word, e in ordered), encoding="utf-8")
print(f"{OUT.relative_to(REPO)}: {len(ordered)} words, {OUT.stat().st_size // 1024} KB")
if __name__ == "__main__":
main()