Repository navigation
Expand file tree
/
Copy pathTextSplitter.swift
More file actions
173 lines (160 loc) · 7.8 KB
/
Copy pathTextSplitter.swift
File metadata and controls
173 lines (160 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import Foundation
/// Splits a selection into paragraph-sized chunks. Paragraphs are the cache +
/// request unit: long single-shot requests are unstable, and paragraph-level
/// cache hits are stable (sentence-level is too fine). A paragraph longer than
/// `maxChars` is split by a character budget (NOT sentence boundaries) so each
/// backend request stays bounded and chunk sizes are uniform; the cut lands on
/// the nearest preceding whitespace to avoid breaking a word.
enum TextSplitter {
static let defaultMaxChars = 700
struct SynthesisInput {
let text: String
let chunks: [String]
let displayText: String
}
/// Local MCP submits final text; do not run normalizers or the pasted-math
/// cleanup on it. Chunking can still trim surrounding whitespace. Manual
/// input keeps the existing cleanup on both the spoken and display sides.
static func synthesisInput(_ text: String, displayText: String?, preserveLiteral: Bool,
maxChars: Int) -> SynthesisInput? {
let spoken = preserveLiteral ? text : cleanInput(text)
let chunks = paragraphs(spoken, maxChars: maxChars)
guard !chunks.isEmpty else { return nil }
let display = displayText ?? spoken
return SynthesisInput(text: spoken, chunks: chunks, displayText: preserveLiteral ? display : cleanInput(display))
}
/// Split a paragraph into sentences for the karaoke-style subtitle. Breaks on
/// sentence terminators (. ! ? … and newlines); doesn't split a "." that sits
/// inside a token (np.where, file.txt, self.x, a decimal like 0.5). Used for
/// display only — audio stays chunked by paragraph.
static func sentences(_ text: String) -> [String] {
let chars = Array(text)
var out: [String] = []
var cur = ""
for (i, ch) in chars.enumerated() {
cur.append(ch)
// A "." immediately followed by a letter or digit is inside a token
// (np.where, file.txt, 0.5), not a sentence end. Real Korean sentence
// periods are followed by space / newline / end / closing punctuation.
let dotInsideToken = ch == "." && i + 1 < chars.count
&& (chars[i + 1].isLetter || chars[i + 1].isNumber)
let isTerminator = (ch == "." || ch == "!" || ch == "?" || ch == "…" || ch == "\n")
&& !dotInsideToken
if isTerminator {
let t = cur.trimmingCharacters(in: .whitespacesAndNewlines)
if !t.isEmpty { out.append(t) }
cur = ""
}
}
let tail = cur.trimmingCharacters(in: .whitespacesAndNewlines)
if !tail.isEmpty { out.append(tail) }
return out.isEmpty ? [text] : out
}
/// Index of the sentence at `fraction` (0…1) through `sentences`, weighted by
/// length. Used to map overall audio progress onto the subtitle text — which
/// is the human-readable source (원본/해설), independent of how the spoken 대본
/// chunked, so the subtitle never falls back to the 대본.
static func sentenceIndex(at fraction: Double, in sentences: [String]) -> Int {
guard sentences.count > 1 else { return 0 }
let lens = sentences.map { Double(max(1, $0.count)) }
let total = lens.reduce(0, +)
let target = Swift.max(0, Swift.min(1, fraction)) * total
var acc = 0.0
for (i, len) in lens.enumerated() { acc += len; if target <= acc { return i } }
return sentences.count - 1
}
static func paragraphs(_ text: String, maxChars: Int = defaultMaxChars) -> [String] {
let trimmed = text.trimmingCharacters(in: .whitespacesAndNewlines)
guard !trimmed.isEmpty else { return [] }
// Newline(s) delimit paragraphs.
let paras = trimmed
.components(separatedBy: .newlines)
.map { $0.trimmingCharacters(in: .whitespaces) }
.filter { !$0.isEmpty }
var out: [String] = []
for p in paras {
if p.count <= maxChars {
out.append(p)
} else {
out.append(contentsOf: splitByChars(p, maxChars: maxChars))
}
}
return out
}
/// Clean pasted source text: collapse single line breaks WITHIN a paragraph
/// into spaces (markdown/math copied with each token on its own line), drop
/// math tokens a renderer (KaTeX) leaked just before the real text, and keep
/// blank lines as paragraph breaks. Run on the original-text panel so the
/// script-generation path downstream is unchanged.
static func cleanInput(_ text: String) -> String {
let t = text.trimmingCharacters(in: .whitespacesAndNewlines)
guard !t.isEmpty else { return "" }
var paras: [String] = []
var cur: [String] = []
func flush() { if !cur.isEmpty { paras.append(dedupMathLeaks(cur)); cur = [] } }
for line in t.components(separatedBy: .newlines) {
let l = line.trimmingCharacters(in: .whitespaces)
if l.isEmpty { flush() } else { cur.append(l) }
}
flush()
return paras.joined(separator: "\n\n")
}
/// Within one paragraph's lines, a run of math-only lines (e.g. "k" or
/// "k","=","1") that the following text line repeats as a prefix ("k는",
/// "k=1은") is a renderer leak — drop the run, keep the text. Otherwise lines
/// are joined with spaces.
private static func dedupMathLeaks(_ lines: [String]) -> String {
var out: [String] = []
var i = 0
while i < lines.count {
if isMathOnly(lines[i]) {
var j = i
var run: [String] = []
while j < lines.count, isMathOnly(lines[j]) { run.append(lines[j]); j += 1 }
let mathNoSpace = run.joined().replacingOccurrences(of: " ", with: "")
if j < lines.count,
!mathNoSpace.isEmpty,
lines[j].replacingOccurrences(of: " ", with: "").hasPrefix(mathNoSpace) {
out.append(lines[j]); i = j + 1 // leaked render → drop the run
} else {
out.append(run.joined(separator: " ")); i = j
}
} else {
out.append(lines[i]); i += 1
}
}
return out.joined(separator: " ")
}
private static let mathChars = CharacterSet(charactersIn:
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 =+-*/^_().")
private static func isMathOnly(_ s: String) -> Bool {
!s.isEmpty && s.count <= 12 && s.unicodeScalars.allSatisfy { mathChars.contains($0) }
}
/// Split a long paragraph into <= maxChars windows, cutting at the last
/// whitespace at/before the limit (hard cut if a token is longer than the
/// window).
private static func splitByChars(_ p: String, maxChars: Int) -> [String] {
var chunks: [String] = []
var s = Substring(p)
while s.count > maxChars {
let limit = s.index(s.startIndex, offsetBy: maxChars)
var cut = limit
var idx = limit
var found = false
while idx > s.startIndex {
let prev = s.index(before: idx)
if s[prev].isWhitespace { cut = prev; found = true; break }
idx = prev
}
if !found { cut = limit } // no space: hard cut
let piece = s[s.startIndex..<cut].trimmingCharacters(in: .whitespaces)
if !piece.isEmpty { chunks.append(piece) }
var next = cut
while next < s.endIndex, s[next].isWhitespace { next = s.index(after: next) }
s = s[next...]
}
let tail = s.trimmingCharacters(in: .whitespaces)
if !tail.isEmpty { chunks.append(tail) }
return chunks
}
}