hex/android/benchmark/typing/portuguese_set.py
Ryan Vogel 0add67e372 android typing: a reproducible Brazilian Portuguese model (pt.hextm, pt.valid, pt.capitals), not wired up yet
Google Books has no Portuguese, so the model is counted from sources licensed for commercial use
with attribution only (CC BY 4.0), each file pinned in benchmark/typing/pt-inputs.tsv by URL, fetch
date, size and SHA-256:
- 14 Leipzig Corpora Collection news downloads (only the downloads; the site and API are CC BY-NC),
  each sentence given to Brazil or Portugal by its site (.br, .pt, else spelling markers).
- ParlaIbero-BR and -PT v2 (Câmara dos Deputados 2009-2025, Assembleia da República 1995-2025).

build-layout-model.py pt: Brazil's counts (181.5M news words, 138.1M words of speech), news counted
ten times a speech for words and alone for word pairs (with speech pairs in, "obrigado" was followed
by "sr" and "deputado"), less spellings retired in 1990 ("idéia", "vôo") and plain twins 20 times
rarer than an accented one ("nao", "voce", Polish's rule), plus Hex's own chat words
(pt-extra-words.tsv, a draft for a Brazilian speaker to read). 100,029 words, 380,416 pairs, 3.60 MB.
build-valid-words.py pt: Brazil's other lowercase spellings, then Portugal's own spellings from
Portugal's counts ("telemóvel", "receção"), so a Portuguese typist's correct word stays as typed.
build-capitals.py pt: case shares from the same Brazilian counts, held as French's are.

Held-out set: portuguese_set.py makes held-out/pt.tsv.gz exactly as polish_set.py makes Polish's,
from Brazilian contributors' Tatoeba sentences; accent keys and seven critical cases added.
quality-gate.py now takes more than two critical cases (Polish already had five).
The APK pins the three new files (SHA256SUMS, verifyTypingModels); no layout or keyboard code
reads them yet, so nothing a person sees changes.

Verified: a from-scratch rebuild on the Studio in a separate folder gave byte-identical files
(pt.hextm 387bf9b6..., pt.valid b92acc12..., pt.capitals 0210f5bd...); 12,800 of the 12,873
held-out reference words are model or list words; on 5,000 other Brazilian Tatoeba sentences the
model reads 6.54 nats per known word (news only 6.55, speech only 6.90, both alike 6.59).
Not yet run: quality-gate.py pt (it compiles the app with Gradle, which waits for the coordinator).
2026-11-24 18:10:17 -05:00

147 lines
7.1 KiB
Python

"""Portuguese held-out typing set, made exactly as polish_set.py makes Polish's: held-out Tatoeba
sentences (CC BY 2.0 FR, or CC0 where released so), each tap sampled from the measured per-key
offset and per-row spread in the English benchmark's input.json meta, 2% of spaces missed, a space
tap landing on a letter typed as that letter. Hex's Portuguese keys are QWERTY at English's 39.5 pt
pitch; á à â ã ç é ê í ó ô õ ú have no key of their own (each is held on its base key), so they are
typed as their base key.
Brazilian Portuguese only: Tatoeba doesn't tell Brazil from Portugal, so a sentence is taken from a
contributor whose sentences use spellings only Brazil writes at least three times as often as ones
only Portugal writes (twenty or more), and never when it has one of Portugal's itself
(build-layout-model.py's PT_MARKERS, plus "estar a" before an infinitive). "--portugal" makes the
mirror set from Portugal's contributors instead. Sentences with an apostrophe or a hyphen are left
out: the synthetic typist has no key for either.
Writes OUTDIR/pt.jsonl (2,000 test rows, as synthetic_sets.py) and pt-dev.jsonl (300), and
OUTDIR/pt.tsv.gz in held-out/'s format: Tatoeba ID, contributor, reference with the source's case,
the literal hit keys with Shift where the reference has a capital (including the final space), and
the taps as "x,y". With --portugal the files are pt-pt.*.
INPUT.json is the English benchmark's (2026-11-20-hex-typing-logs), as for synthetic_sets.py;
por_sentences_detailed.tsv.bz2 is https://downloads.tatoeba.org/exports/per_language/por/.
Usage: python3 portuguese_set.py INPUT.json por_sentences_detailed.tsv[.bz2] OUTDIR [--portugal]"""
import bz2, collections, gzip, importlib, json, os, random, re, sys, unicodedata
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
layout = importlib.import_module("build-layout-model")
meta = json.load(open(sys.argv[1]))["meta"]
source, out_dir = sys.argv[2], sys.argv[3]
PORTUGAL = sys.argv[4:] == ["--portugal"]
NAME = "pt-pt" if PORTUGAL else "pt"
OFFSET = {k: tuple(v) for k, v in meta["offsets"].items()}
ROW_SD = {int(k): tuple(v) for k, v in meta["row_sd"].items()}
SPACE_MU, SPACE_SD = meta["space"][:2], meta["space"][2:]
SPACE_OMIT = meta["space_omit"]
ROW_Y, TOP = [66.5, 122.5, 178.5], 44.5
SPACE = (64.76, 212.0, 238.34, 45.0)
ROWS = ["qwertyuiop", "asdfghjkl", "zxcvbnm"]
ALPHABET = layout.PT_LETTERS
PITCH = 39.5
SEEDS = (2026, 1124)
MARKERS = {"br": [layout.PT_MARKERS["br"], re.compile(r"\b(?:estou|está|estás|estão|estava|estavam|estamos)\s+\w+ndo\b")],
"pt": [layout.PT_MARKERS["pt"], re.compile(r"\b(?:estou|está|estás|estão|estava|estavam|estamos)\s+a\s+\w+[aei]r\b")]}
def geometry():
keys = {}
for r, letters in enumerate(ROWS):
for i, c in enumerate(letters):
keys[c] = (201 + (i - (len(letters) - 1) / 2) * PITCH, ROW_Y[r], r)
return keys
def hit(keys, x, y):
if y >= ROW_Y[2] + 28:
return "space" if SPACE[0] <= x < SPACE[0] + SPACE[2] else None
row = 0 if y < ROW_Y[0] + 28 else 1 if y < ROW_Y[1] + 28 else 2
letters = ROWS[row]
xs = [keys[c][0] for c in letters]
if row == 2 and (x < xs[0] - PITCH / 2 or x >= xs[-1] + PITCH / 2):
return None
return min(letters, key=lambda c: abs(keys[c][0] - x))
def fold(c):
return c if c in "".join(ROWS) else unicodedata.normalize("NFD", c)[0]
def sample_letter(rng, keys, ch):
cx, cy, row = keys[ch]
ox, oy = OFFSET[ch]
sx, sy = ROW_SD[row]
for _ in range(20):
x, y = rng.gauss(cx + ox, sx), max(rng.gauss(cy + oy, sy), TOP)
k = hit(keys, x, y)
if k is not None:
return x, y, k
return cx, cy, ch
def sample_space(rng, keys, force):
for _ in range(20):
x, y = rng.gauss(SPACE_MU[0], SPACE_SD[0]), rng.gauss(SPACE_MU[1], SPACE_SD[1])
k = hit(keys, x, y)
if k == "space" or (not force and k is not None):
return x, y, k
return SPACE[0] + SPACE[2] / 2, SPACE[1] + SPACE[3] / 2, "space"
def has(region, text):
low = text.lower()
return any(m.search(low) for m in MARKERS[region])
def sentences():
word = re.compile(f"^[{ALPHABET}]+$")
rows, votes = [], collections.defaultdict(lambda: [0, 0])
opener = bz2.open if source.endswith(".bz2") else open
for line in opener(source, "rt", encoding="utf-8"):
parts = line.rstrip("\n").split("\t")
if len(parts) < 4 or parts[3] in ("", "\\N"):
continue
text = unicodedata.normalize("NFC", parts[2])
votes[parts[3]][0] += has("br", text)
votes[parts[3]][1] += has("pt", text)
if "'" in text or "\u2019" in text or "-" in text:
continue
cased = re.sub(r"[.,!?;:\"()\u201c\u201d\u2018\u00bb\u00ab\u2013\u2014]", " ", text).split()
ws = [w.lower() for w in cased]
if 3 <= len(ws) <= 14 and all(word.match(w) for w in ws):
rows.append((parts[0], parts[3], cased, ws, text))
mine, other = (1, 0) if PORTUGAL else (0, 1)
owners = {o for o, v in votes.items() if v[mine] >= 20 and v[mine] >= 3 * v[other]}
rows = [r[:4] for r in rows if r[1] in owners and not has("br" if PORTUGAL else "pt", r[4])]
print(NAME, len(owners), "contributors,", len(rows), "eligible sentences")
rng = random.Random(SEEDS[0])
rng.shuffle(rows)
return rows[:2300]
keys = geometry()
rng = random.Random(SEEDS[1])
rows = sentences()
tsv = []
for name, part in (("dev", rows[:300]), ("test", rows[300:2300])):
path = os.path.join(out_dir, NAME + (".jsonl" if name == "test" else "-dev.jsonl"))
with open(path, "w") as f:
for j, (sid, owner, cased, ws) in enumerate(part):
taps, typed, want, ends, shown = [], [], [], [], []
for i, (w, cw) in enumerate(zip(ws, cased)):
for ch, cc in zip(w, cw):
x, y, k = sample_letter(rng, keys, fold(ch))
taps.append([round(x, 2), round(y, 2)]); typed.append(" " if k == "space" else k); want.append(ch)
shown.append(" " if k == "space" else (k.upper() if cc.isupper() else k))
ends.append(len(taps))
last = i == len(ws) - 1
if not last and rng.random() < SPACE_OMIT:
continue
x, y, k = sample_space(rng, keys, last)
taps.append([round(x, 2), round(y, 2)]); typed.append(" " if k == "space" else k); want.append(" ")
shown.append(" " if k == "space" else k)
f.write(json.dumps({"id": f"{NAME}-{name}-{j}", "intended": " ".join(ws), "typed": "".join(typed),
"taps": taps, "want": "".join(want), "ends": ends, "ios_shared": ""}, ensure_ascii=False, separators=(",", ":")) + "\n")
if name == "test":
tsv.append("\t".join([sid, owner, " ".join(cased), "".join(shown), " ".join(f"{x},{y}" for x, y in taps)]))
with open(os.path.join(out_dir, NAME + ".tsv.gz"), "wb") as raw, gzip.GzipFile("", "wb", fileobj=raw, mtime=0) as g:
g.write(("\n".join(tsv) + "\n").encode("utf-8"))
print(NAME, len(rows), "sentences;", len(tsv), "test rows")