diff --git a/deeptutor/agents/math_animator/duration_utils.py b/deeptutor/agents/math_animator/duration_utils.py index d1e40a0af..380d54bbb 100644 --- a/deeptutor/agents/math_animator/duration_utils.py +++ b/deeptutor/agents/math_animator/duration_utils.py @@ -31,7 +31,11 @@ def parse_target_duration_seconds(text: str) -> float | None: continue for match in _MINUTE_PATTERN.finditer(raw): try: - candidates.append(float(match.group("value")) * 60.0) + duration = float(match.group("value")) * 60.0 + seconds_match = _SECOND_PATTERN.match(raw[match.end() :].lstrip()) + if seconds_match: + duration += float(seconds_match.group("value")) + candidates.append(duration) except (TypeError, ValueError): continue if not candidates: diff --git a/tests/agents/math_animator/test_duration_unit_word_boundary.py b/tests/agents/math_animator/test_duration_unit_word_boundary.py index 482599310..d0e42181c 100644 --- a/tests/agents/math_animator/test_duration_unit_word_boundary.py +++ b/tests/agents/math_animator/test_duration_unit_word_boundary.py @@ -26,3 +26,37 @@ def test_duration_next_to_ordinary_words_still_parses() -> None: assert parse_target_duration_seconds("10s.") == 10.0 assert parse_target_duration_seconds("做一个30秒的动画") == 30.0 assert parse_target_duration_seconds("2分钟") == 120.0 + + +@pytest.mark.parametrize( + ("text", "expected"), + [ + ("Create a 1 minute 30 seconds video", 90.0), + ("1minute30seconds", 90.0), + ("1m30s", 90.0), + ("1分钟30秒", 90.0), + ("1.5 minutes 30.5 seconds", 120.5), + ("0.5m15.25s", 45.25), + ("1m30s or 2m10s", 130.0), + ("Intro 1m30s; outro 30 seconds", 90.0), + ], +) +def test_adjacent_minutes_and_seconds_form_one_duration(text: str, expected: float) -> None: + assert parse_target_duration_seconds(text) == expected + + +@pytest.mark.parametrize( + ("text", "expected"), + [ + ("1 minute or 30 seconds", 60.0), + ("1minuteor30seconds", 30.0), + ("30 to 60 seconds", 60.0), + ("30s1m", 60.0), + ("30 seconds then 1 minute", 60.0), + ("Intro 1 minute; outro 30 seconds", 60.0), + ("Intro 2 minutes; outro 30 seconds", 120.0), + ("Explain 3 main ideas with 5 sides in 500ms", None), + ], +) +def test_distinct_duration_targets_still_use_the_largest(text: str, expected: float | None) -> None: + assert parse_target_duration_seconds(text) == expected