Commit d17b5206 d17b52065a8dec1c261851b43e21639162d3cbf0 by 沈秋雨

feat(etl): 实现 LRC 时间戳去除

1 parent 22f7456f
1 import re
2
3 _TIMESTAMP_RE = re.compile(r'\[\d{2}:\d{2}\.\d{2,3}\]')
4 _META_TAG_RE = re.compile(r'\[[a-zA-Z]+:[^\]]*\]')
5
6
7 def strip_timestamps(text: str | None) -> str:
8 if not text:
9 return ''
10 text = _META_TAG_RE.sub('', text)
11 text = _TIMESTAMP_RE.sub('', text)
12 lines = [line.strip() for line in text.splitlines() if line.strip()]
13 return '\n'.join(lines)
File mode changed
1 from etl_to_crawler.lyric import strip_timestamps
2
3 def test_strips_standard_timestamps():
4 lrc = "[00:12.34]第一行歌词\n[01:23.45]第二行歌词"
5 assert strip_timestamps(lrc) == "第一行歌词\n第二行歌词"
6
7 def test_strips_millisecond_timestamps():
8 lrc = "[00:12.345]带三位毫秒"
9 assert strip_timestamps(lrc) == "带三位毫秒"
10
11 def test_strips_meta_tags():
12 lrc = "[ti:歌曲名]\n[ar:歌手]\n[00:01.00]歌词"
13 result = strip_timestamps(lrc)
14 assert "歌词" in result
15 assert "[ti:" not in result
16
17 def test_empty_and_none():
18 assert strip_timestamps(None) == ''
19 assert strip_timestamps('') == ''
20
21 def test_plain_text_unchanged():
22 assert strip_timestamps("没有时间戳的歌词") == "没有时间戳的歌词"