hex/ios/HEXMobileTests/SwipeDecoderTests.swift
Ryan Vogel 9b455f71bf iOS keyboard: a swipe-typing decoder for English and Spanish
SwipeDecoder reads a finger's path across the letter keys as a word, for
the Swipe typing experiment (wired into the keys in the next commit). It
is a shape-matching decoder after SHARK2, written for Hex, on the existing
Google Books typing models (en.hextm, es.hextm) and nothing else:

- the path is resampled to 32 points in key pitches;
- words come straight from the memory-mapped model: its sorted word list
  gives each start letter as one range (TypingDecoderModel.withCodes reads
  codes in place), kept when their last key is near the lift, every key is
  near the path and their ideal path is about as long. No index is built
  or kept between swipes, so the decoder adds no memory at rest;
- each is scored on location (point by point, then DTW for the best 80,
  after a cheap prefilter caps loose swipes at 1,500 words), shape, and a
  heavy-tailed start and end key cost, and weighed with the model's
  word-pair frequency after the word before (stupid backoff, as Spatial
  autocorrect's).

English contractions are read as extra words in three frequency tiers:
Google Books splits "don't" into "do n't", so the model ranks it like
"tom" and lacks "I'm" entirely. Slurs are never read and swear words only
when allowed (TypingDecoder.NeverSuggest from OffensiveWords).

Weights were tuned on synthetic swipes (clean, neat, typical, sloppy
fingers) of the autocorrect benchmark's held-out dev sentences, through
Hex's real 390 pt key geometry; the test sentences were used once, at
the end. Held-out test, typical fingers: English top-1 94.9% / top-3
99.1% (top-5k words 95.9%), Spanish 91.1% / 98.4% (93.7%); decode median
0.78 ms, p99 2.6-2.8 ms, max 4.2 ms on an M5 Max.

Verified: SwipeDecoderTests (new file: the decoder's own fixtures are
paths) pass on an iPhone 13 simulator, iOS 27.2: clean and offset swipes,
Spanish accents and ñ, contractions, context, slurs and swear words.
2026-11-20 17:21:09 -05:00

117 lines
5.9 KiB
Swift

import CoreGraphics
import XCTest
@testable import HEX
/// Swipe typing's decoder (`SwipeDecoder`) on the bundled English and Spanish models, with
/// swipes drawn through Hex's own key geometry. It has its own file because the decoder reads
/// a path without the keys' engine and its fixtures are paths; how the keys type a swipe is
/// in FullKeyboardEngineTests.
@MainActor
final class SwipeDecoderTests: XCTestCase {
private func decoder(_ language: KeyboardLanguage, swearing: Bool = false) throws -> (SwipeDecoder, TypingDecoder.Keys) {
let model = try XCTUnwrap(TypingDecoderModel.bundled(language.rawValue), "the \(language) model is bundled")
var decoder = SwipeDecoder(model: model)
decoder.neverSuggest = TypingDecoder.NeverSuggest(
OffensiveWords.bundled(language.rawValue).blocked(allowSwearing: swearing), model: model
)
if language == .english {
decoder.extraWords = SwipeDecoder.ExtraWords(
AutocorrectEngine.englishContractions, cost: { SwipeDecoder.costOfContraction($0) }, model: model
)
}
let keys = try XCTUnwrap(TypingDecoder.Keys(layout: KeyboardLayout(width: 390, language: language)))
return (decoder, keys)
}
private func read(_ word: String, _ decoder: SwipeDecoder, _ keys: TypingDecoder.Keys, after previous: String? = nil, offset: CGVector = .zero) -> [String] {
decoder.decode(SwipePath.through(word, keys: keys, offset: offset), keys: keys, previous: previous).map(\.text)
}
func testCleanSwipesReadAsTheirWords() throws {
let (en, keys) = try decoder(.english)
for word in ["hello", "the", "keyboard", "world", "typing", "people", "because", "quick", "with", "you", "and"] {
XCTAssertEqual(read(word, en, keys).first, word)
}
}
/// A finger that runs low all the way (as taps land below the keys' centres), or low and
/// to the left.
func testASwipeOffTheCentresStillReads() throws {
let (en, keys) = try decoder(.english)
for off in [CGVector(dx: 0, dy: keys.pitch.height * 0.3), CGVector(dx: -keys.pitch.width / 4, dy: keys.pitch.height / 4)] {
for word in ["hello", "keyboard", "people", "world", "typing", "because", "quick"] {
XCTAssertEqual(read(word, en, keys, offset: off).first, word, "\(off)")
}
}
}
func testSpanishSwipesKeepTheirAccentsAndTheirÑ() throws {
let (es, keys) = try decoder(.spanish)
for word in ["año", "también", "gracias", "mañana", "niño", "teclado"] {
XCTAssertEqual(read(word, es, keys).first, word)
}
XCTAssertEqual(Set(read("esta", es, keys).prefix(2)), ["esta", "está"], "one path, both readings")
}
/// Google Books splits "don't" into "do n't", so the model barely counts it: the decoder
/// reads Hex's English contractions as common words.
func testContractionsReadAsCommonWords() throws {
let (en, keys) = try decoder(.english)
XCTAssertEqual(read("dont", en, keys).first, "don't")
XCTAssertEqual(read("im", en, keys).first, "I'm")
}
func testTheWordBeforePicksBetweenWordsOnOnePath() throws {
let (en, keys) = try decoder(.english)
XCTAssertEqual(Array(read("to", en, keys).prefix(2)), ["to", "too"])
XCTAssertEqual(read("too", en, keys, after: "far").first, "too")
}
func testSlursAreNeverReadAndSwearWordsOnlyWhenAsked() throws {
for language in [KeyboardLanguage.english, .spanish] {
let words = OffensiveWords.bundled(language.rawValue)
for swearing in [false, true] {
let (decoder, keys) = try decoder(language, swearing: swearing)
let probes = language == .english ? ["fuck", "shit", "bitch", "nigger", "faggot"] : ["mierda", "cabron", "maricón"]
for probe in probes {
for reading in read(probe, decoder, keys) {
XCTAssertFalse(words.blocks(reading, allowSwearing: swearing), "\(probe) read as \(reading), swear words \(swearing)")
}
}
}
}
let (swearing, keys) = try decoder(.english, swearing: true)
XCTAssertEqual(read("fuck", swearing, keys).first, "fuck", "with Suggest swear words on")
}
func testAPathTooShortReadsAsNothing() throws {
let (en, keys) = try decoder(.english)
let h = try XCTUnwrap(keys.centres["h"])
XCTAssertTrue(en.decode([h, CGPoint(x: h.x + 4, y: h.y)], keys: keys).isEmpty)
XCTAssertTrue(en.decode([h], keys: keys).isEmpty)
}
}
/// Swipes for tests: straight strokes through the centres of a word's keys.
enum SwipePath {
/// Through the centres of `word`'s keys on `keys` (an accented letter on its base
/// letter's key, an apostrophe on none), a point every `step` points, moved by `offset`.
static func through(_ word: String, keys: TypingDecoder.Keys, offset: CGVector = .zero, step: CGFloat = 6) -> [CGPoint] {
var centres: [CGPoint] = []
for letter in word.lowercased() where letter != "'" {
let base = String(letter).decomposedStringWithCanonicalMapping.unicodeScalars.first.map(Character.init)
guard let centre = keys.centres[letter] ?? base.flatMap({ keys.centres[$0] }) else { continue }
if centres.last != centre { centres.append(centre) }
}
guard let first = centres.first else { return [] }
var points = [first]
for (a, b) in zip(centres, centres.dropFirst()) {
let count = max(1, Int((hypot(b.x - a.x, b.y - a.y) / step).rounded(.up)))
for i in 1...count {
let t = CGFloat(i) / CGFloat(count)
points.append(CGPoint(x: a.x + (b.x - a.x) * t, y: a.y + (b.y - a.y) * t))
}
}
return points.map { CGPoint(x: $0.x + offset.dx, y: $0.y + offset.dy) }
}
}