Files
Sprachmittler/Packages/SprachmittlerKit/Sources/SprachmittlerEngines/RepetitionDetector.swift
T
schihei 64078e2f04 feat(m2-m3): adapter, text pipeline, double-copy capture and Services (T2.1-T2.7, T3.1-T3.4)
M2: TranslationAdapter/AdapterRegistry with documented Hy-MT3 prompts and publisher sampling; all 102 languages with BCP-47 variants, RTL and dense-script flags; normalisation, NLLanguage detection, content classification and exact-join script-aware chunking; protected-token scanning and output validation; prompt policy with one documented fallback; shared TranslationPipeline with Core error mapping. The CLI now reports sourceLanguage and warning codes.

M3: pure DoubleCopyDetector, 120 ms PasteboardWatcher with 750 ms own-write suppression, SystemPasteboard snapshot/restore and markers, CapturePolicy for the 4000/2000 limits, ServicesProvider and the menu-bar action. T3.5 (10-app manual matrix) remains intentionally open.

Docs: ARCHITECTURE, TRANSLATION_OUTPUT, HYMT3_MODEL (§7 spike), SELECTION_AND_CLIPBOARD, PROGRESS; ADR-016 superseded by ADR-017/018.

Verified: make verify green (format, strict lint, scripts, privacy, Debug+Release signatures, package tests); live 7B CLI en→de/de→en/Markdown/JSON/placeholders; empty input exits 2 and unreachable exits 3.
2026-11-26 13:30:32 +01:00

72 lines
2.5 KiB
Swift

/// Detects repetition loops in a streaming translation (`docs/TRANSLATION_OUTPUT.md` §7).
public struct RepetitionDetector: Sendable {
/// The repeated-line threshold.
public static let minimumLineLength = 8
/// How many identical consecutive lines count as a loop.
public static let lineRepeatThreshold = 6
/// The repeated-window length.
public static let windowLength = 64
/// How many identical windows count as a loop.
public static let windowRepeatThreshold = 8
/// How much trailing text is inspected for window loops.
public static let windowSearchLength = 2048
private var buffer = ""
/// Creates a detector.
public init() {}
/// Appends a delta and returns true when a loop is detected.
public mutating func append(_ delta: String) -> Bool {
buffer += delta
if buffer.count > Self.windowSearchLength * 2 {
buffer = String(buffer.suffix(Self.windowSearchLength))
}
return Self.detectsLoop(in: buffer)
}
/// Checks a complete or partial output for a loop.
public static func detectsLoop(in text: String) -> Bool {
hasRepeatedLine(in: text) || hasRepeatedWindow(in: text)
}
private static func hasRepeatedLine(in text: String) -> Bool {
let lines = text
.components(separatedBy: "\n")
.suffix(12)
.map { $0.trimmingCharacters(in: .whitespaces) }
guard !lines.isEmpty else { return false }
var run = 1
for index in 1 ..< lines.count {
if !lines[index].isEmpty, lines[index].count >= minimumLineLength, lines[index] == lines[index - 1] {
run += 1
if run >= lineRepeatThreshold {
return true
}
} else {
run = 1
}
}
return false
}
private static func hasRepeatedWindow(in text: String) -> Bool {
guard text.count >= windowLength else { return false }
let search = String(text.suffix(windowSearchLength))
let window = String(text.suffix(windowLength))
guard search.count >= windowLength * windowRepeatThreshold else { return false }
var count = 0
var range = search.startIndex ..< search.endIndex
while let found = search.range(of: window, range: range) {
count += 1
if count >= windowRepeatThreshold {
return true
}
range = found.upperBound ..< search.endIndex
}
return false
}
}