Update lyricsParse.ts
This commit is contained in:
+22
-34
@@ -114,54 +114,42 @@ const req = {
|
|||||||
* and converts it into the LyricSegment[] structure.
|
* and converts it into the LyricSegment[] structure.
|
||||||
*/
|
*/
|
||||||
function parseFuriganaText(text: string): LyricLine[] {
|
function parseFuriganaText(text: string): LyricLine[] {
|
||||||
const segments: LyricLine[] = [];
|
const segments: LyricLine[] = []
|
||||||
const lines = text.split('\n').filter(line => line.trim() !== '');
|
const lines = text.split('\n').filter(line => line.trim() !== '')
|
||||||
|
|
||||||
// Regex to capture [timestamp] and (the rest of the line)
|
// Regex to capture [timestamp] and (the rest of the line)
|
||||||
const lineRegex = /\[(\d+:\d+\.\d+)\](.*)/;
|
const lineRegex = /\[(\d+:\d+\.\d+)\](.*)/
|
||||||
|
|
||||||
// Regex to parse the lyric part:
|
|
||||||
// It captures either:
|
|
||||||
// 1. `Kanji(furigana)` -> match[1] = Kanji, match[2] = furigana
|
|
||||||
// 2. `PlainText` -> match[3] = PlainText
|
|
||||||
const tokenRegex = /([^(]+)(([^)]+))|([^(]+)/g;
|
|
||||||
|
|
||||||
for (const line of lines) {
|
for (const line of lines) {
|
||||||
const lineMatch = line.trim().match(lineRegex);
|
const lineMatch = line.trim().match(lineRegex)
|
||||||
if (!lineMatch) {
|
if (!lineMatch) {
|
||||||
console.warn(`Skipping unparseable line: ${line}`);
|
console.warn(`Skipping unparseable line: ${line}`)
|
||||||
continue;
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
const time = lineMatch[1] as string;
|
const time = lineMatch[1] as string
|
||||||
const lyricText = lineMatch[2].trim();
|
const lyricText = lineMatch[2].trim()
|
||||||
const lyric: LyricSegment[] = [];
|
const lyric: LyricSegment[] = []
|
||||||
|
|
||||||
const matches = lyricText.matchAll(tokenRegex);
|
// Regex to match Kanji(furigana) or plain text
|
||||||
|
const tokenRegex = /([^(\s]+)(([^)]+))|([^\s]+)/g
|
||||||
|
let match
|
||||||
|
|
||||||
for (const match of matches) {
|
while ((match = tokenRegex.exec(lyricText)) !== null) {
|
||||||
const kanji = match[1];
|
const kanji = match[1]
|
||||||
const furigana = match[2];
|
const furigana = match[2]
|
||||||
const plain = match[3];
|
const plain = match[3]
|
||||||
|
|
||||||
if (kanji !== undefined && furigana !== undefined) {
|
if (kanji && furigana) lyric.push([kanji, furigana])
|
||||||
// This is a Kanji(furigana) group
|
else if (plain) lyric.push(plain)
|
||||||
lyric.push([kanji.trim(), furigana.trim()]);
|
|
||||||
} else if (plain) {
|
|
||||||
// This is a plain text group
|
|
||||||
const trimmedPlain = plain.trim();
|
|
||||||
if (trimmedPlain) {
|
|
||||||
lyric.push(trimmedPlain);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (lyric.length > 0) {
|
if (lyric.length > 0) {
|
||||||
segments.push({ time, lyric });
|
segments.push({ time, lyric })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return segments;
|
return segments
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -185,6 +173,6 @@ export async function aiParseLyrics(raw: string): Promise<LyricLine[]> {
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Failed to parse Furigana text from AI response:', responseText)
|
console.error('Failed to parse Furigana text from AI response:', responseText)
|
||||||
console.error(e)
|
console.error(e)
|
||||||
throw new Error('Failed to parse AI response text.');
|
throw new Error('Failed to parse AI response text.')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user