add: line spliter and ID Tag extractor for LRC parser

add: corresponding test for parser
This commit is contained in:
alikia2x 2024-07-25 02:23:07 +08:00
parent aef964aa35
commit 74d783b5d5
3 changed files with 124 additions and 0 deletions

49
src/lib/lyrics/parser.ts Normal file
View File

@ -0,0 +1,49 @@
export interface ScriptItem {
start: number;
text: string;
end: number;
translation?: string;
words?: ScriptWordsItem[];
singer?: number;
chorus?: string;
}
export interface ScriptWordsItem {
start: number;
end: number;
beginIndex: number;
endIndex: number;
}
export interface LrcJsonData {
ar?: string;
ti?: string;
al?: string;
scripts?: ScriptItem[];
[key: string]: any;
}
interface IDTag {
[key: string]: string;
}
export function splitLine(str: string) {
return str.split('\n').filter((str) => str.trim() !== '');
}
export function ExtractIDTags(lines: string[]) {
let result: IDTag = {};
const IDTagRegex = /^\[(\w*): (.*?)]$/;
let lastMatch = 0;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
const matchResult = line.trim().match(IDTagRegex);
if (matchResult && matchResult.length == 3) {
const tagName = matchResult[1];
const tagValue = matchResult[2];
result[tagName] = tagValue;
}
}
return result;
}

View File

@ -0,0 +1,19 @@
import { describe, expect, it } from 'vitest';
import fs from 'fs';
import { ExtractIDTags, splitLine } from '$lib/lyrics/parser';
describe('LRC parser test', () => {
const test01Buffer = fs.readFileSync('./src/test/resources/test-01.lrc');
const test01Text = test01Buffer.toString('utf-8');
it('Line Split', () => {
const lyrics = test01Text;
const lines = splitLine(lyrics);
expect(lines[26]).toBe('[01:52.991]');
});
it('IDTag Extract', () => {
const lyrics = test01Text;
const lines = splitLine(lyrics);
const idTags = ExtractIDTags(lines);
expect(idTags['ar']).toBe('洛天依');
});
});

View File

@ -0,0 +1,56 @@
[ti: 中华少女·终]
[ar: 洛天依]
[al: 中华少女]
[tool: 歌词滚动姬 https://lrc-maker.github.io]
[00:46.706] 我想要仗剑天涯却陷入纷乱
[00:49.588] 因果与恩怨牵杂等谁来诊断
[00:52.284] 暗箭在身后是否该回身看
[00:55.073] 人心有了鬼心房便要过鬼门关
[00:57.875] 早已茫然染了谁的血的这抹长衫
[01:00.702] 独木桥上独目瞧的人一夫当关
[01:03.581] 复仇或是诅咒缠在身上的宿命
[01:06.591] 棋子在棋盘被固定移动不停转
[01:09.241] 仇恨与仇恨周而复始往返
[01:12.586] 酒楼深胭脂一点分隔光暗
[01:15.205] 数求问天涯海角血债偿还
[01:18.015] 终是神念迷茫只做旁观
[01:21.087] 是非恩怨三生纠葛轮转
[01:23.709] 回望从前苦笑将杯酒斟满
[01:26.573] 那时明月今日仍旧皎洁
[01:29.115] 只叹换拨人看
[01:31.024] 你可是这样的少年
[01:33.971] 梦想着穿越回从前
[01:36.554] 弦月下着青衫抚长剑
[01:42.341] 风起时以血绘长卷
[01:45.276] 三寸剑只手撼江山
[01:47.838] 拂衣去逍遥天地间
[01:52.991]
[02:16.707] 黄藓绿斑苔痕将岁月扒谱
[02:20.077] 望眼欲穿你何时寄来家书
[02:22.788] 踱步间院落飞絮聚散化作愁字
[02:25.601] 当泪水成河能否凫上一位游子
[02:28.050] 当庭间嫣红轻轻闻着雨声
[02:30.841] 电闪雷鸣院中青翠摇曳倚风
[02:33.362] 青丝落指尖模糊记忆更为清晰
[02:36.334] 那一朵英姿遮挡于一抹旌旗飘
[02:39.511] 厮杀与厮杀周而复始招摇
[02:42.576] 血渍滑落于枪尖映照宵小
[02:45.726] 城池下红莲飞溅绽放照耀
[02:48.509] 碧落黄泉再无叨扰
[02:51.338] 北风呼啸三生等待轮转
[02:53.660] 山崖古道思绪被光阴晕染
[02:56.895] 那时明月今日仍旧皎洁
[02:59.293] 只叹孤身人看
[03:01.335] 你可是这样的少年
[03:04.377] 梦想着穿越回从前
[03:06.924] 北风里铁衣冷槊光寒
[03:12.607] 一朝去大小三百战
[03:15.623] 岁月欺万里定江山
[03:18.126] 再与她同游天地间
[03:24.356] 说书人或许会留恋
[03:27.057] 但故事毕竟有终点
[03:29.590] 最好的惊堂木是时间
[03:35.157] 就让我合上这书卷
[03:38.242] 愿那些梦中的玩伴
[03:40.857] 梦醒后仍然是少年
[03:46.139]