"""Unit tests for Stage 3 lyrics hyphenation and proportional timing.""" import pyphen from unmix.pipeline.s3_lyrics import hyphenate_word, split_word_into_syllables_timed def test_hyphenate_word_german(): dic = pyphen.Pyphen(lang="de_DE") # "Guten" -> ["Gu", "ten"] sylls = hyphenate_word("Guten", dic) assert len(sylls) == 2 assert "".join(sylls) == "Guten" # With trailing punctuation and space: "Morgen! " sylls_punct = hyphenate_word("Morgen! ", dic) assert len(sylls_punct) == 2 assert "".join(sylls_punct) == "Morgen! " def test_hyphenate_word_english(): dic = pyphen.Pyphen(lang="en_US") # "Hello" -> ["Hel", "lo"] sylls = hyphenate_word("Hello", dic) assert len(sylls) == 2 assert "".join(sylls) == "Hello" def test_split_word_into_syllables_timed(): dic = pyphen.Pyphen(lang="en_US") # "Hello " duration 1.0s (from 10.0 to 11.0) timed = split_word_into_syllables_timed("Hello ", 10.0, 11.0, dic) assert len(timed) == 2 assert timed[0]["start"] == 10.0 assert timed[1]["end"] == 11.0 assert timed[0]["end"] == timed[1]["start"] # Syllables should be "Hel" and "lo " assert timed[0]["text"] == "Hel" assert timed[1]["text"] == "lo " def test_split_into_vocal_lines_by_gap_and_length(): from unmix.pipeline.s3_lyrics import split_into_vocal_lines def syl(text, start, end, word_end=True): return {"text": text, "start": start, "end": end, "word_end": word_end} # Pause of 1.0 s between words -> two lines sylls = [syl("a ", 0.0, 0.5), syl("b ", 0.5, 1.0), syl("c ", 2.0, 2.5)] assert [len(c) for c in split_into_vocal_lines(sylls)] == [2, 1] # No pause but > max_line_sec -> split at word boundary, never inside a word long_line = [syl("x", 0.0, 2.0, word_end=False), syl("y ", 2.0, 4.0), syl("z ", 4.0, 6.0), syl("w ", 6.0, 8.0)] chunks = split_into_vocal_lines(long_line) assert len(chunks) >= 2 assert chunks[0][-1]["word_end"] is True assert sum(len(c) for c in chunks) == 4 assert split_into_vocal_lines([]) == [] def test_orphan_trailing_word_moves_to_next_segment(): from unmix.pipeline.s3_lyrics import pop_trailing_orphan, prepend_orphan def syl(text, start, end, word_end=True): return {"text": text, "start": start, "end": end, "word_end": word_end} chunks = [[syl("any ", 34.7, 36.1)], [syl("If ", 37.0, 37.8)]] rest, orphan = pop_trailing_orphan(chunks) assert len(rest) == 1 and orphan is not None nxt = prepend_orphan(orphan, [[syl("I ", 40.9, 41.1), syl("lay ", 41.1, 41.4)]]) assert [s["text"] for s in nxt[0]] == ["If ", "I ", "lay "] assert nxt[0][0]["start"] == 40.9 assert nxt[0][1]["start"] >= nxt[0][0]["end"] # Long single word (> 1 s) stays a line of its own chunks2 = [[syl("a ", 1.0, 2.0)], [syl("b ", 5.0, 7.5)]] assert pop_trailing_orphan(chunks2) == (chunks2, None) def test_merge_leading_orphan(): from unmix.pipeline.s3_lyrics import merge_leading_orphan def syl(text, start, end, word_end=True): return {"text": text, "start": start, "end": end, "word_end": word_end} chunks = [[syl("If ", 37.0, 37.8)], [syl("I ", 40.9, 41.1), syl("lay ", 41.1, 41.4)]] merged = merge_leading_orphan(chunks) assert len(merged) == 1 assert [s["text"] for s in merged[0]] == ["If ", "I ", "lay "] assert merged[0][0]["start"] == 40.9 assert len(merge_leading_orphan([[syl("a ", 0.0, 0.5)]])) == 1