hex/android/benchmark/typing/Replay.java
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

183 lines
11 KiB
Java

package local.hex.bench;
import ly.anoma.hex.typing.*;
import org.json.*;
import java.io.*;
import java.util.*;
/** Runs via app_process against the actual installed APK. Never built into Hex. */
public final class Replay {
static KeyGrid originalGeometry(String language) {
// Portrait KeyboardLayout at 402 pt (side 6.5, gap 6, letter height 45, row gap 11), and its
// space bar. German and Bokmål have Android's eleven-key rows at a 36 pt pitch; French is
// AZERTY, its bottom row's apostrophe key (never typed) left out of the letters.
boolean wide = language.equals("de") || language.equals("nb");
double pitch = wide ? 36 : 39.5; Map<Character,KeyPoint> points = new LinkedHashMap<>();
String[] rows = language.equals("de") ? new String[]{"qwertzuiopü", "asdfghjklöä", "yxcvbnm"}
: language.equals("nb") ? new String[]{"qwertyuiopå", "asdfghjkløæ", "zxcvbnm"}
: language.equals("fr") ? new String[]{"azertyuiop", "qsdfghjklm", "wxcvbn'"}
: new String[]{"qwertyuiop", language.equals("es") ? "asdfghjklñ" : "asdfghjkl", "zxcvbnm"};
for (int row=0; row<rows.length; row++) {
String letters=rows[row];
for (int i=0; i<letters.length(); i++)
if (letters.charAt(i) != '\'')
points.put(letters.charAt(i),new KeyPoint(201 + (i-(letters.length()-1)/2.0)*pitch,66.5+56*row));
}
return new KeyGrid(points, pitch, 56, new KeyRect(64.76, 212, 303.1, 257));
}
static String[] words(String s) {
return Arrays.stream(s.toLowerCase(Locale.ROOT).trim().split("\\s+"))
.map(w -> w.replaceAll("[.,!?;:]+$", "")).filter(w -> !w.isEmpty()).toArray(String[]::new);
}
static int edits(String[] ref, String[] hyp) {
int[] d = new int[hyp.length+1]; for(int j=0;j<d.length;j++) d[j]=j;
for(int i=1;i<=ref.length;i++) {
int prev=d[0]; d[0]=i;
for(int j=1;j<d.length;j++) {
int old=d[j]; d[j]=Math.min(Math.min(d[j]+1,d[j-1]+1),prev+(ref[i-1].equals(hyp[j-1])?0:1));
prev=old;
}
}
return d[hyp.length];
}
static void latency(String name, List<Long> ns) {
Collections.sort(ns);
System.out.printf(Locale.ROOT,"latency\t%s\t%d\t%.4f\t%.4f\t%.4f\n",
name,ns.size(),ns.get(ns.size()/2)/1e6,ns.get(ns.size()*95/100)/1e6,ns.get(ns.size()-1)/1e6);
}
// The strip as the keyboard shows it: strip() where the engine has it, else the old
// suggestions() with its literal chip.
static List<String> strip(TypingWord state) throws Exception {
List<String> out = new ArrayList<>();
try {
for (Object s : (List<?>) TypingWord.class.getMethod("strip", boolean.class).invoke(state, true))
out.add((String) s.getClass().getMethod("getText").invoke(s));
} catch (NoSuchMethodException old) {
List<String> words = state.suggestions(); String literal = state.literalChip(words);
if (literal != null) out.add(literal);
out.addAll(words);
}
return out;
}
@SuppressWarnings("unchecked")
static List<String> predictions(TypingWord state, String before) throws Exception {
try {
return (List<String>) TypingWord.class.getMethod("predictions", String.class, Boolean.class).invoke(state, before, null);
} catch (NoSuchMethodException old) {
return (List<String>) TypingWord.class.getMethod("predictions", String.class).invoke(state, before);
}
}
static boolean hit(List<String> slots, String word) {
for (String s : slots) if (s.toLowerCase(Locale.ROOT).replace('\u2019', '\'').equals(word)) return true;
return false;
}
// The replay geometry's touch areas tile each row: a key's centre ± half its pitch. A neighbouring
// letter within reach of the touch, by its distance to that area, in points (iOS's dp).
static Map<Character,Double> near(KeyGrid grid, char hit, double x, double y) {
Map<Character,Double> out = new HashMap<>();
for (Map.Entry<Character,KeyPoint> e : grid.getCentres().entrySet()) {
if (e.getKey() == hit) continue;
KeyPoint c = e.getValue();
double dx = Math.max(0, Math.abs(x - c.getX()) - grid.getPitchX() / 2), dy = Math.max(0, Math.abs(y - c.getY()) - grid.getPitchY() / 2);
double d = Math.sqrt(dx * dx + dy * dy);
if (d < TypingWord.REACH) out.put(e.getKey(), d);
}
return out;
}
// Whether a letter after `before` starts a word the keyboard reads (startsWord, else a boundary).
static boolean startsWord(TypingWord state, String before) throws Exception {
try {
return (Boolean) TypingWord.class.getMethod("startsWord", String.class).invoke(state, before);
} catch (NoSuchMethodException old) {
return TypingWord.Companion.boundary(before.isEmpty() ? null : before.charAt(before.length() - 1));
}
}
static boolean letterOdds = true;
static char likelierKey(TypingWord state, String before, char hit, double x, double y, KeyGrid grid) throws Exception {
if (!letterOdds) return hit;
try {
java.lang.reflect.Method m = TypingWord.class.getMethod("likelierKey", String.class, char.class, Map.class, Collection.class);
return (Character) m.invoke(state, before, hit, near(grid, hit, x, y), grid.getCentres().keySet());
} catch (NoSuchMethodException old) { return hit; }
}
public static void main(String[] args) throws Exception {
String lang=args[2], group=lang.equals("en") ? "test-" : lang+"-test-";
PrintWriter outputs=args.length>3 ? new PrintWriter(new FileWriter(args[3])) : null;
boolean hasIos=false;
TypingModel model=TypingModel.Companion.file(new File(args[0]),lang);
KeyGrid grid=originalGeometry(lang); TypingWord state=new TypingWord(new SpatialDecoder(model), true);
List<Long> letters=new ArrayList<>(), spaces=new ArrayList<>();
int count=0, total=0, matches=0; int[] errors=new int[3], exact=new int[3];
// Strip hits, as iOS's harness counts them: the intended word among the slots once its last
// letter is typed, after its first two letters (words of three or more), and among the
// predictions after the space before it.
int[] hits=new int[6];
try (BufferedReader reader=new BufferedReader(new FileReader(args[1]))) {
String line;
while((line=reader.readLine())!=null) {
JSONObject r=new JSONObject(line);
if(!r.getString("id").equals(group+count)) throw new IllegalArgumentException("Unexpected row "+count);
String typed=r.getString("typed"); JSONArray taps=r.getJSONArray("taps");
if(typed.length()!=taps.length()) throw new IllegalArgumentException("Unaligned taps");
StringBuilder out=new StringBuilder(); state.reset("");
String[] planned=r.getString("intended").toLowerCase(Locale.ROOT).split(" ");
Map<Integer,Integer> ends=new HashMap<>(), early=new HashMap<>();
String want=r.optString("want","");
JSONArray endList=r.optJSONArray("ends");
if(endList!=null) for(int j=0, from=0;j<endList.length();j++) {
int end=endList.getInt(j); ends.put(end,j);
if(end-from>=3) early.put(from+2,j);
from=end+(end<want.length()&&want.charAt(end)==' '?1:0);
}
List<String> shown=Collections.emptyList();
// As the keyboard: a letter starts a word it reads only after a space, an opening
// mark or (French, Italian) an elision; anything else is typed as it is until the next
// space. An apostrophe ends the word as typed.
boolean tracked=true;
for(int i=0;i<typed.length();i++) {
char key=typed.charAt(i); double x=taps.getJSONArray(i).getDouble(0), y=taps.getJSONArray(i).getDouble(1);
long start=System.nanoTime();
if(key!=' '&&!Character.isLetter(key)) {
out.append(key); state.reset(out.toString()); tracked=false; shown=Collections.emptyList();
} else if(key==' '&&!tracked) {
out.append(' '); String before=out.toString(); state.reset(before); shown=predictions(state,before); tracked=true;
} else if(key!=' '&&state.getToken().isEmpty()&&!startsWord(state,out.toString())&&!(tracked&&out.length()==0)) {
out.append(key); tracked=false; shown=Collections.emptyList();
} else if(key==' ') {
Correction fix=state.finish(' ',new KeyPoint(x,y),grid);
if(fix!=null) {
if(!out.toString().endsWith(fix.getOriginal())) throw new IllegalStateException("Correction off the text");
out.replace(out.length()-fix.getOriginal().length(),out.length(),fix.getReplacement());
}
out.append(' '); String before=out.toString(); state.reset(before); shown=predictions(state,before);
} else {
tracked=true;
key=likelierKey(state,out.toString(),key,x,y,grid);
state.letter(String.valueOf(key),x,y,grid); out.append(key); shown=strip(state);
}
(key==' '?spaces:letters).add(System.nanoTime()-start);
Integer j;
if((j=early.get(i+1))!=null) { hits[4]+=hit(shown,planned[j])?1:0; hits[5]++; }
if((j=ends.get(i+1))!=null) { hits[0]+=hit(shown,planned[j])?1:0; hits[1]++; }
if(i<want.length()&&want.charAt(i)==' '&&(j=ends.get(i))!=null&&j+1<planned.length) {
hits[2]+=hit(shown,planned[j+1])?1:0; hits[3]++;
}
}
String[] ref=words(r.getString("intended")); total+=ref.length; count++;
String ios=r.getString("ios_shared"); hasIos|=!ios.isEmpty();
if(outputs!=null) outputs.println(r.getString("id")+"\t"+out.toString().trim());
if (Arrays.equals(words(ios),words(out.toString()))) matches++;
String[] results={typed,ios,out.toString()};
for(int i=0;i<3;i++) { int err=edits(ref,words(results[i])); errors[i]+=err; if(err==0) exact[i]++; }
}
}
if(count!=2000) throw new IllegalArgumentException("Incomplete synthetic set: "+count);
if(outputs!=null) outputs.close();
String[] names={"raw","ios_shared","android_spatial"};
for(int i=0;i<3;i++) if(i!=1||hasIos)
System.out.printf(Locale.ROOT,"wer\t%s\t%d\t%d\t%.4f\t%d/%d\n",lang+"_"+names[i],errors[i],total,100.0*errors[i]/total,exact[i],count);
if(hasIos) System.out.printf("agreement\t%s\t%d/%d\n",lang,matches,count);
if(hits[1]>0) System.out.printf("strip\t%s\tword %d/%d\tnext %d/%d\tafter two letters %d/%d\n",lang,hits[0],hits[1],hits[2],hits[3],hits[4],hits[5]);
latency(lang+"_letter",letters); latency(lang+"_space",spaces);
}
}