Files
Sprachmittler/Packages/SprachmittlerKit/Sources/sprachmittler-cli/DetectCommand.swift
T
schihei 0ab5ae78bf feat(m7-m8): evaluation harness, privacy self-test, localisation and release
M7: corpus manifest loader/validation, chrF/BLEU/token/numeric metrics, raw-output and report.md/report.json writer, and sprachmittler-cli languages/detect/eval. A synthetic 4-segment run with the 7B validated the harness; the private-corpus and 1.8B evaluation and the default-model ADR remain open.

M8: privacy-check root fixture + seeded self-test wired into make verify; English/German localisation for the app shell; end-user USER_GUIDE; release.sh (unsigned archive, Developer ID sign, notarise/staple, DMG, --dry-run); idle CPU/memory and warm 7B measurements in PERFORMANCE.

Docs: PROGRESS M7–M8, MODEL_EVALUATION results log/gate status, PERFORMANCE measurements, TESTING evaluation cases, ADR-008 status. Compatibility/VoiceOver/notarisation gates remain manual.

Verified: make verify green (format, strict lint, privacy self-test, Debug+Release signatures, 227 package tests); CLI languages/detect/eval run; release dry-run.
2026-11-26 14:16:21 +01:00

57 lines
1.7 KiB
Swift

import ArgumentParser
import Foundation
import SprachmittlerText
struct DetectCommand: ParsableCommand {
static let configuration = CommandConfiguration(
commandName: "detect",
abstract: "Detect the source language of text (advisory)."
)
@Option(name: .long, help: "Text to inspect.")
var text: String?
@Option(name: .long, help: "UTF-8 file to inspect.")
var file: String?
@Flag(name: .long, help: "Emit JSON.")
var json = false
func validate() throws {
if (text == nil) == (file == nil) {
throw ValidationError("Use exactly one of --text or --file.")
}
}
func run() throws {
let source: String
if let text {
source = text
} else if let file {
source = try String(contentsOfFile: file, encoding: .utf8)
} else {
return
}
let detection = NaturalLanguageDetector().detect(source)
guard let detection else {
if json {
print(#"{"language":null,"confidence":0,"alternatives":[]}"#)
} else {
print("unknown")
}
return
}
if json {
let object: [String: Any] = [
"language": detection.code,
"confidence": detection.confidence,
"alternatives": detection.alternatives,
]
let data = try JSONSerialization.data(withJSONObject: object, options: [.prettyPrinted, .sortedKeys])
print(String(data: data, encoding: .utf8) ?? "{}")
} else {
print("\(detection.code)\t\(String(format: "%.2f", detection.confidence))")
}
}
}