From 861f6a29f29f79635aef9559ee2f7ddfa4525a7a Mon Sep 17 00:00:00 2001 From: wlkns-dev Date: Thu, 5 Nov 2026 13:29:25 +0100 Subject: [PATCH] fix(ultrastar): keep word boundaries of the leading-space convention parse_ultrastar swallowed the leading space that marks a word start in some files (e.g. Uriah Heep - Lady In Black), gluing a whole line into one 'word' and inflating the WER (4.42 -> 0.43). Leading spaces are now normalised to a trailing space on the previous note. Co-Authored-By: Claude Sonnet 5.5 --- src/unmix/export/ultrastar.py | 9 ++++++++- tests/test_ultrastar.py | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/unmix/export/ultrastar.py b/src/unmix/export/ultrastar.py index c12345e..17c3626 100644 --- a/src/unmix/export/ultrastar.py +++ b/src/unmix/export/ultrastar.py @@ -295,9 +295,16 @@ def parse_ultrastar(file_path: Path) -> UltraStarSong: continue # Note line: : 10 4 2 text - match = re.match(r"^([:*FR])\s+(-?\d+)\s+(\d+)\s+(-?\d+)\s*(.*)$", line) + # The lyric starts after exactly one separator space; further leading spaces mark a word start + # (leading-space convention) and are normalised to a trailing space on the previous note below. + match = re.match(r"^([:*FR])\s+(-?\d+)\s+(\d+)\s+(-?\d+)(?: (.*))?$", line) if match: ntype, s_beat, dur, pitch, text = match.groups() + text = text or "" + if text.startswith(" "): + text = text.lstrip(" ") + if current_line and not current_line[-1].text.endswith(" "): + current_line[-1].text += " " note = UltraStarNote( note_type=ntype, start_beat=int(s_beat), diff --git a/tests/test_ultrastar.py b/tests/test_ultrastar.py index 9136378..e4e3f2e 100644 --- a/tests/test_ultrastar.py +++ b/tests/test_ultrastar.py @@ -196,3 +196,23 @@ E assert len(loaded.lines) == 2 assert loaded.lines[0].syllables[2].type == "golden" + + +def test_parse_ultrastar_leading_space_word_convention(tmp_path): + from unmix.eval.metrics import extract_text_from_reference + from unmix.export.ultrastar import parse_ultrastar + + f = tmp_path / "lead.txt" + f.write_text( + "#TITLE:T\n#ARTIST:A\n#BPM:120\n#GAP:0\n" + ": 0 4 4 She\n: 8 6 9 came\n: 18 2 9 to\n: 22 4 9 ~r\n- 30\n: 32 4 2 me\nE\n", + encoding="utf-8", + ) + song = parse_ultrastar(f) + assert extract_text_from_reference(song) == "She came to~r me" + assert song.lines[0][0].text == "She " + assert song.lines[0][1].text == "came " + # trailing-space convention is unchanged + g = tmp_path / "trail.txt" + g.write_text("#BPM:120\n#GAP:0\n: 0 4 4 We \n: 5 4 4 do \nE\n", encoding="utf-8") + assert extract_text_from_reference(parse_ultrastar(g)) == "We do"