9f308fadd2
Rotate AI idle suggestions with optional remote packs, move history/dictionary onto self-sizing Home preview cards, harden clipboard capture/prompting, and simplify keyboard chrome by dropping most liquid-glass shadows.
36 lines
1.2 KiB
Swift
36 lines
1.2 KiB
Swift
// 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
|
|
}
|
|
}
|