"""Language detection helper - tolerant to short / mixed text.""" from __future__ import annotations from langdetect import DetectorFactory, LangDetectException, detect_langs DetectorFactory.seed = 42 def detect_language(text: str, min_chars: int = 40) -> str | None: """Return ISO 639-1 language code or ``None`` if undetectable.""" if not text or len(text.strip()) < min_chars: return None try: ranked = detect_langs(text) except LangDetectException: return None if not ranked: return None return ranked[0].lang def has_cyrillic(text: str) -> bool: return any("Ѐ" <= ch <= "ӿ" for ch in text)