mirror of
https://github.com/anomalyco/hex.git
synced 2026-11-21 17:56:51 +08:00
* iOS: Italian, French and Portuguese full-keyboard layouts that autocorrect in their own language A TestFlight tester on build 31 with an Italian iPhone reported that Hex kept autocorrecting their Italian into English. KeyboardLanguage only knew en, nb, es and de, so Automatic mapped an it-IT dictation language (and any other language without a layout) to English: English letters, and UITextChecker's en_US dictionary rewriting Italian words. Adds italian/french/portuguese cases. They type on the English QWERTY rows (Apple's Italian and Portuguese keyboards are QWERTY; Hex has no AZERTY yet, so French is QWERTY too), with their own long-press accent order (Italian à è ì ò ù, é right after è), their UITextChecker dictionary (it, fr, pt_BR then pt_PT), and an Italian common two-letter list. Automatic now resolves it-*, fr-* and pt-* to these; the dictation language still wins over the iPhone's keyboards, and Hebrew or Russian still get English. Automatic deliberately ignores Hex's own input mode: its primaryLanguage is the Info.plist's en-US whatever you type. Spatial autocorrect (#2059) only has en and es models; the new layouts get no model and fall back to the dictionary engine in their own language (the decoder also refuses a model of another language). The space bar already names the dictation language ("Italiano"). Snapshot scenes get Italian, French and Portuguese drafts (--dictation-language it-IT, --keyboard-layout it|fr|pt). Verified on an iOS 27.2 iPhone 17 Pro simulator: full HEXMobileTests suite (272 tests, 0 failures, 1 skipped as before). New cases in KeyboardLayoutTests, AutocorrectEngineTests and FullKeyboardEngineTests. Measured on 12 Italian sentences (19 one-slip typos, 59 correct words) with the real dictionary: English fallback fixed 4, miscorrected 4 and broke 19 correct words (Domani->Domain, stai->stay, Sono->Soon, molto->moto); Italian fixed 17, miscorrected 0 and broke none. Renders of the Italian keyboard (typing with the "ritardo" suggestion, the E and I accent menus, "Italiano" on the space bar) checked by eye. * iOS: spoken "at the rate" and "at sign" become "@", joining a spoken email address An Indian English TestFlight tester (build 32) said "at the rate", India's name for @, and got the words. Apple's on-device models don't fix it: checked on macOS 27.2 with Hex's settings and an en_IN voice, SpeechTranscriber (Hex's default) writes "My email is John at the rategmail.com." and "Send it to Priya at signyahoo.com." (gluing the domain to the word before), and DictationTranscriber writes "at the rate gmail.com", though it already writes "priya@yahoo.com" for "at sign" and "john@gmail.com" for "john at gmail dot com". SpokenCleanupRules.spokenSymbols (English only) turns "at the rate" and "at sign" into "@". Before a domain ("gmail dot com", "gmail.com", "co dot in", or glued as above), the word before (with any "dot"-joined parts) and the domain join into one lowercase address, "dot" becoming "." only there: "John at the rate Gmail dot com." becomes "john@gmail.com.". "at the rate" counts only before a domain, so "at the rate of 5%" and "at the rate we planned" stay, and "at" alone is never touched; "at sign" elsewhere is a lone "@" (but not in "at sign language"). Only the matched words change. It runs from CleanupTagger.cleanIfEnabled, which both the app's and in-keyboard dictation call: in every style but Verbatim, with Clean up what I say on or off (the experiment's guard moved below it; the tagger itself still only runs when on). TranscriptFormatter: Polished capitalized after every full stop, which would have written "john@gmail.Com."; a sentence now starts only after ".", "!" or "?" and a space, an address keeps its case (Polished and Notes), and a lone address gets no full stop. Verified on an iOS 27.2 simulator: full HEXMobileTests suite (272 tests, 0 failures, 1 skipped as before), with new cases in TranscriptCleanupTests (Apple's real outputs included) and CleanupTaggerTests.
296 lines
12 KiB
Swift
296 lines
12 KiB
Swift
import Foundation
|
||
|
||
nonisolated enum DictationStyle: String, Codable, CaseIterable, Sendable {
|
||
case polished
|
||
case casual
|
||
case notes
|
||
case verbatim
|
||
|
||
var label: String {
|
||
switch self {
|
||
case .polished: "Polished"
|
||
case .casual: "Casual"
|
||
case .notes: "Notes"
|
||
case .verbatim: "Verbatim"
|
||
}
|
||
}
|
||
|
||
var shortHint: String {
|
||
switch self {
|
||
case .polished: "Clean punctuation and filler trim"
|
||
case .casual: "Lowercase texting, no trailing period"
|
||
case .notes: "Spoken bullets and line breaks"
|
||
case .verbatim: "Exact words from the model"
|
||
}
|
||
}
|
||
|
||
var next: DictationStyle {
|
||
let all = Self.allCases
|
||
guard let index = all.firstIndex(of: self) else { return .polished }
|
||
return all[(index + 1) % all.count]
|
||
}
|
||
}
|
||
|
||
nonisolated enum TranscriptFormatter {
|
||
/// `language` (BCP 47) picks the fillers a style trims; nil is English.
|
||
static func format(_ raw: String, style: DictationStyle, language: String? = nil) -> String {
|
||
let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines)
|
||
guard !trimmed.isEmpty else { return "" }
|
||
let fillers = fillerPattern(for: language)
|
||
|
||
switch style {
|
||
case .verbatim:
|
||
return collapseHorizontalSpaces(in: trimmed)
|
||
|
||
case .polished:
|
||
let cleaned = stripStandaloneFillers(in: trimmed, fillers: fillers)
|
||
return polishSentences(cleaned)
|
||
|
||
case .casual:
|
||
let cleaned = stripStandaloneFillers(in: trimmed, fillers: fillers)
|
||
var lowered = collapseHorizontalSpaces(in: cleaned).lowercased()
|
||
// Keep standalone "i" and "i'm"/"i'll"/"i've"/"i'd" capitalized naturally, or pure casual lowercase?
|
||
// Pure texting style: lowercase start and drop single-sentence trailing period, keep ? and !.
|
||
lowered = fixSpacingBeforePunctuation(in: lowered)
|
||
if lowered.hasSuffix("."), !lowered.dropLast().contains(where: { ".!?".contains($0) }) {
|
||
lowered.removeLast()
|
||
}
|
||
return lowered.trimmingCharacters(in: .whitespacesAndNewlines)
|
||
|
||
case .notes:
|
||
let cleaned = stripStandaloneFillers(in: trimmed, fillers: fillers)
|
||
return formatNotes(cleaned)
|
||
}
|
||
}
|
||
|
||
/// Joins dictated text cleanly with existing text around the insertion cursor: one space
|
||
/// between it and a word before it, and, mid-sentence, a lowercase first word
|
||
/// (`continuingSentence`). `isWord` says whether a lowercase word is in the dictation
|
||
/// language's dictionary; nil leaves the case alone.
|
||
static func insertionText(
|
||
for formatted: String,
|
||
beforeContext: String?,
|
||
afterContext: String?,
|
||
isWord: ((String) -> Bool)? = nil
|
||
) -> String {
|
||
guard !formatted.isEmpty else { return "" }
|
||
var output = formatted
|
||
|
||
if let isWord, !startsSentence(after: beforeContext) {
|
||
output = continuingSentence(output, isWord: isWord)
|
||
}
|
||
|
||
if beforeContext?.last?.isWhitespace ?? true {
|
||
// Never a second space after the one already there.
|
||
output = String(output.drop { $0 == " " || $0 == "\t" })
|
||
} else if let lastChar = beforeContext?.last {
|
||
let needsLeadingSpace = !"([{\"“‘«¿¡".contains(lastChar)
|
||
&& !output.hasPrefix(" ")
|
||
&& !output.hasPrefix("\n")
|
||
&& !isClosingPunctuation(output.first)
|
||
if needsLeadingSpace {
|
||
output = " " + output
|
||
}
|
||
}
|
||
|
||
if let after = afterContext, let firstChar = after.first {
|
||
let needsTrailingSpace = !firstChar.isWhitespace
|
||
&& !isClosingPunctuation(firstChar)
|
||
&& !output.hasSuffix("\n")
|
||
&& !output.hasSuffix(" ")
|
||
if needsTrailingSpace {
|
||
output += " "
|
||
}
|
||
}
|
||
|
||
return output
|
||
}
|
||
|
||
/// Whether text typed before the cursor leaves the next words at a sentence start: none
|
||
/// (or only spaces), a line break, or `.` `!` `?` `…` (closing quotes or brackets
|
||
/// after them count too).
|
||
static func startsSentence(after before: String?) -> Bool {
|
||
guard let before else { return true }
|
||
for character in before.reversed() {
|
||
if character.isNewline { return true }
|
||
if character.isWhitespace || "\"”’')]»".contains(character) { continue }
|
||
return ".!?…。".contains(character)
|
||
}
|
||
return true
|
||
}
|
||
|
||
/// German and Luxembourgish capitalize every noun, and their dictionaries accept nouns in
|
||
/// lowercase, so a dictation there keeps its case rather than lose a noun's capital.
|
||
static func lowercasesContinuations(in language: String) -> Bool {
|
||
let code = Locale(identifier: language).language.languageCode?.identifier ?? language
|
||
return code != "de" && code != "lb"
|
||
}
|
||
|
||
/// Lowercases the first letter of words that continue a sentence, as typing would. Keeps
|
||
/// "I" and its contractions, acronyms ("NASA"), words with capitals inside ("McDonald's"),
|
||
/// and words whose lowercase form `isWord` doesn't know, which are likely names.
|
||
static func continuingSentence(_ text: String, isWord: (String) -> Bool) -> String {
|
||
guard let first = text.first, first.isUppercase else { return text }
|
||
let word = String(text.prefix { $0.isLetter || $0 == "'" || $0 == "’" })
|
||
if word == "I" || word.hasPrefix("I'") || word.hasPrefix("I’") { return text }
|
||
let letters = word.filter(\.isLetter)
|
||
if letters.count >= 2, letters == letters.uppercased() { return text }
|
||
if letters.dropFirst().contains(where: \.isUppercase) { return text }
|
||
guard isWord(word.lowercased()) else { return text }
|
||
return first.lowercased() + text.dropFirst()
|
||
}
|
||
|
||
private static func isClosingPunctuation(_ character: Character?) -> Bool {
|
||
guard let character else { return false }
|
||
return ".,!?;:)]}”".contains(character)
|
||
}
|
||
|
||
/// The hesitation sounds a style trims, per language. English "er" is a word in
|
||
/// Norwegian, Danish, Swedish ("er" = is) and German, and "um" is one in Portuguese, so
|
||
/// each language trims only its own; a language without a list trims nothing. Spanish
|
||
/// trims "eh", "em" and "mmm" (never "e", which is "and"), German "äh", "ähm" and "öhm"
|
||
/// (never "eh", which is "anyway").
|
||
static func fillerPattern(for language: String?) -> String? {
|
||
let code = language.flatMap { Locale(identifier: $0).language.languageCode?.identifier } ?? "en"
|
||
switch code {
|
||
case "en":
|
||
return "um+|uh+|er+|ah+"
|
||
case "nb", "nn", "no", "da", "sv":
|
||
return "e+h+m*|ø+h+m*|ö+h+m*|hm+"
|
||
case "es":
|
||
return "e+h+m*|e+m+|m{2,}|hm+"
|
||
case "de":
|
||
return "ä+h+m*|ö+h+m*|hm+"
|
||
default:
|
||
return nil
|
||
}
|
||
}
|
||
|
||
private static func stripStandaloneFillers(in text: String, fillers: String?) -> String {
|
||
guard let fillers else { return collapseHorizontalSpaces(in: text) }
|
||
let pattern = #"(?i)(?:^|(?<=[\s,]))(?:"# + fillers + #")(?=(?:[\s,.!?]|$)),?\s*"#
|
||
guard let regex = try? NSRegularExpression(pattern: pattern) else { return text }
|
||
let range = NSRange(text.startIndex..<text.endIndex, in: text)
|
||
let stripped = regex.stringByReplacingMatches(in: text, range: range, withTemplate: " ")
|
||
return collapseHorizontalSpaces(in: stripped)
|
||
}
|
||
|
||
private static func polishSentences(_ text: String) -> String {
|
||
guard !text.isEmpty else { return "" }
|
||
var normalized = fixSpacingBeforePunctuation(in: collapseHorizontalSpaces(in: text))
|
||
normalized = capitalizeSentenceStarts(in: normalized)
|
||
// A lone address ("john@gmail.com") is no sentence to end.
|
||
let loneAddress = normalized.contains("@") && !normalized.contains(where: \.isWhitespace)
|
||
if let last = normalized.last, !".!?…\"”)]".contains(last), !loneAddress {
|
||
normalized.append(".")
|
||
}
|
||
return normalized
|
||
}
|
||
|
||
/// Whether the word at `index` is an address ("john@gmail.com"), whose case stays.
|
||
private static func isAddress(_ chars: [Character], at index: Int) -> Bool {
|
||
var end = index
|
||
while end < chars.count, !chars[end].isWhitespace {
|
||
if chars[end] == "@" { return true }
|
||
end += 1
|
||
}
|
||
return false
|
||
}
|
||
|
||
private static func formatNotes(_ text: String) -> String {
|
||
guard !text.isEmpty else { return "" }
|
||
var working = text
|
||
let replacements: [(String, String)] = [
|
||
(#"(?i)\bnew paragraph\b[,.]?\s*"#, "\n\n"),
|
||
(#"(?i)\bnew line\b[,.]?\s*"#, "\n"),
|
||
(#"(?i)(?:^|\s+)(?:bullet point|next bullet|bullet)\b[:,-]?\s*"#, "\n- "),
|
||
]
|
||
for (pattern, template) in replacements {
|
||
if let regex = try? NSRegularExpression(pattern: pattern) {
|
||
let range = NSRange(working.startIndex..<working.endIndex, in: working)
|
||
working = regex.stringByReplacingMatches(in: working, range: range, withTemplate: template)
|
||
}
|
||
}
|
||
let lines = working
|
||
.components(separatedBy: "\n")
|
||
.map { line -> String in
|
||
let trimmedLine = collapseHorizontalSpaces(in: line)
|
||
if trimmedLine.hasPrefix("- ") {
|
||
let body = String(trimmedLine.dropFirst(2)).trimmingCharacters(in: .whitespaces)
|
||
return body.isEmpty ? "" : "- " + capitalizeFirstLetter(fixSpacingBeforePunctuation(in: body))
|
||
}
|
||
return capitalizeFirstLetter(fixSpacingBeforePunctuation(in: trimmedLine))
|
||
}
|
||
return lines.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines)
|
||
}
|
||
|
||
private static func collapseHorizontalSpaces(in text: String) -> String {
|
||
let lines = text.components(separatedBy: "\n").map { line in
|
||
line.replacingOccurrences(of: #"[ \t]+"#, with: " ", options: .regularExpression)
|
||
.trimmingCharacters(in: .whitespaces)
|
||
}
|
||
return lines.joined(separator: "\n").trimmingCharacters(in: .whitespacesAndNewlines)
|
||
}
|
||
|
||
private static func fixSpacingBeforePunctuation(in text: String) -> String {
|
||
var result = text.replacingOccurrences(
|
||
of: #"\s+([,.;:!?])"#,
|
||
with: "$1",
|
||
options: .regularExpression
|
||
)
|
||
result = result.replacingOccurrences(
|
||
of: #"^[,;:]+\s*"#,
|
||
with: "",
|
||
options: .regularExpression
|
||
)
|
||
return result.trimmingCharacters(in: .whitespacesAndNewlines)
|
||
}
|
||
|
||
/// Capitalizes the first letter of each sentence: at the start, after a line break, or
|
||
/// after ".", "!" or "?" and a space. A full stop inside a word ("gmail.com", "e.g.")
|
||
/// starts nothing, and an address keeps its case.
|
||
private static func capitalizeSentenceStarts(in text: String) -> String {
|
||
var chars = Array(text)
|
||
var capitalizeNext = true
|
||
var endedSentence = false
|
||
for index in chars.indices {
|
||
let ch = chars[index]
|
||
if capitalizeNext && ch.isLetter {
|
||
if !isAddress(chars, at: index) {
|
||
for upper in String(ch).uppercased() {
|
||
chars[index] = upper
|
||
break
|
||
}
|
||
}
|
||
capitalizeNext = false
|
||
} else if ch == "\n" {
|
||
capitalizeNext = true
|
||
endedSentence = false
|
||
} else if ".!?".contains(ch) {
|
||
endedSentence = true
|
||
capitalizeNext = false
|
||
} else if ch.isWhitespace {
|
||
if endedSentence {
|
||
capitalizeNext = true
|
||
endedSentence = false
|
||
}
|
||
} else if "\"”’')]".contains(ch), endedSentence {
|
||
continue
|
||
} else if !"\"“(¿¡".contains(ch) {
|
||
capitalizeNext = false
|
||
endedSentence = false
|
||
}
|
||
}
|
||
return String(chars)
|
||
}
|
||
|
||
private static func capitalizeFirstLetter(_ text: String) -> String {
|
||
guard let index = text.firstIndex(where: \.isLetter),
|
||
!isAddress(Array(text), at: text.distance(from: text.startIndex, to: index)) else { return text }
|
||
var result = text
|
||
let upper = String(result[index]).uppercased()
|
||
result.replaceSubrange(index...index, with: upper)
|
||
return result
|
||
}
|
||
}
|