feat(etl): 实现 LRC 时间戳去除
Showing
3 changed files
with
35 additions
and
0 deletions
etl_to_crawler/lyric.py
0 → 100644
| 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) |
tests/__init__.py
0 → 100644
File mode changed
tests/test_lyric.py
0 → 100644
| 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("没有时间戳的歌词") == "没有时间戳的歌词" |
-
Please register or sign in to post a comment