hex/android/benchmark/typing/build-capitals.py
Ryan Vogel f8cef15741
Android typing: next-letter touch areas, and French and Italian (#2260)
* Android typing: the likelier next letter claims a touch on its neighbour's edge

Ports iOS #2091's next-letter odds (FullKeyboardEngine.key(at:)): a touch
within 5 dp of a neighbouring letter's touch area presses that letter instead
when the word list makes it enough likelier to come next ("th" then a touch
on the e/d border types e; "an" then the same touch types d). Each dp claimed
costs 0.8 nats of odds; nothing changes inside a number or other non-word,
and an accented letter without a key counts as its base letter's.

- TypingModel.continuations(prefix): each letter's share of the word list's
  frequency after a prefix, read from the mapped model (the word start's and
  the last prefix's kept).
- TypingWord.likelierKey(before, hit, near, keys): the key to press, given the
  hit key, the neighbouring letters within reach with their distances in dp,
  and the keyboard's letters. The keyboard decides when it applies (autocorrect
  on, an ordinary text field, no caps lock), as iOS does.
- The replay applies it to every letter tap, with touch areas tiling the
  rows of the benchmark layout.

Verified on the Studio (API 35 emulator, installed debug APK, 2,000 held-out
sentences each), word errors before (dev be78a0a4) -> after:
- English 770 (5.55%) -> 721 (5.20%); today's iOS 798 (5.75%)
- Spanish 1,133 (8.52%) -> 1,090 (8.19%); today's iOS 1,205 (9.06%)
- German 774 (5.71%) -> 729 (5.38%)
- Bokmål 654 (5.24%) -> 647 (5.18%)
Sentences better/worse: 93/45, 72/34, 87/45, 47/40 (the typed letters
change, so sentences move both ways; every set is better on balance).
Strip hits after two letters rise in all four (English 4,390 -> 4,686).
testDebugUnitTest: 36 tests, 0 failures, with iOS's next-letter case
re-expressed.

* Android typing: French and Italian word models

Adds French and Italian keyboard models from Google Books Ngram v3 (CC BY
3.0), built like German's: every untagged shard of the language (fre 6/115,
ita 2/60), 1980-2019, spellings from the corpus alone (Hunspell fr is MPL,
it GPL, so no dictionary is imported), 100,000 words a model in the same 3.6
MB HXTM v3 format, and each with a capitals list (fr.capitals, it.capitals)
from the same 1-grams.

Elisions:
- The books keep many as one token ("c'est", "qu'il", "dell'anno"; French
  "l'" and "d'" are split off), so those stay words with their apostrophe,
  read without it being typed. The same letters run together that are ten
  times rarer than the apostrophe form ("cest", "quil", "jai"; 26 French, 4
  Italian forms) are left out of the vocabulary, so space puts it back.
- TypingWord.startsWord(before): a word typed right after an elided article
  or pronoun ("l'", "qu'", "dell'") is one the keyboard reads, so "l'hpmme"
  becomes "l'homme". previousWord takes an elided word without its
  apostrophe.
- In French and Italian a word whose accented spelling is a commoner model
  word is never capitalised from the list ("ecole" isn't "Ecole": books drop
  accents on capitals).

Safety and credits:
- never-suggest.tsv gains curated French (31 slur, 107 swear) and Italian (30
  slur, 112 swear) entries from LDNOOBW 5faf2ba (CC BY 4.0), as the other
  languages were: identity slurs always suppressed, profanity opt-in, ordinary
  senses kept (pédale, finocchio, scopare, chatte stay available), common
  inflections and missing slurs added.
- credits.txt and build.gradle.kts's verified asset list name the new files.

Benchmark: synthetic_de_nb.py is now synthetic_sets.py and also writes
French (AZERTY, the apostrophe typed on its key) and Italian (QWERTY, the
apostrophe an exact tap) sets from held-out Tatoeba sentences; the replay
follows the keyboard (an apostrophe ends the word as typed; a letter after
one starts a read word only after an elision).

