hex/crates/keyboard/core/tools
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Sebastian Herrlinger a30eeca346 Keyboard core: dictation text rules in Rust, identical to iOS
A port of iOS's dictation text rules to core/src/dictation/: the styles
(TranscriptFormatter: format, insertion joining, the sentence and period
helpers), Format lists (SpokenLists), Spoken punctuation (SpokenPunctuation),
Clean up's rules for Spanish, German and Norwegian and its English spoken
symbols and counts (SpokenCleanupRules, with CleanupReconstruction), and
French spacing (FrenchSpacing). The Core ML cleanup tagger, custom vocabulary,
Lowercase and locale parsing stay native: the host passes the tagger's output
and the language's code and region as its Locale reads them (ICU canonicalizes
"no" to "nb", "iw" to "he", "eng" to "en").

The rules keep Swift's semantics where they decide an output: Characters are
grapheme clusters, strings compare by canonical equivalence, case mappings go
scalar by scalar, `folding` is CoreFoundation's CFStringFold (its own case
table and mark rules, fold.rs), and the NSRegularExpression patterns are
matched by hand with ICU's rules (`\s`, `\b`, `\p{L}`, `$`), each pattern a
fixed alternation whose backtracking a greedy match reproduces (regex.rs).
iOS's Unicode data where it differs from Rust's or Rust has none (isLetter,
isNumber with Han numerals, ICU's sets, CF's Grapheme_Extend and folds) is
generated from the iOS 27 simulator (tools/dictation_unicode_tables.py).

The oracle: ios/HEXMobileTests/DictationParity holds copies of the Swift
files (dev 93db6ad7, types renamed with an Oracle suffix) and an env-gated
exporter test (TEST_RUNNER_HEX_DICTATION_PARITY_OUT/_COUNT/_SEED,
TEST_RUNNER_HEX_DICTATION_UNICODE_OUT) that writes JSON Lines from every
string literal in the dictation tests and a SplitMix64 generator built from
the rules' own words.

Parity: tests/dictation_parity.rs replays the committed fixture (3,896 cases,
1 MB) and, with HEX_DICTATION_PARITY, an export of 1,877,572 cases (every test
string through every style, setting and language, plus 150,000 generated
transcripts); a second export of 2,075,191 cases with another seed also
matches. HEX_DICTATION_UNICODE checks all 1,112,064 scalars against iOS. No
differences. One Swift bug is kept, not fixed: spokenSymbols traps on two
addresses said back to back ("a at sign b.com at sign c.com"); the port
panics at the same point and the exporter skips such texts.

Dependencies: unicode-segmentation, because a Swift Character is an extended
grapheme cluster and the rules count, index and compare Characters;
serde_json (dev only) to read the fixtures. No regex crate: ICU's \b, \s and
case folding differ from its, and hand matching is exact.
2026-11-23 01:19:48 +01:00
..
dictation_unicode_tables.py Keyboard core: dictation text rules in Rust, identical to iOS 2026-11-23 01:19:48 +01:00