import Foundation nonisolated enum DictationStyle: String, Codable, CaseIterable, Sendable { case polished case casual case notes case verbatim var label: String { switch self { case .polished: String(localized: "Polished") case .casual: String(localized: "Casual") case .notes: String(localized: "Notes") case .verbatim: String(localized: "Verbatim") } } var shortHint: String { switch self { case .polished: String(localized: "Full sentences, fillers trimmed") case .casual: String(localized: "Texting: lowercase start, no final period") case .notes: String(localized: "Short lines, a list for several items") case .verbatim: String(localized: "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 { /// `text` in `style`. `language` (BCP 47) picks the fillers a style trims and the /// English-only rules ("I", Casual's interjections, Notes' connectives); nil is English. /// - Verbatim: as the speech model wrote it. /// - Polished: full sentences: fillers trimmed, each sentence capitalized ("I" too) and /// ended. /// - Casual: texting: fillers trimmed, sentences may start lowercase (never an acronym /// or a name with inner capitals), English "i", no comma after an opening "yeah" or /// "ok", and no final period on a short message (one line, up to three sentences). /// - Notes: concise lines: spoken "bullet" and "new line" markers, else several /// sentences become a list (and a list after a colon becomes bullets), with no /// opening "so", "yeah" or "and then" and no final periods. 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) let english = SpeechLanguagePicker.isEnglish(language) switch style { case .verbatim: return collapseHorizontalSpaces(in: trimmed) case .polished: let cleaned = stripStandaloneFillers(in: trimmed, fillers: fillers) return polishSentences(cleaned, english: english) case .casual: let cleaned = stripStandaloneFillers(in: trimmed, fillers: fillers) return casualMessage(cleaned, english: english) case .notes: let cleaned = stripStandaloneFillers(in: trimmed, fillers: fillers) return formatNotes(cleaned, english: english) } } /// 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": // Desktop's list (`src/dictation/filler_words.rs`): um, umm, uh, uhh, erm, er, ah. return "um+|uh+|er+m*|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.. String { guard !text.isEmpty else { return "" } var normalized = fixSpacingBeforePunctuation(in: collapseHorizontalSpaces(in: text)) normalized = capitalizeSentenceStarts(in: normalized) if english { normalized = capitalizingPronounI(in: normalized) } // Every paragraph ends as a sentence does, a Chinese or Japanese one with "。". return normalized .components(separatedBy: "\n") .map(endingSentence) .joined(separator: "\n") } /// `line` with a full stop unless it already ends a sentence (or a list's colon), is /// empty, or is a lone address ("john@gmail.com"), which is no sentence to end. private static func endingSentence(_ line: String) -> String { guard let last = line.last, !sentenceClosers.contains(last) else { return line } if line.contains("@"), !line.contains(where: \.isWhitespace) { return line } return line + (usesIdeographicFullStop(last) ? "。" : ".") } /// What may end a sentence already: its mark, or a closing quote or bracket after one. private static let sentenceClosers: Set = [ ".", "!", "?", "…", "\"", "”", ")", "]", ":", "。", "!", "?", "」", "』", ")", ] /// Marks that end a sentence. A full stop counts only before a space, a line break, or the /// end ("gmail.com" and "e.g." end nothing), the ideographic ones anywhere. private static func endsSentence(_ chars: [Character], at index: Int) -> Bool { let ch = chars[index] if "。!?".contains(ch) { return true } guard ".!?…".contains(ch) else { return false } return index + 1 == chars.count || chars[index + 1].isWhitespace } /// Chinese and Japanese end a sentence with "。", not ".". private static func usesIdeographicFullStop(_ character: Character) -> Bool { guard let scalar = character.unicodeScalars.first?.value else { return false } return (0x4E00...0x9FFF).contains(scalar) // CJK unified ideographs || (0x3400...0x4DBF).contains(scalar) // extension A || (0x3040...0x30FF).contains(scalar) // hiragana and katakana || (0xF900...0xFAFF).contains(scalar) // compatibility ideographs } /// English "i", "i'm", "i'll", "i've", "i'd" as "I", "I'm", ...; never "i.e." or an address. private static func capitalizingPronounI(in text: String) -> String { recasingPronounI(in: text, from: "i", to: "I") } /// The English pronoun "I" (alone or in "I'm", "I'll", "I've", "I'd") written as `to`. private static func recasingPronounI(in text: String, from: String, to: String) -> String { guard text.contains(from) else { return text } let pattern = #"(? = [ "yeah", "yes", "yep", "yup", "no", "nope", "nah", "ok", "okay", "oh", "hey", "hi", "hello", "so", "well", "sure", "alright", "cool", "thanks", ] private static func casualMessage(_ text: String, english: Bool) -> String { var message = fixSpacingBeforePunctuation(in: collapseHorizontalSpaces(in: text)) if english { message = droppingInterjectionCommas(in: message) } message = lowercasingSentenceStarts(in: message) if english { message = recasingPronounI(in: message, from: "I", to: "i") } // A short message drops its final full stop; a question or an exclamation keeps its mark. let chars = Array(message) let sentences = chars.indices.filter { endsSentence(chars, at: $0) }.count if !message.contains("\n"), sentences <= 3, let last = message.last, last == "." || last == "。" { message.removeLast() } return message.trimmingCharacters(in: .whitespacesAndNewlines) } /// "Yeah, sounds good. Ok, see you." -> "Yeah sounds good. Ok see you.": an interjection at /// a sentence start loses its comma. private static func droppingInterjectionCommas(in text: String) -> String { var words = text.components(separatedBy: " ") var atStart = true for index in words.indices { let word = words[index] if atStart, word.hasSuffix(","), index + 1 < words.count, casualInterjections.contains(String(word.dropLast()).lowercased()) { words[index].removeLast() } let chars = Array(word) atStart = word.contains("\n") || (!chars.isEmpty && endsSentence(chars + [" "], at: chars.count - 1)) } return words.joined(separator: " ") } /// Lowercases the first letter of each sentence, as people text. Keeps acronyms ("NASA", /// though "OK" becomes "ok"), words with capitals inside ("iPhone", "McDonald's"), and /// addresses. private static func lowercasingSentenceStarts(in text: String) -> String { var chars = Array(text) var atStart = true var index = 0 while index < chars.count { let ch = chars[index] if atStart, ch.isLetter { var end = index while end < chars.count, chars[end].isLetter || chars[end] == "'" || chars[end] == "’" { end += 1 } let word = String(chars[index.. Bool { if word == "OK" { return false } let letters = word.filter(\.isLetter) if letters.count >= 2, letters == letters.uppercased() { return true } return letters.dropFirst().contains(where: \.isUppercase) } /// 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 } // MARK: Notes private static func formatNotes(_ text: String, english: Bool) -> 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..= 2 { lines = sentences.map { "- " + $0 } } else if let list = colonList(in: line, english: english) { lines = [list.intro + ":"] + list.items.map { "- " + $0 } } } return lines .map { line -> String in let trimmedLine = collapseHorizontalSpaces(in: line) if trimmedLine.hasPrefix("- ") { let body = String(trimmedLine.dropFirst(2)).trimmingCharacters(in: .whitespaces) let note = conciseNote(body, english: english) return note.isEmpty ? "" : "- " + note } return conciseNote(trimmedLine, english: english) } .joined(separator: "\n") .trimmingCharacters(in: .whitespacesAndNewlines) } /// One note: no opening "so," "okay," "and" or "then" (English), no final full stop, and /// a capital first letter. private static func conciseNote(_ line: String, english: Bool) -> String { var note = fixSpacingBeforePunctuation(in: line) if english { note = droppingConnectives(from: note) } if let last = note.last, last == "." || last == "。", !note.hasSuffix("...") { note.removeLast() } return capitalizeFirstLetter(note.trimmingCharacters(in: .whitespaces)) } /// Words that open a spoken note but add nothing to a written one. private static let noteConnectives: [String] = [ "and then", "and also", "and", "also", "then", "plus", "so", "okay", "ok", "yeah", "oh", "well", "anyway", "alright", "basically", "first", "firstly", "second", "secondly", "third", "thirdly", "next", "finally", "lastly", ] /// Of those, the ones that may also open a note without a comma after them. private static let bareNoteConnectives: Set = ["and then", "and also", "and", "also", "then", "plus"] private static func droppingConnectives(from line: String) -> String { var note = line var changed = true while changed { changed = false let lowered = note.lowercased() for connective in noteConnectives where lowered.hasPrefix(connective) { let rest = note.dropFirst(connective.count) let next: Substring if rest.hasPrefix(", ") { next = rest.dropFirst(2) } else if rest.hasPrefix(" "), bareNoteConnectives.contains(connective) { next = rest.dropFirst() } else { continue } // Never the whole note, and never into a closing mark ("And?"). guard let first = next.first, first.isLetter || first.isNumber else { continue } note = String(next) changed = true break } } return note } /// Abbreviations whose full stop ends no sentence ("Dr. Patel"). private static let abbreviations: Set = [ "mr", "mrs", "ms", "dr", "st", "jr", "sr", "prof", "vs", "etc", "inc", "ltd", "approx", "e.g", "i.e", ] /// `line`'s sentences, each with its mark; one sentence or none returns `[line]`. private static func noteSentences(in line: String) -> [String] { let chars = Array(line) var sentences: [String] = [] var start = 0 for index in chars.indices where endsSentence(chars, at: index) { if chars[index] == "." { var wordStart = index while wordStart > start, !chars[wordStart - 1].isWhitespace { wordStart -= 1 } let word = String(chars[wordStart.. ("Shopping list", ["eggs", "milk", "bread"]): /// three or more short items (four words at most) after a colon. English splits the last /// "and" or "or" off too; other languages take commas only. private static func colonList(in line: String, english: Bool) -> (intro: String, items: [String])? { guard let colon = line.range(of: ": ") else { return nil } let intro = String(line[..= 3, wordCounts.allSatisfy({ (1...4).contains($0) }), !items.contains(where: { item in item.contains(where: { ".!?。!?".contains($0) }) }) else { return nil } return (intro, items) } 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 } }