e4dcfc49aa
Tests / Import Check (Python 3.13) (push) Has been cancelled
Tests / Import Check (Python 3.14) (push) Has been cancelled
Tests / Python Tests (Python 3.11) (push) Has been cancelled
Tests / Python Tests (Python 3.12) (push) Has been cancelled
Tests / Python Tests (Python 3.14) (push) Has been cancelled
Tests / Test Summary (push) Has been cancelled
Tests / Lint and Format (push) Has been cancelled
Tests / Web Node Tests (push) Has been cancelled
Tests / Import Check (Python 3.11) (push) Has been cancelled
Tests / Import Check (Python 3.12) (push) Has been cancelled
Tests / Python Tests (Python 3.13) (push) Has been cancelled
37 lines
1013 B
Python
37 lines
1013 B
Python
"""Duration helpers for math animator prompts."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
_SECOND_PATTERN = re.compile(
|
|
r"(?P<value>\d+(?:\.\d+)?)\s*(?:s|sec|secs|second|seconds|秒(?:钟)?)",
|
|
re.IGNORECASE,
|
|
)
|
|
_MINUTE_PATTERN = re.compile(
|
|
r"(?P<value>\d+(?:\.\d+)?)\s*(?:m|min|mins|minute|minutes|分钟)",
|
|
re.IGNORECASE,
|
|
)
|
|
|
|
|
|
def parse_target_duration_seconds(text: str) -> float | None:
|
|
"""Parse explicit duration target from user text."""
|
|
raw = str(text or "").strip()
|
|
if not raw:
|
|
return None
|
|
|
|
candidates: list[float] = []
|
|
for match in _SECOND_PATTERN.finditer(raw):
|
|
try:
|
|
candidates.append(float(match.group("value")))
|
|
except (TypeError, ValueError):
|
|
continue
|
|
for match in _MINUTE_PATTERN.finditer(raw):
|
|
try:
|
|
candidates.append(float(match.group("value")) * 60.0)
|
|
except (TypeError, ValueError):
|
|
continue
|
|
if not candidates:
|
|
return None
|
|
return max(candidates)
|