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. boolean wide = language.equals("de") || language.equals("nb"); double pitch = wide ? 36 : 39.5; Map points = new LinkedHashMap<>(); String[] rows = language.equals("de") ? new String[]{"qwertzuiopü", "asdfghjklöä", "yxcvbnm"} : language.equals("nb") ? new String[]{"qwertyuiopå", "asdfghjkløæ", "zxcvbnm"} : new String[]{"qwertyuiop", language.equals("es") ? "asdfghjklñ" : "asdfghjkl", "zxcvbnm"}; for (int row=0; row 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 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 strip(TypingWord state) throws Exception { List 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 words = state.suggestions(); String literal = state.literalChip(words); if (literal != null) out.add(literal); out.addAll(words); } return out; } @SuppressWarnings("unchecked") static List predictions(TypingWord state, String before) throws Exception { try { return (List) TypingWord.class.getMethod("predictions", String.class, Boolean.class).invoke(state, before, null); } catch (NoSuchMethodException old) { return (List) TypingWord.class.getMethod("predictions", String.class).invoke(state, before); } } static boolean hit(List slots, String word) { for (String s : slots) if (s.toLowerCase(Locale.ROOT).replace('\u2019', '\'').equals(word)) return true; return false; } 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 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 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=3) early.put(from+2,j); from=end+(end shown=Collections.emptyList(); for(int i=0;i0) 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); } }