hex/ios/HEXShared/CleanupReconstruction.swift
Sebastian Herrlinger b00240b437 iOS: the dictation rules run in the keyboard core
TranscriptFormatter, SpokenLists, SpokenPunctuation, SpokenCleanupRules,
CleanupReconstruction, FrenchSpacing and CleanupTagger.words keep their Swift
API and call hk_dictation_* (DictationCore.swift moves the text across). The
app and the keyboard both link libhex_keyboard.a, so both paths run the same
rules. DictationStyle, the settings structs, the tagger and the pipeline stay.

Two addresses said back to back still trap, on purpose and in one place,
SpokenCleanupRules.overlappingAddresses(in:), until the owner decides what to
write there (SpokenSymbolsTrapTests documents it, skipped).

In-process parity of the oracle copies against the wrappers: 1,663,830 cases
and, with another seed, 1,813,599 (Release), no differences; 4,218 in a normal
run. HEXMobileTests: 695 tests, 0 failures.
2026-11-23 01:19:57 +01:00

41 lines
1.9 KiB
Swift

import Foundation
import HexKeyboardCore
/// Rebuilds a transcript from the cleanup tagger's KEEP/DELETE labels: joins the kept words
/// and repairs punctuation and capitals left next to a deletion. It only removes characters
/// (commas, hesitation ellipses) or changes the case of a first letter; it never adds,
/// reorders, or changes words, names, or numbers.
///
/// The keyboard core's (`hk_dictation_reconstruct`, crates/keyboard/core/src/dictation/
/// reconstruct.rs), a line-for-line port of the tagger's `js/reconstruct.js` with JavaScript's
/// string semantics: a word's "first letter" is its first UTF-16 unit (an emoji or other
/// astral character is never cased), while the rest of a word is checked by code point.
nonisolated enum CleanupReconstruction {
/// `words` are the transcript's whitespace-separated words; `deletes` has one flag per word.
static func reconstruct(_ words: [String], deletes: [Bool]) -> String {
let flags = deletes.map { $0 ? UInt8(1) : 0 }
return withTexts(words[...]) { texts in
texts.withUnsafeBufferPointer { texts in
flags.withUnsafeBufferPointer { flags in
DictationCore.text(words.joined(separator: " ")) {
hk_dictation_reconstruct(texts.baseAddress, texts.count, flags.baseAddress, flags.count, $0)
}
}
}
}
}
/// `body` with each word as the core reads it, all valid inside `body`.
private static func withTexts<R>(_ words: ArraySlice<String>, _ body: ([HkStr]) -> R) -> R {
var texts: [HkStr] = []
texts.reserveCapacity(words.count)
func next(_ rest: ArraySlice<String>) -> R {
guard let word = rest.first else { return body(texts) }
return DictationCore.withText(word) { text in
texts.append(text)
return next(rest.dropFirst())
}
}
return next(words)
}
}