mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-27 07:58:01 +01:00
* ios: add an off-by-default experiment for swipe typing in more languages Swipe typing reads only English and Spanish today, though the French, Polish and German typing models are bundled and the decoder already maps an accented letter to its base key. Settings » Experiments » Swipe typing in more languages widens the one gate (FullKeyboardSettings.swipesIn) to every language typingModel(for:) has a model for, so French (France), Polish, and German while Spatial autocorrect and its experiment are on swipe too. Off, nothing changes: only English and Spanish swipe. Refs #2426 * ios: name the swipe languages, not "more", in the experiment's copy Keyboard » Swipe typing now lists the layouts that swipe when Swipe typing in more languages is on: English, Spanish, French and Polish, and German too only while Spatial autocorrect and German spatial autocorrect both make its model available. The Experiments row says what it adds. The feature maps record the known gaps: \u00df/\u0153/\u00e6 words have no key and are dropped from swipe candidates, swiped German nouns stay lowercase, French and Polish fall back to the shared never-suggest rows, and the decoder's parameters are tuned on English and Spanish only.
194 lines
11 KiB
Swift
194 lines
11 KiB
Swift
import CoreGraphics
|
||
import XCTest
|
||
@testable import HEX
|
||
|
||
/// Swipe typing's decoder (`SwipeDecoder`) on the bundled English, Spanish, French, German
|
||
/// and Polish 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")
|
||
}
|
||
|
||
/// Settings › Experiments › Swipe typing in more languages reads French (France), German
|
||
/// and Polish on their own layouts too: an accented letter is read on its base letter's
|
||
/// key, and French's AZERTY and German's QWERTZ are the keys as drawn.
|
||
func testFrenchGermanAndPolishSwipesReadTheirWords() throws {
|
||
for (language, word) in [(KeyboardLanguage.french, "bonjour"), (.german, "hallo"), (.polish, "dzień")] {
|
||
let (decoder, keys) = try decoder(language)
|
||
XCTAssertTrue(read(word, decoder, keys).prefix(2).contains(word), "\(language): \(word)")
|
||
}
|
||
}
|
||
|
||
/// 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")
|
||
}
|
||
|
||
/// A straight stroke runs over the letters between its ends: "tired" and "tied" draw one
|
||
/// line from i to e, "wire" and "write" one from w to i and back to e. Only where the
|
||
/// finger slowed tells them apart (X tester: guesses didn't account for the letters under
|
||
/// the swipe), so a pause on a letter the stroke crosses reads it, and the same stroke
|
||
/// without one doesn't.
|
||
func testAPauseOnALetterTheStrokeCrossesReadsIt() throws {
|
||
let (en, keys) = try decoder(.english)
|
||
func read(_ word: String, pausingOn pauses: Set<Int>) -> String? {
|
||
let (points, times) = SwipePath.timed(word, keys: keys, pausingOn: pauses)
|
||
return en.decode(points, times: times, keys: keys).first?.text
|
||
}
|
||
// The pauses are on the letters' keys in order (t i r e d: r is 2).
|
||
XCTAssertEqual(read("tired", pausingOn: [2]), "tired")
|
||
XCTAssertEqual(read("tired", pausingOn: []), "tied", "the same path, no pause on r")
|
||
XCTAssertEqual(read("tire", pausingOn: [2]), "tire")
|
||
XCTAssertEqual(read("tie", pausingOn: []), "tie")
|
||
XCTAssertEqual(read("wire", pausingOn: [2]), "wire")
|
||
XCTAssertEqual(read("write", pausingOn: [1, 3]), "write")
|
||
XCTAssertEqual(read("greet", pausingOn: [1]), "greet")
|
||
XCTAssertEqual(read("get", pausingOn: []), "get")
|
||
}
|
||
|
||
/// A pause off the word's keys counts against it: p to t along the top row is "put" when
|
||
/// the finger slows on u and "pit" on i; slowing on o and u offers the much rarer "pout".
|
||
func testAPauseOffTheWordsKeysCountsAgainstIt() throws {
|
||
let (en, keys) = try decoder(.english)
|
||
func read(_ word: String, pausingOn pauses: Set<Int>) -> [String] {
|
||
let (points, times) = SwipePath.timed(word, keys: keys, pausingOn: pauses)
|
||
return en.decode(points, times: times, keys: keys).map(\.text)
|
||
}
|
||
XCTAssertEqual(read("put", pausingOn: [1]).first, "put")
|
||
XCTAssertEqual(read("pit", pausingOn: [1]).first, "pit")
|
||
XCTAssertTrue(read("pout", pausingOn: [1, 2]).prefix(3).contains("pout"))
|
||
XCTAssertFalse(read("put", pausingOn: [1]).prefix(3).contains("pout"), "no pause on o, no pout")
|
||
// Touches that all arrive at once (a test's, or no clock) read as before.
|
||
let (points, _) = SwipePath.timed("pout", keys: keys, pausingOn: [1, 2])
|
||
let still = [TimeInterval](repeating: 0, count: points.count)
|
||
XCTAssertEqual(en.decode(points, times: still, keys: keys).map(\.text), en.decode(points, keys: keys).map(\.text))
|
||
}
|
||
|
||
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) }
|
||
}
|
||
|
||
/// Through `word`'s keys as `through` does, at an even 750 points a second, with the
|
||
/// touches' times: the finger stays `pause` seconds on the keys at `pausingOn` (indexes
|
||
/// into the word's keys, repeats run together), sending nothing meanwhile, as a still
|
||
/// finger does.
|
||
static func timed(_ word: String, keys: TypingDecoder.Keys, pausingOn pauses: Set<Int>, pause: TimeInterval = 0.15) -> (points: [CGPoint], times: [TimeInterval]) {
|
||
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]
|
||
var times: [TimeInterval] = [0]
|
||
let step: CGFloat = 6, perStep: TimeInterval = 0.008
|
||
for (index, (a, b)) in zip(centres, centres.dropFirst()).enumerated() {
|
||
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))
|
||
// The first point after a paused key comes only once the finger moves on.
|
||
times.append(times[times.count - 1] + perStep + (i == 1 && pauses.contains(index) ? pause : 0))
|
||
}
|
||
}
|
||
return (points, times)
|
||
}
|
||
}
|