Files
Sprachmittler/Packages/SprachmittlerKit/Sources/SprachmittlerText/LanguageDetector.swift
T
schihei 41f1cea769 feat(panel): optional reverse direction for target-language text
Add opt-in automatic reverse translation for a two-language pair. autoSwitchTarget defaults on with fallbackTargetLanguage English (or German when the target is English); the saved target and the effective request target are separate. Confident source-equals-target text uses the fallback, the header and a panel banner show the effective direction, and the banner can override the direction for one capture without changing the saved target. The language picker still changes the saved target, and changing the target to the fallback swaps the pair. JSON/HTML/code handling is unchanged.\n\nFilter near-zero NLLanguageRecognizer hypotheses so the existing high-confidence same-language rule works for real text. Add resolver, settings, view-model, detector and persistence tests; document ADR-022, the panel, settings and user guide. Verified: make verify green; German text with target German/fallback English renders German → English with the banner, English text stays English → German, a real click on the override shows German → German, and a new German capture switches back to English.
2026-11-26 17:34:11 +01:00

58 lines
2.4 KiB
Swift

import Foundation
import NaturalLanguage
/// One language hypothesis from `NLLanguageRecognizer`.
public struct LanguageDetection: Sendable, Equatable {
/// The dominant BCP-47 language code, e.g. `en` or `de`.
public let code: String
/// The recogniser confidence in `code`, from 0 to 1.
public let confidence: Double
/// Candidate codes ordered by confidence, including `code` first.
public let alternatives: [String]
/// Creates a detection value.
public init(code: String, confidence: Double, alternatives: [String]) {
self.code = code
self.confidence = confidence
self.alternatives = alternatives
}
}
/// Source-language detection. Advisory only: it never blocks translation.
public protocol LanguageDetecting: Sendable {
/// Detects the dominant language of `text`, or returns nil for empty/undecidable text.
func detect(_ text: String) -> LanguageDetection?
}
/// `NLLanguageRecognizer`-backed detection (`docs/TRANSLATION_OUTPUT.md` §8).
public struct NaturalLanguageDetector: LanguageDetecting {
/// Creates a detector.
public init() {}
/// Detects the dominant language. Short or mixed text usually reports low confidence.
public func detect(_ text: String) -> LanguageDetection? {
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return nil }
let recognizer = NLLanguageRecognizer()
recognizer.processString(trimmed)
guard let dominant = recognizer.dominantLanguage else { return nil }
let hypotheses = recognizer.languageHypotheses(withMaximum: 5)
// NLLanguageRecognizer returns many near-zero hypotheses. Keep only candidates that
// carry meaningful evidence, so a confident single-language text is not treated as mixed.
let minimumAlternativeConfidence = 0.05
let ordered = hypotheses
.filter { $0.value >= minimumAlternativeConfidence }
.sorted { lhs, rhs in
lhs.value == rhs.value ? lhs.key.rawValue < rhs.key.rawValue : lhs.value > rhs.value
}
.map(\.key.rawValue)
let alternatives = ordered.isEmpty ? [dominant.rawValue] : ordered
return LanguageDetection(
code: dominant.rawValue,
confidence: Double(hypotheses[dominant] ?? 0),
alternatives: alternatives
)
}
}