Verified on the Studio (API 35 emulator, installed debug APK, 2,000 held-out
sentences each): French raw taps 5,316 (38.91%) -> 1,177 (8.62%), Italian
4,778 (39.73%) -> 895 (7.44%). English 721, Spanish 1,090, German 729,
Bokmål 647 unchanged. The keyboard has no French or Italian layout yet, so
none of this reaches a phone until it does. testDebugUnitTest: 37 tests, 0
failures; the never-suggest cases now run on all six models, plus an
elision case.

* Android typing: French and Italian accents the words around decide

French and Italian have pairs of words spelled with the same letters, with
and without an accent, that are both words ("a"/"à", "ou"/"où", "la"/"là",
"ca"/"ça", "du"/"dû", "sur"/"sûr"; Italian "e"/"è", "la"/"là", "li"/"lì",
"da"/"dà", "ne"/"né", "se"/"sé"). Typed without the accent, the decoder
keeps the word as typed, as iOS does with Apple's dictionary, which never
changes one word into another.

ContextWords now has those groups beside English's and chooses between them
with the word pairs, exactly as for English apostrophes (#2144): the word
before decides at the word's own space when it says so clearly, otherwise
the next word revises it ("je vais a paris" -> "je vais à Paris", "il a
faim" stays; "non e esagerato" -> "non è esagerato", "pane e burro" stays).
The accent counts as same-letter change (3 / 1.5 nat margins). English is
unchanged.

Verified on the Studio (API 35 emulator, installed debug APK, 2,000 held-out
sentences each): French 1,177 (8.62%) -> 947 (6.93%) word errors, 231
sentences better and 13 worse; Italian 895 (7.44%) -> 789 (6.56%), 105
better and none worse. English 721, Spanish 1,090, German 729 and Bokmål
647 unchanged. testDebugUnitTest: 38 tests, 0 failures, with French and
Italian cases beside the English ones.
2026-11-21 16:31:32 -05:00

195 lines
9.2 KiB
Python

#!/usr/bin/env python3
"""Build a keyboard language's capitals sidecar (<lang>.capitals) from Google Books Ngram v3
1-grams (2020-02-17, CC BY 3.0), the corpus its .hextm model was counted from.
python3 build-capitals.py en MODEL.hextm WORKDIR OUTPUT.capitals scowl-2020.12.07.tar.gz
python3 build-capitals.py {es|fr|it} MODEL.hextm WORKDIR OUTPUT.capitals
Every shard is streamed (curl | gzip -dc, never stored) and its 1980-2019 counts of all-lowercase
tokens and of tokens with only the first letter capitalised are cached in WORKDIR, so a rerun
resumes and a rule change renders from WORKDIR/counts.tsv in seconds. Tagged tokens ("_") and
other casings are ignored.
Each line is word<TAB>percent (the capitalised share, rounded half up), lowercase, sorted by its
UTF-8 bytes, with a trailing newline, as German's de.capitals:
- a model word whose share is at least 50% over at least 40 uses (the keyboard capitalises
it when typed in lowercase from 95%);
- a word of three letters or more outside the model that the books capitalise at least 90% of
the time and use at least once per million words: names and places ("tom", "olivia",
"tokio"), which the keyboard then takes for real words, as iOS's spelling dictionary does, and
capitalises from 95%. Rarer ones ("goyo", "eby") are more often a typo of a common word. Two model
words run together whose pair is ten times as common ("iwant", "apartir") are left out.
The keyboard capitalises a word typed in lowercase only where iOS's dictionary would know it only
with a capital, so a word that is also an ordinary lowercase word is held at 94% (listed, never
capitalised on its own): in English, one SCOWL (sizes through 70, the lists en.valid comes from)
spells in lowercase ("united", "jersey", "china"); in Spanish, French and Italian, a model word
under 98% ("lima", "granada", "san"). In French and Italian, a word whose accented spelling is a
commoner model word is held, so "ecole" isn't written "Ecole" (books drop accents on capitals:
"Ecole" is "école").
"""
import hashlib, math, multiprocessing, os, re, struct, subprocess, sys, tarfile, unicodedata
BASE = "https://storage.googleapis.com/books/ngrams/books/20200217"
FILES = {"en": ("eng", 24), "es": ("spa", 3), "fr": ("fre", 6), "it": ("ita", 2)}
LETTERS = {"en": "a-z", "es": "a-zñáéíóúü", "fr": "a-zàâçéèêëîïôùûüÿœæ", "it": "a-zàèéìíîòóùú"}
FIRST_YEAR, LAST_YEAR = 1980, 2019
MIN_TOTAL = 40
SOMETIMES = 50
NAME_SHARE = 90
NAME_PER_MILLION = 1
ALWAYS, HELD = 95, 94
JOINED = 10
MODEL_ALWAYS = {"en": 95, "es": 98, "fr": 98, "it": 98}
SCOWL_SHA256 = "5587667caa20c4891390c2d42dbb4d5c4c3f41bee77af1457ece3ba23fb859cc"
def scowl_lowercase(path):
"""SCOWL's lowercase spellings, sizes through 70."""
assert hashlib.sha256(open(path, "rb").read()).hexdigest() == SCOWL_SHA256
words = set()
with tarfile.open(path) as archive:
for entry in archive:
m = re.fullmatch(r".*/final/(english|british|american)-(words|contractions|proper-names|upper|abbreviations)\.(\d+)", entry.name)
if m and int(m[3]) <= 70:
words.update(w for w in archive.extractfile(entry).read().decode("latin-1").split() if w == w.lower())
return words
def model_words(path):
"""The model's words with -ln P(word), and its stored pairs (first, second) with -ln P(second | first)."""
data = open(path, "rb").read()
version, words, pairs, blob, alphabet = struct.unpack_from("<IIIII", data, 4)
assert data[:4] == b"HXTM" and version == 3
letters = {}
for i in range(alphabet):
scalar, code, _, _ = struct.unpack_from("<IBBH", data, 28 + 8 * i)
letters[code] = chr(scalar)
offsets_at = 28 + 8 * alphabet
rows_at = offsets_at + 4 * (words + 1) + words
rows_at += -rows_at % 4
blob_at = rows_at + 4 * (words + 1) + 4 * pairs + pairs
assert blob_at + blob == len(data)
offsets = struct.unpack_from(f"<{words + 1}I", data, offsets_at)
spelled = ["".join(letters[b] for b in data[blob_at + offsets[i]:blob_at + offsets[i + 1]]) for i in range(words)]
unigram = {spelled[i]: data[offsets_at + 4 * (words + 1) + i] / 8 for i in range(words)}
rows = struct.unpack_from(f"<{words + 1}I", data, rows_at)
seconds_at = rows_at + 4 * (words + 1)
seconds = struct.unpack_from(f"<{pairs}I", data, seconds_at)
costs_at = seconds_at + 4 * pairs
pair = {(spelled[a], spelled[seconds[k]]): data[costs_at + k] / 8 for a in range(words) for k in range(rows[a], rows[a + 1])}
return unigram, pair
def joined(word, share, unigram, pair):
"""Whether `word` is two model words run together, as scanned books (and typists) do ("Iwant",
"apartir"): the stored pair is at least JOINED times as common as the word itself (`share` of
the corpus). Those are no names."""
best = 0.0
for k in range(1, len(word)):
a, b = word[:k], word[k:]
if (a, b) in pair:
best = max(best, math.exp(-unigram[a] - pair[(a, b)]))
return best >= JOINED * share
def count_shard(job):
lang, index, url, work = job
out = os.path.join(work, os.path.basename(url) + ".tsv")
if os.path.exists(out):
return out
letters = LETTERS[lang]
token_re = re.compile(f"^[{letters.upper()}{letters}]?[{letters}]+$".encode("utf-8"))
download = subprocess.Popen(["curl", "-sSfL", "--http1.1", "--retry", "8", "--retry-all-errors", url], stdout=subprocess.PIPE)
unzip = subprocess.Popen(["gzip", "-dc"], stdin=download.stdout, stdout=subprocess.PIPE, bufsize=1 << 20)
download.stdout.close()
counts = {}
for line in unzip.stdout:
tab = line.find(b"\t")
token = line[:tab]
if b"_" in token or not token_re.match(token):
continue
total = 0
for field in line[tab + 1:].split(b"\t"):
year, count, _ = field.split(b",", 2)
if FIRST_YEAR <= int(year) <= LAST_YEAR:
total += int(count)
if total >= 4:
counts[token] = counts.get(token, 0) + total
unzip.wait(); download.wait()
if unzip.returncode or download.returncode:
raise RuntimeError(f"{url}: curl {download.returncode}, gzip {unzip.returncode}")
with open(out + ".part", "wb") as f:
for token, count in counts.items():
f.write(token + b"\t" + str(count).encode() + b"\n")
os.replace(out + ".part", out)
print(f"[{index}] {os.path.basename(url)}: {len(counts):,} tokens", flush=True)
return out
def main():
lang, model, work, output = sys.argv[1:5]
lowercase = scowl_lowercase(sys.argv[5]) if lang == "en" else set()
os.makedirs(work, exist_ok=True)
merged = os.path.join(work, "counts.tsv")
if not os.path.exists(merged):
code, n = FILES[lang]
jobs = [(lang, i, f"{BASE}/{code}/1-{i:05d}-of-{n:05d}.gz", work) for i in range(n)]
with multiprocessing.Pool(4) as pool:
paths = pool.map(count_shard, jobs, chunksize=1)
low, cap = {}, {}
for path in paths:
for line in open(path, "rb"):
token, count = line.rstrip(b"\n").split(b"\t")
word = token.decode("utf-8")
key = word.lower()
target = low if word == key else cap
target[key] = target.get(key, 0) + int(count)
with open(merged + ".part", "w", encoding="utf-8") as f:
for key in set(low) | set(cap):
f.write(f"{key}\t{low.get(key, 0)}\t{cap.get(key, 0)}\n")
os.replace(merged + ".part", merged)
unigram, pair = model_words(model)
words = set(unigram)
def folded(w):
return "".join(c for c in unicodedata.normalize("NFD", w) if not unicodedata.combining(c))
accented_twin = set()
if lang in ("fr", "it"):
twins = {}
for w in words:
twins.setdefault(folded(w), []).append(w)
accented_twin = {w for group in twins.values() if len(group) > 1 for w in group
if w == folded(w) and any(o != w and unigram[o] < unigram[w] for o in group)}
corpus = 0
for line in open(merged, encoding="utf-8"):
_, lo, ca = line.rstrip("\n").split("\t")
corpus += int(lo) + int(ca)
rows = []
for line in open(merged, encoding="utf-8"):
key, lo, ca = line.rstrip("\n").split("\t")
lo, ca = int(lo), int(ca)
total = lo + ca
if len(key) < 2 or total < MIN_TOTAL:
continue
share = (200 * ca + total) // (2 * total)
if key in words:
if share < SOMETIMES:
continue
if share < MODEL_ALWAYS[lang]:
share = min(share, HELD)
elif (len(key) < 3 or share < NAME_SHARE or total * 1_000_000 < NAME_PER_MILLION * corpus
or joined(key, total / corpus, unigram, pair)):
continue
if key in lowercase or key in accented_twin:
share = min(share, HELD)
rows.append((key, share))
rows.sort(key=lambda r: r[0].encode("utf-8"))
data = "".join(f"{k}\t{p}\n" for k, p in rows).encode("utf-8")
open(output, "wb").write(data)
inside = sum(1 for k, _ in rows if k in words)
print(f"{len(rows):,} words ({inside:,} in the model, {len(rows) - inside:,} names outside it), "
f"{len(data):,} bytes; {hashlib.sha256(data).hexdigest()}")
if __name__ == "__main__":
main()