Skip to content

Commit 19df7e4

Browse files
Lee Seongjooclaude
andcommitted
Dedup leaked math tokens in source cleanup; prompt dedup rule; auto-update prompt
- cleanInput now drops renderer-leaked math runs that the following text repeats as a prefix: "이웃 수 k k는" → "이웃 수 k는", "k = 1 k=1은" → "k=1은", "k k를" → "k를". Deterministic, so duplicates are gone before the LLM ever sees them. - Default normalization prompt also gains an explicit dedup rule (belt and braces) and equation reading (k=1 → 케이는 일). - saveSettings only persists normalizePrompt when the user customized it, so future default-prompt updates auto-apply (restore default once to adopt). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent e81ab53 commit 19df7e4

3 files changed

Lines changed: 48 additions & 12 deletions

File tree

‎Sources/App/AppState.swift‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,20 @@ final class AppState: ObservableObject {
331331
}
332332

333333
func saveSettings() {
334-
let dict: [String: Any] = [
334+
var dict: [String: Any] = [
335335
"voiceId": voiceId, "voiceName": voiceName, "modelId": modelId,
336336
"backend": backendKind.rawValue, "useCache": useCache, "localBaseURL": localBaseURL,
337337
"maxChunkChars": maxChunkChars,
338338
"normalize": normalizeEnabled, "ollamaModel": ollamaModel, "ollamaURL": ollamaURL,
339-
"normalizePrompt": normalizePrompt,
340339
"normalizeProvider": normalizeProvider.rawValue, "geminiModel": geminiModel,
341340
"stability": voiceSettings.stability, "similarity": voiceSettings.similarityBoost,
342341
"style": voiceSettings.style, "speakerBoost": voiceSettings.useSpeakerBoost,
343342
]
343+
// Only persist the prompt if the user customized it, so default-prompt
344+
// updates auto-apply for everyone who didn't.
345+
if normalizePrompt != TextNormalizer.defaultInstruction {
346+
dict["normalizePrompt"] = normalizePrompt
347+
}
344348
if let d = try? JSONSerialization.data(withJSONObject: dict, options: [.prettyPrinted]) {
345349
try? d.write(to: settingsURL)
346350
}

‎Sources/Core/TextSplitter.swift‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,24 +31,56 @@ enum TextSplitter {
3131
}
3232

3333
/// Clean pasted source text: collapse single line breaks WITHIN a paragraph
34-
/// into spaces (markdown/math copied with each token on its own line), while
35-
/// keeping blank lines as paragraph breaks. Run on the original-text panel so
36-
/// the script-generation path downstream is unchanged.
34+
/// into spaces (markdown/math copied with each token on its own line), drop
35+
/// math tokens a renderer (KaTeX) leaked just before the real text, and keep
36+
/// blank lines as paragraph breaks. Run on the original-text panel so the
37+
/// script-generation path downstream is unchanged.
3738
static func cleanInput(_ text: String) -> String {
3839
let t = text.trimmingCharacters(in: .whitespacesAndNewlines)
3940
guard !t.isEmpty else { return "" }
4041
var paras: [String] = []
4142
var cur: [String] = []
43+
func flush() { if !cur.isEmpty { paras.append(dedupMathLeaks(cur)); cur = [] } }
4244
for line in t.components(separatedBy: .newlines) {
4345
let l = line.trimmingCharacters(in: .whitespaces)
44-
if l.isEmpty {
45-
if !cur.isEmpty { paras.append(cur.joined(separator: " ")); cur = [] }
46+
if l.isEmpty { flush() } else { cur.append(l) }
47+
}
48+
flush()
49+
return paras.joined(separator: "\n\n")
50+
}
51+
52+
/// Within one paragraph's lines, a run of math-only lines (e.g. "k" or
53+
/// "k","=","1") that the following text line repeats as a prefix ("k는",
54+
/// "k=1은") is a renderer leak — drop the run, keep the text. Otherwise lines
55+
/// are joined with spaces.
56+
private static func dedupMathLeaks(_ lines: [String]) -> String {
57+
var out: [String] = []
58+
var i = 0
59+
while i < lines.count {
60+
if isMathOnly(lines[i]) {
61+
var j = i
62+
var run: [String] = []
63+
while j < lines.count, isMathOnly(lines[j]) { run.append(lines[j]); j += 1 }
64+
let mathNoSpace = run.joined().replacingOccurrences(of: " ", with: "")
65+
if j < lines.count,
66+
!mathNoSpace.isEmpty,
67+
lines[j].replacingOccurrences(of: " ", with: "").hasPrefix(mathNoSpace) {
68+
out.append(lines[j]); i = j + 1 // leaked render → drop the run
69+
} else {
70+
out.append(run.joined(separator: " ")); i = j
71+
}
4672
} else {
47-
cur.append(l)
73+
out.append(lines[i]); i += 1
4874
}
4975
}
50-
if !cur.isEmpty { paras.append(cur.joined(separator: " ")) }
51-
return paras.joined(separator: "\n\n")
76+
return out.joined(separator: " ")
77+
}
78+
79+
private static let mathChars = CharacterSet(charactersIn:
80+
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 =+-*/^_().")
81+
82+
private static func isMathOnly(_ s: String) -> Bool {
83+
!s.isEmpty && s.count <= 12 && s.unicodeScalars.allSatisfy { mathChars.contains($0) }
5284
}
5385

5486
/// Split a long paragraph into <= maxChars windows, cutting at the last

‎Sources/TTS/TextNormalizer.swift‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ struct TextNormalizer: Normalizing {
2626
- 변수/첨자/수식: w1 → 더블유 일, x^2 → 엑스 제곱, a_i → 에이 아이, β → 베타, k → 케이, n → 엔
2727
- 등식/부등식: k=1 → 케이는 일, x>0 → 엑스는 영보다 큼
2828
- 기호/단위: % → 퍼센트, ± → 플러스 마이너스, ℃ → 도, ~ → 물결
29-
- 마크다운/수식을 복사하며 깨진 부분(같은 변수가 줄마다 중복되거나 'k = 1'처럼 토큰이 쪼개진 경우)은
30-
원래 의미로 합쳐 자연스러운 한 문장으로 정리합니다.
29+
- 마크다운/수식을 복사하며 생긴 중복·깨짐을 정리합니다. 같은 변수/수식이 바로 이어서 중복되면 하나로 합칩니다.
30+
예: 'k k는' → '케이는', 'k = 1 k=1은' → '케이는 일은', 쪼개진 'k = 1' → '케이는 일'.
3131
- 일반 영어 단어/문장과 한글 문장은 그대로 둡니다.
3232
- 설명·따옴표·머리말 없이, 변환된 텍스트만 출력합니다.
3333
"""

0 commit comments

Comments
 (0)