bitcheck-document / app /utils /text_utils.py
AI Assistant
Update Bitcheck Document Service and test.html
37a1755
raw
history blame contribute delete
614 Bytes
import re
import string
import unicodedata
def normalize_text(text: str | None) -> str:
if not text:
return ""
normalized = unicodedata.normalize("NFKC", text).lower()
normalized = normalized.translate(str.maketrans("", "", string.punctuation))
normalized = re.sub(r"\s+", " ", normalized)
return normalized.strip()
def excerpt_text(text: str | None, max_chars: int = 500) -> str:
if not text:
return ""
collapsed = re.sub(r"\s+", " ", text).strip()
if len(collapsed) <= max_chars:
return collapsed
return collapsed[: max_chars - 3].rstrip() + "..."