fix: combine adjacent minute and second duration units (#2684)

This commit is contained in:
Marcello Pichini
2026-11-30 05:32:40 +02:00
committed by GitHub
parent 79a66cb3c1
commit 6b00f0109c
2 changed files with 39 additions and 1 deletions

View File

@@ -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:

View File

@@ -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