// TranscriptLanguageDetector.swift // OSGKeyboard ยท Shared // // Lightweight script detection for choosing the language of LLM guidance. // This intentionally does not attempt full language identification. import Foundation public enum TranscriptLanguageDetector: Sendable { /// Han characters as a share of non-whitespace, non-punctuation characters. public static func cjkRatio(_ text: String) -> Double { var hanCount = 0 var meaningfulCount = 0 for scalar in text.unicodeScalars { if CharacterSet.whitespacesAndNewlines.contains(scalar) || CharacterSet.punctuationCharacters.contains(scalar) || CharacterSet.symbols.contains(scalar) { continue } meaningfulCount += 1 if HanScript.isIdeograph(scalar) { hanCount += 1 } } guard meaningfulCount > 0 else { return 0 } return Double(hanCount) / Double(meaningfulCount) } /// Mixed Chinese/English transcripts should still receive Chinese guidance. public static func prefersChineseGuidance(_ text: String) -> Bool { cjkRatio(text) >= 0.15 } }