23 lines
643 B
Python
23 lines
643 B
Python
"""Title normalization helpers."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import re
|
||
|
||
|
||
_SPLIT_PATTERN = re.compile(r"[,,。;;::!!??、|/]+")
|
||
_TRAIL_PATTERN = re.compile(r"[,,。;;::!!??、\-_ ]+$")
|
||
|
||
|
||
def compact_title(value: str, max_len: int = 24) -> str:
|
||
text = re.sub(r"\s+", " ", str(value or "")).strip()
|
||
if not text:
|
||
return ""
|
||
|
||
parts = [item.strip() for item in _SPLIT_PATTERN.split(text) if item.strip()]
|
||
title = parts[0] if parts else text
|
||
if len(title) > max_len:
|
||
title = title[:max_len].strip()
|
||
title = _TRAIL_PATTERN.sub("", title)
|
||
return title or text[:max_len].strip()
|