Skip to content

Commit 83f4952

Browse files
fix: an issue that caused capitalization to be lost after word segmentation (#197)
1 parent 2264dc8 commit 83f4952

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

src/modules/segmentation/components/split-word.tsx

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -162,26 +162,28 @@ export const SplitWordDialog = memo(() => {
162162
const handleSplit = useCallback(() => {
163163
if (!targetWordText) return;
164164

165-
const parts: string[] = [];
166-
let lastIndex = 0;
167165
const sortedIndices = Array.from(splitIndices).sort((a, b) => a - b);
166+
const buildSegments = (text: string) => {
167+
const parts: string[] = [];
168+
let lastIndex = 0;
169+
for (const index of sortedIndices) {
170+
if (index <= lastIndex) continue;
171+
parts.push(text.slice(lastIndex, index));
172+
lastIndex = index;
173+
}
174+
parts.push(text.slice(lastIndex));
175+
return parts.filter((p) => p.length > 0);
176+
};
168177

169-
for (const index of sortedIndices) {
170-
parts.push(targetWordText.slice(lastIndex, index));
171-
lastIndex = index;
172-
}
173-
parts.push(targetWordText.slice(lastIndex));
174-
175-
const splittedTextParts = parts.filter((p) => p.length > 0);
178+
const targetSegments = buildSegments(targetWordText);
176179

177-
if (splittedTextParts.length === 0) return;
180+
if (targetSegments.length === 0) return;
178181

179-
const createNewWords = (targetWord: LyricWord): LyricWord[] => {
180-
return recalculateWordTime(
181-
targetWord,
182-
splittedTextParts,
183-
segmentationConfig,
184-
);
182+
const createNewWords = (
183+
targetWord: LyricWord,
184+
segments: string[],
185+
): LyricWord[] => {
186+
return recalculateWordTime(targetWord, segments, segmentationConfig);
185187
};
186188

187189
editLyricLines((state) => {
@@ -195,7 +197,13 @@ export const SplitWordDialog = memo(() => {
195197
: word.word === targetWordText;
196198

197199
if (isMatch) {
198-
return createNewWords(word);
200+
const wordSegments = ignoreCase
201+
? buildSegments(word.word)
202+
: targetSegments;
203+
return createNewWords(
204+
word,
205+
wordSegments.length > 0 ? wordSegments : targetSegments,
206+
);
199207
}
200208
return word;
201209
});
@@ -208,7 +216,7 @@ export const SplitWordDialog = memo(() => {
208216
line.words.splice(
209217
editingState.wordIndex,
210218
1,
211-
...createNewWords(word),
219+
...createNewWords(word, targetSegments),
212220
);
213221
}
214222
}

0 commit comments

Comments
 (0)