File size: 707 Bytes
d0aabb7
64b8f7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# src/utils/text_processing.py

import re


def sanitize_title(title: str, max_words: int) -> str:
	"""Cleans and truncates a string to be used as a title."""
	title = re.sub(r"[\n\r]+", " ", title.strip())
	title = re.sub(r"[\"'`]+", "", title)
	title = re.sub(r"\s+", " ", title)
	words = title.split()
	return " ".join(words[:max_words])

def heuristic_title(text: str, max_words: int) -> str:
	"""Generates a simple title from text as a fallback."""
	cleaned = (text or "New Chat").strip()
	cleaned = re.sub(r"[\n\r]+", " ", cleaned)
	cleaned = re.sub(r"[^\w\s]", "", cleaned)
	cleaned = re.sub(r"\s+", " ", cleaned)
	words = cleaned.split()
	return " ".join(words[:max_words]) if words else "New Chat"